All assignments must be done on the hills server.
All assignments must be submitted in a single word document. The code may be text but running of the program must be captured via a screen shot and pasted into the word document.
Assignment 1 ( Due 1/28/2019 )
There are multiple ways of running a Python script. Use 3 ways to print a single line "This is assignment 1." from a
python script.
1) Create a file called "hw1.py" . Write the python statement in the file. Run the python script using the command :
python3 hw1.py
2) Create a shell script called "hw1.sh" and in the first line
type in
#!/usr/local/bin/python3
Then write your Python statement in the file . Make the file executable using the command:
chmod 777 hw1.sh
Now run the script with the command:
./hw1.sh
3)
Login in to the Hills server. From the command line start the Python interpreter using the command:
python3
Type in your Python statement.
exit the interpreter using the command: exit()
Assignment 2 ( Due 4th Feb )
Write your code in a file named "hw2.py" .
Create 3 lists and place them in 3 variables "list1" , "list2" and "list3" .
list1 will contain the numbers 1,2,3 .
list2 will contain the numbers 4,5,6 .
list3 will contain the numbers 7,8,9 .
Print the contents of the 3 lists .
Create list4 whose contents are the 3 lists created above.
Print the contents of list4 .
Using comprehension create a list5 that contains a list with the sums of list1, list2, list3 .
Print the contents of list5
Print the sum of list5. Your output should look like:
[amittal@hills hw]$ python3 hw2.py
list1: [1, 2, 3]
list2: [4, 5, 6]
list3: [7, 8, 9]
list4: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
list5: [6, 15, 24]
Sum of list5: 45
You can print the string and list in a single print statement by using commas:
print( "list1: " , list1 )
Assignment 3 ( Due Feb 18th )
Variable "line1" contains the following:
line1="This is a sentence. The dog is cute. The dog eats a lot."
Your program should count the repeated words and store the word and the number of times it appears in a dictionary object.
You can use the "if else" construct if you like:
if condition :
#To do
else:
#To do
Also to check if an object is in a container you can use:
if x1 in SomeContainer
and similarly to check if the object is not in the container.
if not x1 in SomeContainer
The output should be as below. The order of the words does not matter:
{'This': 1, 'is': 2, 'a': 2, 'sentence.': 1, 'The': 2, 'dog': 2, 'cute.': 1, 'eats': 1, 'lot.': 1}
Assignment 4 ( Due Mar 6 )
Write a python script to
use the Sieve method to calculate all the primes between 2 and 100 .
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Hint:
Create a list of size 101. You can use the "None" value and multiply the list by 101.
Think of the indexes as 2 to 100 . We are not using the 0 index to make it simpler.
For the numbers 2 to 10 take each number and mark the list. For 2 we are going to take all the multiples of 2
except 2 itself and mark the list with say a value 1 till the multiple of 2 is greater than 100.
So for 2
list1[4] = 1
list1[6] = 1
list1[8] = 1
...
list1[100] = 1
Similarly for 3
list1[6] = 1
list1[9] = 1
list1[12] = 1
...
list1[99] = 1
We do this for the next numbers 4 to 10 . After we are done marking the list we
loop through the list and any elements that are still "None" are the prime numbers.
Assignment 5
Your project involves helping a person compute taxes for 2018. It
will implement some but not all of the functionality of a full blown
tax program. We will work at it in stages. The program when run will
display menus such as:
Main 2018 Tax Menu
1) Work on Forms.
2) Compute Taxes.
3) Quit.
If menu option 1 is selected then we might go to another
menu.
Work on Forms
1) Display current forms.
2) Select an existing form to work on.
3) Add a new form.
4) Quit
Add a new form.
Choose a form to add.
1) 1040
2) Schedule A
3) Schedule B
4) Schedule B
5) Quit
Write a general purpose menu function that takes 3 arguments:
title, subtitle , list1
The title of the menu is printed first followed by the subtitle.
The subtitle can be an empty string. Your function should check if the choice
the user selected is valid and if not then it should print an error message and ask
for user entry again. You can use "try", "except" mechanism to do this. Invalid choice can
be some text or a number not in the valid range. You should check both conditions.
The function will return a number representing the choice the user selected.
Remember the list used in the function could have different number of arguments representing the menu items.
Use the skeleton code below to get started.
Name: main.py
//-----------------------------------------------------------------------------
def myMenu( title, subtitle, list1 ) :
//To do by student
return result
list1 = ["Work on Forms.", "Compute Taxes", "Quit" ]
result = myMenu( "Main 2018 Tax Menu" , "" , list1 )
print( "\nMenu returned the choice:" , result )
list1 = ["Display current Forms.",
"Select an existing form to work on.", "Add a new form.", "Quit" ]
result = myMenu( "Work on Forms" , "" , list1 )
print( "Menu returned the choice:" , result )
//-----------------------------------------------------------------------------
Sample run:
[amittal@hills hw5]$ python3 main.py
Main 2018 Tax Menu
1 ) Work on Forms.
2 ) Compute Taxes
3 ) Quit
Make a selection:5
Invalid choice. Try again
Make a selection:test
Invalid choice. Try again
Make a selection:2
Menu returned the choice: 2
Work on Forms
1 ) Display current Forms.
2 ) Select an existing form to work on.
3 ) Add a new form.
4 ) Quit
Make a selection:4
Menu returned the choice: 4
//-----------------------------------------------------------------------------
Assignment 6
This homework is about calculating taxes based on the income and the
status of a person..
Create a folder say hw6. Copy the file from the below link. You
can also copy the content and paste it into a new file in hw6 called
"tax_table.txt"
https://fog.ccsf.edu/~amittal/tax_table.txt
If you are on a Mac you can use the below commands to download the
file.
wget https://fog.ccsf.edu/~amittal/tax_table.txt
or
curl -o tax_table.txt https://fog.ccsf.edu/~amittal/tax_table.txt
Once you have the file in your hw6 folder. Create a file with ".py" extension.
This file will read each
line from "tax_table.txt" and check if the income falls between the 2 ranges at the
beginning of the line.
Ex:
85,900 85,950 14,912 10,783 14,912 13,520
The range for the income in the above line is $85,900 to $85,950 . The next four columns
represent the status as:
Single, Married Filing Jointly, Married Filing Separately, Head of Household
Single in our program is represented by 1 and Married Filing Jointly by 2 and so on.
Based on the inputs to the function compute the tax. You can use the
"split" , "replace" function if you want to. Run the code for the test
cases mentioned in the code. Your output should look like:
Tax is: 2
Tax is: 7011
Tax is: 5767
Tax is: 14912
Tax is: 10783
//------------------------------------------------------------------
File Name: 1040_tax.py
def calculateTax( income, status ) :
#To do by Student
#Run the following tests
print( "Tax is:" , calculateTax( 18, 1 ) )
print( "Tax is:" , calculateTax( 50310, 1 ) )
print( "Tax is:" , calculateTax( 50310, 4 ) )
print( "Tax is:" , calculateTax( 85910, 1 ) )
print( "Tax is:" , calculateTax( 85910, 2 ) )
Assignment 7
Create a class called "Shape". It should have a global class attribute called "noOfShapes" that keeps
track of total number of shapes created in your program .
Create 2 methods for this class a constructor that takes a name and saves it and a "__str__" method that
returns the shape name.
Create a class Circle that is derived from Shape and add 2 methods to it: "__init__" and "getArea()" .
The constructor takes 2 arguments : "name" and a "radius" . In the constructor call the super constructor
and save the radius. The "getArea()" method can use the formula "3.14 * radius * radius" to calculate the
area.
Create a class called Square that is derived from Shape and add 2 methods to it: "init" and "getArea()" .
The constructor takes 2 arguments: "name" and a "width" . In the constructor call the super constructor
and save the width . The "getArea()" method can use the formula "width * width" to calculate the aread.
Once the classes are created copy the code below .
c1 = Circle( "circle1", 2 )
c2 = Circle( "circle2", 3 )
s1 = Square( "square1", 2 )
s2 = Square( "square2", 3 )
list1 = [ c1 , c2 , s1, s2 ]
for x1 in list1 :
print( x1, "Area is:" , x1.getArea() )
print( "Number of shapes created:" , Shape.noOfShapes )
Your program should output:
Shape name:circle1 Area is: 12.568
Shape name:circle2 Area is: 28.278
Shape name:square1 Area is: 4
Shape name:square2 Area is: 9
Number of shapes created: 4
Assignment 8
from abc import ABCMeta, abstractmethod
#Abstract classes are described on Page 923 of the
#textbook
class AbstractPerson( metaclass=ABCMeta ) :
@abstractmethod
def printPersonDetails( self ):
pass
class Address:
#To do set the variables in the address object
def __init__( self, houseNo, streetNo, city, zipCode ) :
#To do return a string as an address string
def __str__( self ) :
class Person( AbstractPerson ) :
#To do create an address object and save it as a member of
#the class Person and set the id and Name values
#to the Person object
def __init__( self, houseNo, streetNo, city, zipCode , id, Name ) :
#Obtain the string from the address object and also print out
#the id and the name
def printPersonDetails( self ):
p1 = Person( 123,"Wicklow Lane" , "Dublin" , 94568, 1, "John" )
p1.printPersonDetails()
#Explain why the below gives an error
p2 = AbstractPerson()