ode (or file), may have multiple children. If the specified path does not exist, then you should disregard that file. Once you are done adding elements to the filesystem, you should print the directory structure in the specified format. Required modifications: 1. Insertion of a new element 2. Printing of the directory from the root node. 3. Parsing of the input. Input Format First line is the name of the root directory. Second line, n, is the number of subdirectories that follows

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

COMPLETE THE GIVEN CODE BELOW TO SOLVE THE PROBLEM - PYTHON

In this problem, you are going to implement a filesystem using a tree data structure. Similar to a filesystem, each node (or file), may have multiple children. If the specified path does not exist, then you should disregard that file. Once you are done adding elements to the filesystem, you should print the directory structure in the specified format.

Required modifications: 1. Insertion of a new element 2. Printing of the directory from the root node. 3. Parsing of the input.

Input Format

First line is the name of the root directory.

Second line, n, is the number of subdirectories that follows.

For each line that follows, it shows the following format:

Filename-root node,next path,next path,...

Constraints

  1. You may assume that the filenames are all alphabetical characters with no special characters.
  2. n < 100
  3. The maximum path length is 20 (including the root directory).
  4. You may assume that there are no similar filenames within the same directory but may exist outside of the directory (much like a normal filesystem).

Output Format

You should follow the output format indicated in the sample test case with the ordering of files based chronologically.

For the spacing, use the tab (\t) character for the subdirectories.

Rootdir
        Subdir1
            subsubdir1
            subsubdir2
        Subdir2
        Subdir3
            subsubdir1


Sample Input 0

C
12
Users-C
Windows-C
ProgramFiles-C
Adobe-C,ProgramFiles
Steam-C,ProgramFiles
ferdie-C,Users
john-C,Users
guest-C,Users
Firmware-C,Windows
System-C,Windows
Documents-C,Users,ferdie
Downloads-C,Users,ferdie


Sample Output 0

C
    Users
        ferdie
            Documents
            Downloads
        john
        guest
    Windows
        Firmware
        System
    ProgramFiles
        Adobe
        Steam

 

USE THE GIVEN TEMPLATE CODE BELOW:

class FileNode:
    def __init__(self, filename, depth=0):
        """This function initializes our FileNode

        Args:
            filename (string): The filename of the current node.
            depth (int, optional): The depth of the current node,. Defaults to 0.
        """
        self.filename = filename
        self.depth = depth
        # Hint: It is best to implement the subdirectory into a dictionary with keys being the filename and the values being the actual object FileNode.
        self.subdirectories = {}

    def __delete__(self):
        """This function overrides the default deletion of the current object and also deletes the subdirectories"""
        for v in self.subdirectories.values():
            del v
        self.subdirectories.clear()

    def insert(self, filename, path, depth):
        """This function inserts the file into a specific path.
        If the path does not exist, then the folder is not created.

        Args:
            filename (string): The filename of the current folder to be inserted
            path (lists of string): The path of the filename
            depth (int): The depth of the node to be inserted.
        """
        # Part A. Implement the insertion of a new file here. If the given path does not exist, then you may skip the file.

    def print_dir(self):
        """This prints the current directory and accesses the subdirectories as well."""
        # Part B. Implement the printing of the directory here.
        # You should access subdirectories first before moving to the next folder within the same directory.


#### Do not modify this object ####
class FileSystem:
    def __init__(self, root_dir):
        """This is the FileSystem implementation. A root node is required once an instance is created

        Args:
            root_dir (string): Filename of the root node for the file system.
        """
        self.root = FileNode(root_dir)

    def __delete__(self):
        """Deletes the filesystem by deleting the root and deleting the subdirectories automatically due to the override function."""
        del self.root

    def insert(self, filename, path):
        """Inserts a new file with the given path starting from the root node.

        Args:
            filename (string): The filename of the folder to be inserted.
            path (string): The path of the folder to be inserted
        """
        self.root.insert(filename, path, 1)

    def print_system(self):
        """Prints the directory in the given format"""
        self.root.print_dir()


if __name__ == "__main__":
    root_dir = input()
    n = int(input())
    filenames = []

    for i in range(n):
        # Part C. Implement parsing of input lines.
        pass

    #### DO NOT MODIFY BELOW ####
    fs = FileSystem(root_dir)
    for f, p in filenames:
        fs.insert(f, p)

    fs.print_system()

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY