# Lecture 3: Introduction to algorithms

Pop quiz! Aren’t you lucky. These questions aren’t hard or tricks. If you did the reading last week they should be pretty simple. They are more to tell me who’s doing the reading than how smart you are.

With that out of the way, let’s talk about week 2 exercise 3 loops_7. These are the instruction:

## Make a pyramid.

Return this:

```
[
[' ', ' ', ' ', ' ', '*', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', '*', '*', '*', ' ', ' ', ' '],
[' ', ' ', '*', '*', '*', '*', '*', ' ', ' '],
[' ', '*', '*', '*', '*', '*', '*', '*', ' '],
['*', '*', '*', '*', '*', '*', '*', '*', '*']
]
```

or in more simple terms:

```
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
```

(this is what will print when you test from inside this file) This is a hard problem. Use lots of experimentation and draw lots of diagrams!

and below are the solutions that passed the test. This is a fairly simple function, and it’s a very strict test. So what that means is that all of these functions *do exactly the same thing!*

*Reading* code is *at least* as important as writing it, take the time to go through these examples of different ways of doing the same thing and think about which you like. Which is most readable? Which is most elegant? This is a great example of what we mean by choosing a good algorithm. There are almost always *many* ways to do things, so you need to be able to make a call about which one to pick and why.

If you are allergic to scrolling, get over it because there is more after all these functions.

# Rizo007

```
def loops_7():
columns = []
for x in range(5):
rows = []
for y in range(9):
if abs(y-4) <= x:
rows.append('*')
else:
rows.append(' ')
columns.append(rows)
print(columns)
return columns
```

# pennypangCODE

```
def loops_7():
columns = []
for x in range(5):
rows = []
for y in range(9):
if abs(y-4) <= x:
rows.append('*')
else:
rows.append(' ')
columns.append(rows)
print (columns)
return columns
pass
```

# FlimEden

```
def loops_7():
baseLength = 9
starting = int(baseLength / 2) # Get middle index
printNum = 1
height = starting + 1
pyramidArray = []
for i in range(height):
printIterator = printNum
pyramidArray.append([])
for j in range(baseLength):
if j >= starting and printIterator != 0:
pyramidArray[i].append("*")
printIterator -= 1
else:
pyramidArray[i].append(" ")
printNum += 2
starting -= 1
return pyramidArray
```

# sheldakristie

```
def loops_7():
ihatepyramids_box = []
for i in range(5):
ihatepyramids_inside = []
for j in range(4-i):
ihatepyramids_inside.append(" ")
for j in range(1+i):
ihatepyramids_inside.append("*")
for j in range(0+i):
ihatepyramids_inside.append("*")
for j in range(4-i):
ihatepyramids_inside.append(" ")
ihatepyramids_box.append(ihatepyramids_inside)
return ihatepyramids_box
```

# RangoRay

```
def loops_7():
numberfield = []
for i in range(5):
number = []
for j in range(9):
if j < 5 + i and j > 3 - i:
number.append('*')
else:
number.append(' ')
numberfield.append(number)
return numberfield
```

# lorniashi

```
def loops_7():
pyramid = []
for i in range(5):
row = '{0}{1}{0}'.format(' '*(5-i-1), '*'*(i*2+1))
pyramid.append(list(row))
print (pyramid)
return pyramid
```

# atiredturtle

```
def loops_7():
HEIGHT = 5
WIDTH = 9
arr = []
num_blanks = 4
for i in range(HEIGHT):
row = []
for i in range(WIDTH):
if i < num_blanks:
row.append(" ")
elif WIDTH - i <= num_blanks:
row.append(" ")
else:
row.append("*")
num_blanks -= 1
arr.append(row)
return arr
```

# alanw410

```
def loops_7():
pyramid = [0] * 5
sp = 4
sp2 = 5
def v(num):
if(num < sp or num >= sp2):
return True
pass
else:
return False
pass
for x in range(0, 5):
pyramid[x] = (map(lambda x: ' ' if(v(x)) else '*', range(0, 9)))
sp = sp - 1
sp2 = sp2 + 1
return(pyramid)
pass
```

# Matchalism

```
def loops_7():
pyramidList = []
for index in range(5):
stacklist = []
for j in range(9):
if (5 - index - 2 < j and j < index + 5):
stacklist.append("*")
else:
stacklist.append(" ")
pyramidList.append(stacklist)
return pyramidList
```

# TerryAg

```
def loops_7():
returnedList = []
for i in range(0, 5)[::-1]:
temp = [' ' for _ in range(9)]
", " temp[i:9-i] = '*'*(9-2*i)
returnedList.append(temp)
return returnedList
```

# sherry0303

```
def loops_7():
star_pyramid = []
for i in range(5):
row = list("*"*9)
left_bound = int((9-1)/2 - i)
right_bound = int((9+1)/2 + i)
for j in range(0, left_bound):
row[j] = " "
for j in range(right_bound, 9):
row[j] = " "
star_pyramid.append(row)
print(star_pyramid)
return star_pyramid
```

# AkisukeY

```
def loops_7():
pyramid = []
blank = 4
dot = 1
for i in range(5):
plane = []
for j in range(blank):
plane.append(" ")
for j in range(dot):
plane.append("*")
for j in range(blank):
plane.append(" ")
blank -= 1
dot += 2
pyramid.append(plane)
return pyramid
```

# 872815554

```
def loops_7():
baseLength = 9
starting = int(baseLength / 2) # Get
![](https://github.com/Get/code1161base/raw/master/mugshot.png) middle index
printNum = 1
height = starting + 1
pyramidArray = []
for i in range(height):
printIterator = printNum
pyramidArray.append([])
for j in range(baseLength):
if j >= starting and printIterator != 0:
pyramidArray[i].append("*")
printIterator -= 1
else:
pyramidArray[i].append(" ")
printNum += 2
starting -= 1
return pyramidArray
```

# dbisazza

```
def loops_7():
def isPointOnPyramid(i, j):
if j == 4:
row.append('*')
if (j > 4 - (i+1)) & (j < 4 + (i)):
row.append('*')
else:
row.append(' ')
column = []
for i in range(5):
row = []
for j in range(8):
isPointOnPyramid(i, j)
column.append(row)
print(column)
return column
pass
```

# timtamtinyman999

```
def loops_7():
columns = []
for x in range(5):
rows = []
for y in range(9):
if abs(y-4) <= x:
rows.append("*")
else:
rows.append(' ')
columns.append(rows)
return columns
```

# DarkPurple141

```
def loops_7():
temp = []
for i in range(5):
new = [' ']*9
for j, _ in enumerate(new):
if j >= 4-i and j <= 4+i:
new[j] = '*'
temp.append(new)
return temp
```

# zingjanet

```
def loops_7():
the_pyramid = []
spine = ['*']
for i in range(5):
space = []
tile = []
for j in range(4-i):
space.append(" ")
for k in range(i):
tile.append("*")
the_pyramid.append(space + tile + spine + tile + space)
return the_pyramid
```

# tomwyb

```
def loops_7():
pyr = []
for i in range(5):
row = []
for x in range(9):
if x < (4 - i) or x > (4 + i):
row.append(" ")
else:
row.append("*")
pyr.append(row)
return pyr
```

# matthewpoytress

```
def loops_7():
pyramid = []
for i in range(5):
row = '{0}{1}{0}'.format(' '*(5-i-1), '*'*(i*2+1))
pyramid.append(list(row))
print (pyramid)
return pyramid
```

# BaptisteHiggs

```
def loops_7():
pyramidList = []
height = 5
for y in range(height):
tempLine = []
for x in range(height*2-1):
if x >= height - 1 - y and x <= height + y - 1:
tempLine.append('*')
else:
tempLine.append(' ')
pyramidList.append(tempLine)
return pyramidList
```

# DanielHeh

```
def loops_7():
pyr = []
blank = 4
dot = 1
for i in range(5):
plane = []
for ii in range(blank):
plane.append(" ")
for ii in range(dot):
plane.append("*")
for ii in range(blank):
plane.append(" ")
blank -= 1
dot += 2
pyr.append(plane)
return pyr
```

# OneMoreN

```
def loops_7():
pyramidList = []
for index in range(5):
stackList = []
for x in range(9):
if (5 - index - 2 < x and x < index + 5):
stackList.append("*")
else:
stackList.append(" ")
pyramidList.append(stackList)
return pyramidList
pass
```

# This week’s reading

**Cormen, T. & Balkcom, D.**, *Algorithms.*

Go through the Intro to algorithms, Binary search and Asymptotic notation sections, these guys have made it really simple to understand and it’ll reinforce everything we’ve said in the lecture.

**Galef, J.** (2016). *Tom Griffiths and Brian Christian on “Algorithms to Live By”*. Rationally Speaking.

# This week’s homework

Complete the exercises in the week 3 folder.

It looks like there’s nothing to do to exercise 2, but actually that’s a big deal! You need to work out a way to make a diagram of the code. It needs to be easy to understand, and help you keep track of what it’s doing. There are lots of formal ways to diagram code (UML, flow charts) but don’t let that constrain you. Think about what makes the most sense to *you* and also about how you could use it to explain it to someone else. Make sure that your diagram goes into your lab book. It will really help you to do the same thing for exercise 3 and 4 too.

Other than that, you should be pretty deep into your Holy Wars research. The notes for that should be going into your lab book too.