Code Developed in CSCI-1100 Fall 2026
Lecture 1
Module: three_doubles — Finds three consecutive pairs of double letters
Code:
""" Find all words containing three consecutive pairs of double letters
in a file of all English words located at:
http://www.greenteapress.com/thinkpython/code/words.txt
**Modules used:** :py:mod:`urllib`
**Author**: Sibel Adali <adalis@rpi.edu>, Chuck Stewart <cvstewart@gmail.com>
**Returns:** All words matching condition and the count of found words
**Pseudo Code**::
open the file from the web with all the words in English
for each word in the file:
for all positions l in the word
if the letters at positions (l and l+1) are the same
and the letters at positions (l+2 and l+3) are the same
and the letters at positons (l+4 and l+5) are the same then
output word and increment the count
"""
import urllib.request
def has_three_double(word):
"""
Returns True if the word contains three consecutive pairs of
double letters and False otherwise.
"""
for l in range(len(word)-5):
if word[l] == word[l+1] and \
word[l+2]==word[l+3] and \
word[l+4]==word[l+5]:
return True
return False
# Comments that fit in a single line can be put in this format.
# The main body of the program starts here
"""
Assign the location of the words file and go get it.
"""
word_url = 'http://www.greenteapress.com/thinkpython/code/words.txt'
word_file = urllib.request.urlopen(word_url)
'''
Process each word in the file one by one, testing to see if it has
three consecutive doubles. Print it and count it if it does.
'''
count = 0
for word in word_file:
word = word.decode().strip()
if has_three_double(word):
print(word)
count = count + 1
'''
After we've gone through all the words, output a final message based
on the number of words that were counted.
'''
if count == 0:
print('No words found')
elif count == 1:
print("1 word found")
else:
print(count, 'words were found')
Lecture 3
Module: lec03_surface_and_area — Find the surface and area of a cylinder
Code:
pi = 3.14159
radius = input("Radius: ")
radius = float(radius)
height = input("Height: ")
height = float(height)
base_area = pi * radius ** 2
volume = base_area * height
surface_area = 2 * base_area + 2 * pi * radius * height
print("volume is", volume, ", surface area is", surface_area)
Lecture 5
Module: lec05_surface_and_area — Find the surface and area of a cylinder
Code:
'''
This is a program to calculate the surface area and
volume of a cylinder given a radius and a height.
Radius and height are in float and are user inputs.
Sample Execution:
Enter radius (float) => 12
Enter height (float) => 10
Surface area is: 1658.76
Volume is: 4523.89
9/18/2023
'''
import math
def area_circle(radius):
'''
This function returns the area of a circle with a given radius.
radius is the input parameter
'''
area = math.pi * radius ** 2
return area
def area_cylinder(h, r):
'''
Give a height h and radius r, return the surface area of a cylinder.
'''
cap_area = 2 * area_circle(r)
rect_area = math.pi * 2 * r * h
return cap_area + rect_area
if __name__ == "__main__":
r = float(input("Enter radius (float) => "))
h = float(input("Enter height (float) => "))
print("Surface area is: {:.2f}".format(area_cylinder(h, r)))
print("Volume is: {:.2f}".format(h * area_circle(r)))
Lecture 6
Module: lec06_rectangle — Does a given point fall within a rectangle
Code:
'''
Program to demonstrate the use of complex boolean expressions and if/elif/else
clauses. Determine whether a set of coordinates fall within a rectangle given
by the verticies (x0, y0), (x0, y1), (x1, y1), and (x1, y0)
Author: CS1 Staff
Date 9/21/2024
'''
'''
Initialize the rectangle
'''
x0 = 10
x1 = 16
y0 = 32
y1 = 45
'''
Get the target point
'''
x = input("x coordinate ==> ")
print(x)
x = float(x)
y = input("y coordinate ==> ")
print(y)
y = float(y)
'''
If the x coordinate matches x0 or x1 and we are within the y range, we are
on the boundary. Similarly, if the y coordinate matches y0 or y1 and we are
within the x range, we are also on the boundary
'''
if ((x == x0 or x == x1) and (y0 <= y <= y1) or (y == y0 or y == y1) and (x0 <= x <= x1)):
print("Point ({:.2f},{:.2f}) is on the boundary.".format(x, y))
elif (x0 < x < x1) and (y0 < y < y1):
'''
If we are not on the boundary, but we are in range in both x and y,
then we are inside the rectangle
'''
print("Point ({:.2f},{:.2f}) is inside the rectangle.".format(x, y))
else:
'''
If we are not on the boundary and we are not inside the rectangle, then
we must be inside.
'''
print("Point ({:.2f},{:.2f}) is outside the rectangle.".format(x, y))
Lecture 7
Module: lec07_area — Set up a module for area calculations
Code:
'''
Lecture 7 - Area Module
Prof. Charles Stewart
We've gathered the code from our area calculations to form a module
that can be used by other programs.
'''
import math
def circle(radius):
''' Compute and return the area of a circle '''
return math.pi * radius**2
def cylinder(radius,height):
''' Compute and return the surface area of a cylinder '''
circle_area = circle(radius)
height_area = 2 * radius * math.pi * height
return 2*circle_area + height_area
def sphere(radius):
''' Compute and return the surface area of a sphere '''
return 4 * math.pi * radius**2
Module: lec07_use_area — Use the area module in a separate main
Code:
'''
Lecture 7 - Demonstrate the use of the area calculations
Prof. Charles Stewart
'''
import lec07_area
r = 6
h = 10
a1 = lec07_area.circle(r)
a2 = lec07_area.cylinder(r,h)
a3 = lec07_area.sphere(r)
print("Area circle {:.1f}".format(a1))
print("Surface area cylinder {:.1f}".format(a2))
print("Surface area sphere {:.1f}".format(a3))
Module: lec07_images_init — Image chipmunk example
Code:
from PIL import Image
filename = "chipmunk.jpg"
im = Image.open(filename)
print('\n' '********************')
print("Here's the information about", filename)
print(im.format, im.size, im.mode)
gray_im = im.convert('L')
scaled = gray_im.resize( (128,128) )
print("After converting to gray scale and resizing,")
print("the image information has changed to")
print(scaled.format, scaled.size, scaled.mode)
scaled.show()
scaled.save(filename + "_scaled.jpg")
Lecture 9
Module: lec09_co2_percentages — CO2 percentages from class examples
Code:
'''
Demonstrate walking through a list calculating values between pairs of
values. In this instance we are calculating the percent change year-to-year
for CO2 concentration.
'''
co2_levels = [ (2001, 320.03), (2003, 322.16), (2004, 328.07),\
(2006, 323.91), (2008, 341.47), (2009, 348.92),\
(2010, 357.29), (2011, 363.77), (2012, 361.51),\
(2013, 382.47) ]
i=1
percent_change = []
while i< len(co2_levels):
percent_change.append((co2_levels[i][1] - co2_levels[i-1][1]) / co2_levels[i-1][1])
i += 1
print(percent_change)
Module: lec09_loop_variable_examples — Three examples of manipulating loop variables
Code:
'''
Two examples of manipulating loop variables. The first prints out every
other element of the list starting from the first element. The second uses the
loop variable to print out an evergreen tree.
'''
months=['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']
i = 0
while i < len(months):
print(months[i])
i+= 2
'''
Now print out the evergreen.
'''
print()
i = 1
#length = 9
while i < 10:
print((4 - i//2 )*" " + i*'*')
i+= 2
print(3*" "+3*'*')
print(3*" "+3*'*')
'''
Finally, let's let the user pick
an odd value > 3.
'''
print()
field = int(input("Enter an odd number greater than 3: "))
if field % 2 == 1 and field > 3:
j = 1
while j <= field:
print((field - j) // 2 * ' ', j * "*")
j += 2
j = 3
print((field - j) // 2 * ' ', j * "*")
print((field - j) // 2 * ' ', j * "*")
else:
print("{} is not an odd integer greater than 3.".format(field))
Module: lec09_nested_loop — Example of doubly nested loop
Code:
'''
Quick code snippet to demonstrate walking through a list operating on
all pairs of list elements without repeating matches and without operating
on the diagonals.
CS1
'''
L = [2, 21, 12, 8, 5, 31]
i = 0
dist = abs(L[0] - L[1])
indices = 0 ,1
while i < len(L):
j = i +1
while j < len(L):
test_dist = abs(L[i] - L[j])
if test_dist <= dist:
dist = test_dist
indices = i, j
j += 1
i += 1
print("Closest {} at {}.".format(dist, indices))