Difference between revisions of "CSC111 Homework 2 Solutions 2011"

From dftwiki3
Jump to: navigation, search
(hw2b.py)
(hw2b.py)
Line 66: Line 66:
 
</source>
 
</source>
  
 +
==hw2c.py==
 +
<source lang="python">
 +
# hw2c.py
 +
# 111ai JeeHee Oh
 +
# this program prompts the user for a number and displays
 +
# a number of lines corresponding to that number.
 +
#
 +
# Example of user interaction:
 +
# Enter a positive number less than 20: 2
 +
#  *
 +
# ***
 +
#  *
 +
#
  
 +
def main():
 +
   
 +
    num = eval( input ("Enter a positive number less than 20: "))
 +
    print()
 +
 +
    for i in range (num):
 +
        noSpaces = num - i
 +
        noStars  = 2 * i + 1
 +
        print( noSpaces * ' '  +  noStars * '*' )
 +
   
 +
    for a in range (num - 1, 0, -1):
 +
        noSpaces = num - a + 1
 +
        noStars  = 2 * a - 1
 +
        print( noSpaces * ' '  +  noStars * '*' )
 +
   
 +
main()
 +
 +
</source>
 +
<br />
 +
This version is a bit more concise, but works well, too:
 +
<br />
 +
<source lang="python">
 +
#hw2c.py
 +
#Ariana DasGupta 111a-ay
 +
#Emily Glickman 111a-ae
 +
 +
# Program asks used for a positive number
 +
# Program the displays a diamond that has input*2-1
 +
# ex. input = 5
 +
#output =
 +
#    *   
 +
#  *** 
 +
#  ***** 
 +
# *******
 +
#*********
 +
# *******
 +
#  ***** 
 +
#  *** 
 +
#    *   
 +
 +
 +
 +
def main ():
 +
    line = eval(input('Enter a postive number less than 20: \n'))
 +
    for i in range (line-1,0, -1):
 +
        print (" "*(i),"*"*(line-i-1),"*","*"*(line-i-1)," "*(i), sep = '' )
 +
 +
 +
    for i in range (line-1,-1, -1):
 +
        print (" "*(line-i-1),"*"*(i),"*","*"*(i)," "*(line-i-1), sep = '' )
 +
main ()
 +
 +
</source>
 
<br />
 
<br />
 
[[Category:CSC111]][[Category:Python]][[Category:Homework]]
 
[[Category:CSC111]][[Category:Python]][[Category:Homework]]

Revision as of 12:41, 3 October 2011

--D. Thiebaut 13:36, 3 October 2011 (EDT)


Solution Programs

hw2b.py

# hw2b.py
# 111a-bm  Ashleigh King
# 111a-an  Sam Scharr   (working in pair programming)
#
# This program prompts the user for a number and displays
# a number of lines corresponding to that number.
#
# Example of user interaction:
# How many lines do you want printed? 3
# *
# *... **
# **...... -***
# 

def main():
    lines = input( "How many lines do you want printed? " )
    lines = eval( lines )
    for x in range( 0, lines ):
        print( "*" * (x) + "." * (x * 3) + " " + "-" * (x-1) + "*" * (x + 1) )

main()



or this version, also good:

# hw2b.py
# 111a-au Gemma Natori
# Edited by D. Thiebaut
# This program prompts the user for a number and displays 
# a number of lines corresponding to that number.
# Example of user interaction:
# How many lines do you want printed? 3
# *
# *... **
# **...... -***


def main():
    noLines = eval (input ("How many lines do you want printed? "))

    for x in range ( noLines ):
        stars = x
        dots = x * 3
        stars2 = x + 1
        dashes = x - 1
        
        print (stars * "*" + dots * "." + " " + dashes * "-" + stars2 * "*")


main()

hw2c.py

# hw2c.py
# 111ai JeeHee Oh
# this program prompts the user for a number and displays
# a number of lines corresponding to that number.
#
# Example of user interaction:
# Enter a positive number less than 20: 2
#  *
# ***
#  *
#

def main():
    
    num = eval( input ("Enter a positive number less than 20: "))
    print()

    for i in range (num):
        noSpaces = num - i
        noStars   = 2 * i + 1
        print( noSpaces * ' '  +   noStars * '*' )
    
    for a in range (num - 1, 0, -1):
        noSpaces = num - a + 1
        noStars  = 2 * a - 1
        print( noSpaces * ' '  +  noStars * '*' )
    
main()


This version is a bit more concise, but works well, too:

#hw2c.py
#Ariana DasGupta 111a-ay
#Emily Glickman 111a-ae

# Program asks used for a positive number
# Program the displays a diamond that has input*2-1
# ex. input = 5
#output =
#    *    
#   ***   
#  *****  
# ******* 
#*********
# ******* 
#  *****  
#   ***   
#    *    



def main ():
    line = eval(input('Enter a postive number less than 20: \n'))
    for i in range (line-1,0, -1):
        print (" "*(i),"*"*(line-i-1),"*","*"*(line-i-1)," "*(i), sep = '' )


    for i in range (line-1,-1, -1):
        print (" "*(line-i-1),"*"*(i),"*","*"*(i)," "*(line-i-1), sep = '' )
main ()