2/11/14, 1:52 PM
Lecture Notes on 16 Sep 2013
Problem: Prompt the user to enter 3 numbers and print
the numbers in ascending order. Write pseudo code for
the algorithm
# Read the numbers and store them in variables
Read first number
Set a to first number
2/11/14, 1:53 PM
Lecture Notes on 25 Nov 2013
# keeps only letters and removes everything else
def filter_string (st):
s = '
for ch in st:
if (ch >= 'a') and (ch <= 'z'):
s += ch
else:
s += ' '
return s
def main():
# open book
book = open ('hard_times.txt
2/11/14, 1:53 PM
Lecture Notes on 22 Nov 2013
# keeps only letters and removes everything else
def filter_string (st):
s = '
for ch in st:
if (ch >= 'a') and (ch <= 'z'):
s += ch
else:
s += ' '
return s
def main():
# open book
book = open ('hard_times.txt
2/11/14, 1:53 PM
Lecture Notes for 01 Nov 2013
Some Sample Questions for Test 2
1. Write a function that sums the following series to the nth
term
S(n) = 1 / (1 * 2) + 1 / (2 * 3) + . + 1 / (n + 1) * (n + 2)
for n >= 0.
2. Write a function that sums the f
2/11/14, 1:53 PM
Lecture Notes on 18 Nov 2013
def scalar_product (a, b):
sum = 0
for i in range (len(a):
sum += a[i] * b[i]
return sum
def seq_search (a, x):
index = []
for i in range (len(a):
if (x = a[i]):
index.append (i)
return index
def merge (a, b):
2/11/14, 1:53 PM
Lecture Notes on 13 Nov 2013
Solution to Test 2 Problems
# Ques 1
def p (n):
if (n = 0):
return 0
else:
return (2 + 3 * p (n - 1)
def p_iter (n):
sum = 0
for i in range (n):
sum = 2 + 3 * sum
return sum
def p_closed (n):
return (3 * n - 1
2/11/14, 1:53 PM
Lecture Notes 11 Nov 2013
def find_max (b):
max = b[0][0]
for row in b:
for col in row:
if (col > max):
max = col
return max
def find_min (b):
min = b[0][0]
for row in b:
for col in row:
if (col < min):
min = col
return min
def sum_rows (
2/11/14, 1:53 PM
Lecture Notes on 16 Oct 2013
def is_prime (n):
limit = int (n * 0.5) + 1
for divisor in range (2, limit):
if (n % divisor = 0):
return False
return True
def sum_digits (n):
sum = 0
while (n > 0):
sum += n % 10
n = n / 10
return sum
def re
2/11/14, 1:52 PM
Lecture Notes on 04 Oct 2013
def main():
# Sum numbers 1 through 10
sum = 0
counter = 1
while (counter <= 10):
sum = sum + counter
counter = counter + 1
print (sum)
# Version 1: Determine prime or not
# Prompt the user to enter a number
n
2/11/14, 1:52 PM
Lecture Notes on 07 Oct 2013
# Outline of Assignment 5
def main():
# Prompt the user to enter a positive number
n = int (input ('Enter a positive number: ')
# Prompt again if the number is not positive
while (n <= 0):
print ('Negative num
2/11/14, 1:52 PM
Lecture Notes on 27 Sep 2013
* Test 1 on 2 Oct 2013 - in class and closed book
No multiple choice questions
Number conversions
Write code or interpret code
Study guide posted
* Assignment 3 due today
* Code Lab Tutorial exercises for Chap
2/11/14, 1:52 PM
Lecture Notes on 23 Sep 2013
Recapitulate What We Have Done So Far:
* We see the world in binary
* Number conversions: decimal to binary, octal, hexadecimal
and vice versa
* Structure of a computer language
* Algorithms and Pseudocode
* P
History of Computing
2/11/14, 1:51 PM
History of Computing
1. Earliest Traditions
Man need to keep track of things - whether it was the number of things, the measure of distance, or weight, or
time. He used the digits of his hands as the rst counting devi
Algorithms
2/11/14, 1:51 PM
Algorithms
What is an algorithm?
A program is a set of instructions that a computer executes to achieve a certain desired effect - perform a
calculation, render a picture, or produce music. A program is written in a specic prog
Data Structure
2/11/14, 1:52 PM
Tuple
A tuple is like a list. It is an ordered sequence but unlike a list it is immutable. It is created by specifying the
items in the tuple within parentheses and separated by commas.
coord = (7.3, 2.9)
employee_record =
2/11/14, 1:53 PM
Lecture Notes on 15 Nov 2013
# Outline of Word Search
def check_columns (string_cols, word):
# initialize row_num and col_num to zero
# create rev_word that is the reverse of the word
# iterate through the list string_cols
# if word or re