LOGOCOMP1531 - Practice Exam

Q11. Cockroaches! (15 Marks)

Answer this question in q11/cockroaches.ts.

You find that your house is infested with cockroaches. One day it becomes too much and you decide to keep a tally of where the cockroaches are each day. On Monday, Tuesday and Wednesday you write the following files:

monday.txt:

kitchen
bathroom
attic

tuesday.txt:

backyard
kitchen
bedroom
attic

wednesday.txt:

attic
bathroom

Task

Write a function decontaminate(filenames) in cockroaches.ts which takes a list of filenames and counts the frequency of sightings in each file. Every line in each file is a single string which is the sighting. For example, the sightings in wednesday.txt are "attic" and "bathroom". The function returns a dictionary (frequency count) of sightings. Write the function to be type-safe.

The above example would return the following object:

> decontaminate(['monday.txt', 'tuesday.txt', 'wednesday.txt']
{ kitchen: 2, attic: 3, bathroom: 2, bedroom: 1, backyard: 1 })

If any of the filenames cannot be opened for reading, your code should throw an Error.

Please note:

  • You can assume files will be in a locations/ folder in q11.
  • You can assume the files only contain these sightings:
    • attic
    • bathroom
    • backyard
    • bedroom
    • kitchen

Marking

When you think your program is working, you can run some simple automated tests:

  • type-saftey: npm run tsc
  • lint: npm run lint
  • tests: npm run test

You are not allowed to use comments which disable lint and type-check in your code - these will result in an automatic zero mark. This includes: eslint-disable, eslint-disable-line, @ts-ignore, @ts-expect-error, etc.

Submission

The submission will only collect the q11/cockroaches.ts file. All other files are replaced in automarking.

When you've finished working, submit your answer by running:

submit --question 11

If no submissions have been made for a question, we will take the saved file/s from your working directory.