This lab was written and designed to be completed during lab-time for cs250. This lab is not graded for correctness. 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.
This particular lab was designed to be completed on the lab machines in GS 930
Write a complete python program named argv-practice.py
.
This program will report information about the command-line arguments passed to it, and is only meant to handle positional arguments.
Note that for the Script name: ...
printout, you should not hard-code the name of the script.
You should use the sys.argv
list to get the name.
Hint: This program is similar to the ones written in the argv lecture notes
Below are examples of how it is expected to behave when run in bash.
$ python3 argv-practice.py A B C D E F G
Script name: argv-practice.py
Argument count: 7
First argument: A
Last argument: G
$ python3 argv-practice.py -d " " -f 1 input-file.txt
Script name: argv-practice.py
Argument count: 5
First argument: -d
Last argument: input-file.txt
$ python3 argv-practice.py please tell me what you can do to help me
Script name: argv-practice.py
Argument count: 10
First argument: please
Last argument: me
Test your program and make sure you get this same output.
Write a complete python program named sort-args.py
.
This program will take any number of positional arguments on the command line, sort them, and then print them out.
The script name should not be included in the list of arguments sorted and then printed.
Use sys.argv
to access the command-line arguments.
Below are examples of how it is expected to behave when run in bash.
$ python3 sort-args.py candy bleach apple
apple
bleach
candy
$ python3 sort-args.py the quick brown fox jumped over the lazy dog
brown
dog
fox
jumped
lazy
over
quick
the
the
Write a complete python program named bas.py
(stands for Better Aargument Sorting).
This program will take any number of positional arguments on the command line, sort them, and then print them out (like the last problem, but with a twist).
However, the first argument following the name of the script does not count as one of the arguments to sort.
Instead, the program will expect this argument to be either ascending
or descending
.
If ascending
is provided, the remaining arguments will be sorted/printed in ascending order.
If descending
is provided, use descending order.
Lastly, if the first argument after the script name is neither of these, just print them out in the same order.
The script name should not be included in the list of arguments sorted and then printed, as with the last problem.
Use sys.argv
to access the command-line arguments.
Below are examples of how it is expected to behave when run in bash.
In this problem, build off of the code you write in problem 2.
$ python3 bas.py ascending candy bleach starch apple
starch
candy
bleach
apple
$ python3 bas.py descending candy bleach starch apple
apple
bleach
candy
starch
$ python3 bas.py nada candy bleach starch apple
candy
bleach
starch
apple
Write a complete python program named repeater.py
.
This program will expect exactly three positional arguments (not including the script name).
The first one will be a string to be repeated.
The second will be the number of times to repeat the string.
The third is expected to be either upper
or lower
, which will determine of the first argument is converted to upper-case or lower-case.
This program will convert the first argument to upper or lower case depending on the value of the third argument.
Then it will print it out repeatedly based on the value of the second argument.
Below are examples of expected behavior.
$ python3 repeater.py jumpman 3 upper
JUMPMAN JUMPMAN JUMPMAN
$ python3 repeater.py AppLE 5 lower
apple apple apple apple apple
$ python3 repeater.py StarWars 7 lower
starwars starwars starwars starwars starwars starwars starwars