Python Programming Assignments - COSC 235B - Prof. David A. Sykes, Exams of Computer Science

Python programming assignments from cosc 235b course by david a. Sykes. It includes various tasks such as calculating the slope of a line, running python programs, and displaying a horizontal bar chart based on data from a file.

Typology: Exams

Pre 2010

Uploaded on 08/18/2009

koofers-user-dwt
koofers-user-dwt 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COSC 235B David A. Sykes
1
Test #1
March 7, 2008
Name: ___________________________ Pledged: ___________________________
Answer all questions. If you can’t remember Python syntax to do something, write in English what you
would like to do.
1. Show the output of each of the Python programs.
x = 100
m = 0.5
b = 10
y = m * x + b
print "y =", y
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print "1)", len(alphabet)
print "2)", alphabet[2]
print "3)", ord('C') - ord('A')
print "4)", alphabet[2:6]
print "5)", alphabet[:13] + alphabet[13:]
print "6)", alphabet[0:4].lower()
print "->",
for ch in "ABCDEF":
print ch,
print
def foo(amt):
amtLeft = amt
for x in [ 25, 10, 5, 1]:
n = amtLeft / x
print x, ':', n
amtLeft = amtLeft % x
foo(72)
def bar(n):
"Do something"
s = 0
for i in range(1, n+1):
s = s + i
return s
print "bar(3) =", bar(3)
Y = 60.0
Key
1) 26
2) C
3) 2
4) CDEF
5) ABCDEFGHIJKLMNOPQRSTUVWXYZXYZ
6) abcd
-> A B C D E F
25 : 2
10 : 2
5 : 0
1 : 2
bar(3) = 6
pf3
pf4

Partial preview of the text

Download Python Programming Assignments - COSC 235B - Prof. David A. Sykes and more Exams Computer Science in PDF only on Docsity!

Test

March 7, 2008

Name: ___________________________ Pledged: ___________________________

Answer all questions. If you can’t remember Python syntax to do something, write in English what you

would like to do.

1. Show the output of each of the Python programs.

x = 100 m = 0. b = 10 y = m * x + b print "y =", y

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" print "1)", len(alphabet) print "2)", alphabet[2] print "3)", ord('C') - ord('A') print "4)", alphabet[2:6] print "5)", alphabet[:13] + alphabet[13:] print "6)", alphabet[0:4].lower()

print "->", for ch in "ABCDEF": print ch, print

def foo(amt): amtLeft = amt for x in [ 25, 10, 5, 1]: n = amtLeft / x print x, ':', n amtLeft = amtLeft % x

foo(72)

def bar(n): "Do something" s = 0 for i in range(1, n+1): s = s + i return s

print "bar(3) =", bar(3)

Y = 60.

Key

2) C

4) CDEF

5) ABCDEFGHIJKLMNOPQRSTUVWXYZXYZ

6) abcd

-> A B C D E F

bar(3) = 6

2. Two points in a plane are specified using the coordinates (x 1 , y 1 ) and (x 2 , y 2 ). Complete the definition

of a function that returns the slope of a line through two points passed as parameters. Assume x1 ≠

x2. The formula is given by

2 1

2 1 x x

y y slope

def slope(x1, y1, x2, y2):

3. Running the Python program in the top window produces the result in the bottom window. Show

how to modify the program so that it runs correctly.

m = float(y2 – y1) / (x2 – x1)

return m

Use raw_input() instead of input()

5. Write a program that reads data from a file named barchartdata.txt and displays a horizontal bar

chart based on the data contained in the file. Each line of the file contains an integer value N

followed by a space followed by a printable character C. For each line, in order, the program should

display N occurrences C, forming a bar. For example, if barchartdata.txt contains the lines in the

shaded area on the left, then the program generates the output shown on the right.

datafile = open('barchartdata.txt')

for line in datafile:

data = line.split()

height = int(data[0])

char = data[1]

print height * char

datafile.close()