Skip to content

Instantly share code, notes, and snippets.

@inesusvet
Created January 26, 2019 20:39
Show Gist options
  • Save inesusvet/044eed3018635384092bdf78bef5d38b to your computer and use it in GitHub Desktop.
Save inesusvet/044eed3018635384092bdf78bef5d38b to your computer and use it in GitHub Desktop.
Coding dojo in Minsk session result for 2019-01-26
"""
See the full table at http://www.hemi.nsu.ru/mends.htm
"""
MENDELEEV_TABLE = {
'H': 1,
'He': 4,
'Li': 7,
'Be': 9,
'B': 11,
'C': 12,
'N': 14,
'O': 16,
'F': 19,
'Ne': 20,
'Na': 23,
'Mg': 24,
'Al': 27,
'Si': 28,
'P': 31,
'S': 32,
'Cl': 35,
'Ar': 40,
'K': 39,
'Ca': 40,
'Sc': 45,
'Ti': 48,
'V': 51,
'Cr': 52,
'Mn': 55,
'Fe': 56,
'Co': 59,
'Ni': 59,
'Cu': 64,
'Zn': 65,
'Ga': 70,
'Ge': 73,
'As': 75,
'Se': 79,
'Br': 80,
'Kr': 84,
}
def molar_mass(text):
"""
See https://en.wikipedia.org/wiki/Molar_mass
>>> molar_mass('H2O')
18
>>> molar_mass('C2H5OH')
46
>>> molar_mass('H2SO4')
98
"""
return sum(
MENDELEEV_TABLE[atom] * amount
for atom, amount in parse_molekula(text)
)
def parse_molekula(text):
"""
>>> parse_molekula('C2H5')
[['C', 2], ['H', 5]]
>>> parse_molekula('OH')
[['O', 1], ['H', 1]]
>>> parse_molekula('NaCl')
[['Na', 1], ['Cl', 1]]
"""
tokens = tokenize(text)
result = []
for atom_group in tokens:
atom = ''
amount = ''
for c in atom_group:
if c.isalpha():
atom += c
elif c.isdigit():
amount += c
result.append([atom, int(amount if amount else 1)])
return result
def tokenize(text):
"""
>>> tokenize('NaCl')
['Na', 'Cl']
>>> tokenize('H2O')
['H2', 'O']
"""
result = []
buffer = ''
for c in text:
if c.isupper() and buffer == '':
buffer += c
elif c.islower():
buffer += c
elif c.isdigit():
buffer += c
elif c.isupper():
result.append(buffer)
buffer = c
if buffer != '':
result.append(buffer)
return result
#! -*- coding: utf-8 -*-
import chemistry
def compress_string(text):
"""
Написать фукнцию, которая будет "сжимать" переданную строку.
Вместо повтора символов будет указывать сколько раз символ повторялся.
>>> compress_string('aaabbbbcddd')
'a3b4cd3'
>>> compress_string('abcdefgh')
'abcdefgh'
>>> compress_string('aaaaabaaaaa')
'a5ba5'
>>> compress_string('')
''
"""
if text == '':
return ''
result = []
for char, count in split(text):
result.append(char + str(count if count > 1 else ''))
return ''.join(result)
def split(text):
last, count = text[0], 1
for char in text[1:]:
if char == last:
count += 1
else:
yield last, count
last = char
count = 1
yield last, count
def decompress_string(text):
"""
Написать функцию обратную compress_string
>>> compress_string('a3b4cd3')
'aaabbbbcddd'
>>> compress_string('abcdefgh')
'abcdefgh'
>>> compress_string('a5ba5')
'aaaaabaaaaa'
>>> compress_string('')
''
"""
if text == '':
return ''
result = []
last = text[0]
for char in text[1:]:
if char.isdigit():
count += char
elif char != last:
result.append(last * int(count or 1))
count = ''
last = char
result.append(last * int(count or 1))
return ''.join(result)
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment