fromcollectionsimportdefaultdictfromdifflibimportSequenceMatcherfromoperatorimportitemgetterdefsimilar(a,b):returnSequenceMatcher(None,a,b).ratio()defmain():part_one()part_two()defpart_one():withopen('input.txt','r')asinput_file:lines=[lineforlineininput_file]word_twice_count=0word_three_times_count=0forlineinlines:word_count_dic=defaultdict(int)has_char_twice=Falsehas_char_three_times=Falseforcharinline:word_count_dic[char]+=1forkeyinword_count_dic.keys():ifword_count_dic[key]==2:has_char_twice=Trueelifword_count_dic[key]==3:has_char_three_times=Trueifhas_char_twice:word_twice_count+=1ifhas_char_three_times:word_three_times_count+=1checksum=word_twice_count*word_three_times_countprint'checksum is '+str(checksum)defpart_two():withopen('input.txt','r')asinput_file:lines=[lineforlineininput_file]similarity_strings=[(line,comparing_line,similar(line,comparing_line))forlineinlinesforcomparing_lineinlinesiflineisnotcomparing_line]most_similair_strings=max(similarity_strings,key=itemgetter(2))result_word=''.join([char_word_oneforchar_word_one,char_word_twoinzip(most_similair_strings[0],most_similair_strings[1])ifchar_word_one==char_word_two])printresult_wordif__name__=='__main__':main()
My solution with Python, not sure about the second part, not the most fastest solution I think regarding of performance.
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!
defpart_one():word_twice_count=0word_three_times_count=0withopen('input.txt','r')asinput_file:forlineininput_file:line_counter=Counter(line)if2inline_counter.values():word_twice_count+=1if3inline_counter.values():word_three_times_count+=1checksum=word_twice_count*word_three_times_countprint'checksum is '+str(checksum)
My edited solution for part one with collections Counter
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My solution with Python, not sure about the second part, not the most fastest solution I think regarding of performance.
Nice! Don’t forget about collections.Counter for part 1! I didn’t know about difflib though. Cool!
Thanks for the hint! Will do that later!
My edited solution for part one with collections Counter