From 1d749dec39e5bb8343eef6280a8ab7515a5b235d Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 8 Jan 2017 18:17:45 +0100 Subject: [PATCH 01/19] [C++] Challenge 0 --- challenge_0/cpp/sven/README.md | 11 +++++++++++ challenge_0/cpp/sven/src/HelloWorld.cpp | 7 +++++++ 2 files changed, 18 insertions(+) create mode 100644 challenge_0/cpp/sven/README.md create mode 100644 challenge_0/cpp/sven/src/HelloWorld.cpp diff --git a/challenge_0/cpp/sven/README.md b/challenge_0/cpp/sven/README.md new file mode 100644 index 000000000..1f609918f --- /dev/null +++ b/challenge_0/cpp/sven/README.md @@ -0,0 +1,11 @@ +#Challenge_0 Solution + +Under Windows - using the Visual studio c++ compiler: +run "cl.exe src\HelloWorld.cpp" and run the resulting file "HelloWorld.exe". + +cl can be found in C:\Program Files (x86)\\VC\bin +(e.g. C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin) +or you can open a visual studio command prompt which adds the correct paths to its environment. + +Under Linux - using the gcc compiler: +g++ src\HelloWorld.cpp && a.out \ No newline at end of file diff --git a/challenge_0/cpp/sven/src/HelloWorld.cpp b/challenge_0/cpp/sven/src/HelloWorld.cpp new file mode 100644 index 000000000..22246eadc --- /dev/null +++ b/challenge_0/cpp/sven/src/HelloWorld.cpp @@ -0,0 +1,7 @@ +#include + +int main() +{ + std::cout << "Hello World!" << std::endl; + return 0; +} \ No newline at end of file From 0d740bdf3bbb00d328843dc3a1108582c667465e Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 8 Jan 2017 18:18:05 +0100 Subject: [PATCH 02/19] [C#] Challenge 0 --- challenge_0/csharp/sven/README.md | 7 +++++++ challenge_0/csharp/sven/src/HelloWorld.cs | 12 ++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 challenge_0/csharp/sven/README.md create mode 100644 challenge_0/csharp/sven/src/HelloWorld.cs diff --git a/challenge_0/csharp/sven/README.md b/challenge_0/csharp/sven/README.md new file mode 100644 index 000000000..697a91905 --- /dev/null +++ b/challenge_0/csharp/sven/README.md @@ -0,0 +1,7 @@ +#Challenge_0 Solution + +To compile run "csc.exe src\HelloWorld.cs" and run the resulting file "HelloWorld.exe". + +csc can be found in \Microsoft.NET\\<.NET version> +(e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319) +or you can open a visual studio command prompt which adds the correct paths to its environment. \ No newline at end of file diff --git a/challenge_0/csharp/sven/src/HelloWorld.cs b/challenge_0/csharp/sven/src/HelloWorld.cs new file mode 100644 index 000000000..b9c84eb38 --- /dev/null +++ b/challenge_0/csharp/sven/src/HelloWorld.cs @@ -0,0 +1,12 @@ +using System; + +namespace Challenge_0 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} \ No newline at end of file From 557bdfe9f1a7aad31b345f111c02592f80b40728 Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 8 Jan 2017 18:18:20 +0100 Subject: [PATCH 03/19] [C++] Challenge 1 --- challenge_1/cpp/sven/README.md | 12 ++++++++++++ challenge_1/cpp/sven/src/Reverse.cpp | 15 +++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 challenge_1/cpp/sven/README.md create mode 100644 challenge_1/cpp/sven/src/Reverse.cpp diff --git a/challenge_1/cpp/sven/README.md b/challenge_1/cpp/sven/README.md new file mode 100644 index 000000000..727a96f39 --- /dev/null +++ b/challenge_1/cpp/sven/README.md @@ -0,0 +1,12 @@ +#Challenge_0 Solution + +Under Windows - using the Visual studio c++ compiler: +To compile, run: "cl.exe src\Reverse.cpp" +and run the resulting file with your sentence to reverse as arguments: "Reverse.exe Hello World!". + +cl can be found in C:\Program Files (x86)\\VC\bin +(e.g. C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin) +or you can open a visual studio command prompt which adds the correct paths to its environment. + +Under Linux - using the gcc compiler: +g++ src\Reverse.cpp && a.out \ No newline at end of file diff --git a/challenge_1/cpp/sven/src/Reverse.cpp b/challenge_1/cpp/sven/src/Reverse.cpp new file mode 100644 index 000000000..aaf2b3d63 --- /dev/null +++ b/challenge_1/cpp/sven/src/Reverse.cpp @@ -0,0 +1,15 @@ +#include +#include + +int main(int argc, char* argv[]) +{ + for (int i = argc - 1; i > 0; --i) + { + std::string word = argv[i]; + for (std::string::reverse_iterator rit = word.rbegin(); rit != word.rend(); ++rit) + std::cout << *rit; + std::cout << " "; + } + std::cout << std::endl; + return 0; +} \ No newline at end of file From 637fa08f2bfaf8903a0c960d4dd75610265f55bf Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 8 Jan 2017 18:18:39 +0100 Subject: [PATCH 04/19] [C#] Challenge 1 --- challenge_1/csharp/sven/README.md | 8 ++++++++ challenge_1/csharp/sven/src/Reverse.cs | 15 +++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 challenge_1/csharp/sven/README.md create mode 100644 challenge_1/csharp/sven/src/Reverse.cs diff --git a/challenge_1/csharp/sven/README.md b/challenge_1/csharp/sven/README.md new file mode 100644 index 000000000..5886f6b48 --- /dev/null +++ b/challenge_1/csharp/sven/README.md @@ -0,0 +1,8 @@ +#Challenge_1 Solution + +To compile run: "csc.exe src\Reverse.cs" +and run the resulting file with your sentence to reverse as arguments: "Reverse.exe Hello World!". + +csc can be found in \Microsoft.NET\\<.NET version> +(e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319) +or you can open a visual studio command prompt which adds the correct paths to its environment. \ No newline at end of file diff --git a/challenge_1/csharp/sven/src/Reverse.cs b/challenge_1/csharp/sven/src/Reverse.cs new file mode 100644 index 000000000..4e4376dbd --- /dev/null +++ b/challenge_1/csharp/sven/src/Reverse.cs @@ -0,0 +1,15 @@ +using System; +using System.Linq; + +namespace Challenge_1 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine( + args.Aggregate("", (sentence, word) => word.Aggregate("", (w, character) => character + w) + " " + sentence) + ); + } + } +} \ No newline at end of file From f75b6081aa19cd9cb9061eb1eaae78304080c2e1 Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 8 Jan 2017 18:18:55 +0100 Subject: [PATCH 05/19] [C++] Challenge 2 --- challenge_2/cpp/sven/README.md | 12 ++++++++++++ challenge_2/cpp/sven/src/Single.cpp | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 challenge_2/cpp/sven/README.md create mode 100644 challenge_2/cpp/sven/src/Single.cpp diff --git a/challenge_2/cpp/sven/README.md b/challenge_2/cpp/sven/README.md new file mode 100644 index 000000000..64f9a7cc5 --- /dev/null +++ b/challenge_2/cpp/sven/README.md @@ -0,0 +1,12 @@ +#Challenge_0 Solution + +Under Windows - using the Visual studio c++ compiler: +To compile, run: "cl.exe src\Single.cpp" +and run the resulting file with your sequence as arguments: "Single.exe 2 3 4 3 2 3 a a". + +cl can be found in C:\Program Files (x86)\\VC\bin +(e.g. C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin) +or you can open a visual studio command prompt which adds the correct paths to its environment. + +Under Linux - using the gcc compiler: +g++ src\Single.cpp && a.out \ No newline at end of file diff --git a/challenge_2/cpp/sven/src/Single.cpp b/challenge_2/cpp/sven/src/Single.cpp new file mode 100644 index 000000000..396d7989b --- /dev/null +++ b/challenge_2/cpp/sven/src/Single.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + std::unordered_map map; + for (int i =1; i < argc; ++i) + { + std::string el = argv[i]; + if (map.find(el) == map.cend()) + { + map[el] = true; + } + else + { + map[el] = false; + } + } + std::cout << std::find_if(map.cbegin(), map.cend(), [](const std::unordered_map::value_type& vt) { return vt.second; })->first << std::endl; +} \ No newline at end of file From d78b81b672e0dacadba6035f6da1e10c139e0167 Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 8 Jan 2017 18:19:10 +0100 Subject: [PATCH 06/19] [C#] Challenge 2 --- challenge_2/csharp/sven/README.md | 8 ++++++++ challenge_2/csharp/sven/src/Single.cs | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 challenge_2/csharp/sven/README.md create mode 100644 challenge_2/csharp/sven/src/Single.cs diff --git a/challenge_2/csharp/sven/README.md b/challenge_2/csharp/sven/README.md new file mode 100644 index 000000000..c550d4f3c --- /dev/null +++ b/challenge_2/csharp/sven/README.md @@ -0,0 +1,8 @@ +#Challenge_1 Solution + +To compile run: "csc.exe src\Single.cs" +and run the resulting file with your sequence as arguments: "Single.exe 2 3 4 3 2 3 a a". + +csc can be found in \Microsoft.NET\\<.NET version> +(e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319) +or you can open a visual studio command prompt which adds the correct paths to its environment. \ No newline at end of file diff --git a/challenge_2/csharp/sven/src/Single.cs b/challenge_2/csharp/sven/src/Single.cs new file mode 100644 index 000000000..66d44f824 --- /dev/null +++ b/challenge_2/csharp/sven/src/Single.cs @@ -0,0 +1,26 @@ +using System; +using System.Linq; +using System.Collections.Generic; + +namespace Challenge_2 +{ + class Program + { + static void Main(string[] args) + { + Dictionary dic = new Dictionary(); + foreach(string el in args) + { + if(dic.ContainsKey(el)) + { + dic[el] = false; + } + else + { + dic[el] = true; + } + } + Console.WriteLine(dic.First((el) => el.Value).Key); + } + } +} \ No newline at end of file From c00378b9f51bd7796462bda59a68bd3f2ea45650 Mon Sep 17 00:00:00 2001 From: Alexander Payne Date: Mon, 9 Jan 2017 20:17:34 -0700 Subject: [PATCH 07/19] Update README.md --- challenge_2/cpp/sven/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge_2/cpp/sven/README.md b/challenge_2/cpp/sven/README.md index 64f9a7cc5..cd8c0fb1f 100644 --- a/challenge_2/cpp/sven/README.md +++ b/challenge_2/cpp/sven/README.md @@ -1,4 +1,4 @@ -#Challenge_0 Solution +#Challenge_2 Solution Under Windows - using the Visual studio c++ compiler: To compile, run: "cl.exe src\Single.cpp" @@ -9,4 +9,4 @@ cl can be found in C:\Program Files (x86)\\VC\bin or you can open a visual studio command prompt which adds the correct paths to its environment. Under Linux - using the gcc compiler: -g++ src\Single.cpp && a.out \ No newline at end of file +g++ src\Single.cpp && a.out From 93da602f6390fc11b061e4113eb1da253b217229 Mon Sep 17 00:00:00 2001 From: Alexander Payne Date: Mon, 9 Jan 2017 20:19:45 -0700 Subject: [PATCH 08/19] Update README.md --- challenge_2/csharp/sven/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge_2/csharp/sven/README.md b/challenge_2/csharp/sven/README.md index c550d4f3c..16fcfdc4b 100644 --- a/challenge_2/csharp/sven/README.md +++ b/challenge_2/csharp/sven/README.md @@ -1,8 +1,8 @@ -#Challenge_1 Solution +# Challenge_2 Solution To compile run: "csc.exe src\Single.cs" and run the resulting file with your sequence as arguments: "Single.exe 2 3 4 3 2 3 a a". csc can be found in \Microsoft.NET\\<.NET version> (e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319) -or you can open a visual studio command prompt which adds the correct paths to its environment. \ No newline at end of file +or you can open a visual studio command prompt which adds the correct paths to its environment. From 04c47e96d88105341eec0a710ee09855e9750e99 Mon Sep 17 00:00:00 2001 From: Sven Date: Sat, 14 Jan 2017 15:51:34 +0100 Subject: [PATCH 09/19] [C#] Challenge 3 --- challenge_3/csharp/sven/README.md | 8 ++++++++ challenge_3/csharp/sven/src/Majority.cs | 14 ++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 challenge_3/csharp/sven/README.md create mode 100644 challenge_3/csharp/sven/src/Majority.cs diff --git a/challenge_3/csharp/sven/README.md b/challenge_3/csharp/sven/README.md new file mode 100644 index 000000000..71ed78fd5 --- /dev/null +++ b/challenge_3/csharp/sven/README.md @@ -0,0 +1,8 @@ +# Challenge_3 Solution + +To compile run: "csc.exe src\Majority.cs" +and run the resulting file with your sequence as arguments: "Majority.exe 2 3 4 3 2 3 2 2 2 1". + +csc can be found in \Microsoft.NET\\<.NET version> +(e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319) +or you can open a visual studio command prompt which adds the correct paths to its environment. diff --git a/challenge_3/csharp/sven/src/Majority.cs b/challenge_3/csharp/sven/src/Majority.cs new file mode 100644 index 000000000..505d955f7 --- /dev/null +++ b/challenge_3/csharp/sven/src/Majority.cs @@ -0,0 +1,14 @@ +using System; +using System.Linq; +using System.Collections.Generic; + +namespace Challenge_3 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine(args.GroupBy(val => val).OrderBy(g => g.Count()).Last().Key); + } + } +} \ No newline at end of file From 6896bc4f641055c5a81eee3bfc42a148b4c4a1fa Mon Sep 17 00:00:00 2001 From: Sven Date: Sat, 14 Jan 2017 15:56:36 +0100 Subject: [PATCH 10/19] [C++] Challenge 3 --- challenge_3/cpp/sven/README.md | 12 ++++++++++++ challenge_3/cpp/sven/src/Majority.cpp | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 challenge_3/cpp/sven/README.md create mode 100644 challenge_3/cpp/sven/src/Majority.cpp diff --git a/challenge_3/cpp/sven/README.md b/challenge_3/cpp/sven/README.md new file mode 100644 index 000000000..3de6d7d67 --- /dev/null +++ b/challenge_3/cpp/sven/README.md @@ -0,0 +1,12 @@ +#Challenge_3 Solution + +Under Windows - using the Visual studio c++ compiler: +To compile, run: "cl.exe src\Majority.cpp" +and run the resulting file with your sequence as arguments: "Majority.exe 2 3 4 3 2 3 3 3 1". + +cl can be found in C:\Program Files (x86)\\VC\bin +(e.g. C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin) +or you can open a visual studio command prompt which adds the correct paths to its environment. + +Under Linux - using the gcc compiler: +g++ src\Majority.cpp && a.out diff --git a/challenge_3/cpp/sven/src/Majority.cpp b/challenge_3/cpp/sven/src/Majority.cpp new file mode 100644 index 000000000..e9ef5e605 --- /dev/null +++ b/challenge_3/cpp/sven/src/Majority.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + std::unordered_map map; + std::pair majority(argv[1], 1); + + for (int i = 1; i < argc; ++i) + { + map[argv[i]]++; + if(map[argv[i]] > majority.second) + { + majority = std::pair(argv[i], map[argv[i]]); + } + } + std::cout << majority.first << std::endl; +} \ No newline at end of file From 634fb55c229dc3252b7a4d3b959795e5b0b49cd6 Mon Sep 17 00:00:00 2001 From: Sven Date: Sat, 14 Jan 2017 19:37:12 +0100 Subject: [PATCH 11/19] [C++] Challenge 4 --- challenge_4/cpp/sven/README.md | 14 ++++++ challenge_4/cpp/sven/src/Invert.cpp | 78 +++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 challenge_4/cpp/sven/README.md create mode 100644 challenge_4/cpp/sven/src/Invert.cpp diff --git a/challenge_4/cpp/sven/README.md b/challenge_4/cpp/sven/README.md new file mode 100644 index 000000000..3068db0db --- /dev/null +++ b/challenge_4/cpp/sven/README.md @@ -0,0 +1,14 @@ +#Challenge_3 Solution + +Under Windows - using the Visual studio c++ compiler: +To compile, run: "cl.exe src\Invert.cpp" +and run the resulting file with your tree as arguments in preorder position with null termination as '#' sign +For example: "Invert.exe 4 2 1 # # 3 # # 7 6 # # 9 # #". encodes the example tree given for challenge 4 +and "Invert.exe P F B # # H G # # # S R # # Y T # W # # Z # #" encodes the tree as shown here: http://jcsites.juniata.edu/faculty/rhodes/cs2java/images/btreeTrav2.gif + +cl can be found in C:\Program Files (x86)\\VC\bin +(e.g. C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin) +or you can open a visual studio command prompt which adds the correct paths to its environment. + +Under Linux - using the gcc compiler: +g++ src\Invert.cpp && a.out diff --git a/challenge_4/cpp/sven/src/Invert.cpp b/challenge_4/cpp/sven/src/Invert.cpp new file mode 100644 index 000000000..bd672188d --- /dev/null +++ b/challenge_4/cpp/sven/src/Invert.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include + +static const std::string EMPTY = "#"; + +struct BinaryTreeNode +{ + std::string value; + BinaryTreeNode* leftChild; + BinaryTreeNode* rightChild; +}; + +void traverseInOrder(BinaryTreeNode* root, std::function func) +{ + if (root != nullptr) + { + traverseInOrder(root->leftChild, func); + traverseInOrder(root->rightChild, func); + } + func(root); +} + +void traversePreOrder(BinaryTreeNode* root, std::function func) +{ + func(root); + if (root != nullptr) + { + traversePreOrder(root->leftChild, func); + traversePreOrder(root->rightChild, func); + } +} + +BinaryTreeNode* treeFromInputRec(BinaryTreeNode* root, std::vector::const_iterator& pos, std::vector::const_iterator end) +{ + if (*pos != EMPTY) + { + root->leftChild = treeFromInputRec(new BinaryTreeNode{ *pos, nullptr, nullptr }, ++pos, end); + } + pos++; + if (*pos != EMPTY) + { + root->rightChild = treeFromInputRec(new BinaryTreeNode{ *pos, nullptr, nullptr }, ++pos, end); + } + return root; +} + +BinaryTreeNode* binaryTreeFromPreOrderInput(std::vector input) +{ + BinaryTreeNode* root = new BinaryTreeNode{ input.front(), nullptr, nullptr }; + return treeFromInputRec(root, ++input.cbegin(), input.cend()); +} + + +int main(int argc, char* argv[]) +{ + std::vector treeInPreOrder(argv + 1, argv + argc); + BinaryTreeNode* tree = binaryTreeFromPreOrderInput(treeInPreOrder); + traverseInOrder(tree, [](BinaryTreeNode* node) + { + if (node) + { + auto temp = node->leftChild; + node->leftChild = node->rightChild; + node->rightChild = temp; + } + }); + + traversePreOrder(tree, [](BinaryTreeNode* node) + { + std::cout << (node ? node->value : EMPTY) << " "; + }); + std::cout << std::endl; + + +} + From c99ff63dd7cbea4adee958b0166b4754491a1f8f Mon Sep 17 00:00:00 2001 From: Sven Date: Sat, 14 Jan 2017 19:40:58 +0100 Subject: [PATCH 12/19] fixed readme.md --- challenge_4/cpp/sven/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge_4/cpp/sven/README.md b/challenge_4/cpp/sven/README.md index 3068db0db..fd3926ee3 100644 --- a/challenge_4/cpp/sven/README.md +++ b/challenge_4/cpp/sven/README.md @@ -1,4 +1,4 @@ -#Challenge_3 Solution +#Challenge_4 Solution Under Windows - using the Visual studio c++ compiler: To compile, run: "cl.exe src\Invert.cpp" From fd32b86db85a184d10de315e7cea461ae1ceebad Mon Sep 17 00:00:00 2001 From: Sven Date: Sat, 14 Jan 2017 20:44:18 +0100 Subject: [PATCH 13/19] [C#] Challenge 4 --- challenge_4/csharp/sven/README.md | 10 ++++ challenge_4/csharp/sven/src/Invert.cs | 75 +++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 challenge_4/csharp/sven/README.md create mode 100644 challenge_4/csharp/sven/src/Invert.cs diff --git a/challenge_4/csharp/sven/README.md b/challenge_4/csharp/sven/README.md new file mode 100644 index 000000000..d7fa12017 --- /dev/null +++ b/challenge_4/csharp/sven/README.md @@ -0,0 +1,10 @@ +# Challenge_4 Solution + +To compile run: "csc.exe src\Majority.cs" +and run the resulting file with your tree as arguments in preorder position with null termination as '#' sign +For example: "Invert.exe 4 2 1 # # 3 # # 7 6 # # 9 # #". encodes the example tree given for challenge 4 +and "Invert.exe P F B # # H G # # # S R # # Y T # W # # Z # #" encodes the tree as shown here: http://jcsites.juniata.edu/faculty/rhodes/cs2java/images/btreeTrav2.gif + +csc can be found in \Microsoft.NET\\<.NET version> +(e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319) +or you can open a visual studio command prompt which adds the correct paths to its environment. diff --git a/challenge_4/csharp/sven/src/Invert.cs b/challenge_4/csharp/sven/src/Invert.cs new file mode 100644 index 000000000..e35ffa95a --- /dev/null +++ b/challenge_4/csharp/sven/src/Invert.cs @@ -0,0 +1,75 @@ +using System; +using System.Linq; +using System.Collections.Generic; + +namespace Challenge_4 +{ + class BinaryTreeNode + { + public BinaryTreeNode(string val, BinaryTreeNode left, BinaryTreeNode right) + { + value = val; + leftChild = left; + rightChild = right; + } + public string value; + public BinaryTreeNode leftChild; + public BinaryTreeNode rightChild; + } + + class Program + { + const string EMPTY = "#"; + static BinaryTreeNode binaryTreeFromPreOrderInput(ref IEnumerable input) + { + BinaryTreeNode root = new BinaryTreeNode(input.ElementAt(0), null, null); + + if (input.Count() > 1 && input.ElementAt(1) != EMPTY) + { + input = input.Skip(1); + root.leftChild = binaryTreeFromPreOrderInput(ref input); + } + input = input.Skip(1); + if (input.Count() > 1 && input.ElementAt(1) != EMPTY) + { + input = input.Skip(1); + root.rightChild = binaryTreeFromPreOrderInput(ref input); + } + return root; + } + + static BinaryTreeNode traversePreOrder(BinaryTreeNode tree, Func func) + { + tree = func(tree); + if (tree != null) + { + tree.leftChild = traversePreOrder(tree.leftChild, func); + tree.rightChild = traversePreOrder(tree.rightChild, func); + } + return tree; + } + + static BinaryTreeNode traverseInOrder(BinaryTreeNode tree, Func func) + { + if (tree != null) + { + tree.leftChild = traverseInOrder(tree.leftChild, func); + tree.rightChild = traverseInOrder(tree.rightChild, func); + } + return func(tree); + } + + static void Main(string[] args) + { + IEnumerable input = args.AsEnumerable(); + BinaryTreeNode tree = binaryTreeFromPreOrderInput(ref input); + + tree = traverseInOrder(tree, node => node != null ? new BinaryTreeNode(node.value, node.rightChild, node.leftChild) : null); + traversePreOrder(tree, node => { Console.Write((node != null ? node.value : EMPTY) + " "); return node; } ); + + int x = 3; + + //Console.WriteLine(args.GroupBy(val => val).OrderBy(g => g.Count()).Last().Key); + } + } +} \ No newline at end of file From dd7a3c83a6553b1401c39d6d180ed92184d532d5 Mon Sep 17 00:00:00 2001 From: Sven Date: Sat, 14 Jan 2017 21:01:35 +0100 Subject: [PATCH 14/19] cleanup --- challenge_4/csharp/sven/src/Invert.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/challenge_4/csharp/sven/src/Invert.cs b/challenge_4/csharp/sven/src/Invert.cs index e35ffa95a..e615973ef 100644 --- a/challenge_4/csharp/sven/src/Invert.cs +++ b/challenge_4/csharp/sven/src/Invert.cs @@ -66,10 +66,6 @@ static void Main(string[] args) tree = traverseInOrder(tree, node => node != null ? new BinaryTreeNode(node.value, node.rightChild, node.leftChild) : null); traversePreOrder(tree, node => { Console.Write((node != null ? node.value : EMPTY) + " "); return node; } ); - - int x = 3; - - //Console.WriteLine(args.GroupBy(val => val).OrderBy(g => g.Count()).Last().Key); } } } \ No newline at end of file From c86e1f7dee0b2eabdf3f2aac4b913930e0664569 Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 15 Jan 2017 14:14:33 +0100 Subject: [PATCH 15/19] [C#] Challenge 5 --- challenge_5/csharp/sven/README.md | 8 ++++++++ challenge_5/csharp/sven/src/Difference.cs | 14 ++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 challenge_5/csharp/sven/README.md create mode 100644 challenge_5/csharp/sven/src/Difference.cs diff --git a/challenge_5/csharp/sven/README.md b/challenge_5/csharp/sven/README.md new file mode 100644 index 000000000..241ceb0b0 --- /dev/null +++ b/challenge_5/csharp/sven/README.md @@ -0,0 +1,8 @@ +# Challenge_5 Solution + +To compile run: "csc.exe src\Difference.cs" +and run the resulting file with your sequence as arguments: "Single.exe abcdefg bade5cgf". + +csc can be found in \Microsoft.NET\\<.NET version> +(e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319) +or you can open a visual studio command prompt which adds the correct paths to its environment. diff --git a/challenge_5/csharp/sven/src/Difference.cs b/challenge_5/csharp/sven/src/Difference.cs new file mode 100644 index 000000000..fd10146f1 --- /dev/null +++ b/challenge_5/csharp/sven/src/Difference.cs @@ -0,0 +1,14 @@ +using System; +using System.Linq; +using System.Collections.Generic; + +namespace Challenge_5 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine(args[1].Except(args[0]).First()); + } + } +} \ No newline at end of file From af2aa0e1ac9d4b241f60596cb5639587ccfd76fe Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 15 Jan 2017 14:19:22 +0100 Subject: [PATCH 16/19] fixed readme.md --- challenge_5/csharp/sven/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge_5/csharp/sven/README.md b/challenge_5/csharp/sven/README.md index 241ceb0b0..d9f88bbf1 100644 --- a/challenge_5/csharp/sven/README.md +++ b/challenge_5/csharp/sven/README.md @@ -1,7 +1,7 @@ # Challenge_5 Solution To compile run: "csc.exe src\Difference.cs" -and run the resulting file with your sequence as arguments: "Single.exe abcdefg bade5cgf". +and run the resulting file with your sequence as arguments: "Difference.exe abcdefg bade5cgf". csc can be found in \Microsoft.NET\\<.NET version> (e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319) From 32b2885cd521cedbfa567cf34a9fa294b171a08a Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 15 Jan 2017 14:43:05 +0100 Subject: [PATCH 17/19] [C++] Challenge 5 --- challenge_5/cpp/sven/README.md | 12 ++++++++++++ challenge_5/cpp/sven/src/Difference.cpp | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 challenge_5/cpp/sven/README.md create mode 100644 challenge_5/cpp/sven/src/Difference.cpp diff --git a/challenge_5/cpp/sven/README.md b/challenge_5/cpp/sven/README.md new file mode 100644 index 000000000..d5e162e1b --- /dev/null +++ b/challenge_5/cpp/sven/README.md @@ -0,0 +1,12 @@ +#Challenge_5 Solution + +Under Windows - using the Visual studio c++ compiler: +To compile, run: "cl.exe src\Difference.cpp" +and run the resulting file with your sequence as arguments: "Difference.exe abcdefg bade5cgf". + +cl can be found in C:\Program Files (x86)\\VC\bin +(e.g. C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin) +or you can open a visual studio command prompt which adds the correct paths to its environment. + +Under Linux - using the gcc compiler: +g++ src\Difference.cpp && a.out diff --git a/challenge_5/cpp/sven/src/Difference.cpp b/challenge_5/cpp/sven/src/Difference.cpp new file mode 100644 index 000000000..06684ea84 --- /dev/null +++ b/challenge_5/cpp/sven/src/Difference.cpp @@ -0,0 +1,18 @@ +#include +#include +#include + +int main(int argc, char* argv[]) +{ + std::string normalString = argv[1]; + std::string extraChar = argv[2]; + auto it = std::find_if(extraChar.begin(), extraChar.end(), [&normalString](char c) + { + return normalString.find(c) == std::string::npos; + }); + + std::cout << *it << std::endl; + + +} + From 382bf93929b3c970ae54073ad486bc5a96cdb95c Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 29 Jan 2017 16:55:51 +0100 Subject: [PATCH 18/19] [C++] Challenge 6 --- challenge_6/cpp/sven/README.md | 12 ++++++++++++ challenge_6/cpp/sven/src/Ranges.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 challenge_6/cpp/sven/README.md create mode 100644 challenge_6/cpp/sven/src/Ranges.cpp diff --git a/challenge_6/cpp/sven/README.md b/challenge_6/cpp/sven/README.md new file mode 100644 index 000000000..2a58f7ded --- /dev/null +++ b/challenge_6/cpp/sven/README.md @@ -0,0 +1,12 @@ +#Challenge_6 Solution + +Under Windows - using the Visual studio c++ compiler: +To compile, run: "cl.exe src\Ranges.cpp" +and run the resulting file with your sequence as arguments: "Ranges.exe 1 2 3 5 6 10 11 12 13 20". + +cl can be found in C:\Program Files (x86)\\VC\bin +(e.g. C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin) +or you can open a visual studio command prompt which adds the correct paths to its environment. + +Under Linux - using the gcc compiler: +g++ src\Tanges.cpp && a.out diff --git a/challenge_6/cpp/sven/src/Ranges.cpp b/challenge_6/cpp/sven/src/Ranges.cpp new file mode 100644 index 000000000..ab92d67c6 --- /dev/null +++ b/challenge_6/cpp/sven/src/Ranges.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +int main(int argc, char* argv[]) +{ + int rangeBegin = std::stoi(argv[1]); + int rangeEnd = rangeBegin; + for (int i = 2; i < argc; ++i) + { + if (rangeEnd + 1 == std::stoi(argv[i])) + { + rangeEnd++; + } + else + { + std::cout << rangeBegin << "->" << rangeEnd << std::endl; + rangeBegin = std::stoi(argv[i]); + rangeEnd = rangeBegin; + } + } + std::cout << rangeBegin << "->" << rangeEnd << std::endl; +} + From 99e9ad2e80225a75680425c4c62d4f3be35c1111 Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 29 Jan 2017 16:56:11 +0100 Subject: [PATCH 19/19] [C#] Challenge 6 --- challenge_6/csharp/sven/README.md | 8 +++++++ challenge_6/csharp/sven/src/Ranges.cs | 31 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 challenge_6/csharp/sven/README.md create mode 100644 challenge_6/csharp/sven/src/Ranges.cs diff --git a/challenge_6/csharp/sven/README.md b/challenge_6/csharp/sven/README.md new file mode 100644 index 000000000..214898c7e --- /dev/null +++ b/challenge_6/csharp/sven/README.md @@ -0,0 +1,8 @@ +# Challenge_6 Solution + +To compile run: "csc.exe src\Ranges.cs" +and run the resulting file with your sequence as arguments: "Ranges.exe 1 2 3 5 6 10 11 12 13 20". + +csc can be found in \Microsoft.NET\\<.NET version> +(e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319) +or you can open a visual studio command prompt which adds the correct paths to its environment. diff --git a/challenge_6/csharp/sven/src/Ranges.cs b/challenge_6/csharp/sven/src/Ranges.cs new file mode 100644 index 000000000..b8b40fec7 --- /dev/null +++ b/challenge_6/csharp/sven/src/Ranges.cs @@ -0,0 +1,31 @@ +using System; +using System.Linq; +using System.Collections.Generic; + +namespace Challenge_5 +{ + class Program + { + static void Main(string[] args) + { + List list = args.Select(arg => int.Parse(arg)).ToList(); + int rangeBegin = list.First(); + int rangeEnd = rangeBegin; + + for (int i = 1; i < list.Count; i++) + { + if (rangeEnd + 1 == list[i]) + { + rangeEnd++; + } + else + { + Console.WriteLine(rangeBegin + "->" + rangeEnd); + rangeBegin = list[i]; + rangeEnd = rangeBegin; + } + } + Console.WriteLine(rangeBegin + "->" + rangeEnd); + } + } +} \ No newline at end of file