Question: PS 4 - Recursion Deadline: December In this assignment, you will learn how to apply recursion to solve different problems. Note that this assignment, like

PS4- Recursion
Deadline: December
In this assignment, you will learn how to apply recursion to solve different problems. Note that this assignment, like any other assignment, will be graded with additional different test cases. We will check your codes to make sure that your solutions use recursion.
You MUST USE RECURSION in each question.
You MUST NOT USE GLOBAL VARIABLES in any question.
You MUST NOT USE NESTED FUNCTIONS in any question.
You MUST NOT USE eval() function in any question.
You MUST NOT USE ITERATION AT ALL (No, for or while loops, no iterating over a list or string, No list or dictionary comprehensions, ..etc)
You CAN and should use string builtin functions (Hint: However there is no need to use .split())Q2: Hierarchical Directory Parsing and File Summation
This question requires recursively parsing and analyzing a hierarchical directory structure represented as a nested string. The question has three parts.
You MUST USE RECURSION in each question.
You MUST NOT USE GLOBAL VARIABLES in any question.
You MUST NOT USE NESTED FUNCTIONS in any question.
You MUST NOT USE eval() function in any question.
You MUST NOT USE ITERATION AT ALL (No, for or while loops, no iterating over a list or string, No list or dictionary comprehensions, ..etc)
Problem Statement
The directory structure is represented as a nested string with the following format:
Directories: Represented as dir_name[contents], where contents can include files or other directories.
Files: Represented as file_name(size) where size is an integer denoting the file size.
Contents: Multiple files and/or directories are separated by commas.
Note: The file and directory names CAN be different in test cases, you should not assume the the filenames or directory names to be always "file1" or "fileA", your solution should work for the general case independent of file names and directory names.
The goal is to implement recursive functions to:
Count the total number of files.
Calculate the total size of all files.
Find the largest file in the directory.
Functionality
Part a: Count Files
Implement a function count_files(directory) to count the total number of files in the directory structure.
Example Usage:
# Input directory = "root[file1(10),file2(20),subdir1[file3(15),subdir2[file4(25),file5(30)]]]" # Output print(count_files(directory)) # Output: 5
Part b: Sum File Sizes
Extend the functionality to calculate the total size of all files. Implement the function sum_file_sizes(directory).
Example Usage:
# Input directory = "Documents[myFile(10),file2(20),files[file3(15),subdir[file4(25),file5(30)]]]" # Output print(sum_file_sizes(directory)) # Output: 100
Part c: Largest File
Add functionality to find the largest file in the directory structure. Implement the function largest_file(directory).
Example Usage:
# Input directory = "main[fileA(10),fileB(20),subdirA[fileC(15),subdirB[fileD(25),fileE(30)]]]" # Output print(largest_file(directory)) # Output: "fileE"
Constraints
You MUST USE RECURSION in all parts.
Each part must include at least one helper function.
No use of regular expressions or external libraries for parsing is allowed.
Assume the input string is well-formed and free of invalid characters.
Implementation Details
Parsing the Directory Structure
Directories are enclosed in [ and ].
Files are enclosed in ( and ).
Recursive parsing is required to handle nested directories.
Helper Functions
Use helper functions to extract components (files or directories) from within a directory.
Implement recursion to process nested directories.
Function Descriptions
count_files(directory)
Traverses the directory structure recursively.
Counts all files, including those in nested directories.
sum_file_sizes(directory)
Traverses the directory structure recursively.
Sums up the sizes of all files.
largest_file(directory)
Traverses the directory structure recursively.
Tracks the largest file encountered and its size.
Example Directory Structures
Example 1:
Input:
root[file1(10),file2(20),subdir1[file3(15),subdir2[file4(25),file5(30)]]]
Output:
count_files(directory)->5 sum_file_sizes(directory)->100 largest_file(directory)-> "file5"
Example 2:
Input:
main[fileA(5),subdir[fileB(10),fileC(20)]]
Output:
count_files(directory)->3 sum_file_sizes(directory)->35 largest_file(directory)-> "fileC"
Testing
To test the solution, use the provided example directory structures or create custom inputs. Ensure that the recursive logic correctly handles deeply nested directories and varying file sizes.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!