Homework 3  Arrays, Loops, and Then Some
Due: Friday, July 16, 2010
Introduction
This assignment is designed to introduce you to arrays as well as reinforce your understanding
of Exceptions, loops, and Java, in general.
It asks you to build a Bookshelf c
#Rula AlBaghdadi
#Principles of Computing
#Sunday, September 9th 2012
# 10:30am  11:50am
from turtle import *
#multiple positions and nested loops
#the 3x3 grid has multiple repeating units: square and row
def grid3x3 ():
for k in range (3): #this overa
Logic
1) Recall that a variable can store different kinds of data. A variable can hold a Boolean (logical) value
of True or False. The following Python function prints out what is legal to do for a person in the U.S.
given the person's age, gender, and ci
Random Generators
1) This question deals with generating random numbers using a linear congruential generator (LCG).
Recall the conditions that must hold for an LCG with parameters a, c, and m to have its maximum
period of m:
c and m must be relatively pr
Python Introduction
1) For each of the following Python expressions, show how they are evaluated by evaluating one
operation at a time and showing the reduced expression until you have a final answer. For example, if
the Python expression is 3 * 5 + 5, yo
Computing History and Binary Representations
1) Why is Jacquard's loom considered to be a milestone in the history of computing?
Answer briefly using at most 3 sentences.
2) The development of the Von Neuman Architecture is considered as a key step in the
BigO, Lists, and Search
1) Suppose words is a list of strings.
a) Assuming words contains at least four strings, write a single python statement that prints the thirdtolast string in words without changing the list.
b) Write a python function mash(words
Recursion
1) You are given the following recursive function in Python:
def f(n) :
if n % 2 = 0 or n = 1 :
return n
else :
return f(n / 2) + f(n  2)
Trace the computation of f(9) by drawing a recursion tree (like the one for Fibonacci numbers on the
slide
Internet, Security
1) Open a command line terminal on an Andrew machine and run the following commands. Describe in
a sentence or two what you understand from the output of each command. You are not expected to
understand every detail; try to extract the
Bits
1) Consider the 8bit value 10101011. This might be interpreted as an unsigned integer, signed integer,
or ASCII character transmitted with odd parity (the parity bit as the leftmost bit).
a) What is its value (in decimal notation) if it is interpre
2D Lists
1) Recall from class that we have shown you how to compute the total sum of all integers in a twodimensional list (representing a matrix) as follows:
def print_sum(matrix):
sum = 0
for row in range(0,len(matrix):
for col in range(0,len(matrix[ro
Objects
The ideas behind object oriented programming were rst developed in the 1960s and have been widely
adopted in a variety of programming languages since that time.
Every data item in Python is an object.
The word class refers to a type of data.
W
Manar Naboulsi
9 September, 2012
I.
Nested Loops:
A. List repeated items:
Lines 4 lines make a small square
Squares 3 squares make a row
Rows 3 rows make a large square
B. Start from insideout
Single small square:
We want to repeat this square 3 time
Principles of Computing
9/16/2012
Concept of Variables, Values & Types
Variables:
We have seen it before in TURTLE>
parameter input
the i in for i in range
Its like a box where we put the value inside
Variables have labels
3
X=3
X

taking a 3 and pu
Creating a Spiral
o
We already know how to create lines of set sizes by using the forward()
and right/left() functions
o
In order to create lines of varying sizes, the variable that we use in a for
loop comes in handy
o
If we were to guess how to do this
060912
Class Notes by Fatima Amir
Principles of Computing 15110
Making a definition with Python
Codes can also be defined with one word so that if one has to repeat that specific code, the
defined word can be used instead. This makes the job simpler a
Tues. Sept 11
from turtle import *
def spiral():
for i in range(1, 13):
#use (1, 13) and not(12) to start the counting from 1 and not from zero
fd(10*i) #shortcut of forward
lt(90) #shortcut of left
def spiral2(NumSpikes):
for i in range (NumSpikes):
fd(1
090912
Class Notes by Fatima Amir
Principles of Computing 15110
Nested Loops
o Problem: How can we make a grid like the one below?
Lets break this problem down into smaller pieces. To make a grid, you need to initially make a
square. Heres how you wil
#Rula AlBaghdadi
#Principles of Computing
#Tuesday, 11th September 2012
#10:30am11:am
from turtle import *
def spiral ():
for i in range (1,13): #This is the defined range, because while you seek 12
repetitions, for i in range (12) counts from 011 (12
040912
Class Notes by Fatima Amir
Principles of Computing 15110
Introduction to Python
A turtle is the little arrow like thing in Python that we are able to command
through different codes, as shown below.
Coding with Python
o Always start with from
September 16th 2012
Class notes
Variables and Values
We can imagine the Variable as a large box and the values is the things we put in that box.
Variables must be called by a letter.
The symbol equal is called assignment operator
3
X
Think about this proc
110912
Class Notes by Fatima Amir
Principles of Computing 15110
Problem #1: Making a concentric square
o Here are the Python commands:
o
Heres the outcome:
110912
Class Notes by Fatima Amir
Principles of Computing 15110
Problem #2: Making a spiral
General Advices of improving your grades:
1. Go to the ARC with ALL your previous quizzes and exercises to discuss it with the TA or peer
tutor.
2. Try connecting the activities we do in class with the ones in the quizm because most of the time
they have
Variables, values, and types
Examples of variables we have already seen are:
The parameters of a function are variables
The "i" in the loop of a defined function
A variable is like a labeled box; the contents of the box (values) are stored and
retrieved
160912
Class Notes by Fatima Amir
Principles of Computing 15110
Variables and values
In the past, the variables we have worked with are:
i, j, k etc for the range loop
As parameters\
Variables can be understood with a simple analogy.
A variable is l
#variables and values
x=3
y=2
z=x+y
the = sign is called the assignment operator, it assigns a value to a variable
print"x is", x
takes whatever is written after the print and pastes it
, means followed by
It shows "x is 3"
print"x is", x
print"x is", y
p
Programming Technique
Programmers write helper functions as a way of focusing on a single aspect of a complex problem,
allowing them to design and test the code for each piece separately.
Many Python programmers have adopted a process from software engi
Search and Sort
Two fundamental operations used in this wide variety of information management application is
searching and sorting.
search algorithms scan through a collection to locate a particular item of interest, and sort algorithms
reorganize coll
15110 Spring 2011 Midterm 1
80 minutes
1.
[10 pts; 1 pt each] True or False (circle one):
a. TRUE or FALSE: Any binary number that ends in a 1 must be odd.
b. TRUE or FALSE: Any binary number that contains exactly one 1 must be a power of 2.
c. TRUE or F
Recursion
def recursiveFunction():
if (this is the base case):
# no recursion allowed here!
do something nonrecursive
else:
# this is the recursive case!
do something recursive
def interleave(list1, list2):
# assume list1 and list2 are samelength lists
<answers.txt>
a. 28 % 5
> 3
integer
b. 20 + 35 * 2
> 90
integer
Why is this different from (20 + 35) * 2? > 35*2 gets computed before 20+35
c. 2 / 3 * 3
> 2.0
floating point
d. 2 / 3 * 3
> 0
integer
Why is this different from 2 / 3 * 3? > 2/3 is an inte
Sample Answers for Problem Set 1
15110  Spring 2016
1. Jacquard's loom used punched cards to input the patterns to be
weaved by the loom. If there was a hole in the card in a particular
location, then a hook could pass through the card, grasp a thread, a
<print_even.py>
def print_for_loop(n):
for i in range(0, n+1):
if i % 2 = 0:
print(i)
def print_while_loop(n):
i = 0
while i < n+1:
if i % 2 = 0:
print(i)
<add_even.py>
def add_for_loop(n):
num_sum = 0
for i in range(0, n+1):
if i % 2 = 0:
num_