def test_sort_words(): source = unit6utils.get_input_file("unit6_testinput_02.txt") expected = unit6utils.get_input_file("unit6_expectedoutput_02.txt") destination = unit6utils.get_temp_file("unit6_output_02.txt") sort_words(source, destination) result = [word.strip() for word in open(destination)] expected = [word.strip() for word in open(expected)] assert expected == result
def test_anagram_sort(): source = unit6utils.get_input_file("unit6_testinput_03.txt") expected = unit6utils.get_input_file("unit6_expectedoutput_03.txt") destination = unit6utils.get_temp_file("unit6_output_03.txt") anagram_sort(source, destination) result = [word.strip() for word in open(destination)] print result expected = [word.strip() for word in open(expected)] print expected assert expected == result
def test_anagram_sort(): import sys print("sdv") print(sys.argv) source = unit6utils.get_input_file("unit6_testinput_03.txt") expected = unit6utils.get_input_file("unit6_expectedoutput_03.txt") destination = unit6utils.get_temp_file("unit6_output_03.txt") anagram_sort(source, destination) result = [word.strip() for word in open(destination)] expected = [word.strip() for word in open(expected)] assert expected == result
def main(): s = sys.argv[1] + ".txt" d = sys.argv[1] + "-results.txt" source = unit6utils.get_input_file(s) destination = unit6utils.get_temp_file(d) unit6_assignment_03.anagram_sort(source, destination)
Main_words = [ word.strip() + '\n' for word in sr_file.readlines() if word[0] != ' ' and word[0] != '#' and word[0] != '\n' ] while len(Main_words) != 0: words = Main_words I = Main_words[0] Anagram_dict[I] = [I] for word in words[1:]: if is_anagram(I.lower(), word.lower()): Anagram_dict[I].append(word) Main_words.remove(word) Main_words.remove(I) output = [ tuple(sorted(x, key=lambda x: x.lower())) for x in Anagram_dict.values() ] output.sort(key=lambda x: x[0].lower()) output.sort(key=lambda x: len(x), reverse=True) result = [word for Tuple in output for word in Tuple] ds_file.truncate() ds_file.writelines(result) ds_file.close() if __name__ == "__main__": import unit6utils source = unit6utils.get_input_file("unit6_testinput_03.txt") destination = unit6utils.get_temp_file("unit6_output_03.txt") anagram_sort(source, destination) #sys.exit(main())
processed_words.append(words[j]) if len(anagram_group) > 0: anagram_group = sorted(anagram_group, key=lambda s: s.lower()) anagram_groups.append(anagram_group) anagram_groups = sorted(anagram_groups, key=lambda lst: lst[0].lower()) anagram_groups = sorted(anagram_groups, key=lambda lst: (len(lst)), reverse=True) f.close() f = open(destination, 'w') output = "" for group in anagram_groups: output += '\n'.join(group) output += '\n' f.write(output) f.close() if __name__ == "__main__": fin = sys.argv[1] fout = fin[:fin.find('.txt')] + "-results.txt" try: source = unit6utils.get_input_file(fin) destination = unit6utils.get_input_file(fout) anagram_sort(source, destination) except: print("Some error occured! Try again.") else: print('Completed successfully.') #sys.exit(main())
words = {} fword = 0 word = line.strip() for letter in word: fword = fword + ord(letter) result1 = check1(word, fword,k) destination.write('%s:' % word) num=1 for key,value in result1.items(): if num>k-1: destination.write('%s\n' % key) break destination.write('%s '%key) num+=1 destination.close() if __name__ == "__main__": sys.argv.append('lowercase60k.txt') sys.argv.append('destination.txt') sys.argv.append(2) inFile = sys.argv[1] outfile = sys.argv[2] k=sys.argv[2] source = unit6utils.get_input_file(inFile) destination = unit6utils.get_temp_file(outfile) fval(source,destination,1)
b = map(number_char_product, a) i = 0 j = 0 t = [] c = [] try: while i < len(b): if b[i] == b[j]: t.append(a[j]) j += 1 else: c.append(t) t = [] i = j except IndexError: c.append(t) c = [sorted(i, key=lambda x: x.lower()) for i in c] c = sorted(c, key=lambda x: (len(x), -number_char_product(x[0][0].lower())), reverse=True) with open(destination, 'w') as f: for s in c: for j in s: f.write(j + '\n') if __name__ == "__main__": source = unit6utils.get_input_file(sys.argv[1]) destination = unit6utils.get_temp_file(sys.argv[1][:-4] + '-results.txt') anagram_sort(source, destination) #sys.exit(main())