Difference between revisions of "CSC111 Lab 4 Solution Programs"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <source lang="python"> # Solution snipets for Lab 4 for c in "abcdefghijklmnopqrstuvwxyz": print( "a" + c ) print() for first in "ab": for second in "abcde...")
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
--[[User:Thiebaut|D. Thiebaut]] 10:03, 29 September 2011 (EDT)
+
--[[User:Thiebaut|D. Thiebaut]] 10:03, 29 September 2011 (EDT)<br />
 +
Updated by --[[User:Thiebaut|D. Thiebaut]] 10:04, 30 September 2011 (EDT)
 
----
 
----
 
<source lang="python">
 
<source lang="python">
 
 
# Solution snipets for Lab 4
 
# Solution snipets for Lab 4
 +
# D.Thiebaut
  
for c in "abcdefghijklmnopqrstuvwxyz":
+
def greetings():
     print( "a" + c )
+
    text = """
 +
      *****************************************************
 +
      * Solution programs for Lab 4                      *
 +
      *****************************************************
 +
      """
 +
     print( text )
 +
   
  
print()
+
def main():
for first in "ab":
+
     greetings()
    for second in "abcdefghijklmnopqrstuvwxyz":
 
        print( first + second )
 
   
 
print()
 
for first in "abc":
 
     for second in "abcdefghijklmnopqrstuvwxyz":
 
        print( first + second )
 
  
print()
+
    print( "\nAll aa, ab, ac,... on separate lines\n" )
for first in "abc":
+
     for c in "abcdefghijklmnopqrstuvwxyz":
     for second in "abcdefghijklmnopqrstuvwxyz":
+
         print( "a" + c )
         print( "111a-" + first + second )
 
  
print( "\nmini challenge\n" )
+
    print( "\nAll aa, ab, ... az, ba, bb, ... bz\n" )
 +
    for first in "ab":
 +
        for second in "abcdefghijklmnopqrstuvwxyz":
 +
            print( first + second )
  
for second in "abcdefghijklmnopqrstuvwxyz":
+
    print( "\nAll aa, ab, ...az, ba, bb, ... bz, ca, cb... cz\n" )
 
     for first in "abc":
 
     for first in "abc":
         print( first + second, end=' ' ) #quote space quote
+
         for second in "abcdefghijklmnopqrstuvwxyz":
    print()
+
            print( first + second )
  
print( "\nChallenge #2\n" )
+
    print( "\bSame with counter + 111a- in front\n" )
 
+
    count = 1
zoo = [ "pig", "horse", "elephant" ]
+
    for first in "abc":
count = 1
+
        for second in "abcdefghijklmnopqrstuvwxyz":
for animal in zoo:
+
            print( "%2d" % count, "111a-" + first + second )
    for i in range( count ):
+
            count = count + 1
        print( animal, end=" " ) # that's a space between quotes
+
        print()
     print()
+
       
     count = count + 1
+
    print( "\nmini challenge\n" )
 +
   
 +
    for second in "abcdefghijklmnopqrstuvwxyz":
 +
        for first in "abc":
 +
            print( first + second, end=' ' ) #quote space quote
 +
        print()
 +
   
 +
     print( "\nChallenge #2\n" )
 +
   
 +
     zoo = [ "pig", "horse", "elephant" ]
  
print( "\nChallenge #3\n" )
+
    # variation #1
 +
    print("\n\n\n-- Variation #1" )
 +
    count = 1
 +
    for animal in zoo:
 +
        print( "Animal #%d:" % (count), end=" " )
 +
        print( animal )
 +
        count = count + 1
  
semesters = [ "sprint", "fall" ]
+
    # variation #2
years     = [ "2010", "2011", "2012", "2013" ]
+
    print("\n\n\n-- Variation #2" )
for year in years:
+
     count = 1
     for sem in semesters:
+
     for animal in zoo:
         print( sem, year )
+
         print( "Animal #%d: %s" % ( count, animal ) )
 +
        count = count + 1
  
# or
+
    # variation #3
print()
+
    print("\n\n\n-- Variation #3" )
semesters = [ "sprint", "fall" ]
+
     count = 1
years     = [ 2010, 2011, 2012, 2013 ]
+
    for animal in zoo:
for year in years:
+
        countString = "%d" % count # countString is now the
    for sem in semesters:
+
                                  # string equivalent of count
         print( sem, year )
+
         print( "Animal #" + countString + ": " + animal )
 +
        count = count + 1
  
# or
+
    # answer to the challenge
print()
+
    print("\nAnswer to the challenge\n\n" )
semesters = [ "sprint", "fall" ]
+
    count = 1
for year in range( 2010, 2014):
+
    for animal in zoo:
    for sem in semesters:
+
        print( "about to print", animal, count, "time(s)..." )
        print( sem, year )
+
        for i in range( count ):
 +
            print( animal, end=" " )  # that's a space between quotes
 +
        print()
 +
        count = count + 1
 +
   
 +
    print( "\nChallenge #3\n" )
 +
   
 +
    semesters = [ "spring", "fall" ]
 +
    years    = [ "2010", "2011", "2012", "2013" ]
 +
    for year in years:
 +
        for sem in semesters:
 +
            print( sem, year )
 +
   
 +
    # or
 +
    print("\nor...\n")
 +
    semesters = [ "spring", "fall" ]
 +
    years    = [ 2010, 2011, 2012, 2013 ]
 +
    for year in years:
 +
        for sem in semesters:
 +
            print( sem, year )
 +
   
 +
    # or
 +
    print("\nor...\n" )
 +
    semesters = [ "spring", "fall" ]
 +
    for year in range( 2010, 2014):
 +
        for sem in semesters:
 +
            print( sem, year )
 +
   
 +
   
 +
    print( "\n\n Challenge #4\n\n" )
 +
    for i in range( 1,10 ):
 +
          for j in range( 1, 10 ):
 +
              print( i*j, sep=" ", end=" " ) 
 +
          print()
  
 +
    print( "\n\n" )
 +
    for i in range( 1,10 ):
 +
          for j in range( 1,i+1 ):
 +
              print( "%3d" % (i*j), sep=" ", end=" " ) 
 +
          print()
  
print( "\n\n Challenge #4\n\n" )
+
main()
for i in range( 1,10 ):
 
      for j in range( 1, 10 ):
 
          print( i*j, sep=" ", end=" " )  # no space between the double quotes
 
      print()
 
  
for i in range( 1,10 ):
 
      for j in range( 1,i+1 ):
 
          print( "%3d" % (i*j), sep=" ", end=" " )  # no space between the double quotes
 
      print()
 
  
 
</source>
 
</source>

Latest revision as of 10:05, 30 September 2011

--D. Thiebaut 10:03, 29 September 2011 (EDT)
Updated by --D. Thiebaut 10:04, 30 September 2011 (EDT)


# Solution snipets for Lab 4
# D.Thiebaut

def greetings():
    text = """
       *****************************************************
       * Solution programs for Lab 4                       *
       *****************************************************
       """
    print( text )
    

def main():
    greetings()

    print( "\nAll aa, ab, ac,... on separate lines\n" )
    for c in "abcdefghijklmnopqrstuvwxyz":
        print( "a" + c )

    print( "\nAll aa, ab, ... az, ba, bb, ... bz\n" )
    for first in "ab":
        for second in "abcdefghijklmnopqrstuvwxyz":
            print( first + second )

    print( "\nAll aa, ab, ...az, ba, bb, ... bz, ca, cb... cz\n" )
    for first in "abc":
        for second in "abcdefghijklmnopqrstuvwxyz":
            print( first + second )

    print( "\bSame with counter + 111a- in front\n" )
    count = 1
    for first in "abc":
        for second in "abcdefghijklmnopqrstuvwxyz":
            print( "%2d" % count, "111a-" + first + second )
            count = count + 1
        print()
        
    print( "\nmini challenge\n" )
     
    for second in "abcdefghijklmnopqrstuvwxyz":
        for first in "abc":
            print( first + second, end=' ' ) #quote space quote
        print()
     
    print( "\nChallenge #2\n" )
     
    zoo = [ "pig", "horse", "elephant" ]

    # variation #1
    print("\n\n\n-- Variation #1" )
    count = 1
    for animal in zoo:
        print( "Animal #%d:" % (count), end=" " )
        print( animal )
        count = count + 1

    # variation #2
    print("\n\n\n-- Variation #2" )
    count = 1
    for animal in zoo:
        print( "Animal #%d: %s" % ( count, animal ) )
        count = count + 1

    # variation #3
    print("\n\n\n-- Variation #3" )
    count = 1
    for animal in zoo:
        countString = "%d" % count # countString is now the
                                   # string equivalent of count
        print( "Animal #" + countString + ": " + animal )
        count = count + 1

    # answer to the challenge
    print("\nAnswer to the challenge\n\n" )
    count = 1
    for animal in zoo:
        print( "about to print", animal, count, "time(s)..." )
        for i in range( count ):
            print( animal, end=" " )  # that's a space between quotes
        print()
        count = count + 1
     
    print( "\nChallenge #3\n" )
     
    semesters = [ "spring", "fall" ]
    years     = [ "2010", "2011", "2012", "2013" ]
    for year in years:
        for sem in semesters:
            print( sem, year )
     
    # or
    print("\nor...\n")
    semesters = [ "spring", "fall" ]
    years     = [ 2010, 2011, 2012, 2013 ]
    for year in years:
        for sem in semesters:
            print( sem, year )
     
    # or
    print("\nor...\n" )
    semesters = [ "spring", "fall" ]
    for year in range( 2010, 2014):
        for sem in semesters:
            print( sem, year )
     
     
    print( "\n\n Challenge #4\n\n" )
    for i in range( 1,10 ):
          for j in range( 1, 10 ):
              print( i*j, sep=" ", end=" " )   
          print()

    print( "\n\n" )
    for i in range( 1,10 ):
          for j in range( 1,i+1 ):
              print( "%3d" % (i*j), sep=" ", end=" " )   
          print()

main()