Difference between revisions of "CSC111 Homework 3 Solutions"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ ---- ==Problem 2== <source lang="python"> . # hw3b.py …')
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 03:23, 25 February 2010 (UTC)
 
--[[User:Thiebaut|D. Thiebaut]] 03:23, 25 February 2010 (UTC)
 
----
 
----
 
+
<onlydft>
 
==Problem 2==
 
==Problem 2==
  
Line 7: Line 7:
 
.
 
.
  
# hw3b.py                                                                                                                                                                                                                
+
# hw3b.py                                                                                                                            
# 111c-as                                                                                                                                                                                                                
+
# 111c-as                                                                                                                                                      
 
# Xiao Huang                 
 
# Xiao Huang                 
# (Edited by D. Thiebaut)                                                                                                                                                                                            
+
# (Edited by D. Thiebaut)                                                                                    
 
# This program prompts the user for a number of 1-line poems to print,
 
# This program prompts the user for a number of 1-line poems to print,
 
#  and generates random poems made of random subjects, random adverbs,
 
#  and generates random poems made of random subjects, random adverbs,
Line 20: Line 20:
 
     print "Welcome to the Random poems of the Day!"
 
     print "Welcome to the Random poems of the Day!"
  
     # Ask for how many poems to generate                                                                                                                                                                                
+
     # Ask for how many poems to generate                                                                          
 
     n = input("How many poems would you like me to generate for you?")
 
     n = input("How many poems would you like me to generate for you?")
  
     # Create lists lists of subjects, adverbs, verbs and so on.                                                                                                                                                          
+
     # Create lists lists of subjects, adverbs, verbs and so on.                                                        
 
     subjects  = [ "Hello Kitty", "Professor 111", "Coco Chanel", "Penny" ]
 
     subjects  = [ "Hello Kitty", "Professor 111", "Coco Chanel", "Penny" ]
 
     adverbs  = [ "mysteriously", "gently", "lazily", "violently", "doubtfully" ]
 
     adverbs  = [ "mysteriously", "gently", "lazily", "violently", "doubtfully" ]
Line 48: Line 48:
 
.
 
.
 
</source>
 
</source>
 +
 +
==Problem 3==
 +
 +
<source lang="python">
 +
.
 +
 +
# hw3c.py
 +
# 111c-aw
 +
# Andrea Spain
 +
# This program takes a list of words and extracts every 54th word,
 +
# starting at the 54th. It then prints them, forming a quote. The
 +
# name of the author is printed on a second line.
 +
 +
from hw3words import words  #--- words is a list of words ---#
 +
 +
def main():
 +
    #--- Creates empty list. ---#
 +
    quote = [ ]
 +
 +
    #--- Starting at index 54 in words, appends items to the list using a step
 +
    # of 54. It stops at the length of the list. ---#
 +
    for i in range(54, len( words ), 54):
 +
        quote.append( words[ i ] )
 +
 +
    #--- Prints the quote part itself, minus the last word. ---#
 +
    for i in range( 0, len(quote)-3 ):   
 +
        print quote[i],
 +
   
 +
    #--- Prints the last word of the quote. ---#
 +
    print quote[-3]
 +
   
 +
    #--- Prints the last two words (the name) onto a separate line. ---#
 +
    for i in range( -2, 0):
 +
        print quote[i],
 +
   
 +
main()
 +
 +
.
 +
</source>
 +
 +
</onlydft>
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC111]][[Category:Python]][[Category:Homework]]

Latest revision as of 08:44, 7 September 2011

--D. Thiebaut 03:23, 25 February 2010 (UTC)



...