Demo entry 6718403

morse

   

Submitted by anonymous on Mar 04, 2018 at 19:04
Language: HTML. Code size: 5.9 kB.

def letter_to_code(letter):
    return letter_to_code[letter]

space_between_letters = "..." #3
space_between_words = "......." #7

def word_to_code(word):
    word1 = word.upper()
    newWord = ""
    for i in (list(word1)):  #see comments in next def 
        if i in code:
            if i == word1[-1]:
                newWord += code[i] 
            else:
                newWord += code[i]
                newWord += space_between_letters
    return newWord

    #takes a word(string without spaces) and produces Morse string

def sentence_to_code(s):
    
    s1 = s.upper() #makes everything uppercase in case a string contains lowercase letters
    newS = ""
    s1 = s1.split(" ")
    
    for i in (s1):
        if i == s1[0]: #if its the first index in the list (string/word)
            for letter in i:
                if letter in code:
                    if letter == i[0]:
                        newS+= code[letter]
                    else: 
                        newS += "..."
                        newS += code[letter]
        else:
            for letter in i:
                if letter in code:
                    if letter == i[0]:
                        newS += "......."
                        newS += code[letter]
                    else: 
                        newS += "..."
                        newS += code[letter]


    return newS
    

def code_to_letters(s):
    
    d = {}
    for k,v in code.items(): # reverse the dictionary. then look for your morse letter
        d[v] = k
    if s in d:
        return d[s]
    #takes a single morse code entry and returns the letter

def code_to_sentence(s):
    sentence = ""
    newS = s.split(".......") #splits the given string into a list of lists, each list starting after "......."
    newS = [i.split("...") for i in newS] # this was hard. this splits a list in a list, by going in and splitting each individual list where it finds "..."
    
    for list in newS:                     # but it keeps it in the same amount of lists from the original split (".......")
        sentence += " " # to iterate the big list and add a space 
        for letter in list: # to iterate over the smaller lists
            if letter in d:
                sentence += d[letter]
    return sentence[1:] # my code has it start with a space, so to compare things without error, this returns it at the first letter (index 1) rather than the space (index 0)
    

    
    # takes a morse code sentences and returns a string of words

code = {'A': '=.===', 
'B':'===.=.=.=', 
'C': '===.=.===.=', 
'D': '===.=.=', 
'E': '=', 
'F': '=.=.===.=', 
'G': '===.===.=', 
'H': '=.=.=.=', 
'I': '=.=',
'J': '=.===.===.===', 
'K': '===.=.===', 
'L': '=.===.=.=', 
'M': '===.===', 
'N': '===.=', 
'O': '===.===.===', 
'P': '=.===.===.=', 
'Q': '===.===.=.===', 
'R': '=.===.=', 
'S': '=.=.=', 
'T': '===', 
'U': '=.=.===', 
'V': '=.=.=.===', 
'W': '=.===.===', 
'X': '===.=.=.===', 
'Y': '===.=.===.===', 
'Z': '===.===.=.=',}

'''a': '=.===', 
'b':'===.=.=.=', 
'c': '===.=.===.=', 
'd': '===.=.=', 
'e': '=', 
'f': '=.=.===.=', 
'g': '===.===.=', 
'h': '=.=.=.=', 
'i': '=.=',
'j': '=.===.===.===', 
'k': '===.=.===', 
'l': '=.===.=.=', 
'm': '===.===', 
'n': '===.=', 
'o': '===.===.===', 
'p': '=.===.===.=', 
'q': '===.===.=.===', 
'r': '=.===.=', 
's': '=.=.=', 
't': '===', 
'u': '=.=.===', 
'v': '=.=.=.===', 
'w': '=.===.===', 
'x': '===.=.=.===', 
'y': '===.=.===.===', 
'z': '===.===.=.='''

#reverse morse code dictionary code-> letter

d = {}
for k,v in code.items():
    d[v] = k

#testing 

def testing(s,c):
    if sentence_to_code(s) == c:
        print("S->c for {0} passed".format(s))
    else:
        print("S->c for {0} failed.".format(s))
    if code_to_sentence(c) == s:
        print("C->s for {0} passed.".format(s))
    else:
        print("C->s for {0} failed".format(s))

test_strings = ["MORSE CODE", "I NEED MONEY", "ORDER PIZZA"]


test_code = ['===.===...===.===.===...=.===.=...=.=.=...=.......===.=.===.=...===.===.===...===.=.=...=', 
             '=.=.......===.=...=...=...===.=.=.......===.===...===.===.===...===.=...=...===.=.===.===', 
             '===.===.===...=.===.=...===.=.=...=...=.===.=.......=.===.===.=...=.=...===.===.=.=...===.===.=.=...=.===']

test_code1 = '===.===...===.===.===...=.===.=...=.=.=...=.......===.=.===.=...===.===.===...===.=.=...=' 
test_code2 = '=.=.......===.=...=...=...===.=.=.......===.===...===.===.===...===.=...=...===.=.===.==='
test_code3 = '===.===.===...=.===.=...===.=.=...=...=.===.=.......=.===.===.=...=.=...===.===.=.=...===.===.=.=...=.==='

code_from_txt = "===.=.=...===.===.===.......===.=...===.===.===...===.......=.===.=...=.=.===...=.=.=...===...=.===.=.=...=.......===.===...===.=.===.===.......=.===.===.===...=.=...===.===...===.===...=.=...=...=.=.====...=.=.=.=...=.=...=.=.=.......=.=...=.=.=.......=.===.===...=.===.=...===.===.===...===.=...===.===.====.=.===.=...=.===...=.===.===.=...=.=.===...===.......===.===...=.=...=.=.=.=...=.=.......===.=.=...===.===.===...=.===.=.=...=...==="
for s,c in zip(test_strings, test_code):  # I tried to use triple quotes to move it into multiple lines and make it less ugly, but it messed up the code somehow
    testing(s,c)                          # so I left it ugly. 

print(code_to_sentence(test_code1)) # >>> MORSE CODE
print(code_to_sentence(test_code2)) # >>> I NEED MONEY
print(code_to_sentence(test_code3)) # >>> ORDER PIZZA

print(sentence_to_code("I NEED MONEY")) #yay rewriting entire blocks of code
print(sentence_to_code("Morse Code"))  # because you don't account for a parameter and everything goes crazy when you try to edit one line

print(code_to_sentence(code_from_txt))
 # DO NOT RUSTLE MY JIMMIEHIS IS WRONAPUT MIHI DOLET

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).