CMU 15-112: Fundamentals of Programming and Computer Science
Week 1: Data and Variables
- To start:
- Create a folder named 'Week1'
- Download1. dataVariablesPractice.py
- Edit the downloaded file using your preferred editor
- Do not use conditionals, strings, loops, lists, or recursion during this practice.
- Use only data, variables, operators and function calls.
Code Tracing
What will this code print? Figure it out by hand, then run the code to confirm. Then slightly edit the code and try again.
- Trace #1 of 3:
import math
def p(x): print(x, end=' ') # prints and stays on same line
p(3 - 1 + 2 * 6 // 5)
p(3 - 1 + 2 * 6 / 5)
p(2**4/10 + 2**4//10)
p(max(8/3,math.ceil(8/3)))
print()
- Trace #2 of 3:
def f(x): return 4*x + 2
def g(x): return f(x//2 + 1)
def h(x):
print(f(x-3))
x -= 1
print(g(x)+x)
x %= 4
return g(f(x) % 4) // 2
print(1 + h(6))
- Trace #3 of 3:
# foo(x) is used by ct1
def foo(x):
x + 1
return x- 1
def ct1(x):
print(x + 3)
y=foo(x%2 + foo(12))
print("y:",y)
x=8
print("study")
print(ct1(x))
Free Response (Problem-Solving)
- eggCartons(eggs)
Write the function eggCartons(eggs) that takes a non-negative integer number of eggs, and returns the smallest integer number of
cartons required to hold that many eggs, where a carton may hold up to 12 eggs.
- distance(x1, y1, x2, y2)
Write the function distance(x1, y1, x2, y2) that takes four int or float values x1, y1, x2, y2 that represent the two points (x1, y1) and (x2, y2), and returns the distance between those points as a float.
- triangleArea(s1, s2, s3)
Write the function triangleArea(s1, s2, s3) that takes 3 floats representing sides and
returns the area of the triangle that has those lengths of its side.
Assume that only legal input is provided. Hint: you will probably wish to
use Heron's Formula.
- triangleAreaByCoordinates(x1, y1, x2, y2, x3, y3)
Write the function triangleAreaByCoordinates(x1, y1, x2, y2, x3, y3) that takes 6 int or float values that represent the three points (x1,y1),
(x2,y2), and (x3,y3), and returns as a float the area of the triangle formed by those three points. Hint: you should make constructive use
of the functions you just wrote above or use the internet to find a formula. A challenge would be to try both.
- lineIntersection(m1, b1, m2, b2)
Write the function lineIntersection(m1, b1, m2, b2) that takes four int or float values representing the 2 lines:
y = m1*x + b1
y = m2*x + b2
This function returns the x value of the point of intersection of
the two lines. Assume all the lines are intersecting at a point and not parallel or identical.
- nthFibonacciNumber(n)
Background: The Fibonacci numbers are defined by F(n) = F(n-1) + F(n-2). There are different conventions on whether 0 is a Fibonacci
number, and whether counting starts at n=0 or at n=1. Here, we will assume that 0 is not a Fibonacci number, and that counting starts at
n= 1, so F(1)=1, and F(2)=2. With this in mind, write the function nthFibonacciNumber(n) that takes a non-negative int n and returns the nth
Fibonacci number. Some test cases are provided for you. You can use
Binet's Fibonacci Number Formula which (amazingly) uses the
golden ratio to compute this result.
- Bonus : isFactor(f, n)
Without using any conditionals, write the function isFactor(f, n) that takes two int values f and n, and returns True if f is a factor of n,
and False otherwise. Note that every integer is a factor of 0.