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 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 diff --git a/challenge_4/cpp/sven/README.md b/challenge_4/cpp/sven/README.md new file mode 100644 index 000000000..fd3926ee3 --- /dev/null +++ b/challenge_4/cpp/sven/README.md @@ -0,0 +1,14 @@ +#Challenge_4 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; + + +} + 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..e615973ef --- /dev/null +++ b/challenge_4/csharp/sven/src/Invert.cs @@ -0,0 +1,71 @@ +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; } ); + } + } +} \ No newline at end of file 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; + + +} + diff --git a/challenge_5/csharp/sven/README.md b/challenge_5/csharp/sven/README.md new file mode 100644 index 000000000..d9f88bbf1 --- /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: "Difference.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 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; +} + 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