Lesson 7: Pattern Challenges

In this set of exercises, we'll play with number patterns, shape patterns, and other types of repetition and variation. This is to get you thinking more flexibly about what you can do with code, so that you can better apply it to practical situations later.

Fibonacci

The fibonacci sequence of numbers starts from 1 and 1, and then rest come from adding the two prior numbers and putting the answer after them, making a list. Here are the first numbers:

1 1 2 3 5 8 13 21 34 55 89

So 1 + 1 = 2. 1 + 2 = 3. 2 + 3 = 5. 3 + 5 = 8. And so on.

Write a program to calculate and print the first 30 fibonacci numbers. The two 1's that start the sequence are given automatically; have the program calculate the rest.


In [ ]:

Headlines

Write a program that asks for a headline text, and then prints a bar, a centered headline, and another bar. The bar should be 80 characters wide, using the = sign as the bar. The headline text should be centered within the width of the bars.

=====================
       center
=====================

You can find out the length of the headline text by calling the len() function on the variable, like this:

size = len(headline)

In [ ]:

Arrow

Write a program that prints a text-art arrow like below (it does not need to have spaces ahead of it; that's just the way it shows up here). The program should ask the user for the width of the widest row, and then print the right size arrow to match.

#
##
###
####
#####
######
#######
######
#####
####
###
##
#

The widest row can be as much as 60 characters wide, so you don't want to have to make the rows by hand. This is an ideal place for a loop that makes the row the appropriate width. You might even like to create a drawRow() function with a loop inside it.

You also need to figure out how many rows you need based on the width of the widest row. There's some arithmetic involved here. Take your time and work it out on paper, possibly gridded graph paper so you can count the boxes and think about it carefully.


In [ ]:

Mountain Range

Now we're going to flip it sideways. (Yes this will be completely new logic.) Ask the user for two numbers. One is the height of the mountains. The other is the number of mountains. Neither number will be bigger than 8. This is an example of mountains of height 3 with a total of 8 mountains.

   #    #    #    #    #    #    #    #
  ###  ###  ###  ###  ###  ###  ###  ###  
##########################################

A single mountain looks like this.

  #
 ###
#####

Write a program that asks for the number of rows high for the mountain, and asks how many mountains to print. Don't worry if it ends up too wide for your web browser when there are a lot of mountains; just focus on getting the overall logic correct.


In [ ]:

Hundreds Chart

When children are learning to count to 100, there's a board of numbers often used to help them understand number relations. It's call a hundred chart. It looks like this:

 0  1  2  3  4  5  6  7  8  9
10 11 12 13 14 15 16 17 18 19 
20 21 22 23 24 25 26 27 28 29 
30 31 32 33 34 35 36 37 38 39 
40 41 42 43 44 45 46 47 48 49
... and so on.

Write a program that uses looping and if statements to print a well-formatted hundred chart from 0 to 99. You can print the 100 in the last row without worrying about its formatting.

Note that you can use \n to indicate a newline:

print "Hello\nJenny"

In [1]:
print "Hello\nJenny"


Hello
Jenny

In [ ]:

Fence Perimeter

Farmer Brown is creating a large garden. The local wildlife are very enthusiastic about eating her lettuce and tomatoes, so she wants to put a fence around the entire field. It will include a door somewhere, but you don't need to worry about that; the door will use the same materials as the rest of the fence.

She wants you to write a program that accepts a width and length of the field (in feet) and then calculates how many fence posts and how many board lengths are needed to surround the field.

Fence posts are 2 feet apart (measured from center of the post to center of the next post so it's exactly 2 feet). Fields will be a width and length that are each multiples of 2 so you don't have to worry about weirdness in that math.

Between each fence post will be one board. (She's going to hang netting from the board, all the way to the ground, so one board will be enough.)

Here's an example field that is 4 feet by 4 feet.

x--x--x
|     |
x     x
|     |
x--x--x

Notice how an edge has 2 boards but 3 fenceposts? And yet, in total, this 4' x 4' field needs 8 boards, and 8 fence posts.

The fact that one side is 2 boards but 3 fenceposts is called the "fencepost problem" - it's about paying attention to what happens in reality at the end of your loop. "Fencepost problem" or "off by 1 error" is a term you'll hear frequently in coding. The "error" piece refers to the fact that we often make mental mistakes when coding, resulting in a count that's 1 different from what it should be.

Write a program that accepts a width in feet and a length in feet (they may not necessarily be the same), and figures out how many boards and how many fence posts are needed to surround the field. It does NOT need to print a picture (that one was just to demonstrate the idea), just print the numbers.

Draw out the answers on paper for the following examples to test your work.

W: 2  L: 2
W: 4  L: 4
W: 4  L: 8

In [ ]:


In [ ]: