$25
Set up for the test by creating a new directory called test09, changing to this directory, and fetching the provided code by running these commands:
$ mkdir test09
$ cd test09
$ 2041 fetch test09
Or, if you're not working on CSE, you can download the provided code as a zip file or a tar file.
Write a Perl program distinct_lines.pl which given a single argument n reads lines from standard input until n different lines have been read.
It should then print a message (exactly as below) indicating how many lines were read. It should then stop. It should not read futher input
If end-of-input is reached before n different lines it should print a message indicating how many lines were read.
Your program should ignore case and white-space when comapring lines.
You can assume your program is given a single positive integer as argument.
$ ./distinct_lines.pl 3 hi hello world hi hello world hello world bye
3 distinct lines seen after 6 lines read.
$ ./distinct_lines.pl 3 hi hello world hi
hello world HELLO world bye
3 distinct lines seen after 6 lines read.
$ ./distinct_lines.pl 4 how are you are how are well
4 distinct lines seen after 7 lines read.
$ ./distinct_lines.pl 3 how are you
3 distinct lines seen after 3 lines read.
$ ./distinct_lines.pl 7 hello how are you
Ctrl-D
End of input reached after 4 lines read - 7 different lines not seen.
No error checking is necessary.
When you think your program is working you can autotest to run some simple automated tests:
$ 2041 autotest distinct_lines
When you are finished working on this exercise you must submit your work by running give:
$ give cs2041 test09_distinct_lines distinct_lines.pl
Your task is to write a Shell script eating.sh which prints a list of the food we have bought at the supermarket in the last week.
It will be given as its first argument a file containing a list of supermarket bills.
The file will be in this format:
[
[
{"name": "Rice", "price": "$4.29"},
{"name": "Butter", "price": "$4.00"}
],
[
{"name": "Cheese", "price": "$8.82"},
{"name": "Rice", "price": "$6.84"},
{"name": "Pasta", "price": "$7.87"}
],
[
{"name": "Peanut Butter", "price": "$6.93"},
{"name": "Bread", "price": "$3.32"}, {"name": "Noodles", "price": "$4.71"}
],
[
{"name": "Cheese", "price": "$4.60"}, {"name": "Rice", "price": "$8.23"},
{"name": "Bread", "price": "$5.99"},
{"name": "Pasta", "price": "$0.54"},
{"name": "Ramen", "price": "$0.98"}
],
[
{"name": "Noodles", "price": "$0.38"}
]
]
If eating.sh was given this file as its first argument it should print
Bread
Butter
Cheese Noodles
Pasta
Peanut Butter
Ramen
Rice
bills.json contains an example list of bills
Download bills.json, or copy it to your CSE account using the following command:
$ cp -n /web/cs2041/20T2/activities/eating/bills.json .
Here is how eating.sh should behave:
$ eating.sh bills.json
Bread
Butter
Cheese Noodles
Pasta
Peanut Butter
Ramen
Rice
You must print the food in alphabetic order.
Each type of food must be printed only once.
Your function does not have to handle differences in case or white-space.
You can assume the formating of the file is the same as the example above. Your program does not have to handle the many other ways a JSON file might be formatted.
You can assume the "name" and "price" fields are always in the same order as the above example. Your answer must be Shell. You can not use other languages such as Perl, Javascript, Python or C.
No error checking is necessary.
When you think your program is working you can autotest to run some simple automated tests:
Your task is to write a Perl program json_count.pl which takes as its first argument a whale species and its second argument a file containing a list of whale observations, and prints the number of whales of that species in the file.
The file will be in this format:
[
{
"date": "09/09/18",
"how_many": 11,
"species": "Orca"
},
{
"date": "04/11/17",
"how_many": 41,
"species": "Long-finned pilot whale"
},
{
"date": "17/03/18",
"how_many": 30,
"species": "Southern right whale"
},
{
"date": "17/08/18",
"how_many": 31,
"species": "Orca"
}, ]
If json_count.pl was given "Orca" as its first argument and the above file as its second argument , it should print 42.
For example:
$ ./json_count.pl Orca whales.json
117
$ ./json_count.pl 'Striped dolphin' whales.json
19
$ ./json_count.pl 'Southern right whale' whales.json
64
$ ./json_count.pl 'Whale Shark' whales.json
0
You can assume the formating of the file is the same as the example above. Your program does not have to handle the many other ways a JSON file might be formatted.
You can assume the "date", "how_many" and "species" fields in whale observations are always in the same order as the above example.
Your answer must be Perl only. You can not use other languages such as Shell, Python or C.
You may not run external programs, e.g. via system or backquotes.
You may use any Perl module installed on CSE systems.