This lab was written and designed to be completed during lab-time for cs250. This lab is not graded and does not need to be turned in. However, you should work through all of the problems, as it will help you on assignments and exams. If you cannot complete the lab in the alloted time, feel free to continue at home.
Write a script named
This script will take as input a string and a name of a file.
For each line in the input file, the script will determine if the string appears within it.
This script will take two positional arguments.
The first is the string which will be searched for in each line of the input file.
The second is the file, which the script will read.
Recall that in class, we talked about how to iterate through the lines of a file. The general for is to do something like this:
while read VAR_TO_STORE_LINE ; do # Commands in here! done < some-file.txt
To iterate through each line of the input file in
line-len.sh, you will want to do something similar.
For each line, You need to do a search to determine if the string given on the command line appears within it.
Thus, within this
while loop, you will need to do a search operations, store the result, and then determine what to do with an
If the string does appear in the line, print
If not, print
Say we have the text file
$ cat recipie.txt 30 pounds of sugar 2 gallons of water 2 pints of lime juice 4 ounces of citrate of caffeine 2 ounces of citric acid 1 ounce of extract of vanilla 6 drams (3/4 ounce) of fluid extract of cola 6 drams of fluid extract of coca
Below are a few example runs of
$ ./line-len.sh "ounce" recipie.txt Missing string! Missing string! Missing string! Found string! Found string! Found string! Found string! Missing string! $ ./line-len.sh "ounces" recipie.txt Missing string! Missing string! Missing string! Found string! Found string! Missing string! Missing string! Missing string! $ ./line-len.sh "of cit" recipie.txt Missing string! Missing string! Missing string! Found string! Found string! Missing string! Missing string! Missing string! $ ./line-len.sh "does not match" recipie.txt Missing string! Missing string! Missing string! Missing string! Missing string! Missing string! Missing string! Missing string! $
Below are several python statements/expressions.
For each one, figure out what the python console will return/print on paper, before running it in the python console.
Once you’ve come up with your answer, run the command in the python console to check your answer.
If you get it right, great!
If you get it wrong, make sure you understand why (if you don’t understand, ask or Google).
Note that some of the below may produce an error.
If you think it will, let that be your answer.
To start up the python console, open up the terminal app and run
>>> 5 + (2 * 3 + (7 - 1)) / 2
>>> 100 // (5 ** 2 + 10) - 20
>>> 40 - 4 * 4 / 2 + 18 - 10 / 2
>>> im_on_the_7th_floor = False >>> im_on_the_8th_floor = False >>> im_on_the_9th_floor = True >>> im_on_the_7th_floor and im_on_the_8th_floor
>>> im_on_the_7th_floor and im_on_the_8th_floor or im_on_the_9th_floor
>>> (not im_on_the_8th_floor) or im_on_the_7th_floor
>>> im_on_the_9th_floor and ( not not im_on_the_8th_floor ) or im_on_the_7th_floor
>>> line = "The critics said "The movie is great" after leaving the theater."
>>> name = "Sherlock Holmes" >>> type(name)
>>> number = 1 + 2 + 3 >>> type(number)
>>> number = 1 + 2 + 3 + 4 + 5.5 >>> type(number)
>>> age = 35 >>> description = "He is " + age + " years old." >>> print(description)
>>> name = "John Watson" >>> age = 47 >>> print( name + " is " + str(age) + " years old." )
In this problem, you will write a python script. We have not officially covered this topic in lecture yet, but it is not too different from how it is done with bash scripts. Go ahead and read through and follow along with the “Scripts” section of lecture notes 7. Once you are comfortable writing a python script, you can continue with this problem.
Write a script named
This script will print out the following snippet of the Lord of the Rings, Two Towers movie script:
LEGOLAS Come, Gimli! We are gaining on them! GIMLI [Panting] I am wasted on cross-country! We dwarves are natural sprinters! Very dangerous over short distances! [The trackers come over a hill and pause as they gaze across the plains below.] ARAGORN Rohan. Home of the horse-lords. There is something strange at work here. Some evil gives speed to these creatures, sets its will against us. [Legolas runs ahead and looks out to the horizon.] ARAGORN Legolas, what do your Elf eyes see? LEGOLAS The Uruks turn northeast. They’re taking the hobbits to Isengard!
Write a python script named
This script should start by declaring the following few variables:
title = "Star Wars" year_released = 1977 lead_actor = "Mark Hamill" secondary_actor = "Harrison Ford" description = "Planets, Space-ships, Lightsabers, Oh My!"
Feel free to change these variables to whatever values you want.
After declaring these variables in your script, should write some code that prints out a movie summary that looks like the following:
"Star Wars" was released in 1977. The lead actor was Mark Hamill and the secondary actor was Harrison Ford. The movie is about: Planets, Space-ships, Lightsabers, Oh My!
You should not hard-code the values into the string(s) that you are printing.
The calls to
print() should use the variables in the printout, and covert the variables to strings where necessary.