Before proceeding - read this entire document thoroughly!
You should work on this lab with your partner and submit one lab report.
Scheme. It is easy to download and install DrScheme on just about any computer. You may find parts of the first four chapters of How to Design Programs helpful.
Python. It is almost as easy to download and install Python. Finding supporting documents for this very basic introduction to Python is tricky (we will revisit Python in more detail midway through the semester). Most of what is out there will likely complicate matters, but you might try looking at the first few lessons (particularly 2 through 4) of this beginner's Python Tutorial.
You do not need to download either piece of software. You do not even need to own a computer. Both programming languages are available on the machines in this lab (Science 104) which you have access to during normal daytime hours when there is not a class using the room. If you wish access to the lab in the evenings, let me know.
the interactions window. Write Scheme expressions that correspond to:
defining values in the definitions window.
x ← 5 y ← x3 z ← y - 1Use the interactions window to see what the resulting value of z is.
defining functions in the definitions window.
(define (celsiusToFahrenheit c)
(+ 32 (/ (* 9 c) 5)))
Write the inverse function that converts Fahrenheit back to
Celsius. Test out your function in the interactions
window to verify that 212oF is equivalent to
100oC.
conditional expressions.
input n
if n is even then:
output n / 2
else:
output 3 * n + 1
the interactions window. Write Python expressions that correspond to:
the definitions window. Here is a program that computes and prints the sum of the first n positive integers (in this case, n is 5):
n = 5
s = 0
i = 1
while i <= n:
s = s + i
i = i + 1
print s
Modify the program so that it computes and prints the factorial
of n when n is 6 (that is the product of the first six
positive integers). Run the program to make sure you get the correct
result: 720.
Answer these in your lab report.
Write a Scheme expression to compute the approximate number of seconds in a century. (By approximate I mean assuming 365 days per year.) Include in your report both the Scheme expression and the resulting total number of seconds.
Write a function triple? that takes three arguments, a, b, and c, and returns true if the they form a Pythagorean triple (in other words if a2 + b2 = c2) and false if they do not. Be sure to test your function thoroughly.
Experiment with the predefined operators min and max. Write a function mid that takes three numbers and returns the middle value. If there are ties, that is okay: think of placing the three numbers in order, and taking the second of the three values. So (mid 37 12 55) should return 37, (mid 14 99 14) should return 14, and (mid 101 8 101) should return 101.
Do some research on leap years. You know that 1996, 2000, 2004, and 2008 are all leap years and none of the years in between are. But did you know that 1900 is not a leap year? Write pseudocode for an algorithm that determines whether a given year is a leap year. You can assume that the computing agent can perform the "remainder" operation.
Implement your solution as a Scheme function called leapYear? that returns true if the input corresponds to a leap year and false otherwise.
Write a new version of the daysInMonth function so that given a month number and a year it returns the correct number of days in that month.
In both Scheme and Python, using only +, -, *, / and the ability to define variables: write a program that computes googol (one followed by one-hundred zeroes) using as few characters as possible. (Obviously, you can do it with 101 characters. See how much smaller you can make your program.)
Now play around with Python and see if you can find an even more compact way to express googol.
Write pseudocode for an algorithm that given a number n, if n is odd, computes the sum of the squares from 1 to n2, if n is even computes the sum of the cubes from 1 to n3.
Implement your solution as a Python program and use it to compute the sum of the squares from 1 to 9 and the sum of the cubes from 1 to 10.
Given what you have seen in each language, which language do you prefer and why?