Difference between revisions of "CSC111 HW 1 2011"

From dftwiki3
Jump to: navigation, search
(Problem #2)
 
(11 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
__TOC__
 
__TOC__
 
<br />
 
<br />
<center>
+
<!--center>
 
<font size="+2">Page under construction!</font>
 
<font size="+2">Page under construction!</font>
 
<br \>[[File:UnderConstruction.jpg|300px]]
 
<br \>[[File:UnderConstruction.jpg|300px]]
</center>
+
</center-->
  
 
<br />
 
<br />
Line 19: Line 19:
 
If you work in pairs, include both names and account number at the top of your programs, in a comment.
 
If you work in pairs, include both names and account number at the top of your programs, in a comment.
  
 +
Don't worry if you do not know yet how to submit your program in your Linux account.  You can start with idle and save your program to your H: drive or to your Desktop, if you are using your own computer.  After Thursday, you will know how to transfer your programs to your Linux account and submit them from there.
 
</bluebox>
 
</bluebox>
  
=Problem 1=
+
<br />
 +
<font color="magenta">
 +
'''A note about copy/pasting from Idle in Windows to your 111a-xx account:'''</font> It appears that Putty is not very friendly and won't allow you to paste your code in emacs.  There shouldn't be any problems with the Mac, but if you have done work with Idle on Windows the best is to install the '''ssh''' program that is available from the Smith ITS office (see the end of [http://cs.smith.edu/dftwiki/index.php/CSC111_Lab_2_2011#Information_about_installing_and_using_Secure_Shell_on_a_Windows_PC_or_a_Mac Lab #2]).  You'll have a better time with this package than with Putty.
 +
<br />
 +
<br />
 +
=Problem #1=
  
 
* Write a python program called '''hw1a.py''' that uses a list of names, defined as follows:
 
* Write a python program called '''hw1a.py''' that uses a list of names, defined as follows:
Line 29: Line 35:
 
:and prints them with boxes around them.  This time the boxes are all 20 characters in length.  The length of the box is fixed.
 
:and prints them with boxes around them.  This time the boxes are all 20 characters in length.  The length of the box is fixed.
  
  +------------------+
+
  &nbsp;+------------------+
  | Sleepy           |
+
  &nbsp;|&nbsp;Sleepy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
  +------------------+
+
  &nbsp;+------------------+
   
+
  &nbsp;
  +------------------+
+
  &nbsp;+------------------+
  | Sneezy           |
+
  &nbsp;|&nbsp;Sneezy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
  +------------------+
+
  &nbsp;+------------------+
 
   
 
   
 
  etc...
 
  etc...
Line 43: Line 49:
 
* When you are done, transfer your program to your Linux 111a-xx account, and submit it as follows (more info on how to do that in class on Thursday 9/15).
 
* When you are done, transfer your program to your Linux 111a-xx account, and submit it as follows (more info on how to do that in class on Thursday 9/15).
  
   submit hw1 hw1a.py
+
   rsubmit hw1 hw1a.py
  
 
=Problem #2=
 
=Problem #2=
Line 53: Line 59:
 
* Your assignment is to write a program called '''hw1b.py''' that displays the list of names ''centered'' in a box that is 20 characters in length.
 
* Your assignment is to write a program called '''hw1b.py''' that displays the list of names ''centered'' in a box that is 20 characters in length.
  
  +------------------+
+
  &nbsp;+------------------+
  |     Sleepy     |
+
  &nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sleepy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
  +------------------+
+
  &nbsp;+------------------+
   
+
  &nbsp;
  +------------------+
+
  &nbsp;+------------------+
  |     Sneezy     |
+
  &nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sneezy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
  +------------------+
+
  &nbsp;+------------------+
 
   
 
   
 
  etc...
 
  etc...
Line 81: Line 87:
 
* When you are done, transfer your program to your Linux 111a-xx account, and submit it as follows:
 
* When you are done, transfer your program to your Linux 111a-xx account, and submit it as follows:
  
   submit hw1 hw1b.py
+
   rsubmit hw1 hw1b.py
  
 
=Problem #3=
 
=Problem #3=
  
* Create a program called '''hw1c.py''' that uses the same list as in Problem #2 and that outputs the names in this fashion:
+
* Create a program called '''hw1c.py''' that uses the same list as in Problem #1 and that outputs the names in this fashion:
  
  +------------------+
+
  &nbsp;+------------------+
  |Sleepy           |
+
  &nbsp;|Sleepy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
  |Sneezy           |
+
  &nbsp;|Sneezy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
  |Bashful           |
+
  &nbsp;|Bashful&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
  |Happy             |
+
  &nbsp;|Happy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
  |Grumpy           |
+
  &nbsp;|Grumpy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
  |Dopey             |
+
  &nbsp;|Dopey&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
  |Doc               |
+
  &nbsp;|Doc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
  +------------------+  
+
  &nbsp;+------------------+&nbsp;
  
 
:Note that there are now only two lines of dashes, and all the names fit ''inside the box''.
 
:Note that there are now only two lines of dashes, and all the names fit ''inside the box''.
Line 101: Line 107:
 
* Submit your program as follows:
 
* Submit your program as follows:
  
     submit hw1 hw1c.py
+
     rsubmit hw1 hw1c.py
  
 
=Problem #4=
 
=Problem #4=
  
* Create a program called '''hw1d.py''' that uses the same list as in Problem #2 and that outputs the names in this fashion:
+
* Create a program called '''hw1d.py''' that uses the same list as in Problem #1 and that outputs the names in this fashion:
  
+------------------+
+
&nbsp;+------------------+
|           Sleepy|
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sleepy|
|           Sneezy|
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sneezy|
|           Bashful|
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bashful|
|             Happy|
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Happy|
|           Grumpy|
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Grumpy|
|             Dopey|
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dopey|
|               Doc|  
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Doc|&nbsp;
+------------------+
+
&nbsp;+------------------+
  
 
:Note that there are now only two lines of dashes, and all the names fit ''inside the box''.
 
:Note that there are now only two lines of dashes, and all the names fit ''inside the box''.
Line 121: Line 127:
 
* Submit your program as follows:
 
* Submit your program as follows:
  
     submit hw1 hw1d.py
+
     rsubmit hw1 hw1d.py
  
 
=Problem #5=
 
=Problem #5=
Line 127: Line 133:
 
* Create a program called '''hw1e.py''' that uses the same list as in Problem #1 and that outputs the names in this fashion:
 
* Create a program called '''hw1e.py''' that uses the same list as in Problem #1 and that outputs the names in this fashion:
  
+-------------------------------------+
+
&nbsp;+-------------------------------------+
|           Sleepy | Sleepy           |
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sleepy&nbsp;|&nbsp;Sleepy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
|           Sneezy | Sneezy           |
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sneezy&nbsp;|&nbsp;Sneezy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
|         Bashful | Bashful         |
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bashful&nbsp;|&nbsp;Bashful&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
|           Happy | Happy           |
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Happy&nbsp;|&nbsp;Happy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
|           Grumpy | Grumpy           |
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Grumpy&nbsp;|&nbsp;Grumpy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
|           Dopey | Dopey           |
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dopey&nbsp;|&nbsp;Dopey&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
|             Doc | Doc             |  
+
&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Doc&nbsp;|&nbsp;Doc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;
+-------------------------------------+
+
&nbsp;+-------------------------------------+
 
 
 
* Your program must use a loop.
 
* Your program must use a loop.
 
* Submit your program as follows:
 
* Submit your program as follows:
  
   submit hw1 hw1e.py
+
   rsubmit hw1 hw1e.py
 +
 
 +
<tanbox>
 +
Hint: Try first to make your program generate this output:
 +
 
 +
Sleepy Sleepy 
 +
Sneezy Sneezy         
 +
Bashful Bashful         
 +
Happy Happy           
 +
Grumpy Grumpy         
 +
Dopey Dopey           
 +
Doc Doc           
 +
 
 +
Then, when you have this working, modify your program to get this:
 +
 
 +
Sleepy | Sleepy 
 +
Sneezy | Sneezy         
 +
Bashful | Bashful         
 +
Happy | Happy           
 +
Grumpy | Grumpy         
 +
Dopey | Dopey           
 +
Doc | Doc           
 +
 
 +
Then add the '''|''' symbols at both ends of each line.  Then add the extra spaces.  Then the top and bottom lines of dashes...
 +
</tanbox>
 +
<br />
  
 
=Optional and Extra Credit=
 
=Optional and Extra Credit=
Line 152: Line 182:
 
* and that outputs the names all on one line, in a box, as follows:
 
* and that outputs the names all on one line, in a box, as follows:
  
+---------------------------------------------+
+
&nbsp;+---------------------------------------------+
|Sleepy Sneezy Bashful Happy Grumpy Dopey Doc |  
+
&nbsp;|Sleepy&nbsp;Sneezy&nbsp;Bashful&nbsp;Happy&nbsp;Grumpy&nbsp;Dopey&nbsp;Doc&nbsp;|&nbsp;
+---------------------------------------------+
+
&nbsp;+---------------------------------------------+
  
 
* Submit your program as follows:
 
* Submit your program as follows:
  
   submit hw1 hw1x.py
+
   rsubmit hw1 hw1x.py
  
 
<br />
 
<br />
 
<tanbox>Hint:  Look at the ''end='\n' '' option provided by the ''print'' statement, and documented in the first chapter of Zele.  You can also figure out how the ''end'' option works by trying out these different python statements:
 
<tanbox>Hint:  Look at the ''end='\n' '' option provided by the ''print'' statement, and documented in the first chapter of Zele.  You can also figure out how the ''end'' option works by trying out these different python statements:
 
+
<source lang="python">
 
   name = "hello"
 
   name = "hello"
 
   print( name,  end='\n' )
 
   print( name,  end='\n' )
Line 176: Line 206:
 
   print( name,  end=',' )
 
   print( name,  end=',' )
 
   print( "there" )
 
   print( "there" )
 +
</source>
 +
</tanbox>
 
   
 
   
 +
<br />
 +
<tanbox>
 +
Another Hint:  Run the statements below in Idle and figure out how this could be useful to you:
 +
<source lang="python">
 +
  list = [ 1, 3, 7, 20 ]
 +
  sum = 0
 +
  for num in list:
 +
      sum = sum + num
 +
  print( "sum = ", sum )
 +
</source>
 
</tanbox>
 
</tanbox>
 
 
<br />
 
<br />
 
<br />
 
<br />
 
<br />
 
<br />
 
[[Category:CSC111]][[Category:Homework]]
 
[[Category:CSC111]][[Category:Homework]]

Latest revision as of 10:16, 30 January 2014

--D. Thiebaut 13:59, 13 September 2011 (EDT)





This homework assignment is due on Tuesday 9/20 evening, at midnight. Assignments submitted after this date and time will not be graded.

You are required to work on Homework 1 or Homework 2 in pairs, following the recommendations of the paper on pair programming that we have covered/will cover in class on 9/15.

If you cannot work in pairs for this assignment, then you will have to do so for Homework #2.

If you work in pairs, include both names and account number at the top of your programs, in a comment.

Don't worry if you do not know yet how to submit your program in your Linux account. You can start with idle and save your program to your H: drive or to your Desktop, if you are using your own computer. After Thursday, you will know how to transfer your programs to your Linux account and submit them from there.


A note about copy/pasting from Idle in Windows to your 111a-xx account: It appears that Putty is not very friendly and won't allow you to paste your code in emacs. There shouldn't be any problems with the Mac, but if you have done work with Idle on Windows the best is to install the ssh program that is available from the Smith ITS office (see the end of Lab #2). You'll have a better time with this package than with Putty.

Problem #1

  • Write a python program called hw1a.py that uses a list of names, defined as follows:
  dwarfNames = [ "Sleepy", "Sneezy", "Bashful", "Happy", "Grumpy", "Dopey", "Doc" ]
and prints them with boxes around them. This time the boxes are all 20 characters in length. The length of the box is fixed.
 +------------------+
 | Sleepy           |
 +------------------+
 
 +------------------+
 | Sneezy           |
 +------------------+

etc...
  • Note that there is one blank line printed between each box.
  • Create a program that uses a main function, as we did in class on Tuesday.
  • When you are done, transfer your program to your Linux 111a-xx account, and submit it as follows (more info on how to do that in class on Thursday 9/15).
 rsubmit hw1 hw1a.py

Problem #2

  • Same problem as Problem #1, but this time the list is slightly different. Some names have a space at the end, some not. The reason is that I want all names to contain an even number of characters.
  dwarfNames = [ "Sleepy", "Sneezy", "Bashful ", "Happy ", "Grumpy", "Dopey ", "Doc " ]
  • Your assignment is to write a program called hw1b.py that displays the list of names centered in a box that is 20 characters in length.
 +------------------+
 |      Sleepy      |
 +------------------+
 
 +------------------+
 |      Sneezy      |
 +------------------+

etc...


Hints: we saw in class, with the temperature conversion program, that to divide one number by another number, we use the / symbol, and the result is a real number. Here, however, you want to get the integer result, so that you can use it to generate many minus or dash symbols. The operator that gives you the integer quotient when dividing an integer by another integer is //.

To understand what I mean, try these statements in Idle:

 print( 10 / 3 )
 print( 10 // 3 )
 print( 100 / 3 )
 print( 100 // 3 )
 print( 3 / 10 )
 print( 3 // 10 )

We say that the / operator gives you the real result, while // gives you the integer result.


  • When you are done, transfer your program to your Linux 111a-xx account, and submit it as follows:
 rsubmit hw1 hw1b.py

Problem #3

  • Create a program called hw1c.py that uses the same list as in Problem #1 and that outputs the names in this fashion:
 +------------------+
 |Sleepy            |
 |Sneezy            |
 |Bashful           |
 |Happy             |
 |Grumpy            |
 |Dopey             |
 |Doc               |
 +------------------+ 
Note that there are now only two lines of dashes, and all the names fit inside the box.
  • Submit your program as follows:
   rsubmit hw1 hw1c.py

Problem #4

  • Create a program called hw1d.py that uses the same list as in Problem #1 and that outputs the names in this fashion:

 +------------------+  |            Sleepy|  |            Sneezy|  |           Bashful|  |             Happy|  |            Grumpy|  |             Dopey|  |               Doc|   +------------------+

Note that there are now only two lines of dashes, and all the names fit inside the box.
  • Submit your program as follows:
   rsubmit hw1 hw1d.py

Problem #5

  • Create a program called hw1e.py that uses the same list as in Problem #1 and that outputs the names in this fashion:

 +-------------------------------------+  |           Sleepy | Sleepy           |  |           Sneezy | Sneezy           |  |          Bashful | Bashful          |  |            Happy | Happy            |  |           Grumpy | Grumpy           |  |            Dopey | Dopey            |  |              Doc | Doc              |   +-------------------------------------+

  • Your program must use a loop.
  • Submit your program as follows:
  rsubmit hw1 hw1e.py

Hint: Try first to make your program generate this output:

Sleepy Sleepy  
Sneezy Sneezy           
Bashful Bashful          
Happy Happy            
Grumpy Grumpy           
Dopey Dopey            
Doc Doc             

Then, when you have this working, modify your program to get this:

Sleepy | Sleepy  
Sneezy | Sneezy           
Bashful | Bashful          
Happy | Happy            
Grumpy | Grumpy           
Dopey | Dopey            
Doc | Doc             

Then add the | symbols at both ends of each line. Then add the extra spaces. Then the top and bottom lines of dashes...


Optional and Extra Credit

  • This problem is only for people who have done some programming before and feel they need some more challenge than provided by the other problems. TAs are not allowed to help with this extra-credit problem. If this problem seems too challenging, don't attempt to work on it; I am making it available only to people with previous programming experience.
  • Write a program called hw1x.py that uses the list of names defined as follows:
dwarfNames = [ "Sleepy", "Sneezy", "Bashful", "Happy", "Grumpy", "Dopey", "Doc" ]
  • and that outputs the names all on one line, in a box, as follows:

 +---------------------------------------------+  |Sleepy Sneezy Bashful Happy Grumpy Dopey Doc |   +---------------------------------------------+

  • Submit your program as follows:
 rsubmit hw1 hw1x.py


Hint: Look at the end='\n' option provided by the print statement, and documented in the first chapter of Zele. You can also figure out how the end option works by trying out these different python statements:
  name = "hello"
  print( name,  end='\n' )
  print( "there" )
 
  print( "-" * 80 )
 
  print( name,  end=' ' )  # there is space between the two quotes
  print( "there" )
 
  print( "-" * 80 )
 
  print( name,  end=',' )
  print( "there" )


Another Hint: Run the statements below in Idle and figure out how this could be useful to you:

  list = [ 1, 3, 7, 20 ]
  sum = 0
  for num in list:
       sum = sum + num
  print( "sum = ", sum )