CS8, 09M, UCSB

H05: (Based on Miller/Ranum, Chapter 3 (pp. 91-92) and Chapter 4 (up to p. 128) Total points: ? (printable PDF)

Accepted: on paper, in lab (either at 11am, or 12:30pm, in Cooper Lab) on Thursday, August 20th
No email submission allowed.

Name: (2 pts)______________________________   UCSBNetID (2 pts)  _____________________

Lab Section (1 pts) Circle one:         Thu 11am                 Thu 12:30pm                     Unknown

Circle the one you plan to attend, which may or may not be the one you are registered for.


Review pp. 91-92 of Chapter 3. Then answer these questions:

  1. (2 pts) Pages 91-92 talks about how characters are represented using bits. What is the number that is used to represent the lowercase letter 'a'?


  2. (2 pts) What is the Python function that converts a single character into its numeric equivalent?


  3. (2 pts) What is the Python function that converts a number into the corresponding character?


  4. (4 pts) For this question, you'll need the Python prompt.

    Take the first letter of your first name, and write it here (as a capital letter)

    Then take the first letter of your last name and write it here (as a capital letter)

    Then, use the function you mentioned in question 2 above to convert those letters to numbers. Write the corresponding numbers here:

  5. Review the material on p. 86 through 89 about string indexing, slicing, and other operations. Then, write two functions definitions:


    1. (10 pts) Write the definition of a function called first that takes a string s as a parmeter, and returns only the first character in the string. For example first("Gauchos") returns "G" and first("CS8") returns "C".








    2. (10 pts) Write the definition of a function called rest that takes a string s as a parameter and returns everything EXCEPT the first character in the string. For example rest("Gauchos") returns "auchos" and rest("CS8") returns "S8".








      Please turn over for more problems

      Continued from other side


  6. Review Chapter 4, pages 120-127 about lists. Then answer these questions. You may find the Python prompt helpful in answering these.

    1. (5 pts) Here is a Python session that illustrates how we can use a for loop to iterate over a list. Iterate means "process each item in sequence".

      >>> for name in ["Dave", "Sheila", "Maria"]:
      	   print("Hello " + name)
      
      	
      Hello Dave
      Hello Sheila
      Hello Maria
      >>>           
                
      Given that, what would be the output of the following?


      >>> total = 0
      >>> for name in ["Dave", "Sheila", "Maria"]: total = total + len(name) >>> print("total = " + str(total))
    2. (10 pts) We can define a function that takes a list as a parameter. For example, this function counts how many times the word "dog" appears in a list.
      def dogCount(theList):
          
          count = 0 # initialize count
          for word in theList:
              if word=="dog":
                 count = count + 1
      
          return count

      check_expect("dogCount test 1", dogCount(["dog","cat","dog"]), 2) check_expect("dogCount test 2", dogCount(["cat", "mouse", "tiger"]), 0) check_expect("dogCount test 3", dogCount(["cat", "dog", "tiger"]), 1) check_expect("dogCount test 4", dogCount([]), 0) # [] is the empty list
      We know from the reading in Chapter 4 that we can have a list of numbers too, such as [3,5,-9,2,4,-8] or [2,3,87,1,-2].

      Write a function called countNeg that takes a list of numbers as its input and returns how many of the numbers in the list are negative.

      Hint: this isn't really different from counting how many times the word "dog" appears. Instead of looking for the word "dog", we are looking to see if the number is negative. We might want to use a different variable instead of word—perhaps num would be a good choice?

      You could test the function by calling it like this at the Python command prompt:

      >>> countNeg([4,20,-32,14,-56])
      You should get the answer 2 back. Try it on other lists as well.

End of H05