python
midterm - CSC 401 - Fall 2020
midterm - CSC 401 - Fall 2020 Project Guidelines: read this section first
Rules Directions Grading Notes
Problems Cite Sources (10 pts) gct (20 pts) alterCase (20 pts) priceTShirt (25 pts) benford (25 pts)
Project Guidelines: read this section first
Rules
You MUST:
work on your own write your own code cite any sources of information (outside of course notes) that you use to solve the problems (include urls in comments in your code)
You MUST NOT:
collaborate with others solicit advice or ask questions on the internet or from any other person or source except for the instructor post code, solutions, questions or problem descriptions share solutions or answer questions from other students
You MAY:
use course materials research existing material on the internet or python docs ask the instructor questions to clarify the assignment
Directions
1. download files from d2l to your working folder
2. create a Python module midterm.py
3. implement your solutions (functions) in midterm.py
4. run the doctest to check your solutions and fix errors (see note below). Here is the snippet:
5. submit your solutions to d2l > submissions > project listing
Grading Notes
Run the supplied doctest (same examples as in this document). However, the instructor will run a more thorough doctest during grading that checks additional cases. For each problem, make sure to consider other cases that may not appear in test but that fit the problem description. Please note that the instructor will compare your solutions versus those submitted by other students as well as search the internet for any postings related to this project. The comparison compares the structure of your code, not just variable names etc.
Problems
Cite Sources (10 pts)
For some of the problems below, it is expected that you will need to use references outside the class materials.
For each function below, place a comment inside and at the top of that function that cites any additional sources that you used for that problem.
be specific! - provide a link to the actual webpage that you found useful, not just the general site
If you did not use any sources for that problem, include a comment that states something like "only course materials used".
Points will be deducted if the comments are not provided, or are incomplete or inaccurate.
gct (20 pts)
Implement a function gct according to these guidelines:
The function accepts a single positive integer which represents a number of tablespoons of a liquid. You may assume that this number is always between 0 and 25599 (inclusive of both). The function returns a formatted str containing an equivalent number of gallons, cups and tablespoons. The answer should use as many gallons, then cups as possible. For example gct(312) would return '01g,03c,08t' as 312 tablespoons is equivalent to 1 gallon, 3 cups, and 8 tablespoons.
if __name__ == '__main__':
import doctest
print( doctest.testfile('midtermTEST.py'))
1
2
3
def function(...) :
# sources used: ...
# or, perhaps:
# only course materials used
1
2
3
4
Each of the output quantities (gallons, cups, tablespoons) should occupy two digits. If the values have less than two digits, they should be padded with leading zeroes. I.e., 2 cups is represented by '02c' . If you look things up, cite them (same goes for all problems)
alterCase (20 pts)
Implement a function alterCase that converts a word to "alterCase". Given a word, using any mix of upper and lower case letters, the function then returns the same word, except that the first letter is upper case, the second is lower case, and then cases of letters alternate throughout the rest of the word.
priceTShirt (25 pts)
Implement a function priceTShirt that computes the price of a T-shirt.
The function accepts two arguments: a) the size of the T-shirt, one of 'S','M','L' and b) a str containing a slogan to be written on the shirt.
The shirts have a base price of 12 dollars for small shirts, 15 dollars for medium and 18 dollars for large.
The cost of the slogan depends on the amount of ink used to print the characters:
each lower case letter costs 25 cents, each upper case letter costs 30 cents, each punctuation mark, anyone of marks appearing in .,!'"?: cost 20 cents, whitespace, either a space or newline character, cost 0 cents (no ink!). you may assume the slogan has no characters other than those mentioned above
>>> gct(312)
'01g,03c,08t'
>>> gct(777)
'03g,00c,09t'
>>> gct(25599)
'99g,15c,15t'
1
2
3
4
5
6
>>> alterCase('apple')
'ApPlE'
>>> alterCase('ABRACADABRA')
'AbRaCaDaBrA'
>>> alterCase('Mississippi')
'MiSsIsSiPpI'
>>> alterCase('apPLE')
'ApPlE'
1
2
3
4
5
6
7
8
benford (25 pts)
Many distributions of numbers satisfy Benford's law which says that the leading digit of the distribution should be '1' approximately 30.1% of the time, see Benford's law at Wikipedia. Implement a function benford that computes and returns the percentage of the values in the file that have leading digit '1'. Details:
the function accepts one argument, the name of the file
the function should return the percentage of these numbers that have a leading digit of '1'. this value should be expressed as a decimal, i.e. .301 means 30.1%)
the data is a little messy, make sure to open the files to take a look:
the first line of each file is a header line and should not be included in your data. after the header line, each line contains the name of an item (lake, city, counry) and the corresponding numerical value for that item (acres, elevation, population). For these numerical values that you want to compute the percentage that have leading digit '1'. some names are more complicated than single words, e.g., there is a lake named "Agassiz-Oslon WMA" and a city named "Ho Chi Minh City". But the numbers are always the last thing on the line. not all numbers have the same format, some have commas, some have decimals
Hints:
work with simpleSample.txt first, it has four numbers, only one of which has leading digit '1' as a first pass, make sure that you can iterate over the lines and find the leading digits. Make sure that you are only collecting digits, not letters or anything else. Then worry about computing the percentage of 1's.
Let the instructor know if you are having trouble reading the files. In particular, this may occur on a Mac.
Data sources (data edited slightly). Don't use these, use the files above!
Minnesota Lakes - https://en.wikipedia.org/wiki/List_of_lakes_of_Minnesota
>>> priceTShirt('S',"Vote!")
13.25
>>> priceTShirt('L',"Madam, \nI'm Adam")
21.3
>>> priceTShirt('L',"Taco cat!")
20.0
>>> priceTShirt("M","A man, a plan, a canal: Panama.")
21.15
>>> priceTShirt("M"," \n\n\n ")==15
True
1
2
3
4
5
6
7
8
9
10
>>> benford('simpleSample.txt')
0.25
>>> benford('MinnesotaLakes.txt')
0.28138718173836696
>>> benford('cityElevations.txt')
0.38823529411764707
>>> benford('countryPopulations.txt')
0.30472103004291845
1
2
3
4
5
6
7
8
City Elevations - https://en.wikipedia.org/wiki/List_of_cities_by_elevation Country Populations (July 1, 2019) - https://en.wikipedia.org/wiki/List_of_countries_by_popula tion_(United_Nations)
midterm - CSC 401 - Fall 2020 Project Guidelines: read this section first
Rules Directions Grading Notes
Problems Cite Sources (10 pts) gct (20 pts) alterCase (20 pts) priceTShirt (25 pts) benford (25 pts)
- midterm - CSC 401 - Fall 2020
- Project Guidelines: read this section first
- Rules
- Directions
- Grading Notes
- Problems
- Cite Sources (10 pts)
- gct (20 pts)
- alterCase (20 pts)
- priceTShirt (25 pts)
- benford (25 pts)