Lab Ground Rules

Important: Read Lab Ground Rules in the resources page.

[30pts] Part 1: Shopping Cart

Tips

Your task

Write a program p1.py that
  1. reads in a data file containing fruit name/price-per-pound pairs (e.g. fruit1.txt, fruit2.txt)
    note: If the file can't be opened, the program should print the message File not found! and exit.
  2. reads and executes the following commands:
Assume the user always enters a valid command from the three above.

Sample Runs
~/$ python3 p1.py
Filename: fruit1.txt
command: add 3 lbs mango
command: price asdf
Error! asdf not found!
command: price peaches
Error! peaches not found!
command: price blueberries
blueberries are $1.59 per pound
command: add 2.1 lbs blueberries
command: checkout
total is $5.469
~/$ python3 p1.py
Filename: fruit2.txt
command: add 2.0 lbs oranges
command: price kiwi
kiwi are $1.07 per pound
command: price starfruit
Error! starfruit not found!
command: add 1.5 lbs peas
Error! peas not found!
command: add 1.5 lbs plums
command: checkout
total is $2.505
~/$ python3 p1.py
Filename: foobar.txt
File not found!

Note:

You must pass all the testcases in the submit server.

[30pts] Part 2: Hexdump

hexdump a useful utility program that shows the contents of a file in binary and text form.

Download ex1_test.py and two.pcap.

$ cat ex1_test.py
from ex1 import *
a = 0b11010101
pr(a)
b = zero_out_top(a, 3)
pr(b)
c = set_one_at(a, 2)
pr(c)
d = set_zero_at(a, 1)
pr(d)

$ hexdump -C ex1_test.py
00000000  66 72 6f 6d 20 65 78 31  20 69 6d 70 6f 72 74 20  |from ex1 import |
00000010  2a 0a 61 20 3d 20 30 62  31 31 30 31 30 31 30 31  |*.a = 0b11010101|
00000020  0a 70 72 28 61 29 0a 62  20 3d 20 7a 65 72 6f 5f  |.pr(a).b = zero_|
00000030  6f 75 74 5f 74 6f 70 28  61 2c 20 33 29 0a 70 72  |out_top(a, 3).pr|
00000040  28 62 29 0a 63 20 3d 20  73 65 74 5f 6f 6e 65 5f  |(b).c = set_one_|
00000050  61 74 28 61 2c 20 32 29  0a 70 72 28 63 29 0a 64  |at(a, 2).pr(c).d|
00000060  20 3d 20 73 65 74 5f 7a  65 72 6f 5f 61 74 28 61  | = set_zero_at(a|
00000070  2c 20 31 29 0a 70 72 28  64 29 0a 0a 0a           |, 1).pr(d)...|
0000007d

Your task

Write p2.py that exactly does what hexdump does. A sample run:
$ python3 p2.py ex1_test.py
00000000  66 72 6f 6d 20 65 78 31  20 69 6d 70 6f 72 74 20  |from ex1 import |
00000010  2a 0a 61 20 3d 20 30 62  31 31 30 31 30 31 30 31  |*.a = 0b11010101|
00000020  0a 70 72 28 61 29 0a 62  20 3d 20 7a 65 72 6f 5f  |.pr(a).b = zero_|
00000030  6f 75 74 5f 74 6f 70 28  61 2c 20 33 29 0a 70 72  |out_top(a, 3).pr|
00000040  28 62 29 0a 63 20 3d 20  73 65 74 5f 6f 6e 65 5f  |(b).c = set_one_|
00000050  61 74 28 61 2c 20 32 29  0a 70 72 28 63 29 0a 64  |at(a, 2).pr(c).d|
00000060  20 3d 20 73 65 74 5f 7a  65 72 6f 5f 61 74 28 61  | = set_zero_at(a|
00000070  2c 20 31 29 0a 70 72 28  64 29 0a 0a 0a           |, 1).pr(d)...|
0000007d

Tips

[30pts] Part 3: Bit manipulations

Write a program p3.py so that the following code works as follows.
>>> from p3 import *
>>> a = 0b10110101
>>> pr(a)
dec: 181
hex: 0xb5
bin: 10110101
>>> b = zero_out_top(a,3)
>>> pr(b)
dec: 21
hex: 0x15
bin: 00010101
>>> c = set_one_at(a, 4)
>>> pr(c)
dec: 189
hex: 0xbd
bin: 10111101
>>> d = set_zero_at(c, 4)
>>> pr(d)
dec: 181
hex: 0xb5
bin: 10110101

Warning: No strings! No loops!

The purpose of this part is learning how to manipulate bits. You are not allowed to use strings or loops in this part.

You will get 0 point:

  • If you use any string (except using format strings like f"..." in the function pr()).
  • If you use any loop.

Other requirements

  • The code only needs to work with 8-bit numbers.
  • Note that p3.py should provide the following functions:
    • pr(): Similar to the above. Make sure it always prints 8 bits.
    • zero_out_top(a, n): Outputs a number where top n bits are zeroed out from a.
    • set_one_at(a, i): Outputs a number where the ith bit of a is set to 1. The index goes from left to right, starting with 0.
    • set_zero_at(a, i): Outputs a number where the ith bit of a is set to 0.
You may want to save time for debugging by creating a test python file as follows:
# p3_test.py
from p3 import *
a = 0b10110101
pr(a)
b = zero_out_top(a,3)
pr(b)
c = set_one_at(a, 4)
pr(c)
d = set_zero_at(c, 4)
pr(d)

[10pts] Lab Report and Submission

Write a lab report by using the provided template. The writing quality of the lab report matters.
~/bin/submit -c=IT430 -p=lab01 *.py lab01.docx
NOTE: