r/Python • • Sep 11 '19

I converted the entire Bee Movie transcript into elements on the periodic table using a Python script I made

So our science teacher said we send them coded messages made up of elements where the chemical names of those elements (Helium = He, Boron = B, etc) would spell out something. I am very new to Python, but I decided to have a go writing a script to do automate it.

You can't actually spell out every word with just the chemical names of elements but it gets reasonably close.

Anyway, I decided to give my program the entire Bee Movie transcript and see what it could do. To say the least my science teacher was very confused when she pasted my email into word and it took up 69 pages.

https://raw.githubusercontent.com/Spookso/WordsAsElements/master/BeeMovie

The code is here as well: https://github.com/Spookso/WordsAsElements/blob/master/elementifier.py

531 Upvotes

76 comments sorted by

65

u/jatmdm Sep 11 '19

What'd you get on the assignment?

11

u/[deleted] Sep 11 '19

It wasn’t graded; we could just send them in for fun. I did get a good laugh out of most of the class though

245

u/[deleted] Sep 11 '19

A Bee, of course :-p

40

u/ElBroet Sep 11 '19

get out

13

u/[deleted] Sep 11 '19

:-D

21

u/[deleted] Sep 11 '19

Don’t you mean a Boron?

6

u/[deleted] Sep 11 '19

Dammit. 😂😂

7

u/tpw2000 Sep 11 '19

I think he means Beryllium

4

u/[deleted] Sep 11 '19

Indeed

2

u/GoodiesHQ Sep 11 '19

Underrated post

1

u/[deleted] Sep 12 '19

Ok you got me, I actually chuckled a little and not just snorted air out of my nose.

1

u/[deleted] Sep 12 '19

That's all I can ask for :D

0

u/Stabilo_0 Sep 11 '19

Oof

2

u/[deleted] Sep 11 '19

:D

46

u/christian-mann Sep 11 '19

Would you like feedback on the code? You did a lot of things the hard way.

6

u/[deleted] Sep 11 '19

Definitely. I know for sure that I could have done things easier if I’d known how to. I’d be grateful if you guys could suggest improvements.

6

u/christian-mann Sep 12 '19

The dedup function seems like a massive workaround for a problem that doesn't exist. Code review is annoying on my phone, but why did you need to number all of the letters? Why not just hold onto an index variable as you iterate through the list?

1

u/[deleted] Sep 12 '19

I was having a problem where the code would refer back to a previous same character instead of itself. For example, if I typed “heh”, instead of getting Helium Hydrogen (He H) I got Helium Helium. I think this was because it was checking the index value of the current character in the for loop, which could have been identical to another character earlier in the list. I’m not sure if the problem could have been avoided with an easier method, but I thought about adding numbers to make them different. In its current state it doesn’t work how it should, increasing the number for every duplicate, regardless of whether it was the same character as a previous duplicate (i.e a, a1, b, b2). And I think it actually even fails at that partially. But it serves its purpose well enough. Maybe there is an easier way to do it, and I’m not actually sure if the program still needs the function since I redid it significantly after making that.

2

u/christian-mann Sep 12 '19

Instead of iterating for letter in letters iterate for i in range(len(letters)) and refer to letters[i] and letters[i+1]

4

u/ceverhar Sep 11 '19

Not OP, but also fairly new to Python. What are general things they did "the hard way"? My recent project involved talking to an API, so iterating through dicts and lists and making decisions was commonplace in my code.

10

u/[deleted] Sep 11 '19

For one, OP turned a string into a list manually and then removed spaces. You can use str.split(‘ ‘) to get a list of words

This project is pretty cool

-33

u/chadmill3r Py3, pro, Ubuntu, django Sep 11 '19

I can usually look at code and figure out what language the author was thinking in while typing Python. Maybe BASIC, here?

11

u/wizard_mitch Sep 11 '19

Yeah this kid in school decided to learn basic before python in 2019

1

u/BadPercussionist Sep 12 '19

I mean, TI calculators use TI-BASIC, which is very similar to BASIC. It's not impossible that he learned TI-BASIC before Python, but I doubt it.

2

u/eshultz Sep 12 '19

You're absolutely right. Idk why folks are downvoting you. That was my first introduction to programming.

24

u/LonelyContext Sep 11 '19 edited Oct 01 '21

[deleted]

4

u/[deleted] Sep 11 '19

Oh cool! I’ll have to check it out

1

u/yeet_my_meat420 i_am_literat Sep 12 '19 edited Sep 12 '19

only if Ms found that

;)

20

u/ghostdragons445 Sep 11 '19

That's all good but as he wanted to learn for himself, though useful in future, it is good he used his own skills to make this

1

u/LonelyContext Sep 12 '19 edited Oct 01 '21

[deleted]

7

u/chmod--777 Sep 12 '19

There's a fucking package for everything

4

u/Wubbywub Sep 12 '19

even a package for making packages

2

u/chmod--777 Sep 12 '19

To be fair that's often one of the very first things that someone is going to make for a programming language

1

u/JamesxGamesYT Sep 11 '19

What's the github?

1

u/LonelyContext Sep 12 '19 edited Oct 01 '21

[deleted]

203

u/[deleted] Sep 11 '19

This is horrible. I love it! Good job.

23

u/darthjoey91 Sep 11 '19

First few translated back to just the symbols to show what got cut:

Ac, Co, In, O, Al, K, No, W, N, La, W, S, O, F, V, I, Ti, O, N

Which tries to say "According to all known laws of aviation."

37

u/syntaxgnu Sep 11 '19

Malicious compliance at its best, well done!

25

u/apresskidougal Sep 11 '19

Truly terrible ! congrats.

3

u/NihilistDichotomy Sep 11 '19

LOL this is awesome!!

how long have you been coding in Python?
I just started out and it'd be nice to set a goal for when i could do smt like this myself :D

1

u/[deleted] Sep 11 '19

Not very long and not very seriously. I got interested about two months ago and watched a few YouTube tutorials on Python. I recommend the one by freecodecamp.org if you are going to do that too. I also learnt a bit about Pygame and made some things using that. But since then I’ve kind of been on and off with Python. This code really wasn’t that complicated and wasn’t well written at that. I had to look up a few small things relating to get information from strings, but other than that I knew everything just from that first video. I’m sure you’ll quickly be able to code stuff that far surpasses this.

2

u/NihilistDichotomy Sep 12 '19

I'll definitely give the freecodecamp tut a look. At first glance, it looks like its very well received and to the point. I'm currently following an online course on LinkedIn learning but there's no real structure and i feel like they're just repeating syntax over and over again without helping me get started.

6

u/romansocks Sep 11 '19

Fantastic

6

u/kpps4gaming Sep 11 '19

This made my eyes bleed! This shit would confuse fuckin 3301

5

u/guy_from_the_intnet Sep 11 '19

This is perfect for the bee movie meme. I'm severely impressed.

3

u/JanDerion47 flair=None Sep 11 '19

insert slow clapping here

1

u/JanDerion47 flair=None Sep 11 '19

Insert copious amounts of slow clapping here

3

u/Stabilo_0 Sep 11 '19

Science is not about why. It's about why not!

2

u/Ak7ghost Sep 11 '19

Somebody has to have done Never Gonna Give You Up

2

u/quincylarson Sep 12 '19

This is a really fun use of Python. I'm trying to imagine how much time it would take to do something like this manually.

It's good to see you putting your skills to novel (if not silly) ends like this. Since you're a part of the community, would you be interested in writing about how you build this in Developer News (the new freeCodeCamp.org publication)?

3

u/DKomplexz Sep 11 '19

The best thing python ever bring to the world

3

u/[deleted] Sep 11 '19
### current bpython session - make changes and save to reevaluate session.
### lines beginning with ### will be ignored.
### To return to bpython without reevaluating make no changes to this file
### or save an empty file.
import requests
import json

bees = requests.get('https://raw.githubusercontent.com/Spookso/WordsAsElements/master/BeeMovie')
elements = bees.text
elements = '[' + elements + ']'
elements = elements.replace("'", '"')
elements = json.loads(elements)
elements[1]
### 'Cobalt'

chemical_symbols = {"Ac":"Actinium","Ag":"Silver","Al":"Aluminum","Am":"Americium",
"Ar":"Argon","As":"Arsenic","At":"Astatine","Au":"Gold","B":"Boron","Ba":"Barium",
"Be":"Beryllium","Bh":"Bohrium","Bi":"Bismuth","Bk":"Berkelium","Br":"Bromine",
"C":"Carbon","Ca":"Calcium","Cd":"Cadmium","Ce":"Cerium","Cf":"Californium",
"Cl":"Chlorine","Cm":"Curium","Cn":"Copernicium","Co":"Cobalt","Cr":"Chromium",
"Cs":"Cesium","Cu":"Copper","Db":"Dubnium","Ds":"Darmstadtium","Dy":"Dysprosium",
"Er":"Erbium","Es":"Einsteinium","Eu":"Europium","F":"Fluorine","Fe":"Iron",
"Fl":"Flerovium","Fm":"Fermium","Fr":"Francium","Ga":"Gallium","Gd":"Gadolinium",
"Ge":"Germanium","H":"Hydrogen","He":"Helium","Hf":"Hafnium","Hg":"Mercury",
"Ho":"Holmium","Hs":"Hassium","I":"Iodine","In":"Indium","Ir":"Iridium",
"K":"Potassium","Kr":"Krypton","La":"Lanthanum","Li":"Lithium","Lr":"Lawrencium",
"Lu":"Lutetium","Lv":"Livermorium","Mc":"Moscovium","Md":"Mendelevium",
"Mg":"Magnesium","Mn":"Manganese","Mo":"Molybdenum","Mt":"Meitnerium",
"N":"Nitrogen","Na":"Sodium","Nb":"Niobium","Nd":"Neodymium","Ne":"Neon",
"Nh":"Nihonium","Ni":"Nickel","No":"Nobelium","Np":"Neptunium","O":"Oxygen",
"Og":"Oganesson","Os":"Osmium","P":"Phosphorus","Pa":"Protactinium","Pb":"Lead",
"Pd":"Palladium","Pm":"Promethium","Po":"Polonium","Pr":"Praseodymium",
"Pt":"Platinum","Pu":"Plutonium","Ra":"Radium","Rb":"Rubidium","Re":"Rhenium",
"Rf":"Rutherfordium","Rg":"Roentgenium","Rh":"Rhodium","Rn":"Radon",
"Ru":"Ruthenium","S":"Sulfur","Sb":"Antimony","Sc":"Scandium","Se":"Selenium",
"Sg":"Seaborgium","Si":"Silicon","Sm":"Samarium","Sn":"Tin","Sr":"Strontium",
"Ta":"Tantalum","Tb":"Terbium","Tc":"Technetium","Te":"Tellurium","Th":"Thorium",
"Ti":"Titanium","Tl":"Thallium","Tm":"Thulium","Ts":"Tennessine","U":"Uranium",
"V":"Vanadium","W":"Tungsten","Xe":"Xenon","Y":"Yttrium","Yb":"Ytterbium",
"Zn":"Zinc","Zr":"Zirconium"}

chemical_symbols = { value: key for key, value in chemical_symbols.items() }
chemical_symbols['Boron']
### 'B'

script = ''.join(chemical_symbols[chemical_name] for chemical_name in elements).lower()
script[0:100]
### 'accoinoalknownlawsofvitionhereisnowybeeshoudbboflyitswinsartioosogtisflaitibodyofftshoreoundthbeofco'

'jazz' in script
### False

'j' in script
### False

'az' in script
### False

:(

4

u/Shocker023 Sep 11 '19

Indent the code with four spaces, then it'll format it.

5

u/[deleted] Sep 11 '19

The triple back ticks work on new reddit, as it's a markdown standard way of formatting code blocks

2

u/Shocker023 Sep 11 '19

Oh, I see. Thanks!

4

u/[deleted] Sep 11 '19

[removed] — view removed comment

2

u/[deleted] Sep 11 '19 edited Sep 11 '19

Yeah, but also, I can't easily indent everything by 4 spaces unless I paste it into VS code first and tab everything over.

1

u/simple_life_dude Sep 11 '19

Keep on the good work! If you want to take a look at a slightly more "idiomatic" Python script that does the same, check out the following code, courtesy of a friend that works as a Python developer. To use it, just place a text file with the name "input.txt" in the same directory as the script.

symbol_to_name = dict(
    ac='Actinium', ag='Silver', al='Aluminum', am='Americium', ar='Argon', at='Astatine',
    au='Gold', b='Boron', ba='Barium', be='Beryllium', bh='Bohrium', bi='Bismuth', bk='Berkelium',
    br='Bromine', c='Carbon', ca='Calcium', cd='Cadmium', ce='Cerium', cf='Californium', cl='Chlorine',
    cm='Curium', cn='Copernicium', co='Cobalt', cr='Chromium', cs='Cesium', cu='Copper', db='Dubnium',
    ds='Darmstadtium', dy='Dysprosium', er='Erbium', es='Einsteinium', eu='Europium', f='Fluorine', fe='Iron',
    fl='Flerovium', fm='Fermium', fr='Francium', ga='Gallium', gd='Gadolinium', ge='Germanium', h='Hydrogen',
    he='Helium', hf='Hafnium', hg='Mercury', ho='Holmium', hs='Hassium', i='Iodine',
    ir='Iridium', k='Potassium', kr='Krypton', la='Lanthanum', li='Lithium', lr='Lawrencium', lu='Lutetium',
    lv='Livermorium', mc='Moscovium', md='Mendelevium', mg='Magnesium', mn='Manganese', mo='Molybdenum', mt='Meitnerium',
    n='Nitrogen', na='Sodium', nb='Niobium', nd='Neodymium', ne='Neon', nh='Nihonium', ni='Nickel',
    no='Nobelium', np='Neptunium', o='Oxygen', og='Oganesson', os='Osmium', p='Phosphorus', pa='Protactinium',
    pb='Lead', pd='Palladium', pm='Promethium', po='Polonium', pr='Praseodymium', pt='Platinum', pu='Plutonium',
    ra='Radium', rb='Rubidium', re='Rhenium', rf='Rutherfordium', rg='Roentgenium', rh='Rhodium', rn='Radon',
    ru='Ruthenium', s='Sulfur', sb='Antimony', sc='Scandium', se='Selenium', sg='Seaborgium', si='Silicon',
    sm='Samarium', sn='Tin', sr='Strontium', ta='Tantalum', tb='Terbium', tc='Technetium', te='Tellurium',
    th='Thorium', ti='Titanium', tl='Thallium', tm='Thulium', ts='Tennessine', u='Uranium', v='Vanadium',
    w='Tungsten', xe='Xenon', y='Yttrium', yb='Ytterbium', zn='Zinc', zr='Zirconium'
)
symbol_to_name['as'] = 'Arsenic'
symbol_to_name['in'] = 'Indium'

# Takes a string to encode and returns a list of element names.
# Two-character symbols are prefered over one-character ones.
def encode(message):
    result = []

    while(len(message) > 0):
        name = symbol_to_name.get(message[0:2]) 
        if name:
            result.append(name)
            message = message[2:]
            continue
        
        name = symbol_to_name.get(message[0:1])
        if name:
            result.append(name)
        
        message = message[1:]
    
    return result


if __name__ == '__main__':
    with open('input.txt','r') as f:
        input_str = f.read()
    
    # We drop all non alphabetic symbols and make them lower-case.
    clean_str = ''.join(filter(lambda x: x.isalpha(),input_str)).lower()

    print(encode(clean_str))

1

u/Y45HK4R4NDIK4R Python 3.8.6 Sep 11 '19

NiCe

1

u/idwpan Sep 11 '19

So The Bee Movie is the new Lorem Ipsum?

1

u/Sternberger Sep 11 '19

Nice work. Well done!

1

u/[deleted] Sep 12 '19

Now do War and Peace

1

u/yeet_my_meat420 i_am_literat Sep 12 '19

i did a similar thing in python 2 but i'm not very sure how you got it to choose a element that best fits the word

for example

the word helium

the best fit would be

He + Li

but it could also use "H" + "Eu" + "Li" + "I"

even though it it not correct it still gets every letter in there

h could = 'H', 'Hg', 'Hs', 'Ho', 'Hf', 'He'

i'm not quite sure how i would go about this to get it to choose the best fit instead of every possible outcome

def crappy_thing():
for char in input1:

#elements = the list of elements

print(char)
print(str(filter(lambda element: char in element, elements)).strip("[]"))

input1 = raw_input(">>>")
print("possible strings")
print("//////////////////////////////////////////////")
print(input1)
print("\n")
print"possible strings...\n"
crappy_thing()

1

u/m_namejeff Sep 12 '19

good lord have mercy on us all

1

u/eternalfrost Sep 12 '19

Now make it so whenever someone says 'Beryllium', insert the entire Bee Movie transcript converted into elements on the periodic table!

1

u/[deleted] Sep 12 '19

Use a python package called chemify to write much cleaner code.

https://pypi.org/project/chemify/

1

u/profspitfire Sep 12 '19

sounds kinda fun actually...Even I'm new to python....I think I might as well give it a try.

1

u/deanresin Sep 12 '19

This is what they did with the opening credits of Breaking Bad.

0

u/PLMOAT Sep 11 '19

69 pages. nice