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
PS 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 splitQ: 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 dirnamecontents where contents can include files or other directories.
Files: Represented as filenamesize where size is an integer denoting the file size.
Contents: Multiple files andor 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 "file 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 countfilesdirectory to count the total number of files in the directory structure.
Example Usage:
# Input directory "rootfilefilesubdirfilesubdirfilefile # Output printcountfilesdirectory # Output:
Part b: Sum File Sizes
Extend the functionality to calculate the total size of all files. Implement the function sumfilesizesdirectory
Example Usage:
# Input directory "DocumentsmyFilefilefilesfilesubdirfilefile # Output printsumfilesizesdirectory # Output:
Part c: Largest File
Add functionality to find the largest file in the directory structure. Implement the function largestfiledirectory
Example Usage:
# Input directory "mainfileAfileBsubdirAfileCsubdirBfileDfileE # Output printlargestfiledirectory # 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 wellformed 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
countfilesdirectory
Traverses the directory structure recursively.
Counts all files, including those in nested directories.
sumfilesizesdirectory
Traverses the directory structure recursively.
Sums up the sizes of all files.
largestfiledirectory
Traverses the directory structure recursively.
Tracks the largest file encountered and its size.
Example Directory Structures
Example :
Input:
rootfilefilesubdirfilesubdirfilefile
Output:
countfilesdirectory sumfilesizesdirectory largestfiledirectory "file
Example :
Input:
mainfileAsubdirfileBfileC
Output:
countfilesdirectory sumfilesizesdirectory largestfiledirectory "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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
