A file system is represented as a tree. There are two kinds of nodes:
DirectoryNode: has a children list. A child can be either a DirectoryNode or a FileNode (the two types can be freely mixed).FileNode: has a name and a content.The root of the tree is a DirectoryNode.
Write a function to encrypt all files in the file system tree.
Input:
DirectoryNode ├── FileNode(name="file1.txt", content="hello") ├── DirectoryNode │ ├── FileNode(name="file2.txt", content="world") │ └── FileNode(name="file3.txt", content="!")
Output: The content of all files should be encrypted.
Input:
DirectoryNode ├── FileNode(name="file1.txt", content="abc") └── DirectoryNode ├── FileNode(name="file2.txt", content="def") └── FileNode(name="file3.txt", content="ghi")
Output: The content of all files should be encrypted.
FileNode.`python class DirectoryNode: def init(self): self.children = []
class FileNode: def init(self, name, content): self.name = name self.content = content
def encrypt_all_files(node, encryption_key): if isinstance(node, FileNode): # Encrypt the file content using the encryption key node.content = encrypt(node.content, encryption_key) elif isinstance(node, DirectoryNode): for child in node.children: encrypt_all_files(child, encryption_key)
def encrypt(content, key): # Implement the encryption logic here encrypted_content = "" for char in content: # Simple example: XOR with the key encrypted_content += chr(ord(char) ^ ord(key)) return encrypted_content
root = DirectoryNode() root.children.append(FileNode("file1.txt", "hello")) sub_dir = DirectoryNode() sub_dir.children.append(FileNode("file2.txt", "world")) sub_dir.children.append(FileNode("file3.txt", "!")) root.children.append(sub_dir)
encryption_key = "key" encrypt_all_files(root, encryption_key) `
NOT_FOUND: No additional information or variations of this problem were found in the web searches conducted. The above solution is based on the provided excerpt and general knowledge of tree traversal and encryption.