Name:
Answer Key

SEE BODY TAG

SEE BODY TAG

 

SEE BODY TAG


Write scripts for the following questions, and upload them to the course website.
Put your name in a comment in each script.
  1. [20 pts] Name this program "q:if".

    if-elif-else

    Write a Python program that demonstrates the "Input-Process-Output" approach. The program must prompt for and input three integers. It must then process them by raising the first number to the power given by the second to yield a result.

    Then determine whether the result is even, or exactly divisible by the third number. If the result is even, print the phrase "Even" and the result of raising to the power. If it is not even, but it is exactly divisible by the third number, print the phrase "Exactly divisible", the result of raising to the power, and the quotient. Otherwise (it's not exactly divisible) print the result, the quotient, and the remainder.

    Runs of your program must look similar to this:

    $: python3 q_if.py
    base number? 4
    power? 3
    divisor? 7
    Even: 64
    $: 
    $: python3 q_if.py
    base number? 3
    power? 4
    divisor? 7
    result 81, quotient 11, remainder 4
    $: 
    $: python3 q_if.py
    base number? 3
    power? 4
    divisor? 6
    result 81, quotient 13, remainder 3
    $: 
    $: python3 q_if.py
    base number? 3
    power? 4
    divisor? 9
    Exactly divisible
    result 81,  quotient 9
    $: 
    
        
     
  2. [20 pts] Name this program "q:try".

    try-except block

    Write a program that uses a while loop to (a) input an integer from the keyboard; (b) print the value of a function of that integer; and (c) repeat the process until the user presses "Enter" without entering anything (zero-length input string).

    Your program must include a function to do this calculation for the list:      y = f(n) = ( (1/n)**(1/3) )

    You must use a try-except block to catch any exceptions in the input, and another try-except block to catch any exceptions in the calculation. If the exception is a ZeroDivisionError, give y the value "math.inf" and include it in the list. For any other exception, print a brief error message and skip to the next value of n.

    $:  python3 q_try.py
    integer? 3
    3 --> 0.346681+0.600468j
    integer? 9
    9 --> 0.240375+0.416342j
    integer? -2
    -2 --> 0.793701
    integer? 0
    0 --> inf
    integer? abc
    Not an integer.
    integer? -10
    -10 --> 0.464159
    integer?
    $: 
    
     
  3. [20 pts] Name this script "q:cmdline".

    Command-line and loop

    Write a script that builds a list. It must accept as many command-line arguments as there are, convert each to floating-point number, and append them to the list. Then it must print out how many numbers there are, the minimum value, and the maximum value.

    If there are no command-line arguments it must print an error message and quit.

    Some runs of your script might look like this:
    $:  python3 q_cmdline.py
    Enter numbers on the command-line.
    $: 
    $:  python3 q_cmdline.py  645.64  0.31  64e2  354.061
    4 numbers:  minimum 0.310, maximum 6400.000
    $: 
    $:  python3 q_cmdline.py  -456.023 6486.026 -1.1 7e7 300000.3 45 8
    7 numbers:  minimum -456.023, maximum 70000000.000
    $: 
    $:  python3 q_cmdline.py  34  987  546  987  3210  53  
    6 numbers:  minimum 34.000, maximum 3210.000
    $: 
    
     
  4. [20 pts] Name this script "q:functions".

    Functions

    Write a program that accepts two command-line arguments, converts them to floating-point numbers x1 and x2, and uses a function named mystery() to calculate a value based on x1 and x2. Also put your program in a main() function, and call "main()" to do the work. The "main()" function must use the "mystery()" function in a loop, to print a formatted table of values.

    The "mystery()" function must calculate the following function:      y = sin(x1) × cos(n × x2) * exp( -n ) for values of n from 0 through 10.

    The output must be formatted as in the example below.

    $: python3 q_functions.py  3.4 -0.5
     n       x1     n*x2    f(x1,x2,n)
    -- -------- --------  ------------
     0    3.400   -0.000     -0.255541
     1    3.400   -0.500     -0.082500
     2    3.400   -1.000     -0.018686
     3    3.400   -1.500     -0.000900
     4    3.400   -2.000      0.001948
     5    3.400   -2.500      0.001379
     6    3.400   -3.000      0.000627
     7    3.400   -3.500      0.000218
     8    3.400   -4.000      0.000056
     9    3.400   -4.500      0.000007
    10    3.400   -5.000     -0.000003
    -- -------- --------  ------------
    $: 
    
        
     
  5. [20 pts] Name this program "q:dict".

    Dictionary, file writing

    Write a program to read and process a text file. Download the file here:

    The file contains information about standard colors used on webpages. Each line contains the name of the color, the hexadecimal string that produces it, and the Red, Green, Blue values that produce it. There are also blank lines, and comment lines that start with a "hashtag". For example:

    	DarkSlateBlue 	#483D8B 	72 61 139
    	MediumSlateBlue 	#7B68EE 	123 104 238
    
    #Green HTML Color Names
    	GreenYellow 	#ADFF2F 	173 255 47
    	Chartreuse 	#7FFF00 	127 255 0
    	LawnGreen 	#7CFC00 	124 252 0
    

    Process the file by splitting each line into its fields. Skip any blank or comment lines. Create a dictionary whose keys are field 1, the hexadecimal string (second field on each line), with the value being a tuple containing the Red, Green, Blue, components and the name (fields 2, 3, 4, and 0). Then sort the dictionary according to the keys, and write the dictionary to an output file named "sorted-colors.txt".

    The output file should be formatted to look like this:

     #000000  (   0,    0,    0)  Black
     #000080  (   0,    0,  128)  Navy
     #00008B  (   0,    0,  139)  DarkBlue
     #0000CD  (   0,    0,  205)  MediumBlue
     #0000FF  (   0,    0,  255)  Blue
     #006400  (   0,  100,    0)  DarkGreen
     #008000  (   0,  128,    0)  Green
     #008080  (   0,  128,  128)  Teal
     #008B8B  (   0,  139,  139)  DarkCyan
     #00BFFF  (   0,  191,  255)  DeepSkyBlue
     #00CED1  (   0,  206,  209)  DarkTurquoise
     #00FA9A  (   0,  250,  154)  MediumSpringGreen
     #00FF00  (   0,  255,    0)  Lime
     #00FF7F  (   0,  255,  127)  SpringGreen
     #00FFFF  (   0,  255,  255)  Cyan
     #191970  (  25,   25,  112)  MidnightBlue
    
        
    
        
     
  6. [20 pts]

    File processing

    Name this script "q:file".

    file-input/formatting

    Download these files:

    • sp00.txt. This file contains one line that contains some text, on multiple lines.
    • sp8.txt. A weak joke.

    Write a script that processes this file:

    • Prompt for and input the name of the text file.
    • Open the file, read in each line, split it into words, lowercase each word, and add them all to a list of words.

      Also add the count of words in each line to a list of counts.

    • Sort the list of words.
    • Print out the list of words, the number of lines, and the average number of words per line (calculated as the sum of the list of lengths, divided by the length of the list).

      The words must be right-justified, in a field 12 characters wide. The average must show 2 decimal places.

    An example run of the program, with its output file, should look like this:

    $:  python3 q_file.py
    filename? sp8.txt
              a:
            beer
         bottles
          bottom
            end.
              in
              is
      minnesota?
              of
              on
            open
           other
         printed
              q:
             the
            what
    2 lines, average: 8.00 words per line
    $: