DESCRIPTION:
You are given a string of numbers between 0-9. Find the average of these numbers and return it as a floored whole number (ie: no decimal places) written out as a string. Eg:
"zero nine five two" -> "four"
If the string is empty or includes a number greater than 9, return "n/a"
def average_string(s):
s=s.split(" ")
adict={}
adict["zero"]="0"
adict["one"]="1"
adict["two"]="2"
adict["three"]="3"
adict["four"]="4"
adict["five"]="5"
adict["six"]="6"
adict["seven"]="7"
adict["eight"]="8"
adict["nine"]="9"
ans=0
for i in range(len(s)):
try:
s[i]=s[i].replace(s[i],adict[s[i]])
except:
return "n/a"
ans+=int(s[i])
return ([k for k,v in adict.items() if v == str(ans/len(s))[0]])[0]
'알고리즘 문제 > CodeWar' 카테고리의 다른 글
Transform to Prime (0) | 2022.11.06 |
---|---|
Autocomplete! Yay! (0) | 2022.11.06 |
Delete a digit (0) | 2022.11.04 |
Mean Square Error (0) | 2022.11.04 |
What century is it? (0) | 2022.11.04 |