CSC111 HW 1 Solutions 2011

From dftwiki3
Revision as of 10:22, 26 September 2011 by Thiebaut (talk | contribs) (Solution programs for Homework #1)
Jump to: navigation, search

--D. Thiebaut 11:08, 26 September 2011 (EDT)


Solution programs for Homework #1

# hw1a.py
# Kate Aloisio 111a-ah
# Edited by D. Thiebaut
# This program prints all the names in a box, one box per name,
# and a blank line between boxes


def main():
    # the list of names
    dwarfNames = [ "Sleepy", "Sneezy", "Bashful", "Happy", "Grumpy", "Dopey", "Doc" ]

    # print the names with boxes around them
    for name in dwarfNames:
        noDash = 18
        noChars = len(name)
        noSpace = noDash - noChars - 2
        line = "+" + "-"*noDash + "+"
        print(line)
        print("|", name, " "*noSpace + "|")
        print(line)
        print()

main()


# hw1b.py
# 111a-ah
# Kate Aloisio 
# Edited by D. Thiebaut
# This version of the program prints all the names centered
# in each box.

def main():
    # the list of names
    dwarfNames = [ "Sleepy", "Sneezy", "Bashful ", "Happy ",
                  "Grumpy", "Dopey ", "Doc " ]

    # print the box around all of them
    for name in dwarfNames:
        noDash = 18
        noChars = len(name)
        # compute number of spaces around name
        noSpace = (noDash - noChars - 2)//2
       
        # create line of dash symbols 
        line = "+" + "-"*noDash + "+"

        # print box around name
        print(line)
        print("|", " "*noSpace + name, " "*noSpace + "|")
        print(line)
        print()

main()


# hw1c.py
# 111a-ah
# Kate Aloisio
# Edited by D. Thiebaut
# prints all the names with a big box around them and 
# left-justifies them

def main():
    # the names
    dwarfNames = [ "Sleepy", "Sneezy", "Bashful", "Happy",
                   "Grumpy", "Dopey", "Doc" ]

    # length of the box
    noDash=18
    # create line of dash symbols
    line = "+" + "-"*noDash + "+"

    # print top line
    print(line)

    # print names left-justified
    for name in dwarfNames:
        noChars = len(name)
        noSpace = noDash - noChars - 2
        print("|" + name, " "*noSpace, "|")

    # bottom line
    print(line)

main()


# hw1d.py
# 111a-ah
# Kate Aloisio
# Edited by D. Thiebaut
# Prints the names right-justified in a box


def main():
    # the names
    dwarfNames = [ "Sleepy", "Sneezy", "Bashful", "Happy",
                   "Grumpy", "Dopey", "Doc" ]

    # number of dash symbols
    noDash = 18

    # create a line and print it
    line = "+" + "-"*noDash + "+"
    print(line)

    # print all the names
    for name in dwarfNames:
        noChars = len(name)
        noSpace = noDash - noChars - 2
        print("|", " "*noSpace, name + "|")

    # print bottom line
    print(line)

main()


# hw1e.py
# 111a-ah
# Kate Aloisio
# Edited by D. Thiebaut
# prints name mirrored in a box, with a vertical bar in the middle.
# Names in left column are right justified, names in right column are
# left justified.

def main():
    dwarfNames = [ "Sleepy", "Sneezy", "Bashful", "Happy",
                   "Grumpy", "Dopey", "Doc" ]

    # total width of the box
    noDash = 37

    # line of dash symbols
    line = "+" + "-"*noDash + "+"
    print(line)

    # print names in two columns
    for name in dwarfNames:
        noChars = len(name)
        noSpace = noDash - noChars - 20
        print("|" + " "*noSpace + name + " | " + name + " "*noSpace + "|")

    # print bottom line
    print(line)

main()


# hw1x.py
# 111a-ah
# Kate Aloisio
# Edited by D. Thiebaut
# Prints all the names on a single line with a box around them:
#
# +---------------------------------------------+
# |Sleepy Sneezy Bashful Happy Grumpy Dopey Doc |
# +---------------------------------------------+
#

def main():
    # names in a list
    dwarfNames = [ "Sleepy", "Sneezy", "Bashful", "Happy",
                   "Grumpy", "Dopey", "Doc" ]

    # compute total length of all the names with a separating space
    noDash = 0
    for name in dwarfNames:
        noDash = noDash + len(name) + 1 # 1 for extra space
        
    # create a line with that many dash symbols
    line = "+" + "-"*noDash + "+"
    print(line)

    # print left vertical bar
    print("|", end="")

    # print all the names one after the other
    for name in dwarfNames:
        print( name, end=" ")

    # print ending vertical bar.
    print("|", end="\n")

    # bottom closing line of dash symbols.
    print(line)

main()