RU beehive logo ITEC dept promo banner
ITEC 109
2014spring
ibarland

homeinfolectslabsexamshws
D2Lbreeze (snow day)tutor/PIs

hw01
calling functions
types; composing functions

Edit, as mentioned in class: If you want to work at home, and have questions/problems downloading JES, and/or connecting to your H: drive from off-campus, you can ask on the D2L Discussion Board (under the topic “tools”).

For this homework, type your answers in the top half of JES, and save to a file hw01.py. Your file should start with your name/info, and include one additional function for this homework:

# your name
# ITEC109 hw01 -- see http://www.radford.edu/~itec109/2014spring-ibarland/Homeworks/hw01.html 


# We will use some string-library functions for this hw:
import string


# A helper function, for this homework:
#
# substring -- given a string, a start-index, and a stop-index,
#   return the characters from start-index up to (but not including) stop-index.
#   Indices are 0-based.
#   Examples:   substring( "radford", 3, 6) = "for"
#               substring( "radford", 0, 3) = "rad"
#
def substring(str,a,b):
  return str[a:b]

Coming soon: A list of all the functions we've talked about in class.

  1. What python type would be most appropriate for each of the following information?
    1. The population of Virginia?                 
    2. The size of Virginia, in square miles?                 
    3. The name of Radford's representative, in the US House of Representatives?                 
    4. The first initial, of that representative?                 
  2. What two things can + mean in python, depending on context?
  3. For each of the following functions, one example is given. Fill in the columns for (a) the type(s) given to the function, and (b) the return-type of the function.
    functionexampleinput type(s)return-type
    len len("hello") = 5                                  
    pow pow(3.14, 2.718) = 22.42                 ,                                  
    chr chr(65) = 'A'                                  
    ord ord('A') = 65                                  
    sqrt sqrt(98.6) = 9.929                                  
    string.capitalize string.capitalize("hello") = "Hello"                                  
    string.upper string.upper("hello") = "HELLO"                                  
    string.find string.find("tyrannosaurus","ann") = 3                 ,                                  
  4. Give a python expression to calculate each of the following. Give just the expression, not the answer. Your answer should include only the data in the problem, the four basic arithmetic operators (+, -, *, /), and the python functions we've mentioned in class. Precede each expression by a comment-line with its number, e.g.
    #  2f:
    2+3
    
    #  2g:
    4*(5+6)
          
    1. the product of 4 and 5                 
    2. the sum of 4 and 5                 
    3. The result of concatenating "hi t" with "here"                 
    4. The character-code corresponding to: the character 'j'.                 
    5. The character corresponding to: character-code 90.                 
    6. The string "radford" with the first letter capitalized.                 
    7. The length of "abracadabra!".                 
    8. The square root of 25.                 
    9. The eighth power of 2.                 
    10. The larger of -20 and -30.
      hint:Use the function max.
                      
  5. Same as the previous problem, except the answers involve calling more than one function; you'll nest the function-calls so that the answer from one function is used as input for another:

    Give a python expression to calculate each of the following. Give just the expression, not the answer. Your answer should include only the data in the problem, the four basic arithmetic operators (+, -, *, /), and the python functions we've mentioned in class. Precede each expression by a comment-line with its number, e.g.

    1. Two added with: the product of 4 and 5                 
    2. Two multiplied with: the sum of 4 and 5                 
    3. The square root of the eighth power of 2.                 
    4. The larger of 210 and 10².
      hint:Remember our friend max.
                      
    5. The result of concatenating "hi t" with: the capitalized version of the word "here"                 
    6. The character-code corresponding to: the character 'j' plus 5.                 
    7. The character corresponding to: the character-code 70 plus 2.                 
    8. The character corresponding to: the character(-code for) 'j' shifted by 5.                 
    9. The character that is 'a' shifted backwards by 32.                 
    10. The string "radford" with the first letter capitalized.                 
    11. The length of "abracadabra!", divided by two.                 
    12. The substring of "abracadabra!" starting from index 0 up to (but not including) (index) half the length of the string.
      Note:Your answer will mention the string "abracadabra!" twice.
                      
    13. The substring of "abracadabra!" starting from index half-its-length up to (but not including) index its-length.                 
    14. The substring of "abracadabra!" starting from index 0, up to index: the smaller of its length and 5.
      hint:max has a friend named min.
                      
    15. The substring of "cat" starting from index 0, up to index: the smaller of its length and 5.                 
    16. [CHALLENGE:] The middle-third of the string "abracadabra!".                 

homeinfolectslabsexamshws
D2Lbreeze (snow day)tutor/PIs


©2014, Ian Barland, Radford University
Last modified 2014.Feb.10 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme