Hello World! An Invitation to Computer Science

Laboratory 0: A gentle introduction to programming

Report due, via email, week of February 11 by start of your group conference that week.

Before proceeding - read this entire document thoroughly!

You should work on this lab with your partner and submit one lab report.


Goals


Resources

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.


Warm-up exercises

Scheme programming and the DrScheme environment

The Scheme portions of this lab are to be run using DrScheme with the language set to "Beginning Student".
  1. the interactions window. Write Scheme expressions that correspond to:

    1. 52 - 42
    2. taking the difference between five and three; multiplying it by seven; adding nine to that result.
    3. the number of inches in a mile (hint: there are 5280 feet in a mile).

  2. defining values in the definitions window.

    1. Write Scheme definitions that correspond to the following pseudocode:
        x ← 5
        yx3
        zy - 1
      
      Use the interactions window to see what the resulting value of z is.
    2. Write Scheme definitions that correspond to the following prose description: Let fav be your favorite number let diff be the difference between the square of one more than your favorite number and the square of two more than your favorite number. Use variables to avoid redundancy. For example, if you change your favorite number, you should only have to make one change in your program; if you change diff to be the difference between the square of one more than your favorite number and the square of three more than your favorite number you should only have to change a 2 to a 3.

  3. defining functions in the definitions window.

    1. Write a Scheme function that computes the cube of its input. Test out your function in the interactions window.
    2. Write a Scheme function that computes the sum of the cubes of two numbers. Make use of the cube function you just defined for the previous problem. Test out your function in the interactions window to verify that 43 + 103 is 1064.
    3. Here is a function that converts Celsius to Fahrenheit:
        (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.

  4. conditional expressions.

    1. Write a function corresponding to the following pseudocode:
        input n
        if n is even then:
          output n / 2
        else:
          output 3 * n + 1
      
    2. Write a function daysInMonth that given a number between 1 and 12, corresponding to a month, returns the number of days in that month for this year (2008).

A brief introduction to Python using IDLE

  1. the interactions window. Write Python expressions that correspond to:

    1. taking the difference between five and three; multiplying it by seven; adding nine to that result.
    2. the number of inches in a mile (hint: there are 5280 feet in a mile).

  2. 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.


Out-of-lab exercises

Answer these in your lab report.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. Given what you have seen in each language, which language do you prefer and why?