Python homework help
This assignment consists of three scripts.
All scripts should consist of a main function that includes a call to a function that includes the code.
First Script – A Dictionary of Lists:
In this problem you will use a file which is named USPresidents.txt The USPresidents.txt file is located in Canvas. You will need to copy this file to your local working directory.
Log in to Canvas.
Select CIS 41A.
Select files.
Select USPresidents.txt
Move your cursor to the right of the green checkmark icon.
Click the down arrow next to the gear.
Click Download
Click Save File and OK
The file should be on your desktop or download directory.
Move the file to the directory containing your Python program.
The file has 44 lines of data, one for each president in the history of the Unites States. Each line of data contains two pieces of data separated by a space: the two letter abbreviation of the name of the state where the president was born, and the name of the president (for your convenience, the president’s name has been converted to a single string – George Washington has been converted to George_Washington).
Using the data from the file, you need to build a dictionary of states and the presidents born in those states. Each key will be a state abbreviation and each value will be a list of presidents.
After building the dictionary, determine the state with the most presidents and how many presidents were born there. Print their names.
Sample Execution Results:
The state with the most presidents is VA with 8 presidents:_x000D_ George_Washington_x000D_ James_Madison_x000D_ James_Monroe_x000D_ John_Tyler_x000D_ Etc._x000D_
Second Script – Dictionary Keys and Sets:
Build a second dictionary from the USPresidents.txt file described in the previous exercise. Each key will again be a state abbreviation, however, the value will be the count of presidents from that state.
Create a set of the ten most populous US states (CA, TX, FL, NY, IL, PA, OH, GA, NC, MI).
Then create a new set that represents a set of populous US states that have had presidents born in them (you should be able to do this with one line of code).
Print a count of this new set along with an alpha-sorted listing of these states and how many presidents have been born in them.
Sample Execution Results:
8 of the 10 high population states have had presidents born in them:_x000D_ CA 1_x000D_ GA 1_x000D_ IL 1_x000D_ NC 2 _x000D_ Etc._x000D_
Third Script – Variable-Length Keyword Arguments – kwargs:
Here, we will be passing a variable number of keyword arguments to a function. You might recognize that the terminology “variable number of keyword arguments” is just another way of describing a dictionary. In fact, this is what we will be doing – passing a dictionary to a function and using certain of the key(word) values – see functions (scroll down to the section called Arbitrary Number of Keyword Parameters).
For this exercise, imagine that there is a complex piece of equipment, perhaps a car or a spacecraft, and that all of its various subsystems periodically send out status messages to be read and evaluated by an overseer system. Each message contains just a small amount of data – perhaps only one or two keywords out of the hundreds of things that overseer system must monitor. However, we will restrict ourselves to only three things: temperature, CO2level, and miscError.
Write an overseerSystem function that has a kwargs argument.
Within the function, test to see if the keyword temperature exists in kwargs. If it does, and the temperature is greater than 500, print a warning with the temperature.
Also test to see if the keyword CO2level exists in kwargs. If it does, and the CO2level is greater than .15, print a warning with the CO2level.
Lastly, test to see if the keyword miscError exists in kwargs. If it does, print a warning with the miscError number.
Within main, create five messages and call the overseerSystem function with each message.
Message1 is temperature:550
Message2 is temperature:475
Message3 is temperature:450, miscError: 404
Message4 is CO2level:.18
Message5 is CO2level:.2, miscError:503
Sample Execution Results:
Warning: temperature is 550_x000D_ Misc error # 404_x000D_ Warning: CO2level is 0.18_x000D_ Warning: CO2level is 0.2_x000D_ Misc error # 503_x000D_
For each script add the following at the end, showing your results:
'''
Execution results:
paste execution results here
Unit I take-home assignment
At the top of your problem put the following multi-line comment, with your information:
'''
Your name as registered, with optional nickname in parentheses
CIS 41A Fall 2017
Unit I take-home assignment '''
This assignment consists of one script.
For this script, the test code should be after your class code – don’t worry about calling from main.
First Script – Operator Overloading:
We will be building a BritCoins class that allows you to work with British money as it was denominated up to 1971 – see: old coins While there were a variety of types of coins, we will only be concerned with pounds, shillings, and pennies. A shilling was worth 12 pennies, and a pound was worth 20 shillings. Therefore, a pound was worth 240 pennies.
The class will allow you to initialize a BritCoins object with any given amount of pounds, shillings and pennies, add and subtract BritCoins objects, and to print a string that represents the value of the coins used to initialize a BritCoins object.
Certain aspects of the BritCoins class will parallel the Length class example shown here: magic methods (scroll down to the Example class: Length section). However, there will be some significant differences.
The idea of the BritCoins class is that you will pass the class a dictionary of coinTypes (keys) and the quantities of each coinType (values). In other words, we will be using kwargs as we did in the third exercise of the Lab H Takehome. The class will then calculate how many pennies these coins represent and save this totalPennies value. The totalPennies value will later be used for addition and subtraction as well as for generating the coin value string.
Building the class:
Create a BritCoins class with the following methods: __init__, __add__ , __sub__ , __str__. There will also be a private class dictionary called __coinValues as follows (this is similar to the __metric variable in the Length example).
__coinValues = {"pound":240, "shilling":12, "penny":1} #value of each type of coin in penny
The __init__ method should have the parameters self, and **kwargs. Start by initializing a self.totalPennies attribute to zero. Then iterate through the kwargs. For each coinType in kwargs, add the value in pennies of that type and quantity of coin to a self.totalPennies. Determine this value by multiplying the number of coins (get this from kwargs) by the coin value (get this from __coinValues – do not hardcode the values!). As an example, if init receives 4 pound, 3 shilling and 2 penny, totalPennies should be 998 (4*240+3*12+2*1).
The __add__ method should have the parameters self and other. Calculate a local total by adding self.totalPennies to other.totalPennies. Then return this value. However, there are two crucial considerations here. The first is that when we add two BritCoins objects, the result of this addition should also be a BritCoins object. Therefore, what we want to return isn’t the total, but is instead a new BritCoins object that has been initialized with the appropriate coinage. So, we need to create and return an instance of the class. We can do this within the class itself! The second consideration is that when we create this instance, we need to provide an appropriate argument for BritCoins. We can’t just pass the total (of say, 998) – we need to pass a dictionary like {“penny”:998}. Further, we need to use appropriate notation when making this BritCoins call (as you did when testing the overseerSystem function in the Lab H Takehome).
The __sub__ method should have the parameters self and other. It is similar to the add method except that you subtract other from self instead of adding it.
The __str__ method has the parameter self and should return a string that represents the value of the BritCoins object. The string should be formatted as shown below, showing pounds, shillings and pennies. From self.totalPennies, you will need to calculate how many of each coin type there are using floor division or some other technique. Note that this string representation won’t necessarily be the coins used to initialize the BritCoins object – it will be the value of the BritCoins object. For example, if the BritCoins object was initialized with 25 shillings, the return should be 1 pounds 5 shillings 0 pennies.
Test the BritCoins class with the following data:
c1 = 4 pound, 24 shilling, 3 penny
c2 = 3 pound, 4 shilling, 5 penny
c3 = c1 + c2
c4 = c1 – c2
Then print c1, c2, c3 and c4.
Sample Execution Results:
5 pounds 4 shillings 3 pennies_x000D_ 3 pounds 4 shillings 5 pennies_x000D_ 8 pounds 8 shillings 8 pennies_x000D_ 1 pounds 19 shillings 10 pennies_x000D_
For each script add the following at the end, showing your results:
'''
Execution results:
paste execution results here '''
Print and staple together the three scripts, including the execution results, and turn them in.
Unit J take-home assignment
At the top of your problem put the following multi-line comment, with your information:
'''
Your name as registered, with optional nickname in parentheses
CIS 41A Fall 2017
Unit J take-home assignment '''
This assignment consists of one script.
The script will contain all of the classes as well as the code to test the classes.
First Script – Inheritance with Multiple Children:
We will be building a suite of classes to manage library patrons and the books that they check out (a library patron is someone who has a library account).
The library has two types of patrons, adult and juvenile, and two types of books, adult and juvenile. Juvenile patrons can only check out juvenile books, while adult patrons can check out any type of book. Additionally, juvenile patrons have a limit of 2 books that they may have checked out at one time, while adult patrons have a limit of 4 books that they may have checked out at one time.
We will write three classes: a parent LibraryPatron class, and AdultPatron and JuvenilePatron child classes. Ideally, we would also have a Book class, but we will simplify things here by making a book be a list with two elements: the title, and the book type – adult or juvenile (see the testing code below).
LibraryPatron class:
The LibraryPatron class has the following four methods: __init__, checkOutBook, returnBook, and printCheckedOutBooks.
The __init__ method should have the parameters self and name, and should store the name as an attribute (name is the name of the patron). There should also be an additional attribute called booksCheckedOut which should be initialized as an empty list. This attribute will store a list of the book titles that the patron currently has checked out.
The checkOutBook method should have the parameters self, checkOutLimit and bookTitle. If the patron is at their checkout limit, print a “Sorry” message to the patron. Otherwise, append the bookTitle to the patron’s booksCheckedOut list and print a “Checkout” message, as shown below.
The returnBook method should have the parameters self and book. Here, book is a book list object – you will need to extract the book title from the list. Remove the book title from the patron’s list of checked out book titles and print a “returned” message.
The printCheckedOutBooks method should have the parameter self. Print a message along and print all the patron’s checked out book titles, as shown below.
AdultPatron class:
The AdultPatron class inherits from the LibraryPatron class and has the following two methods: __init__, checkOutBook.
The __init__ method should have the parameters self and name. Call LibraryPatron’s __init__ to store the name. There should also be an additional attribute called checkOutLimit which should be initialized with a value of 4.
The checkOutBook method should have the parameters self and book. Here again, book is a book list object. Call LibraryPatron’s checkOutBook method, using the patron’s checkOutLimit and the book title as arguments.
JuvenilePatron class:
The JuvenilePatron class inherits from the LibraryPatron class and has the following two methods: __init__, checkOutBook.
The __init__ method should have the parameters self and name. Call LibraryPatron’s __init__ to store the name. There should also be an additional attribute called checkOutLimit which should be initialized with a value of 2.
The checkOutBook method should have the parameters self and book. Here again, book is a book list object. If the book is not a a juvenile book, print a “Sorry” message as shown below. Otherwise, call LibraryPatron’s checkOutBook method, using the patron’s checkOutLimit and the book title as arguments.
Test with the following code:
book1 = ["Alice in Wonderland", "Juvenile"]_x000D_
book2 = ["The Cat in the Hat", "Juvenile"]_x000D_
book3 = ["Harry Potter and the Sorcerer's Stone", "Juvenile"]_x000D_
book4 = ["The Hobbit", "Juvenile"]_x000D_
book5 = ["The Da Vinci Code", "Adult"]_x000D_
book6 = ["The Girl with the Dragon Tattoo", "Adult"]_x000D_
_x000D_
patron1 = JuvenilePatron("Jimmy")_x000D_
patron2 = AdultPatron("Sophia")_x000D_
_x000D_
patron1.checkOutBook(book6)_x000D_
patron1.checkOutBook(book1)_x000D_
patron1.checkOutBook(book2)_x000D_
patron1.printCheckedOutBooks()_x000D_
patron1.checkOutBook(book3)_x000D_
patron1.returnBook(book1)_x000D_
patron1.checkOutBook(book3)_x000D_
patron1.printCheckedOutBooks()_x000D_
patron2.checkOutBook(book5)_x000D_
patron2.checkOutBook(book4)_x000D_
patron2.printCheckedOutBooks()_x000D_
Sample Execution Results:
Sorry Jimmy The Girl with the Dragon Tattoo is an adult book_x000D_ Jimmy has checked out Alice in Wonderland_x000D_ Jimmy has checked out The Cat in the Hat_x000D_ Jimmy has the following books checked out:_x000D_ Alice in Wonderland_x000D_ The Cat in the Hat_x000D_ Sorry Jimmy you are at your limit of 2 books_x000D_ Jimmy has returned Alice in Wonderland_x000D_ Jimmy has checked out Harry Potter and the Sorcerer's Stone_x000D_ Jimmy has the following books checked out:_x000D_ The Cat in the Hat_x000D_ Harry Potter and the Sorcerer's Stone_x000D_ Sophia has checked out The Da Vinci Code_x000D_ Sophia has checked out The Hobbit_x000D_ Sophia has the following books checked out:_x000D_ The Da Vinci Code_x000D_ The Hobbit_x000D_
For the script add the following at the end, showing your results:
'''
Execution results:
paste execution results here '''
Print and staple together the script, including the execution results, and turn it in.
Unit K
In-class assignment
APPENDIX E bitwise operators – See the reading assignments for information on accessing appendix E.
Bitwise operators and Number base conversion
There is an introduction, then a two part assignment. The first part consists of some hand calculations while the second part consists of a very simple script.
Bitwise operations – an Introduction:
Bitwise operations work with stored bit (binary) data. As I’m sure you recall, most computer data is stored in a binary sequence of zeros and ones. Bitwise operations allow you to make a variety of logical comparisons between two different bits which then generate a third bit result. Python’s bitwise operations allow you to perform these comparisons on sequences of bits, generating a result that is itself a sequence of bits. Conceptually, what is actually happening is that Python compares the first bit from the first bit sequence to the first bit from the second bit sequence, generating the first bit result, then the second bit from the first bit sequence to the second bit from the second bit sequence, generating the second bit result, and so on.
As an example, 1101 AND 1001 produces 1011 (more on this later).
You may recognize that the terminology “a sequence of bits” can simply be another way to say a number that has been stored in base 2. For example, the number 13 (base 10) in base 2 is 1101. If you need a refresher on base 2, see: number bases .
Bitwise Operators And, Or, Xor:
For an overview of bitwise operators and how they can be useful, see: bitwise operations .
For a table of results from bitwise operations, see Table 3 in the reading assignment for Appendix E in Canvas.
And:
The And operator can be thought of as answering the question: do both of these two bits have the value 1? If so, the result is 1 (True). Otherwise the result is 0 (False). Example:
1101 & 1001 produces 1001
Or:
The Or operator can be thought of as answering the question: does either one of these two bits have the value 1? If so, the result is 1 (True). Otherwise the result is 0 (False). Example:
1101 | 1001 produces 1101
Xor:
The Xor operator can be thought of as answering the question: does one and only one of these two bits have the value 1? If so, the result is 1 (True). Otherwise the result is 0 (False). Example:
1101 ^ 1001 produces 0100
Bitshifting:
There are also various shift operations, but we won’t be concerning ourselves with them in this class.
Displaying base 10 numbers in base 2:
There a format option that allows you to display base 10 numbers in binary (base 2):
Print(format(13, 'b'))_x000D_
Prints 1101
Part 1 of 2 – Hand converting numbers from one base to another:
Show your calculations and results for the following on a piece of paper.
Convert the following base 10 numbers to base 2:
- 7
- 12
- 41
Convert the following base 2 numbers to base 10:
- 10101
- 1010
Part 2 of 2 – Using bitwise operators in a script:
Write a simple script that calculates the following and displays the results in binary (base 2):
- 12 and 41
- 12 or 41
- 12 xor 41
"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.
Discount Code: CIPD30
Click ORDER NOW..


