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.
Begin each script with a comment that includes your name, the date, and the question number.

  1. [25 pts] Name this script "question".

    Write a Python3 script that expects to be run from the command line, with one argument. The argument is a filename. You must open and read the contents of the file. Your script must find all the words in the file and build a list of them; then sort the list alphabetically; and finally report the number of words, and the first and last words of the sorted list.

    Here are some sample data files that can be downloaded and used for testing purposes: sp0.txt, happy-fun-ball.txt, naval-ship-names.txt.

    The instructor's solution runs like this:
    $ python3 question1.py
    Usage: question1.py 
    $
    $ python3 question1.py happy.txt
      filename: happy.txt
    # of words: 278
    first word: $14.95
     last word: women,
    $
    $ python3 question1.py sp0.txt
      filename: sp0.txt
    # of words: 538
    first word: 'A
     last word: your
    $
    $ python3 question1.py naval-ship-names.txt
      filename: naval-ship-names.txt
    # of words: 1430
    first word: "Marianas
     last word: years
    $
    
  2. [25 pts] Name this script "question".

    Write a Python3 script that expects to be run from the command line, with one argument. The argument is a file name; the file can be assumed to be in the same directory as the script itself.

    Download these data files and save them in your working directory. You can use these for testing purposes: small.data, big.data, fracs.data.

    The files contain a series of floating-point numbers, one per line. Your script must read all the numbers and build a list of them. (Remember to convert them from text strings to actual numbers!) Then calculate and report some statistics on the list.

    The required outputs are number of items, average (arithmetic mean), geometric mean, and harmonic mean. Your script must define separate functions to calculate these values. They are defined as:

    • an = average = (sum of the values) divided by (number of values)
    • gn = geometric mean = nth root of (product of the n values)
    • hn = harmonic mean = n divided by (sum of the inverses of the n values)

    Each of these functions will need to loop over the array to total up the sum or product.

    Example runs are shown here:

    $ python3 question2.py small.data
    data: small.data
      n: 10
    average: -6.471000
    geomean: 36.919416
    harmean: -53.035068
    $ python3 question2.py big.data
    data: big.data
      n: 10000
    average: 0.599920
    geomean: inf
    harmean: 37.305815
    $ python3 question2.py fracs.data
    data: fracs.data
      n: 100
    average: 0.744130
    geomean: 0.729085
    harmean: 0.714036
    

  3. [25 pts] Name this script "question".

    Download and save to your computer the file ien137.txt. You will use this file as input to your script.

    Write a Python3 script that opens the "ien137.txt" file and reads it into one large variable. Use a regular expression to find all occurrences of either the string "Big-Endian" or the string "Little-Endian". Report the number of matches of each.

    An example run should look like this:
    $ python3 question3.py ien137.txt
    ['Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endian', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endian', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians', 'Big-Endians']
    Big: 34
    ['Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endian', 'Little-Endians', 'Little-Endian', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endian', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians', 'Little-Endians']
    Little: 31
    $
    
  4. [25 pts] Name this script "question".

    Download and save to your computer the file ien37.txt. You will use this file as input to your script.

    Write a Python3 script that reads lines from the "ien137.txt" file; find every line that includes both the phrase "Little-Endians" and the phrase "Big-Endians". Count (and report) the number of lines that match, and show whatever is between the two phrases, including the phrases themselves.

    An example run should look like this:
    $ python3 question4.py
    Little-Endians  and  the  Big-Endians
    Little-Endians'  or the Big-Endians
    Little-Endians or by Big-Endians
    Little-Endians  and  the  Big-Endians
    Little-Endians are Little-Endians and Big-Endians
    Little-Endians and the Big-Endians
    6 lines match.
    
  5. [25 pts] Name this script "question".

    Write a Python3 script that reads two URLs (webpages), reports how many lines are in each one, and saves each one in a separate text file.

    The two URLs to read are "http://bloomu.edu/computer-science/", and "https://montcs.bloomu.edu/215/index.html".

    Name the two output files "urlA.html" and "urlB.html".

    An example run might look like this (counts and file sizes may vary over time):
    $ python3 question4.py
    http://bloomu.edu/computer-science/: 580 lines
    https://montcs.bloomu.edu/215/index.html: 308 lines
    $ ls -l url*.html
    -rw-r--r-- 1 bobmon bobmon 34417 Feb 16 15:58 urlA.html
    -rw-r--r-- 1 bobmon bobmon  9685 Feb 16 15:58 urlB.html
    $