Created
December 7, 2022 14:53
-
-
Save tslater2006/ca4073e4732da04db6b33a7aa45476df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* PROMPT: | |
Help me parse this terminal output using C#. The output represents a series of `cd` and `ls` commands, these commands start with '$'. if a `cd` command is followed by `..` this means to move up a level. otherwise it means to navigate to the folder whose name is given. | |
The remaining output is either a file, which is shown by file size and followed by file name, or it is a directory which starts with `dir` and is followed by the directory name. | |
Here is an example input: */ | |
using System; | |
using System.Collections.Generic; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// The input string | |
string input = "$ cd /\n$ ls\ndir a\n14848514 b.txt\n8504156 c.dat\ndir d\n$ cd a\n$ ls\ndir e\n29116 f\n2557 g\n62596 h.lst\n$ cd e\n$ ls\n584 i\n$ cd ..\n$ cd ..\n$ cd d\n$ ls\n4060174 j\n8033020 d.log\n5626152 d.ext\n7214296 k"; | |
// Split the input string into lines | |
string[] lines = input.Split('\n'); | |
// The root directory | |
Dictionary<string, object> root = new Dictionary<string, object>(); | |
root.Add("name", "/"); | |
root.Add("type", "dir"); | |
root.Add("children", new List<Dictionary<string, object>>()); | |
// The current directory | |
Dictionary<string, object> current = root; | |
// Parse the input | |
foreach (string line in lines) | |
{ | |
if (line.StartsWith("$")) | |
{ | |
// Parse a command | |
string[] parts = line.Split(' '); | |
if (parts[1] == "cd") | |
{ | |
// Handle a `cd` command | |
if (parts[2] == "..") | |
{ | |
// Move up a level | |
current = (Dictionary<string, object>)current["parent"]; | |
} | |
else | |
{ | |
// Move to a subdirectory | |
string name = parts[2]; | |
Dictionary<string, object> dir = FindChild(current, name); | |
if (dir == null) | |
{ | |
// Create the subdirectory if it doesn't exist | |
dir = new Dictionary<string, object>(); | |
dir.Add("name", name); | |
dir.Add("type", "dir"); | |
dir.Add("parent", current); | |
dir.Add("children", new List<Dictionary<string, object>>()); | |
((List<Dictionary<string, object>>)current["children"]).Add(dir); | |
} | |
current = dir; | |
} | |
} | |
else if (parts[1] == "ls") | |
{ | |
// Handle an `ls` command, but do nothing here | |
// The directory contents are handled in the next iteration | |
} | |
} | |
else | |
{ | |
// Parse a file or directory | |
string[] parts = line.Split(' '); | |
Dictionary<string, object> item = new Dictionary<string, object>(); | |
item.Add("name", parts[1]); | |
if (parts[0] == "dir") | |
{ | |
// The item is a directory | |
item.Add("type", "dir"); | |
item.Add("parent", current); | |
item.Add("children", new List<Dictionary<string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment