--- id: seminar-08 title: 8th seminar description: | Manipulating with files only char-by-char and a magic tree. --- # 8th seminar bonus assignment [Source](pathname:///files/pb071/bonuses/08/) ## 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 the `counting` tests - `check-counting-bonus` - runs the `counting` tests with bonus implemented - `check` - runs both `counting` and `counting-bonus` tests - `clean` - 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 [string-to-be-counted] ``` Arguments that are passed to the program represent: - `` - path to the file where we count the words - `` - path to the file where we output the count - (optional argument) `[string-to-be-counted]` - in case you implement bonus, otherwise we default to word `ananas` ;) In skeleton you are given 3 empty, but documented, functions to implement. 1. `count_anything` - function accepts input file and substring to be counted in the file, returns the count. 2. `count_ananas` - same as `count_anything`, but specialized for ananases, the default implementation from the skeleton expects you to implement `count_anything` and therefore it just calls the other function. 3. `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 fileTree it represents
``` 8;4 5;3 3;2 2;1 1;0 nil 4;0 7;1 6;0 nil 11;2 10;1 9;0 nil 12;0 ``` ![tree](/files/pb071/bonuses/08/tree.png)
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. --- Ideally submit the assignment through the merge request. Step-by-step tutorial is present [here](../mr). For setting assignee my xlogin is `xfocko`. In case you do not want to experiment on GitLab, send me the source code via email, but please prefix subject with: `[PB071/14][seminar-08]` Deadline for the submission of the bonus is **May 4th 24:00**.