#Name: Herbert H. Lehman #Date: September 15, 2013 #This program prints: hello world print("Hello world")
Points | Criteria | Description |
2 | Readability and Documentation | Proper names for variables and functions are used in all instances, well-organised code includes header and internal documentation. |
2 | Input/Output | Correct I/O parameters, appropriate data types, sufficient description provided for the user. |
3 | Computation/Processing | Correct results are computed, appropriate operations and functions are used. |
3 | Efficiency | Clever shortcuts are used to produce more compact code. |
2 | Extra Credit | Submission 24 Hrs or 48 Hrs ahead of deadline. |
Submit the following programs via Blackboard:
For example, if your first name is Eric, you would use Euros.
If your name begins with | Use the currency | $1 is worth |
---|---|---|
A | Afghan Afghani (AFN) | 51.08 |
B | Bangladeshi Taka (BDT) | 80.58 |
C | Costa Rican Colon (CRC) | 499.38 |
D | Danish Krone (DKK) | 5.67 |
E | Euro (EUR) | 0.77 |
F | Falkland Island Pound (FKP) | 0.62 |
G | Guatemalan Quetzal (GTQ) | 0.13 |
H | Hungarian Forint (HUF) | 0.00456 |
I | Indonesian Rupiah (IDR) | 9639.99 |
J | Japanese Yen (JPY) | 83.85 |
K | Kenyan Shilling (KES) | 0.0116 |
L | Lebanese Pound (LPP) | 0.000664 |
M | Moroccan Dirham (MAD) | 0.118 |
N | Nepalese Rupee (NPR) | 0.0115 |
O | Omani Rial (OMR) | 0.384 |
P | Polish Zloty | 3.11 |
Q | Qatari Riyal (QAR) | 0.275 |
R | Russian Ruble (RUB) | 30.80 |
S | Somali Shilling (SOS) | 0.000619 |
T | Thai Baht (THB) | 30.59 |
U | Ukrainian Hryvna (UAH) | 8.10 |
V | Venezuelan Bolivar (VEH) | 4.30 |
W | Samoan Tala (WST) | 2.27 |
X | East Caribbean Dollar (XCD) | 2.70 |
Y | Yemeni Rial (YER) | 0.00477 |
Z | Zimbabwean Dollar (ZWD) | 361.90 |
Submit the following programs via Blackboard:
Sameh's Currency Converter Converts from dollars into Somali Shilling, Afghan Afghanis, Moroccan Dirhams, Euros, and Hungarian Forints. Choose a currency ('S', 'A', 'M', 'E', 'H'): h What amount: 1000 The chosen currency is: Hungarian Forints The converted amount is: 4.56
Here is a sample run of the program:
Please enter clothing prices (4 items), separated by commas: 100, 9.99, 200, 159.99 Your receipt: 100.00 9.99 200.00 T 159.99 T --------- 500.58
(The `T' indicates which items are taxed. Do not worry about alligning the output in the receipt, we will learn how to do that at a later time.
Please enter the starting amount of money: 1000000 Day 1: $1000000 Day 2: $500000 Day 3: $250000 Day 4: $125000 Day 5: $62500 Day 6: $31250
Submit the following programs via Blackboard:
>>> Enter an integer between 0 and 9 (inclusive): 9 nine >>> Enter an integer between 0 and 9 (inclusive): 10 Error: invalid input.
>>> Enter an integer between 1 and 7 (inclusive): 1 Monday >>> Enter an integer between 1 and 7 (inclusive): 2 Tuesday >>> Enter an integer between 1 and 7 (inclusive): 8 Error: invalid input.
Ask user for an integer if the number is even print a message saying that even numbers are great else print a message saying what an odd number
Hint: An easy way to test if a number is even is to see if there is a remainder after dividing by 2. For example 5 divided by 2 has remainder 1 so is not even. While 12 divided by 2 has remainder 0 so is even.
Submit the following programs via Blackboard:
Enter the year you were born (between 1940 and 1999 inclusive): 2001 Invalid input, please try again... Enter the year you were born (between 1940 and 1999 inclusive): 1930 Invalid input, please try again... Enter the year you were born (between 1940 and 1999 inclusive): 1982 This year you turn 31
How many times should I print my name? (Enter a positive number): -17 Invalid input, please try again... How many times should I print my name? (Enter a positive number): 0 Invalid input, please try again... How many times should I print my name? (Enter a positive number): 4 Sandeep Sandeep Sandeep Sandeep
How big a grid would you like? (Between 1 and 50 inclusive): 67 Invalid input, please try again... How big a grid would you like? (Between 1 and 50 inclusive): -2 Invalid input, please try again... How big a grid would you like? (Between 1 and 50 inclusive): 5 ***** ***** ***** ***** *****
How big a triangle would you like? (Between 1 and 50 inclusive): 82 Invalid input, please try again... How big a triangle would you like? (Between 1 and 50 inclusive): -9 Invalid input, please try again... How big a triangle would you like? (Between 1 and 50 inclusive): 6 * ** *** **** ***** ******
Please enter a positive number: -5 Invalid input, please try again... Please enter a positive number: 0 Invalid input, please try again... Please enter a positive number: 50 The multiple of 7 below 50 are: 7 14 21 28 35 42 49
Submit the following programs via Blackboard:
x = prompt the user for a positive integer and verify input y = x while x > 1: print "x="x, "y="y x = x/2 y = y*2For example:
Please enter a positive number: -5 Invalid input, please try again... Please enter a positive number: 0 Invalid input, please try again... Please enter a positive number: 50 x=50 y=50 x=25 y=100 x=12.5 y=200 x=6.25 y=400 x=3.125 y=800 x=1.5625 y=1600
end = prompt the user for a positive integer and verify input i = 0 while i < end: print "i="i, "squared="i2, "cubed="i3 i = i + 1For example:
Please enter a positive number: -5 Invalid input, please try again... Please enter a positive number: 0 Invalid input, please try again... Please enter a positive number: 6 i=0 squared=0 cubed=0 i=1 squared=1 cubed=1 i=2 squared=4 cubed=8 i=3 squared=9 cubed=27 i=4 squared=16 cubed=64 i=5 squared=25 cubed=125
iterations = prompt the user for a positive integer and verify input
i = 6
end = i + (iterations * 2) - 1
while i < end:
print "i="i, "square root="√i
i = i + 2
For example:
Please enter a positive number: -5 Invalid input, please try again... Please enter a positive number: 0 Invalid input, please try again... Please enter a positive number: 5 i=6 square root=2.449489742783178 i=8 square root=2.8284271247461903 i=10 square root=3.1622776601683795 i=12 square root=3.4641016151377544 i=14 square root=3.7416573867739413
Please enter a positive number: -5 Invalid input, please try again... Please enter a positive number: 0 0! = 1Another example:
Please enter a positive number: 4 4! = 24
Submit the following programs via Blackboard:
def main(): x1,y1 = getpoint() # prompt user for the coordinates of a point (x1,y1) x2,y2 = getpoint() # prompt user for the coordinates of a point (x2,y2) d = getDistance(x1,y1,x2,y2) # Calculates and returns distance from (x1,y1) to (x2,y2) display(d) # Displays the calculated distance. main()
That is, write the functions getPoint(), getDistance(), and display(). Include all functions, including the main() function above in the file you submit.
Hint: The distance between two points (x1,y1) and (x2,y2) is given by d = sqrt((x2 - x1)2 + (y2 - y1)2)
Submit the following programs via Blackboard:
win.getMouse() win.close()
win.getMouse() win.close()
win.getMouse() win.close()
win.getMouse() win.close()
win.getMouse() win.close()
Submit the following programs via Blackboard:
tom.right(n) tom.forward(10)If n is odd, you would have:
tom.left(n) tom.forward(10)
scr.exitonclick()
hexagon(turtle, x): while x > 10: for i = 0 to 6: move turtle forward x steps turn turtle 60 degrees to right x = x -10Include in your submitted program a main function that creates a turtle and calls the the hexagon function with your turtle and 50 as the input parameters.
scr.exitonclick()
angle = randrange(0,360) tom.right(angle) tom.forward(10)
scr.exitonclick()
Draw a flower by repeatedly calling your function. Your flower should have at least 10 petals. For example,
(Note: you can change the color that your turtle, using the function, color(). For example, if you turtle is called flower to change it's color to purple, write flower.color("purple").)
scr.exitonclick()
function leaf(turtle) Move turtle forward 50 steps Rotate turtle left 100 degrees Move turtle forward 30 steps Rotate turtle left 50 degrees Move turtle forward 30 steps Rotate turtle left 100 degrees Move turtle forward 50 steps Rotate turtle right 160 degrees function stem(turtle) Rotate turtle right 95 degrees Move turtle forward 70 steps Rotate turtle left 5 degrees Move turtle forward 70 steps function main() create a screen create a turtle loop four times leaf(turtle) stem(turtle) wait for click and close
scr.exitonclick()
Submit the following programs via Blackboard:
Your program should print out your name to the screen and then ask the user to enter a string. You should then print out how long the string is in terms of the length of your name (that is, the length of the user's string divided by your length). For example:
The measuring string is "Kate" Please enter a string: Hello world Your string is 2.75 Kate's long.
While, if your name was Daniel, your program would look like:
The measuring string is "Daniel" Please enter a string: Hello world Your string is 1.8333 Daniel's long.
H e r b e r t H . L e h m a n e r b e r t H . L e h m a n H r b e r t H . L e h m a n H e b e r t H . L e h m a n H e r e r t H . L e h m a n H e r b r t H . L e h m a n H e r b e t H . L e h m a n H e r b e r H . L e h m a n H e r b e r t H . L e h m a n H e r b e r t . L e h m a n H e r b e r t H L e h m a n H e r b e r t H . L e h m a n H e r b e r t H . e h m a n H e r b e r t H . L h m a n H e r b e r t H . L e m a n H e r b e r t H . L e h a n H e r b e r t H . L e h m n H e r b e r t H . L e h m a
['San Francisco', 'Christchurch ', 'Sydney ', 'Bangkok ', 'Copenhagen '] S C S B C a h y a o n r d n p i n g e F s e k n r t y o h a c k a n h g c u e i r n s c c h o
Hint: Make sure all place names are the same length. If they are not add spaces to the shorter names to make them all equal length.
Hint: If you have a list referenced by the variable places, then places[0] refers to the first place in your list, in this case 'San Francisco'.
Hint: Also, places[0][i] refers to the character at index i of the first word in your list. Example: places[1][3] refers to the letter 'i' in the string 'Christchurch'.
For example, if the file inputTemplate.txt contained:
New York, New York 11 March 2013 **INSERT NAME HERE** **INSERT CITY HERE** Dear **INSERT NAME HERE**, Thank you for your service to New York City, and, in particular, to the education of its residents. Those in **INSERT CITY HERE** appreciate it! Best wishes to **INSERT NAME HERE** and your family, --CUNY
A sample run of the program would be:
Please enter the name of the template file: inputTemplate.txt Please enter names of recipients seperated by commas: Herbert H. Lehman, Bernard M. Baruch, Fiorello H. LaGuardia Please list cities seperated by commas: Bronx NY, New York NY, Queens NY Your customized letters are below: New York, New York 11 March 2013 Herbert H. Lehman Bronx NY Dear Herbert H. Lehman, Thank you for your service to New York City, and, in particular, to the education of its residents. Those in Bronx NY appreciate it! Best wishes to Herbert H. Lehman and your family, --CUNY New York, New York 11 March 2013 Bernard M. Baruch New York NY Dear Bernard M. Baruch, Thank you for your service to New York City, and, in particular, to the education of its residents. Those in New York NY appreciate it! Best wishes to Bernard M. Baruch and your family, --CUNY New York, New York 11 March 2013 Fiorello H. LaGuardia Queens NY Dear Fiorello H. LaGuardia, Thank you for your service to New York City, and, in particular, to the education of its residents. Those in Queens NY appreciate it! Best wishes to Fiorello H. LaGuardia and your family, --CUNY
01234567890123456789012345 This line has more than 20 characters. This one has less And this one has lots, lots, lots, more than 20 characters!
A sample run of your program would look like:
Please enter a file name : myspecialfile.txt Please enter a number between 10 & 20: 9 Invalid input, please try again... Please enter a number between 10 & 20: 21 Invalid input, please try again... Please enter a number between 10 & 20: 20 Here is your output formated to a max of 20 characters per line: 01234567890123456789 012345 This line has more t han 20 characters. This one has less And this one has lot s, lots, lots, more than 20 characters!
Another sample run of your program would look like:
Please enter a file name : myspecialfile.txt Please enter a number between 10 & 20: 15 Here is your output formated to a max of 15 characters per line: 012345678901234 56789012345 This line has m ore than 20 cha racters. This one has le ss And this one ha s lots, lots, l ots, more than 20 characters!
Hint: break the problem in to parts: first write a program that will print lines from a file to the screen. Then modify your initial program to only print lines up to the length selected. And, finally, to finish the program, then add in the code that breaks up the lines according to the length selected.
Submit the following programs via Blackboard:
algorithm fibonacci(n) if n is 0 or a negative number return 0 else if n < 2 return 1 else return fibonacci(n-1)+fibonacci(n-2)Include in your submitted program a main() function that prompts the user for a number, calls your function, and then displays the answer.
[' ','a','b','c','d',...'y','z']
2 : 292 3 : 536 4 : 810 5 : 1100 6 : 1428 7 : 1631 8 : 1439 9 : 1100 10 : 825 11 : 543 12 : 296
Eric, Sameh, Ali, Tom, Kostos, Tony, KatherineAnd your name is Kostos, the ending list would be:
Kostos, Sameh, Tom, Tony, Ali, Eric, KatherineIf your name is Eric, the ending list would be:
Eric, Katherine, Kostos, Sameh, Tom, Tony, Ali
In your submitted file, include a main() function that prints out your name and demonstrates that the sort algorithm works.
Hint: you only need to change the "comparison" part of the sort, but you will need to make it more complicated. If you are trying to figure out which of two strings, say string1 and string2 come first, you need to compare them first to your name. If both are before or both are after your name, you can compare in the same way as before. If one of the strings comes before your name and the other after your name, you will have additional comparisons.
algorithm stoogesort(L, i, j) if L[j] < L[i] then swap L[i] and L[j] if j - i > 1 then t := (j - i + 1)/3 stoogesort(L, i, j-t) stoogesort(L, i+t, j) stoogesort(L, i, j-t) return L
In your submitted file, include a main() function that creates a list L containing integers in some random order and calls stoogesort with the call stoogesort(L, 0, length(L)-1) and then prints out L.