Skip to content

[C++] [C#] Challenge 3, 4, 5 & 6 (Unreviewed) #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions challenge_3/cpp/sven/README.md
Original file line number Diff line number Diff line change
@@ -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)\<Visual Studio folder>\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
20 changes: 20 additions & 0 deletions challenge_3/cpp/sven/src/Majority.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>

int main(int argc, char* argv[])
{
std::unordered_map<std::string, int> map;
std::pair<std::string, int> majority(argv[1], 1);

for (int i = 1; i < argc; ++i)
{
map[argv[i]]++;
if(map[argv[i]] > majority.second)
{
majority = std::pair<std::string, int>(argv[i], map[argv[i]]);
}
}
std::cout << majority.first << std::endl;
}
8 changes: 8 additions & 0 deletions challenge_3/csharp/sven/README.md
Original file line number Diff line number Diff line change
@@ -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 <windows installation folder>\Microsoft.NET\<Framework bitness>\<.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.
14 changes: 14 additions & 0 deletions challenge_3/csharp/sven/src/Majority.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
14 changes: 14 additions & 0 deletions challenge_4/cpp/sven/README.md
Original file line number Diff line number Diff line change
@@ -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)\<Visual Studio folder>\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
78 changes: 78 additions & 0 deletions challenge_4/cpp/sven/src/Invert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <iostream>
#include <string>
#include <vector>
#include <functional>

static const std::string EMPTY = "#";

struct BinaryTreeNode
{
std::string value;
BinaryTreeNode* leftChild;
BinaryTreeNode* rightChild;
};

void traverseInOrder(BinaryTreeNode* root, std::function<void(BinaryTreeNode*)> func)
{
if (root != nullptr)
{
traverseInOrder(root->leftChild, func);
traverseInOrder(root->rightChild, func);
}
func(root);
}

void traversePreOrder(BinaryTreeNode* root, std::function<void(BinaryTreeNode*)> func)
{
func(root);
if (root != nullptr)
{
traversePreOrder(root->leftChild, func);
traversePreOrder(root->rightChild, func);
}
}

BinaryTreeNode* treeFromInputRec(BinaryTreeNode* root, std::vector<std::string>::const_iterator& pos, std::vector<std::string>::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<std::string> input)
{
BinaryTreeNode* root = new BinaryTreeNode{ input.front(), nullptr, nullptr };
return treeFromInputRec(root, ++input.cbegin(), input.cend());
}


int main(int argc, char* argv[])
{
std::vector<std::string> 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;


}

10 changes: 10 additions & 0 deletions challenge_4/csharp/sven/README.md
Original file line number Diff line number Diff line change
@@ -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 <windows installation folder>\Microsoft.NET\<Framework bitness>\<.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.
71 changes: 71 additions & 0 deletions challenge_4/csharp/sven/src/Invert.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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<BinaryTreeNode, BinaryTreeNode> 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<BinaryTreeNode, BinaryTreeNode> 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<string> 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; } );
}
}
}
12 changes: 12 additions & 0 deletions challenge_5/cpp/sven/README.md
Original file line number Diff line number Diff line change
@@ -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)\<Visual Studio folder>\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
18 changes: 18 additions & 0 deletions challenge_5/cpp/sven/src/Difference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <string>
#include <algorithm>

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;


}

8 changes: 8 additions & 0 deletions challenge_5/csharp/sven/README.md
Original file line number Diff line number Diff line change
@@ -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 <windows installation folder>\Microsoft.NET\<Framework bitness>\<.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.
14 changes: 14 additions & 0 deletions challenge_5/csharp/sven/src/Difference.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
12 changes: 12 additions & 0 deletions challenge_6/cpp/sven/README.md
Original file line number Diff line number Diff line change
@@ -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)\<Visual Studio folder>\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
24 changes: 24 additions & 0 deletions challenge_6/cpp/sven/src/Ranges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
#include <string>
#include <algorithm>

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;
}

8 changes: 8 additions & 0 deletions challenge_6/csharp/sven/README.md
Original file line number Diff line number Diff line change
@@ -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 <windows installation folder>\Microsoft.NET\<Framework bitness>\<.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.
31 changes: 31 additions & 0 deletions challenge_6/csharp/sven/src/Ranges.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Linq;
using System.Collections.Generic;

namespace Challenge_5
{
class Program
{
static void Main(string[] args)
{
List<int> 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);
}
}
}