.. _homework5: Homework 5 (Due 6 pm, Wednesday, 11/15/2017) ########################################################################## Please submit your homework to your git repo by **6 pm, Wednesday, 11/15/2017**. 1. Download a file :download:`words.txt <./words.txt>` and study the example code below: .. literalinclude:: ./words_example.py :language: python :linenos: (a) Write a function which reads the first N many words from the file ``words.txt`` and builds a list with one element per word. N is a user input number (Hint: use the ``append`` method). In addition, how can you read in ALL of the words and use them to build a list without knowing the total number of words in ``words.txt``? (b) Write a function which reads the first N many words from ``words.txt`` and stores them as keys in a dictionary. It doesn't matter what the values are. Use the ``in`` operator to check whether a user input string (e.g., ``aa``) is in the dictionary. Again, please do the same to build a dictionary that includes ALL the words as keys in ``words.txt``. 2. Write a Python routine that reads in :download:`words.txt <./words.txt>` as a string and counts how many times each individual chracter appears. Your screen output will show the information in column where each line contains a pair in tuple ``('character', frequency)``. Your answer should be able to produce a result that look like:: ('\n', 113809) ('e', 106758) ('s', 86547) ('i', 77412) ('a', 68582) ('r', 64965) ('n', 60513) ('t', 57059) ('o', 54542) ('l', 47011) ('d', 34552) ('c', 34287) ('u', 31161) ('g', 27848) ('p', 25789) ('m', 24741) ('h', 20200) ('b', 17798) ('y', 13473) ('f', 12714) ('k', 9370) ('v', 9186) ('w', 8535) ('z', 3750) ('x', 2700) ('j', 1780) ('q', 1632) Hints: (1) Take a look at the `histogram.py example `_ of using dictionary as a set of counters. (2) A simple routine that reads in ``words.txt`` and converts it to a string may look like:: def file2string(filename): fstring=open(filename).read() return fstring 3. Implement a Python routine that converts a length in a given unit system into others systems. The unit systems we are considering include: * meter, yard, mile, inch, and foot. The conversion factors are given as:: 1 meter = 0.000621 miles = 39.370079 inches = 3.28084 feet = 1.093613 yards The routine takes two inputs from users: 1. a numerical value of length, 2. a unit system of the length input Your routine should convert a length in a requested unit system into the other systems. Please make sure that your implementation should be general enough to take any unit system as an input, and convert it to the rest. For instance, the output needs to look like:: $ python yourRoutine.py Please input a length (number only): 10 Please type a unit system (meter, mile, inch, foot, yard): yard ['0.00567842554907 mile', '360.000100584 inch', '30.000009144 foot', '9.14400249448 meter'] $ python yourRoutine.py Please input a length (number only): 159 Please type a unit system (meter, mile, inch, foot, yard): yard ['0.0902869662303 mile', '5724.00159929 inch', '477.00014539 foot', '145.389639662 meter'] and similarly for the rest unit systems. In addition, please extend your routine to convert a requested length into the SI multiples for meter: * nanometer (nm), micrometer (um), millimiter (mm), centimeter (cm), meter (m), kilimeter (km). The conversion factors are given by:: 1 meter = 10^9 nm = 10^6 um = 10^3 mm = 10^2 cm = 10^(-3) km The full output now looks like, for the first case:: Please input a length (number only): 10 Please type a unit system (meter, mile, inch, foot, yard): yard ['0.00567842554907 mile', '360.000100584 inch', '30.000009144 foot', '9.14400249448 meter'] ['9144002494.48 nm', '9144002.49448 um', '9144.00249448 mm', '914.400249448 cm', '0.00914400249448 km'] and for the second case:: Please input a length (number only): 159 Please type a unit system (meter, mile, inch, foot, yard): yard ['0.0902869662303 mile', '5724.00159929 inch', '477.00014539 foot', '145.389639662 meter'] ['1.45389639662e+11 nm', '145389639.662 um', '145389.639662 mm', '14538.9639662 cm', '0.145389639662 km'] .. note:: It is absolutely up to you how to output your result, in other words, you don't need to follow the output style shown in the above. However, your converted numbers should be correct, followed by the corresponding unit systems (e.g., ``477.00014539 foot``, etc.). .. note:: Your entire implementation, including inline comments and empty lines, is going to be ~ 100 lines or so. If you end up with writing, say, 500 lines of code, something is not quite right... 4. Post your homework solutions (and your interesting findings) 24 hours after the deadline, i.e., you can start posting your solutions after 6 pm, Thursday, 11/16/17 and before 6 pm, Froday, 11/17/17. It is recommended that you post your codes in an organized way.