diff --git a/Dockerfile b/Dockerfile index 714139a32..b648ea7d8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -109,4 +109,3 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ RUN pip install wheel matplotlib numpy coconut scons RUN sudo sh -c 'npm install -g typescript' - diff --git a/SConstruct b/SConstruct index e5ecf82d9..74e05ea85 100644 --- a/SConstruct +++ b/SConstruct @@ -28,6 +28,7 @@ available_languages = { 'bash', 'c', 'cpp', + 'csharp', 'fortran', 'java', 'julia', @@ -43,6 +44,7 @@ available_languages = { languages_to_import = { 'coconut': ['coconut'], + 'csharp': ['mcs'], 'go': ['go'], 'rust': ['rustc', 'cargo'], 'kotlin': ['kotlin'], @@ -77,6 +79,7 @@ languages = { 'c': 'c', 'coconut': 'coco', 'cpp': 'cpp', + 'csharp': 'cs', 'fortran': 'f90', 'go': 'go', 'java': 'java', diff --git a/builders/mcs.py b/builders/mcs.py new file mode 100644 index 000000000..07f1bd76c --- /dev/null +++ b/builders/mcs.py @@ -0,0 +1,37 @@ +from SCons.Builder import Builder +import SCons.Util + +class ToolMCSWarning(SCons.Warnings.SConsWarning): + pass + +class MCSNotFound(ToolMCSWarning): + pass + +SCons.Warnings.enableWarningClass(ToolMCSWarning) + +def _detect(env): + try: + return env['mcs'] + except KeyError: + pass + + mcs = env.WhereIs('mcs') + if mcs: + return mcs + + SCons.Warnings.warn(MCSNotFound, 'Could not find mcs executable') + +def exists(env): + env.Detect('mcs') + +def generate(env): + env['MCS'] = _detect(env) + env['MCSFLAGS'] = [] + + mcs_builder = Builder( + action='"$MCS" -out:$TARGET $MCSFLAGS $SOURCES', + src_suffix='.cs', + suffix='$PROGSUFFIX', + ) + + env.Append(BUILDERS={'MCS': mcs_builder}) diff --git a/contents/huffman_encoding/code/csharp/HuffmanCoding.cs b/contents/huffman_encoding/code/csharp/HuffmanCoding.cs index ef711af26..cc8a711cd 100644 --- a/contents/huffman_encoding/code/csharp/HuffmanCoding.cs +++ b/contents/huffman_encoding/code/csharp/HuffmanCoding.cs @@ -133,25 +133,26 @@ private Node CreateTree(string input) return nodePriorityList.Pop(); } + + private void CreateDictionary(Node node, string bitString, Dictionary localDictionary) + { + if (node.IsLeaf) + localDictionary.Add(node.Key[0], bitString); + else + { + if (node.LeftChild != null) + CreateDictionary(node.LeftChild, bitString + '0', localDictionary); + if (node.RightChild != null) + CreateDictionary(node.RightChild, bitString + '1', localDictionary); + } + } + private Dictionary CreateDictionary(Node root) { // We're using a string instead of a actual bits here, since it makes the code somewhat more readable and this is an educational example. var dictionary = new Dictionary(); CreateDictionary(root, "", dictionary); return dictionary; - - void CreateDictionary(Node node, string bitString, Dictionary localDictionary) - { - if (node.IsLeaf) - localDictionary.Add(node.Key[0], bitString); - else - { - if (node.LeftChild != null) - CreateDictionary(node.LeftChild, bitString + '0', localDictionary); - if (node.RightChild != null) - CreateDictionary(node.RightChild, bitString + '1', localDictionary); - } - } } @@ -165,4 +166,4 @@ private string CreateBitString(string input, Dictionary dictionary return bitString; } } -} \ No newline at end of file +} diff --git a/contents/tree_traversal/code/csharp/Tree.cs b/contents/tree_traversal/code/csharp/Tree.cs index 28df47c91..5e8bc263c 100644 --- a/contents/tree_traversal/code/csharp/Tree.cs +++ b/contents/tree_traversal/code/csharp/Tree.cs @@ -30,29 +30,51 @@ private Tree(int id, int depthCount, int childrenCount) } } + private void DFSRecursive(Tree tree) { + Console.Write(tree.Id + " "); + + foreach (var c in tree._children) + DFSRecursive(c); + } + public void DFSRecursive() { DFSRecursive(this); - void DFSRecursive(Tree tree) - { - Console.Write(tree.Id + " "); + } - foreach (var c in tree._children) - DFSRecursive(c); - } + private void DFSRecursivePostorder(Tree tree) + { + foreach (var c in tree._children) + DFSRecursivePostorder(c); + + Console.Write(tree.Id + " "); } public void DFSRecursivePostorder() { DFSRecursivePostorder(this); - void DFSRecursivePostorder(Tree tree) - { - foreach (var c in tree._children) - DFSRecursivePostorder(c); + } - Console.Write(tree.Id + " "); + private void DFSRecursiveInorderBinary(Tree tree) + { + switch (tree._children.Count) + { + case 2: + DFSRecursiveInorderBinary(tree._children[0]); + Console.Write(tree.Id + " "); + DFSRecursiveInorderBinary(tree._children[1]); + break; + case 1: + DFSRecursiveInorderBinary(tree._children[0]); + Console.Write(tree.Id + " "); + break; + case 0: + Console.Write(tree.Id + " "); + break; + default: + throw new Exception("Not binary tree!"); } } @@ -60,26 +82,6 @@ public void DFSRecursiveInorderBinary() { DFSRecursiveInorderBinary(this); - void DFSRecursiveInorderBinary(Tree tree) - { - switch (tree._children.Count) - { - case 2: - DFSRecursiveInorderBinary(tree._children[0]); - Console.Write(tree.Id + " "); - DFSRecursiveInorderBinary(tree._children[1]); - break; - case 1: - DFSRecursiveInorderBinary(tree._children[0]); - Console.Write(tree.Id + " "); - break; - case 0: - Console.Write(tree.Id + " "); - break; - default: - throw new Exception("Not binary tree!"); - } - } } public void DFSStack() diff --git a/sconscripts/csharp_SConscript b/sconscripts/csharp_SConscript new file mode 100644 index 000000000..366471f19 --- /dev/null +++ b/sconscripts/csharp_SConscript @@ -0,0 +1,15 @@ +Import('files_to_compile env') + +language = files_to_compile[0].language +chapter = files_to_compile[0].chapter + +from collections import defaultdict +chapter_files = defaultdict(list) + +for file_info in files_to_compile: + chapter_files[file_info.chapter].append(file_info.path) + +for chapter, files in chapter_files.items(): + build_target = f'#/build/{language}/{chapter}/{chapter}' + build_result = env.MCS(build_target, [str(file) for file in files]) + env.Alias(str(chapter), build_result)