I've been trying to make this more challenging for myself but I couldn't find a way to achieve this goal. So I wonder if its possible to separate words that have no space betweenThemLikeThisJustInASingleString
Here's my code:
def translate_morse(string):
morse_a = {
'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':'--..',1:'.----',2:'..---',3:'...--',4:'....-',5:'.....',6:'-....',7:'--...',8:'---..',9:'----.',0:'-----'}
translated = ""
for i in string.split(' '):
for key, value in morse_a.iteritems():
if i == value:
translated += key
print translated
print """
###################################
# #
# Morse Alphabet Translator #
# Author: Blackwolf #
# Date: 2016-29-05 #
# #
###################################
# #
# USAGE: #
# After each morse letter you #
# will need to seperate (space) #
# each letter #
###################################
"""
finished = False
while finished != True:
translate_morse(raw_input("Enter morse: "))
ask_ifFinished = raw_input('Are you done? (Y/N): ').lower()
if ask_ifFinished == 'y':
finished = True
If you want to insert a space before every capital letter:
import re
string = 'betweenThemLikeThisJustInASingleString'
print(re.sub('([A-Z])', r' \1', string))
Output:
between Them Like This Just In A Single String
There's no perfect solution for splitting up words where the boundary isn't marked. How can the computer tell that singlestring
should be split into single string
and not sing lest ring
( lest
is an English word if you're not aware)? There are heuristics you could apply to make decent guesses but they're complicated and limited. You could also list all the possible solutions but that will also be difficult at your level.
First post on stackoverflow :)
Was looking at how to solve this problem for something else and figured I'd share it.
REQUIREMENTS
def spacer(str):
sentence = [] # create empty list
sentence.append(str[0]) # put first letter in list. First letter doesn't need a space.
for char in str[1::]: # begin iteration after first letter
if char.islower():
sentence.append(char) # if character is lower add to list
elif char.isupper():
sentence.append( " ") # if character is upper add a space to list
sentence.append(char) # then add the upper case character to list
result = ''.join(sentence) # use () join to convert the list to a string
return result # return end result
RUN IT
string='betweenThemLikeThisJustInASingleString'
spacer(string)
OUTPUT
'between Them Like This Just In A Single String'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.