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. Programming with Turtles. Read and follow the examples of 3.1 and 3.2 in How to think like a computer scientist and briefly scan section 3.6.

    1. Experiment with this code fragment:

      import turtle
      window = turtle.Screen()
      mike = turtle.Turtle()
      
      window.bgcolor('yellow')    # change the background color as you see fit
      mike.shape('square')        # change my shape as you see fit
      
      for _ in range(16):   # you can change the number of iterations
          # you can reorder these commands and change their parameters as you see fit
          mike.color('green')
          mike.speed(1)
          mike.penup()
          mike.stamp()
          mike.forward(17)
          mike.right(30)
          mike.pendown()
          mike.left(45)
          mike.forward(34)
      
      window.mainloop()
    2. Try exercise 13.

    3. Try exercise 11. You may use a for loop for this problem.


  3. Python basics

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

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

      >>> type(99)
      >>> type('99')
      >>> type('computer')
      >>> type("computer")
      >>> type(computer)
      >>> computer = 99
      >>> type(computer)
      >>> type('computer science')
      >>> 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.

      >>> input('type something: ')       # type 34
      >>> int(input('type something: '))  # type '34'
      >>> 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:

      >>> 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:

      >>> word = 'the'
      >>> len(word)
      >>> print(word)
      >>> phrase = word + ' time'
      >>> print(phrase)
      >>> phrase = 'now is ' + phrase
      >>> print(phrase)
      >>> 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:

      >>> 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:

      >>> 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:

      >>> 17 / 5
      >>> 17 // 5
      >>> 17 % 5
      >>> 77 / 77
      >>> 77 // 77
      >>> 77 % 77
      >>> 11 // 13
      >>> 11 % 13
      >>> 13 // 11
      >>> 13 % 11
      >>> (3 * 7) + 1
      >>> (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:

      >>> weeks_days_hours()
      How many hours? 9
      = 0 weeks, 0 days, 9 hours
      >>> weeks_days_hours()
      How many hours? 76
      = 0 weeks, 3 days, 4 hours
      >>> 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:

      >>> find_row_column()
      Enter a number: 8
      8 appears in row 1 and column 3
       >>> 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.