15-112 Fundamentals of Programming

Notes - Lecture 2.2


Nested Loops

# The following prints Hello! 80 times (why?)
for y in range(10):
    for x in range(8):
        print("Hello!")


# How many times will Hello! get printed here:
for y in range(4):
    for x in range(y):
        print("Hello!")

# When y = 0, number of iterations of the inner loop is 0
# When y = 1, number of iterations of the inner loop is 1
# When y = 2, number of iterations of the inner loop is 2
# When y = 3, number of iterations of the inner loop is 3
# Total number of iterations: 6


# Print a rectangle composed of stars with the given height and width 
def printRectangle(height, width):
    for row in range(height):
        for col in range(width):
            print("*", end= " ")
        print() 

printRectangle(4, 3)
# This prints:
# * * *
# * * *
# * * *
# * * *


# Multiplication table (adjust the spacing to make it look pretty)
for y in range(1, 10):
    for x in range(1, 10):
        print(y*x, end=" ")
    print()


# Print a triangle composed of stars with the given height
def printTriangle(height):
    for y in range(height):
        for x in range(height-y):
            print("*", end=" ")
    print()

printTriangle(4)
# This prints:
# * * * *
# * * *
# * * 
# * 

# You can "eliminate" nested loops using helper functions
def printTriangle(height):
    for y in range(height):
        printStarRow(height-y)
    print()

def printStarRow(n):
    for x in range(n):
        print("*", end=" ")


# Given a string s, check for duplicated characters
def hasDuplicates(s):
    for i in range(len(s)-1):
        for j in range(i+1,len(s)):
            if(s[i] == s[j]): return True
    return False

Programming Style

See here.

Top-down Design

See here.

How to approach a programming problem:
  1. Understand the problem
  2. Devise a plan
    1. Use explicit, clear, small steps
    2. Don't require human memory or intuition
  3. Translate the algorithm into code
    1. Write test cases
    2. Write code (starting here is a big mistake!)
    3. Test code
  4. Examine and review
For step 2 (devising a plan), here are a few useful strategies:
  1. Top-down design (also known as "divide and conquer")
    • Break up the problem into smaller parts.
    • Assume solutions to smaller parts exist.
    • Combine them to get the overall solution.
    • Solve each smaller part separately.
  2. Incremental layers of complexity
    • Start with basic functionality.
    • Add more functionality.
    • Build your program layer by layer.
    For example, suppose you are writing the Pong Game.
    • Start with a ball bouncing around.
    • Add paddles.
    • Make paddles move up and down with keystrokes.
    • Make the ball interact with the paddles.
    • Implement scoring a goal.
    • Keep track of scores.
    • ...
  3. Solving a simplified version
    • Identify a meaningful simplified version of the problem.
    • Solve the simplified version.
    • This can give you good intuition on how to solve the general problem. The solution to the simplified version can sometimes serve as a helper function.


Valid CSS! Valid XHTML 1.0 Strict