I need to make a function that receives a string of numbers and the ouput is the letters that correspond to those numbers, like if you are sending a message on a cell phone. For example to get the letter 'A' the input should be '2', to get the letter 'B', the input should be '22', etc. For example:
>>>number_to_word('222 '233' '3'):
"CAFE"
The program needs to "go around" the number if the limit of the number is reached. For example, the letter 'A', can be these following inputs: '2', '2222','2222222', etc. Just like if your sending a text message on cell phone when you get pass 'C' ( which is'222' ) the program goes again to 'A', making '2222' it's key. Also, in the input, if the string is '233' the program must separate the different numbers, so this ('233') will be equal this ('2' '33')
I made a dictionary like this:
dic={'2':'A', '22':'B', '222':'C', '3':'D', '33':'E', '333':'F',..... etc}
But I don't know how to make the program "go around" if the input is '2222', and how do I take that number and assign it to the letter 'A'.
Feel free to ask any questions if you don't understand what I'm asking I would be glad to explain it with more detail. Thank you.
This seems to give the desired result:
NUMBERS = {'2': 'A', '22': 'B', '222': 'C', '3': 'D', '33':'E', '333': 'F'}
def normalize_number(number):
for item in number.split():
if len(set(item)) == 1:
yield item
else:
last = item[0]
res = [last]
for entry in item[1:]:
if entry == last:
res.append(entry)
else:
yield ''.join(res)
res = [entry]
last = entry
yield ''.join(res)
def number_to_word(number):
res = []
for item in normalize_number(number):
try:
res.append(NUMBERS[item])
except KeyError:
if len(item) >= 4:
end = len(item) % 3
if end == 0:
end = 3
res.append(NUMBERS[item[:end]])
return ''.join(res)
Test it:
>>> number_to_word('222 2 333 33')
'CAFE'
>>> number_to_word('222 2 333 3333')
'CAFE'
>>> number_to_word('222 2 333 333333')
'CAFE'
>>> number_to_word('222 2333 3333')
'CAFE'
The function normalize_number() turns numbers that have different digits into multiple numbers with only the same digit:
>>> list(normalize_number('222 2 333 3333'))
['222', '2', '333', '3333']
>>> list(normalize_number('222 2333 3333'))
['222', '2', '333', '3333']
>>> list(normalize_number('222 2 333 53333'))
['222', '2', '333', '5', '3333']
Related
The file contents are:
min:1,2,3,4,5,6
max:1,2,3,4,5,6
avg:1,2,3,4,5,6
Code:
def avg_calc():
total = 0
nums_inLine = line[4: -1]
num_list = []
num_list = nums_inLine.split(',')
length = len(num_list)
print(num_list)
with open('input.txt', 'r', encoding='utf-8-sig') as in_file:
content = in_file.readlines()
for line in content:
if 'min' in line:
min_calc()
elif 'max' in line:
max_calc()
elif 'avg' in line:
avg_calc()
My problem is that the '6' in the third line disappears or replaced by a whitespace. Why does it do that?
Output:
['1', '2', '3', '4', '5', '']
final goal is to cast each element in the list to an int then perform calculations to calculate the average of the numbers.
I have two lists of different lengths and i want match the items based on their actual relation. One list is the secondary structure elements and other list is aligned sequence. I want to match the secondary structure to its residues in the other list. And adjust the length of secondary structure by inserting '-' to that of gaps in the aligned sequence. The items in ss corresponds to RRCAVVTG in seq.
ss=['-', '-', 'E', 'E', 'E', 'E', 'S', 'S']
seq≈["---------------RRCAVVTG"]
for m in seq:
found=[i for i in list(m)]
sscount=0
sscount1=0
for char,ssi in zip(found,ss):
if char!='-' :
print char , sscount, ssi
sscount+=1
else:
print char, sscount1, '#'
sscount1+=1
The expected results:
---------------##EEEESS
---------------RRCAVVTG
But i get the following results:
- 0 #
- 1 #
- 2 #
- 3 #
- 4 #
- 5 #
- 6 #
- 7 #
I hope I understood the question right. First we fill the string ss with - and then compare it to the string inside seq using zip():
ss = ['-', '-', 'E', 'E', 'E', 'E', 'S', 'S']
seq = ["---------------RRCAVVTG"]
out = ''
for ch1, ch2 in zip('{:->{}}'.format(''.join(ss), len(seq[0])), seq[0]):
if ch1=='-' and ch2 !='-':
out += '#'
elif ch1=='-' and ch2 == '-':
out += '-'
else:
out += ch1
print(out)
print(seq[0])
Prints:
---------------##EEEESS
---------------RRCAVVTG
for m in seq:
found=[i for i in list(m)]
sscount=0
sscount1=0
num=0
for char,ssi in zip(found,itertools.cycle(ss)):
if char!='-' :
print char , sscount, ss[num]
d.append(ss[num])
num+=1
sscount+=1
else:
print char, sscount1, '#'
sscount1+=1
I need to convert a given string of numbers to the word those numbers correspond to. For example:
>>>number_to_word ('222 2 333 33')
'CAFE'
The numbers work like they do on a cell phone, you hit once on the second button and you get an 'A', you hit twice and you get an 'B', etc. Let's say I want the letter 'E', I'd have to press the third button twice.
I would like to have some help trying to understand the easiest way to do this function. I have thought on creating a dictionary with the key being the letter and the value being the number, like this:
dic={'A':'2', 'B':'22', 'C':'222', 'D':'3', 'E':'33',etc...}
And then using a 'for' cycle to read all the numbers the in the string, but I do not know how to start.
You need to reverse your dictionary:
def number_to_word(number):
dic = {'2': 'A', '22': 'B', '222': 'C', '3': 'D', '33': 'E', '333': 'F'}
return ''.join(dic[n] for n in number.split())
>>> number_to_word('222 2 333 33')
'CAFE'
Let's start inside out. number.split() splits the text with your number at white space characters:
>>> number = '222 2 333 33'
>>> number.split()
['222', '2', '333', '33']
We use a generator expression ((dic[n] for n in number.split())) to find the letter for each number. Here is a list comprehension that does nearly the same but also shows the result as a list:
>>> [dic[n] for n in number.split()]
['C', 'A', 'F', 'E']
This lets n run through all elements in the list with the numbers and uses n as the key in the dictionary dic to get the corresponding letter.
Finally, we use the method join() with an empty string as spectator to turn the list into a string:
>>> ''.join([dic[n] for n in number.split()])
'CAFE'
I'm trying to implement a hangman game. I want part of the function to check if a letter is correct or incorrect. After a letter is found to be correct it will place the letter in a "used letters" list and a "correct letters list" The correct letters list will be built as the game goes on. I'd like it to sort the list to match the hidden word as the game is going.
For instance let's use the word "hardware"
If someone guessed "e, a, and h" it would come out like
correct = ["e", "a", "h"]
I would like it to sort the list so it would go
correct = ["h", "a", "e"]
then
correct = ["h", "a", "r", "a", "e"] after r has been guessed.
I also need to know if it would also see that "a" is in there twice and place it twice.
My code that doesn't allow you to win but you can lose. It's a work in progress.
I also can't get the letters left counter to work. I've made the code print the list to check if it was adding the letters. it is. So I don't know what's up there.
def hangman():
correct = []
guessed = []
guess = ""
words = ["source", "alpha", "patch", "system"]
sWord = random.choice(words)
wLen = len(sWord)
cLen = len(correct)
remaining = int(wLen - cLen)
print "Welcome to hangman.\n"
print "You've got 3 tries or the guy dies."
turns = 3
while turns > 0:
guess = str(raw_input("Take a guess. >"))
if guess in sWord:
correct.append(guess)
guessed.append(guess)
print "Great!, %d letters left." % remaining
else:
print "Incorrect, this poor guy's life is in your hands."
guessed.append(guess)
turns -= 1
print "You have %d turns left." % turns
if turns == 0:
print "HE'S DEAD AND IT'S ALL YOUR FAULT! ARE YOU HAPPY?"
print "YOU LOST ON PURPOSE, DIDN'T YOU?!"
hangman()
I'm not entirely clear on the desired behavior because:
correct = ["h", "a", "r", "a", "e"] after r has been guessed.
This is strange because a has only been guessed once, but shows up for each time it appears in hardware. Should r should also appear twice? If that is the correct behavior, then a very simple list comprehension can be used:
def result(guesses, key):
print [c for c in key if c in guesses]
In [560]: result('eah', 'hardware')
['h', 'a', 'a', 'e']
In [561]: result('eahr', 'hardware')
['h', 'a', 'r', 'a', 'r', 'e']
Iterate the letters in key and include them if the letter has been used as a "guess".
You can also have it insert a place holder for unfound characters fairly easily by using:
def result(guesses, key):
print [c if c in guesses else '_' for c in key]
print ' '.join([c if c in guesses else '_' for c in key])
In [567]: result('eah', 'hardware')
['h', 'a', '_', '_', '_', 'a', '_', 'e']
h a _ _ _ a _ e
In [568]: result('eahr', 'hardware')
['h', 'a', 'r', '_', '_', 'a', 'r', 'e']
h a r _ _ a r e
In [569]: result('eahrzw12', 'hardware')
['h', 'a', 'r', '_', 'w', 'a', 'r', 'e']
h a r _ w a r e
Can someone please suggest an approach to write the piece of codes that will automatically maps the letter in letter_str onto the dic_key (a dict key string type that contains dashes that match the length of words in word_lst)?
So, the mapping only occurs if a letter appears in every words in the list at the same position no matter how many words is in the list.
If no letter appears at any position for all the words in the word list then the new_dic_key would be '----'. Please see the examples below
Thanks
word_lst = ['ague', 'bute', 'byre', 'came', 'case', 'doze']
dic_key = '----'
letters_str ='abcdefghijklmnopqrstuvwxyz'
new_dic_key = '---e'
if
word_list = ['bute', 'byre']
new_dic_key = 'b--e'
or
word_list = ['drek', 'drew', 'dyes']
new_dic_key = 'd-e-'
If the words in the word_list will be of the same length this code will give what you want:
word_list = ['drek', 'drew', 'dyes']
cols = []
for i in range(len(word_list[0])):
cols.append([])
for word in word_list:
for i, ch in enumerate(word):
cols[i].append(ch)
pattern = [item[0] if len(set(item)) == 1 else '-' for item in cols]
print ''.join(pattern)
d-e-
Explanation:
We initialize cols to be a list of list. It will contain two dimensional representation of the letters in the words of word_list. After populating cols this is what it looks like:
[['d', 'd', 'd'], ['r', 'r', 'y'], ['e', 'e', 'e'], ['k', 'w', 's']]
So the final result new_dic_key will contain the letter only if all elements in the sub-list above have the same letter, otherwise it will contain a -. This is achieved using the list comprehension for pattern.
Hope it helps.