From 98427a8654e233f36d4850f784d092a29946e730 Mon Sep 17 00:00:00 2001 From: Asim Ihsan Date: Sun, 26 Jan 2014 14:51:41 +0000 Subject: [PATCH 1/3] tree_zig_zag - working on C++ solution --- tree_zig_zag/.gitignore | 3 +- tree_zig_zag/solutions/tree_zig_zag.cpp | 48 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tree_zig_zag/solutions/tree_zig_zag.cpp diff --git a/tree_zig_zag/.gitignore b/tree_zig_zag/.gitignore index b69b36f..5179ea3 100644 --- a/tree_zig_zag/.gitignore +++ b/tree_zig_zag/.gitignore @@ -1 +1,2 @@ -_doc \ No newline at end of file +_doc +solutions/tree_zig_zag \ No newline at end of file diff --git a/tree_zig_zag/solutions/tree_zig_zag.cpp b/tree_zig_zag/solutions/tree_zig_zag.cpp new file mode 100644 index 0000000..bef1d77 --- /dev/null +++ b/tree_zig_zag/solutions/tree_zig_zag.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +class Node { +public: + // constructor + Node(int value) : left(NULL), right(NULL) { + this->value = value; + } + + // destructor + ~Node() { + } + + // copy constructor + Node(const Node& o) { + } + + // assignment + Node& operator=(const Node &o) { + } + +private: + int value; + Node* left; + Node* right; +}; + + +int main() { + std::string tmp; + while(std::getline(std::cin, tmp)) { + std::vector nums; + std::istringstream ss(tmp); + int ti; + while (ss >> ti) + nums.push_back(ti); + + for (std::vector::const_iterator iter = nums.begin(); + iter != nums.end(); ++iter) + { + std::cout << *iter << " "; + } + std::cout << std::endl; + } +} From b72dd9701189862a1a55ae4a41f2c56165d34886 Mon Sep 17 00:00:00 2001 From: Asim Ihsan Date: Sun, 26 Jan 2014 17:43:35 +0000 Subject: [PATCH 2/3] tree_zig_zag - basic Node class for C++ solution --- tree_zig_zag/solutions/tree_zig_zag.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tree_zig_zag/solutions/tree_zig_zag.cpp b/tree_zig_zag/solutions/tree_zig_zag.cpp index bef1d77..871251e 100644 --- a/tree_zig_zag/solutions/tree_zig_zag.cpp +++ b/tree_zig_zag/solutions/tree_zig_zag.cpp @@ -3,24 +3,21 @@ #include #include + class Node { public: - // constructor - Node(int value) : left(NULL), right(NULL) { - this->value = value; - } - - // destructor - ~Node() { + Node() : value(-1), left(NULL), right(NULL) { } - // copy constructor - Node(const Node& o) { + Node(int value) : left(NULL), right(NULL) { + this->value = value; } - // assignment - Node& operator=(const Node &o) { - } + Node* getLeft() { return left; } + void setLeft(Node* node) { left = node; } + Node* getRight() { return right; } + void setRight(Node* node) { right = node; } + int getValue() { return value; } private: int value; From 71d0dfef1242f969545b1754d4b89de345e23b5c Mon Sep 17 00:00:00 2001 From: Asim Ihsan Date: Tue, 28 Jan 2014 00:16:31 +0000 Subject: [PATCH 3/3] coin_change - initial commit of Python solution Use memoization as an indirect form of dynamic programming. --- coin_change/solutions/coin_change_solution.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 coin_change/solutions/coin_change_solution.py diff --git a/coin_change/solutions/coin_change_solution.py b/coin_change/solutions/coin_change_solution.py new file mode 100644 index 0000000..fef052f --- /dev/null +++ b/coin_change/solutions/coin_change_solution.py @@ -0,0 +1,39 @@ +import cPickle as pickle +import sys + + +def memoize(func): + cache = {} + + def wrapper(*args, **kwargs): + key = pickle.dumps(args) + pickle.dumps(kwargs) + if key not in cache: + cache[key] = func(*args, **kwargs) + return cache[key] + return wrapper + + +@memoize +def solve(coins, amount): + if len(coins) == 0: + return 0 + if len(coins) == 1: + return 1 if amount % coins[0] == 0 else 0 + ways = 0 + current_coin, rest_of_coins = coins[0], coins[1:] + i = 0 + while amount - i * current_coin >= 0: + ways += solve(rest_of_coins, amount - i * current_coin) + i += 1 + return ways + + +def main(): + coins = sorted(map(int, sys.stdin.readline().strip().split(",")), + reverse=True) + amount = int(sys.stdin.readline().strip()) + ways = solve(coins, amount) + print(ways) + +if __name__ == "__main__": + main()