---
# System prepended metadata

title: 'CS200 Bridge 3: Files and Directories'
tags: [CS200Prep, ' 2021']

---

# CS200 Bridge Assignment: Files and Directories

*[Work in Pyret, putting your work in a file called `filesystem-code.arr`]*

Imagine that you wanted to write programs for managing directories and files on a computer. Each directory has a name and can contain both subdirectories and files. Each file has a name and size. 

Your goal is to design data structures (Pyret datatypes) to capture files and directory hierarchies, then to write a series of functions that process directory hierarchies (starting from a single "top-level" directory, like a Documents/My Documents folder).

As always, you should write tests for your functions.

:::danger
## Do task 0 BEFORE moving on to the other tasks
:::

0. Propose Pyret datatypes that will capture nested directories and files. You may assume that there is one top directory that all other files/directories lie within. 

1. **After finishing task 0,** take a look at our [provided datatypes](https://code.pyret.org/editor#share=1DWZyLmzW2b8TXFnJbiNK0jfqXRprG9pP&v=6ae21ea). Comment out your proposal from task 0, and include a short reflection on how your proposed datatype differs from ours. Even if your proposed datatype matches ours, answer the following question in your reflection:

:::spoiler Expand for task 1 question
What advantage is there to splitting up the definition into two datatypes? In other words, what would we have lost out on if we had defined files and directories as variants under one type?

*(It might make sense to keep this question in the back of your mind while you work on the tasks below, and then come back and answer it)*
:::

**For the following tasks, use our provided datatypes.**

:::info office hours recording
See [**here**](https://brown.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=f7907303-5dd6-4c8e-9a16-b305014820d0) for the summer 2025 office hours recording on this assignment. *Note: as with all of the office hours recordings, you will get a lot more out of watching this recording after you've attempted the problems in this assignment. **Especially for this assignment, please complete task 0 before watching the recording, because the recording assumes you've already thought through these datatypes.***
:::

2. Write a function `count-files` that takes a single top-level directory and returns a number indicating how many files are nested somewhere within that directory (no matter how deep).

3. Write a function `repeated-dirs` that takes a single top-level directory and returns a boolean indicating whether any two directories within the hierarchy share a name (that is, the function should return false if and only if all of the directory names in the hierarchy are unique).

4. Write a function `find-path` that takes a single top-level directory and the name of a file and returns a list of the (sub)directories that one must open in order to find the named file. If the filename is not within the directories, the function should `raise("File not found")`. Assume that the given filename is in your system at most once.

   For example, if you had a directory for cs111 homework containing a subdirectory for each assignment, then 
   
      ```find-path(cs111-dir, "hwk4code.arr")```
      
   would return ``[list: "CS111", "hwk4"]`` (assuming hwk4code.arr is in the hwk4 directory)
   
   :::warning
   **Hints:** 
   
   (1) Work with examples as you do this -- this problem can easily get a lot harder than it should if you don't figure out how to leverage the recursion, and examples can help you do that. Write several examples for different cases before you try to write code.
   
   (2) Don't be afraid to write helper function(s)
   
   (3) Follow the recursion template. 
   
   (4) Get the easy cases working, then fill in the harder ones. Post questions if you want to check whether you are heading along the right track. Planning this out and taking it step by step will probably save you a ton of time on this assignment.
   :::

## Reflection

In a block comment, write a few sentences about what you’ve learned from these problems. Also mention any questions you have (if any) based on these exercises.

## Handin

Turn in a file `filesystem-code.arr` with your code. In particular, make sure you have tested your functions with multiple directories and files.

:::warning
***Information about the Autograder***

Just like previous assignments, Gradescope will run our autograder tests on your code and display the results.  **It is your responsibility to make sure that all autograder tests pass**.

* Make sure you're using our datatype definitions for the problem, and that the functions are defined accordingly (e.g. `count-files(top-level :: Dir) -> Number`)
* The hints from the previous two assignments apply -- check the filename, check the function definitions, and write plenty of tests of your own.
:::