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 ther 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.3.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: chemify-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 8ecd27d5e2d7b79837fe5fa2836c38f582302413c1e6f39e33776ce2be7e6383
MD5 8a107fa3013f202178892590933e01be
BLAKE2b-256 91a93473e9ae41abe34dbfdebb230b9af90b59ef85ac3a3cf52eda43711203c0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: chemify-0.0.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ba9749c3a1ec56a2c3c59574d34f7f033ab2983b8c6ea71cbf0b70ecc1b84123
MD5 ba4c9006625ad9b66c0928bad0356805
BLAKE2b-256 2b8ab6e24698c3e7d4cd9a5b9d76c5f98d1979ed86d76d25d7908fcffa32491d

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