4.4 KiB
id | title | description |
---|---|---|
seminar-08 | 8th seminar | Manipulating with files only char-by-char and a magic tree. |
8th seminar bonus assignment
Introduction
In this bonus you can implement two tasks, one of them has a bonus part with generic solution.
One is focused on counting ananas or in case of generic version any substring in the file, but with a restriction on the function you use.
Other one has a more algorithmic spirit.
For this bonus you can get at maximum 2.5 K₡.
Warning
DO NOT COMMIT test data to your own git repository, since the tests include files that exceed 10MB by themselves. Even if they are on separate branch, they take up the space.
Testing
For testing you are provided with python script (requires click
to be installed:
pip3 install --user click
) and Makefile
that provides following targets:
check-counting
- runs thecounting
testscheck-counting-bonus
- runs thecounting
tests with bonus implementedcheck
- runs bothcounting
andcounting-bonus
testsclean
- removes output files from the test runs
Task no. 1: Counting (0.75 K₡)
Your first task is to make smallish program that counts occurences of specific (or given) word from file and writes the number to other file.
Usage of the program is:
Usage: ./counting <input-file> <output-file> [string-to-be-counted]
Arguments that are passed to the program represent:
<input-file>
- path to the file where we count the words<output-file>
- path to the file where we output the count- (optional argument)
[string-to-be-counted]
- in case you implement bonus, otherwise we default to wordananas
;)
In skeleton you are given 3 empty, but documented, functions to implement.
count_anything
- function accepts input file and substring to be counted in the file, returns the count.count_ananas
- same ascount_anything
, but specialized for ananases, the default implementation from the skeleton expects you to implementcount_anything
and therefore it just calls the other function.write_number
- function that writes the number to the file, why would you need the function is explained later :)
Requirements
For manipulation with the files you are only allowed to use fopen
, fclose
,
fgetc
and fputc
. Functions like fprintf
(except for stderr
or logging) and
fscanf
are forbidden.
In case you struggle and want to use one of those functions, the solution will be penalized by 50% of points.
Bonus part (0.75 K₡)
Bonus part of this assignment is to implement count_anything
rather than count_ananas
.
Smaller hint: This task does not need dynamic allocation :) You just need one good helper function and the right idea ;)
Task no. 2: Weird trees (1 K₡)
In this task we are crossing our paths with algorithms and data structures. Your task is to write a program that constructs tree from the file that is given as an argument and pretty-prints it.
Input file consists of lines, that include key
and rank
in form key;rank
or nil
. Why would we have nil
in a file? The file represents pre-order iteration
through the tree. Leaves never have rank different than 0, so you can safely assume
2 non-existing nil
s in the input after you read such node ;)
Example input file | Tree it represents |
---|---|
|
In this task you are only provided with different trees in the test-trees
directory.
Implementation and format of the pretty-print is totally up to you. :)
Example of mine for the tree above:
8 (rank = 4)
+-- 5 (rank = 3)
| +-- 3 (rank = 2)
| | +-- 2 (rank = 1)
| | | +-- 1 (rank = 0)
| | +-- 4 (rank = 0)
| +-- 7 (rank = 1)
| +-- 6 (rank = 0)
+-- 11 (rank = 2)
+-- 10 (rank = 1)
| +-- 9 (rank = 0)
+-- 12 (rank = 0)
Can you find out what are those trees? :)
Submitting
In case you have any questions, feel free to reach out to me.