-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathunit6_assignment_03.py
More file actions
56 lines (41 loc) · 2.4 KB
/
Copy pathunit6_assignment_03.py
File metadata and controls
56 lines (41 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
__author__ = 'Kalyan'
notes = '''
This problem will require you to put together many things you have learnt
in earlier units to solve a problem.
In particular you will use functions, nested functions, file i/o, functions, lists, dicts, iterators, generators,
comprehensions, sorting etc.
Read the constraints carefully and account for all of them. This is slightly
bigger than problems you have seen so far, so decompose it to smaller problems
and solve and test them independently and finally put them together.
Write subroutines which solve specific subproblems and test them independently instead of writing one big
mammoth function.
Do not modify the input file, the same constraints for processing input hold as for unit6_assignment_02
'''
problem = '''
Given an input file of words (mixed case). Group those words into anagram groups and write them
into the destination file so that words in larger anagram groups come before words in smaller anagram sets.
With in an anagram group, order them in case insensitive ascending sorting order.
If 2 anagram groups have same count, then set with smaller starting word comes first.
For e.g. if source contains (ant, Tan, cat, TAC, Act, bat, Tab), the anagram groups are (ant, Tan), (bat, Tab)
and (Act, cat, TAC) and destination should contain Act, cat, TAC, ant, Tan, bat, Tab (one word in each line).
the (ant, Tan) set comes before (bat, Tab) as ant < bat.
At first sight, this looks like a big problem, but you can decompose into smaller problems and crack each one.
source - file containing words, one word per line, some words may be capitalized, some may not be.
- read words from the source file.
- group them into anagrams. how?
- sort each group in a case insensitive manner
- sort these groups by length (desc) and in case of tie, the first word of each group
- write out these groups into destination
'''
import unit6utils
import string
def anagram_sort(source, destination):
pass
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)]
expected = [word.strip() for word in open(expected)]
assert expected == result