Skip to main content

Write any phrase as a list of chemical element symbols from the periodic table

Project description

Python Library that is aimed at writing phrases in chemical element symbols

What do you mean?

Consider my name, ‘Nicholas Georgescu’. It can be written as the chemical symbols ‘Ni’ ‘C’ ‘Ho’ ‘La’ ‘S’, ‘Ge’ ‘O’ ‘Rg’ ‘Es’ ‘Cu’, i.e. nickel, carbon, holmium, lanthanum, sulfur… germanium, oxygen, roentgenium, einsteinium, copper. This library lets you do that for literally any piece of text.

Ok… why?

Real Answer: I always liked that you could spell my full name out of chemical elements ever since the induction of roentgenium in 2004 (My middle name doesn’t work though; it’s my greatest shame). It was a game I would play in high school when in the car on roadtrips looking at street names, etc.

Smarter Answer: This actually is non-trivial to implement because chemical symbols can be either one or two letter in length. Suppose a string starts with ‘Nar’. Now there is no element ‘R’, but there is ‘N’, ‘Na’, and ‘Ar’, as well as other elements that start with R like ‘Rg’, ‘Ru’, ‘Rh’, etc. So, should this be chunked as ‘N, Ar’, or ‘Na, R..’? If the next letter is an ‘C’, the former only works. If the next letter is a ‘G’, the latter only works. If the next letter is a ‘U’, either works, but maybe you run into problems with later letters. This gets messy quickly. Hence it’s really a test of implementation. If you understand how to solve this problem you can solve other similar problems that people think of as recursive (like the google interview ‘Knight Dialer’ problem) rather easily without actually needing to use recursion.

Implementation

The way to solve this is using a recursive-descent algorithm using a while loop. The basic structure of this loop is:

todo = [[item1, processing_data],[item2, processing_data], ... ]
done = []
while len(todo)>0:
    item_to_workon = todo.pop()

    for each_branch in branches:
        stuff.do()
        data.process()

        if end_condition.met()
            done.append(processing_data)
        else
            todo.append([stuff, processing_data])

return done

That is, pop an item off the todo list, process the node, and put the resulting nodes back on the todo list (unless the end condition is met in which case it’s done). Then return the done list when your todo list is empty.

So it starts with the first letter, tries one or two element combinations, and, if it succeeds in finding a match, puts the item (remaining characters) back on the todo list along with processing data, i.e. the list of elements. If it runs out of possible paths through the rest of the word and you haven’t hit the end of the word, that means that you can’t spell it with the given elements.

This implementation, I hope you’ll agree, is a lot cleaner and understandable than using recursion. It’s also less computationally intensive because you don’t need to redefine a new variable space with its own scope at every node (i.e. every function call), thereby using less resources.

Getting the library

To get the library, pip install chemify.

Using the library

The library contains two functions, chemify and chemify_words. The first is targeted at single words or lists of words, the latter is a rather quick way to run through a phrase keeping the words separate (i.e. the last letter of a word won’t be combined with the first letter of the next).

An example is:

>>> from chemify import chemify
>>> chemify('georgescu')
['[Ge][O][Rg][Es][Cu]', '[Ge][O][Rg][Es][C][U]']

>>> chemify('georgescu',shortest=True)`
'[Ge][O][Rg][Es][Cu]'

or:

>>> from chemify import chemify_words
>>> chemify_words('In case of fire, use stairs.')
'[In] [Ca][Se] [O][F] [F][I][Re] [U][Se] [S][Ta][Ir][S]'

but:

>>> chemify_words('In case of fire, do not use elevator')
 "do" failed to convert
 "not" failed to convert
 "elevator" failed to convert

Look at the function help for more input/output options, output upon success or failure to convert, custom elements, etc.

Misc

Email nsgeorgescu@gmail.com with issues and questions or open an issue at https://github.com/NGeorgescu/chemify or if you think there’s some functionality that would be cool to add.

Thanks and Enjoy!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chemify-0.0.4.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

chemify-0.0.4-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

Details for the file chemify-0.0.4.tar.gz.

File metadata

  • Download URL: chemify-0.0.4.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for chemify-0.0.4.tar.gz
Algorithm Hash digest
SHA256 914cb3a5158535a8f25d84c6225e4bd8fef37f21867b77c583bd22e4a445ed08
MD5 0286d2feff1fc89284be1467c0b8e00a
BLAKE2b-256 c7a5e0ac9caa59201fc8a361383f2608c633abec4409ca1580fce71df893a5a5

See more details on using hashes here.

File details

Details for the file chemify-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: chemify-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 18.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for chemify-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e13b7972a60daae62dbce0ba30dff86e39e174e2bb960f56c4d5a3e88af2127c
MD5 dc1c0a545c037b77a8affd4cf7756eff
BLAKE2b-256 351575de55760f3c8caace34b6d38d14a07d2f60a7f9bcde3dffc9ccce1202aa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page