Introduction to Computer Programming: Lab 0

Goals

Instructions


  1. Programming with Blocks

    1. Try this simple block-programming puzzle to get used to how blocks are manipulated. It should only take a few minutes to complete. (It is not trying to teach you anything interesting about animal biology!)

    2. Now try these maze-navigation puzzles. The blocks do all the hard work for you; you can see what the solution would look like in another programming language (JavaScript) when you complete each level. You should not worry about the JavaScript syntax (that is another course entirely) it is there to prove to you that the abstract, graphical blockly system is equivalent to more traditional text-based code. See how many levels you can complete in 15 minutes or so. For extra credit, when time permits, try the 10th and final maze-navigation puzzle.


  2. Python basics

    1. A warm up. Try these examples in order, one at a time:

       In []: 33 + 47
       In []: x
       In []: x = 5
       In []: x
       In []: x = x + 1
       In []: x
       In []: print('x')
       In []: print(x)
       In []: print('the value of x is', x, '.')
    2. Data types and the type function. Now try these examples in order:

       In []: type(99)
       In []: type('99')
       In []: type('computer')
       In []: type("computer")
       In []: type(computer)
       In []: computer = 99
       In []: type(computer)
       In []: type('computer science')
       In []: type(computer science)
    3. You should feel comfortable about why some very similar looking (to humans!) expressions produced different results. Now write three more examples using the type function that produce three different results distinct from any of the above examples.

    4. Inputting numbers. Try the three examples below. Be able to explain why the result produced is different in each case.

       In []: input('type something: ')       # type 34
       In []: int(input('type something: '))  # type '34'
       In []: int(input('type something: '))  # type 'thirty-four'
    5. Inches per mile. Complete the following program to compute the number of inches in a given number of miles:

       def inches_for_miles():
           miles = int(input('Enter a number of miles: '))
           total = ...   # <--- replace the ... with the correct Python code
           print('There are', total, 'inches in', miles, 'miles.')

      Remember there are 12 inches in a foot and 5280 feet in a mile. Example:

       In []: inches_for_miles()
       Enter a number of miles: 6
       There are 380160 inches in 6 miles.
    6. An algorithm’s inverse. Here is a program that converts Celsius to Fahrenheit:

       def c2f():
           c = int(input('What is the Celsius temperature? '))
           f = (9.0/5.0) * c + 32
           print(c, 'degrees Celsius is', f, 'degrees Fahrenheit.')

      Write a similarly structured program that achieves the inverse, namely converts Fahrenheit to Celsius.

    7. Googol (the number, not the search engine). Complete this program that computes one googol (one followed by one-hundred zeroes) using only numbers, +, -, *, / and the ability to define variables: using as few characters as possible. (Obviously, you can do it with 101 characters. See how much smaller you can make your program.)

       def googol():
           print('one googol =', ...)

      Think about the power of symbolic abstraction and how it applies here.

    8. String exercises. Try these in order, one at a time:

       In []: word = 'the'
       In []: len(word)
       In []: print(word)
       In []: phrase = word + ' time'
       In []: print(phrase)
       In []: phrase = 'now is ' + phrase
       In []: print(phrase)
       In []: len(phrase)
    9. String length. Write a function that asks the user to enter some text and then reports how many characters were entered. Example:

       In []: say_length()
       Enter some text: now is the time
       That phrase has 15 characters in it.
    10. String concatenation. Write a function that asks the user to enter their first name and then their last name. Use the concatenation of those two strings to report back how many letters are in their name. (Careful: the space displayed between the first and last name should not count toward the length of the full name.) Example:

       In []: full_name()
       Enter your first name: Jim
       Jim, now enter your last name: Marshall
       Jim Marshall, your full name has 11 characters in it.
    11. Integer division and remainder. Try these in order, one at a time:

       In []: 17 / 5
       In []: 17 // 5
       In []: 17 % 5
       In []: 77 / 77
       In []: 77 // 77
       In []: 77 % 77
       In []: 11 // 13
       In []: 11 % 13
       In []: 13 // 11
       In []: 13 % 11
       In []: (3 * 7) + 1
       In []: (22 - 1) // 7
    12. Write a program that asks the user for a total number of hours, and converts this to the corresponding number of weeks, days, and hours. Hint: the remainder (%) and integer division (//) operators might come in handy here. Examples:

       In []: weeks_days_hours()
       How many hours? 9
       = 0 weeks, 0 days, 9 hours
       In []: weeks_days_hours()
       How many hours? 76
       = 0 weeks, 3 days, 4 hours
       In []: weeks_days_hours()
       How many hours?  1000
       = 5 weeks, 6 days, 16 hours
    13. Grid counting. Imagine that you are counting the on a grid that has five columns (numbered 0 through 4) as such:

                 column #
               0  1  2  3  4
       row# |----------------
         0  |  0  1  2  3  4
         1  |  5  6  7  8  9
         2  | 10 11   ...
         3  | 15 ...
         4  | 20 ...
         5  | 25 ...

      Write a program that asks user for a number and outputs the row and column for that number. For instance:

       In []: find_row_column()
       Enter a number: 8
       8 appears in row 1 and column 3
        In []: find_row_column()
       Enter a number: 17
       17 appears in row 3 and column 2

      Hint: make use of the remainder (%) and integer division (//) operators.