티스토리 뷰

python lecture/algorism

[edu] Applied chemistry

burningrizen 2018. 11. 2. 23:53

Apart from having a unique name, chemical elements are also identified by a unique symbolic representation. Chemical symbols are one or two letters from the Latin alphabet, but can be three letters when the element has a provisional name. Chemical symbols are written with an uppercase first letter, followed by zero ore more lowercase letters.

symbols used by alchemists

Earlier chemical symbols stem from classical Latin and Greek vocabulary. For some elements, this is because the material was known in ancient times, while for others, the name is a more recent invention. For example, He is the symbol for helium (New Latin name; not known in ancient Roman times), Pb for lead (plumbum in Latin), and Hg for mercury (hydrargyrum in Greek). Some symbols come from other sources, like W for tungsten (Wolfram in German; not known in Roman times).

Provisional symbols assigned to newly or not-yet synthesized elements use 3-letter symbols based on their atomic numbers. For example, Uno was the temporary symbol for hassium (element 108) which had the temporary name of unniloctium.

In Chinese, each chemical element has a dedicated character, usually created for the purpose. However, Latin symbols are also used, especially in formulas.

Assignment

To get rid of the fact that there isn't always a direct connection between the name of a chemical element and its symbolic representation, we have decided to use a systematic way to assign a new symbol to each chemical element. These are the selection criteria we have already agreed upon:

  1. each symbol must start with an uppercase letter, followed by zero or more lowercase letters

  2. symbols should be formed by discarding letters from the name of the chemical element (without making a distinction between uppercase and lowercase letters, but taking into account the order of the letters as they occur in the name of the chemical element)

This still opens the possibility to derive many candidate symbols from the name of a chemical element. The following additional selection criteria are still under discussion:

  1. all chemical elements should have fixed-length symbols (for example two-letter symbols)

  2. of all candidate symbols, preference is given to the one that comes first/last alphabetically

In order to help selecting new symbols for the chemical elements, you are asked to:

  • Write a function isValid that takes the symbolic representation and the name of a chemical element as two string arguments. The function must return a Boolean value that indicates whether the given symbol meets criteria (1) and (2) for the chemical element with the given name. The function also has an optional third parameter length that may take an integer nN0. If a value is explicitly passed to the parameter length, the Boolean value returned by the function should in addition also indicate whether the given symbol has length n(cfr. criteria (3)).

  • Write a function symbols that takes the name of a chemical element. The function must return a set containing all two-letter symbols that meet criteria (1) and (2). These are all combinations of any two letters in the name of the element, where the first letter occurs before the second letter in the name, with the first letter converted to uppercase and the second to lowercase.

  • Write a function preference that takes the name of a chemical element. The function also has an optional second parameter last that may take a Boolean value (default value: False). The function should return the two-letter symbol that meets criteria (1) and (2), and that comes first (parameter last has value False) or last (parameter last has value True) alphabetically of all two-letter symbols that meet these criteria.

Example

>>> isValid('Zer', 'Zeddemorium')
True
>>> isValid('Zer', 'Zeddemorium', 2)
False
>>> isValid('di', 'Zeddemorium')
False

>>> symbols('Iron')
{'Ir', 'Io', 'In', 'Ro', 'Rn', 'On'}
>>> symbols('Neon')
{'Eo', 'Nn', 'No', 'En', 'Ne', 'On'}

>>> preference('Iron')
'In'
>>> preference('Iron', last=True)
'Ro'
>>> preference('Neon')
'En'
>>> preference('Neon', True)
'On'

Epilogue

When Hitler's army marched into Copenhagen, Niels Bohr had to decide how to safeguard the Nobel medals of James Franck and Max von Laue, which they had entrusted to him. Sending gold out of the country was almost a capital offense, and the physicists' names were engraved on the medals, making such an attempt doubly risky. Burying the medals seemed uncertain as well. Finally his friend — the Hungarian radiochemist George de Hevesy — invented a novel solution: he dissolved the medals in a jar of aqua regia, which Bohr left on a shelf in his laboratory while he fled to Sweden.

When he returned in 1945, the jar was still there. Bohr had the gold recovered, and the Nobel Foundation recast it into two medals.

Nobel Prize winners

Chemist Hermann Francis Mark also found a clever way to escape Germany with his money: he used it to buy platinum wire, which he fashioned into coat hangers. Once he had brought these successfully through customs, he sold them to recover the money.















> 전체 심볼의 인덱스 순서를 순열(permutation)을 사용, 전체 인덱스를 반영하여 예외 케이스 없음


import itertools


def get_idxs_group(symbol, ori):
idxs_group = list()
for char in symbol:
idxs = list()
for i in range(len(ori)):
if ori[i].lower() == char.lower():
idxs.append(i)
idxs_group.append(idxs)
return idxs_group


def is_order(idxs_group):
idx_list = list()
for idxs in idxs_group:
for idx in idxs:
if idx not in idx_list:
idx_list.append(idx)

for symbols_order in itertools.permutations(idx_list, len(idxs_group)):
is_orders = True
for i in range(len(symbols_order)-1):
if symbols_order[i] > symbols_order[i + 1] or symbols_order[i] not in idxs_group[i]:
is_orders = False
if is_orders:
return True
return False


def isValid(symbol, ori, length=0):
# is length
if length != 0 and len(symbol) != length:
return False
# is exist symbol in ori
for char in symbol:
if char.lower() not in ori.lower():
return False
# check upper or lower case
if not symbol[0].isupper() or not symbol[1:].islower() if len(symbol) > 1 else False:
return False
return is_order(get_idxs_group(symbol, ori))


def symbols(name):
result = list()
for item in itertools.combinations(name, 2):
symbol = item[0].upper() + item[1].lower()
if isValid(symbol, name):
result.append(symbol)
return set(result)


def preference(name, last=False):
symbol_list = list(symbols(name))
symbol_list.sort()
return symbol_list.pop() if last else symbol_list.pop(0)






> 순열(permutation) 사용하지 않음, 바로 다음 인덱스의 관계만 확인, 해당 문제에서는 예외 케이스가 없어서

문제 없지만, 바로 다음 인덱스는 문제가 없지만 전체 인덱스가 문제가 있을 수 있다. 


import itertools


def get_idxs_group(symbol, ori):
idxs_group = list()
for char in symbol:
idxs = list()
for i in range(len(ori)):
if ori[i].lower() == char.lower():
idxs.append(i)
idxs_group.append(idxs)
return idxs_group


def is_order(idxs_group):
prev = -1
for idxs in idxs_group:
is_order = False
for idx in idxs:
if idx > prev:
is_order = True
prev = idx
break
if not is_order:
return False
return True


def isValid(symbol, ori, length=0):
# is length
if length != 0 and len(symbol) != length:
return False
# is exist symbol in ori
for char in symbol:
if char.lower() not in ori.lower():
return False
# check upper or lower case
if not symbol[0].isupper() or not symbol[1:].islower() if len(symbol) > 1 else False:
return False
return is_order(get_idxs_group(symbol, ori))


def symbols(name):
result = list()
combi = itertools.combinations(name, 2)
for item in combi:
symbol = item[0].upper() + item[1].lower()
if isValid(symbol, name):
result.append(symbol)
return set(result)


def preference(name, last=False):
symbol_list = list(symbols(name))
symbol_list.sort()
return symbol_list.pop() if last else symbol_list.pop(0)




'python lecture > algorism' 카테고리의 다른 글

[edu] Seasons  (0) 2018.11.06
[edu] Isomers  (0) 2018.11.04
[edu] Friday the 13th  (0) 2018.10.24
[edu] Colorful fruits  (0) 2018.10.24
[eud] bubble sorting  (0) 2018.10.15
댓글