def give_me_word(text):
ans=dict()
the_text=""
for i in text:
if i in ans:
ans[i]+=1
else:
ans[i]=1
comparison=ans['_']
sorted_list=sorted(ans.values(),reverse=True)
rev_dict = dict((v,k) for k,v in ans.iteritems())
for i in sorted_list:
if(i>=comparison and rev_dict[i]!='_'):
the_text=the_text+rev_dict[i]
elif rev_dict[i]=='_':
break
return the_text
text="the_text"
print give_me_word(text)
Not fully optimized script to do the same in python
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Nice! Thanks for sharing. :) Have you seen the built-in Counter class? This seems like a perfect use-case for it.
fromcollectionsimportCounterblob="..."letters="abcdefghijklmnopqrstuvwxyz_"defmessage(letters,blob):hist=Counter(blob)# Counter takes any iterable and makes# a histogram of its items!sorted_letters=''.join(x[0]forxinhist.most_common())returnsorted_letters.split('_')[0]print(message(letters,blob))
I wondered if I could do better performance-wise, based on knowledge of the problem that the more general built-in classes couldn't take advantage of. Ended up with this:
import string
longString="..."
def decode(msg):
# Testing suggests this is a little faster than using defaultdict
charCountHash = {}.fromkeys(string.ascii_lowercase,0)
charCountHash['_'] = 0
for ch in msg:
charCountHash[ch] += 1
c = charCountHash['_']
resultHash = {charCountHash[k]: k for k in charCountHash.keys() \
if charCountHash[k] > c }
return ''.join( [ resultHash[index] for index in \
sorted(resultHash.keys(), reverse=True) ] )
print(decode(longString))
Testing both implementations a few times suggests this way is faster, but only 1% or so.
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Nice! I haven't used fromkeys before. That's a nice alternative to defaultdict (and you don't have to import anything!) if you already know the keys you need.
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Not fully optimized script to do the same in python
Python is on my list of things to learn soon and can't help but remark how much it "looks" like Ruby without knowing much about it yet.
"...without knowing much about it yet." This is great about python. Nice challenge this one. :-)
Nice! Thanks for sharing. :) Have you seen the built-in Counter class? This seems like a perfect use-case for it.
I wondered if I could do better performance-wise, based on knowledge of the problem that the more general built-in classes couldn't take advantage of. Ended up with this:
Testing both implementations a few times suggests this way is faster, but only 1% or so.
Might try this in go, just for the practice...
Nice! I haven't used
fromkeys
before. That's a nice alternative to defaultdict (and you don't have to import anything!) if you already know the keys you need.Bonus! On my machine, this version is even 5 times faster than the best Ruby version!
This is great. Will use counter effectively. Thanks for showing me counter(). :-)