Difference between revisions of "CSC111 Homework 2"
(Created page with 'Not ready yet... Category:CSC111Category:Homework') |
|||
Line 1: | Line 1: | ||
− | + | =Problem #1: Triangles= | |
+ | |||
+ | Using python in interactive mode, type the following python statements one after the other, and try to figure out what is going on: | ||
+ | <code><pre> | ||
+ | >>> line = "!" | ||
+ | |||
+ | >>> line | ||
+ | |||
+ | >>> line = 5 * "!" | ||
+ | |||
+ | >>> line = 20 * "a" | ||
+ | |||
+ | >>> line | ||
+ | |||
+ | >>> n = 7 | ||
+ | |||
+ | >>> line = n * "b" | ||
+ | |||
+ | >>> line | ||
+ | </pre></code> | ||
+ | |||
+ | Using this new discovery, write a program (calle it '''hw2a.py''') that uses a for-loop to display the following triangle: | ||
+ | <code><pre> | ||
+ | * | ||
+ | ** | ||
+ | *** | ||
+ | **** | ||
+ | ***** | ||
+ | </pre></code> | ||
+ | Every time your program runs, it just displays the same pattern: five lines. First line with one star, last line with five stars. | ||
+ | When this test program works well, modify it so that it will first ask the user how many lines the triangle should have, and then the program displays triangle with that number of lines. | ||
+ | |||
+ | Here's an example of how your program should work (the user input is underlined): | ||
+ | |||
+ | <code><pre> | ||
+ | <u>python hw2b.py</u> | ||
+ | |||
+ | How many lines? <u>7</u> | ||
+ | |||
+ | * | ||
+ | ** | ||
+ | *** | ||
+ | **** | ||
+ | ***** | ||
+ | ****** | ||
+ | ******* | ||
+ | </pre></code> | ||
+ | |||
+ | When your program works well, you are ready to write the final program, which is the one you will submit for this assignment. | ||
+ | |||
+ | Modify your program (or write a new one) that will prompt the user for a number and use this number to print several triangles made of stars on the screen. The number entered by the user will define the number of lines used to display the triangles. | ||
+ | |||
+ | Here is an example of how the program will work when the user enters 5: | ||
+ | |||
+ | <code><pre> | ||
+ | |||
+ | <u>python hw2a.py</u> | ||
+ | |||
+ | How many lines? <u>5</u> | ||
+ | |||
+ | triangle 1 | ||
+ | |||
+ | * | ||
+ | ** | ||
+ | *** | ||
+ | **** | ||
+ | ***** | ||
+ | |||
+ | triangle 2 | ||
+ | |||
+ | ***** | ||
+ | **** | ||
+ | *** | ||
+ | ** | ||
+ | * | ||
+ | |||
+ | triangle 3 | ||
+ | |||
+ | * | ||
+ | ** | ||
+ | *** | ||
+ | **** | ||
+ | ***** | ||
+ | |||
+ | triangle 4 | ||
+ | |||
+ | ***** | ||
+ | **** | ||
+ | *** | ||
+ | ** | ||
+ | * | ||
+ | </pre></code> | ||
+ | |||
+ | Note that all the triangles are flush against the left margin, but it is okay if your solution has an extra space there, in particular for Triangles 3 and 4. | ||
+ | |||
+ | Submit your program as follows: | ||
+ | |||
+ | |||
+ | submit hw2 hw2a.py | ||
+ | |||
+ | ==Hints== | ||
+ | |||
+ | For Triangle 3 and Triangle 4, make your program output them in a different format first. This will help you figure out how to actually print them. For example, make your program output the triangles with dots and stars. When it works, then replace the dots by spaces! | ||
+ | |||
+ | <code><pre> | ||
+ | triangle 3 | ||
+ | |||
+ | ....* | ||
+ | ...** | ||
+ | ..*** | ||
+ | .**** | ||
+ | ***** | ||
+ | |||
+ | triangle 4 | ||
+ | |||
+ | ***** | ||
+ | .**** | ||
+ | ..*** | ||
+ | ...** | ||
+ | ....* | ||
+ | </pre></code> | ||
+ | |||
+ | =Problem #2: Old Mac Donald's Farm revisited= | ||
+ | |||
+ | |||
Revision as of 13:53, 3 February 2010
Problem #1: Triangles
Using python in interactive mode, type the following python statements one after the other, and try to figure out what is going on:
>>> line = "!"
>>> line
>>> line = 5 * "!"
>>> line = 20 * "a"
>>> line
>>> n = 7
>>> line = n * "b"
>>> line
Using this new discovery, write a program (calle it hw2a.py) that uses a for-loop to display the following triangle:
*
**
***
****
*****
Every time your program runs, it just displays the same pattern: five lines. First line with one star, last line with five stars. When this test program works well, modify it so that it will first ask the user how many lines the triangle should have, and then the program displays triangle with that number of lines.
Here's an example of how your program should work (the user input is underlined):
<u>python hw2b.py</u>
How many lines? <u>7</u>
*
**
***
****
*****
******
*******
When your program works well, you are ready to write the final program, which is the one you will submit for this assignment.
Modify your program (or write a new one) that will prompt the user for a number and use this number to print several triangles made of stars on the screen. The number entered by the user will define the number of lines used to display the triangles.
Here is an example of how the program will work when the user enters 5:
<u>python hw2a.py</u>
How many lines? <u>5</u>
triangle 1
*
**
***
****
*****
triangle 2
*****
****
***
**
*
triangle 3
*
**
***
****
*****
triangle 4
*****
****
***
**
*
Note that all the triangles are flush against the left margin, but it is okay if your solution has an extra space there, in particular for Triangles 3 and 4.
Submit your program as follows:
submit hw2 hw2a.py
Hints
For Triangle 3 and Triangle 4, make your program output them in a different format first. This will help you figure out how to actually print them. For example, make your program output the triangles with dots and stars. When it works, then replace the dots by spaces!
triangle 3
....*
...**
..***
.****
*****
triangle 4
*****
.****
..***
...**
....*