From 0b7c79c4bcc9072e23e4fb9aa9a94d96c02bb648 Mon Sep 17 00:00:00 2001 From: whiterd Date: Sun, 8 Jan 2017 22:30:54 -0600 Subject: [PATCH 1/7] challenge_0 in python --- challenge_0/python/whiterd/README.md | 0 challenge_0/python/whiterd/src/hello_world.py | 5 +++++ 2 files changed, 5 insertions(+) create mode 100644 challenge_0/python/whiterd/README.md create mode 100644 challenge_0/python/whiterd/src/hello_world.py diff --git a/challenge_0/python/whiterd/README.md b/challenge_0/python/whiterd/README.md new file mode 100644 index 000000000..e69de29bb diff --git a/challenge_0/python/whiterd/src/hello_world.py b/challenge_0/python/whiterd/src/hello_world.py new file mode 100644 index 000000000..b11ed0060 --- /dev/null +++ b/challenge_0/python/whiterd/src/hello_world.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python + +from __future__ import print_function + +print('Hello World!') From 62caeb7472a1b7413d192466f9822f02bea15943 Mon Sep 17 00:00:00 2001 From: whiterd Date: Mon, 9 Jan 2017 12:36:21 -0600 Subject: [PATCH 2/7] challenge_1 in python --- challenge_1/python/whiterd/README.md | 4 ++++ challenge_1/python/whiterd/src/reverse_me.py | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 challenge_1/python/whiterd/README.md create mode 100644 challenge_1/python/whiterd/src/reverse_me.py diff --git a/challenge_1/python/whiterd/README.md b/challenge_1/python/whiterd/README.md new file mode 100644 index 000000000..cbb35a8d0 --- /dev/null +++ b/challenge_1/python/whiterd/README.md @@ -0,0 +1,4 @@ + +Input: string + +Output: a reversed string of the given input \ No newline at end of file diff --git a/challenge_1/python/whiterd/src/reverse_me.py b/challenge_1/python/whiterd/src/reverse_me.py new file mode 100644 index 000000000..506121778 --- /dev/null +++ b/challenge_1/python/whiterd/src/reverse_me.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python + +from __future__ import print_function + +user_input = input('Enter text to be reversed: ') + +print(user_input[::-1]) From f4664fe47c03100c60c767b105681c0c2a9d715c Mon Sep 17 00:00:00 2001 From: whiterd Date: Mon, 9 Jan 2017 19:26:14 -0600 Subject: [PATCH 3/7] [Python] Challenge_2 --- challenge_2/python/whiterd/README.md | 4 ++++ .../python/whiterd/src/find_distinct.py | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 challenge_2/python/whiterd/README.md create mode 100644 challenge_2/python/whiterd/src/find_distinct.py diff --git a/challenge_2/python/whiterd/README.md b/challenge_2/python/whiterd/README.md new file mode 100644 index 000000000..4a3e4601b --- /dev/null +++ b/challenge_2/python/whiterd/README.md @@ -0,0 +1,4 @@ + +Input: a list of integers. + +Output: the only non-repeated integer. \ No newline at end of file diff --git a/challenge_2/python/whiterd/src/find_distinct.py b/challenge_2/python/whiterd/src/find_distinct.py new file mode 100644 index 000000000..a0a44bc76 --- /dev/null +++ b/challenge_2/python/whiterd/src/find_distinct.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python + +''' + Input: An array of random integers where every integer is repeated except + for a single one. + + Output: The single integer that does NOT repeat. +''' + +from __future__ import print_function + +array_1 = [2,3,4,2,3,5,4,6,4,6,9,10,9,8,7,8,10,7] +array_2 = [2,'a','l',3,'l',4,'k',2,3,4,'a',6,'c',4,'m',6,'m','k',9,10,9,8,7,8,10,7] + +def find_distinct(array): + for i in array: + if array.count(i) == 1: + return i + + +if __name__ == '__main__': + print(find_distinct(array_1)) + print(find_distinct(array_2)) From 0bc2f1f3e71559e5d1ad008d07c9927427844e0e Mon Sep 17 00:00:00 2001 From: whiterd Date: Mon, 9 Jan 2017 22:01:45 -0600 Subject: [PATCH 4/7] [Python] Challenge_3 --- challenge_3/python/whiterd/README.md | 8 ++++++++ challenge_3/python/whiterd/src/find_major.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 challenge_3/python/whiterd/README.md create mode 100644 challenge_3/python/whiterd/src/find_major.py diff --git a/challenge_3/python/whiterd/README.md b/challenge_3/python/whiterd/README.md new file mode 100644 index 000000000..c90d61dbf --- /dev/null +++ b/challenge_3/python/whiterd/README.md @@ -0,0 +1,8 @@ +Find Majority Element +--------------------- + +* Given an array of size n, find the majority element. The majority element is the element that appears more than n/2 times. + +* Assume: + * the array is non-empty. + * the majority element always exist in the array. \ No newline at end of file diff --git a/challenge_3/python/whiterd/src/find_major.py b/challenge_3/python/whiterd/src/find_major.py new file mode 100644 index 000000000..f712576fd --- /dev/null +++ b/challenge_3/python/whiterd/src/find_major.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +''' + Input: An array of integers. + + Output: The single integer that occurs most often. +''' + +from __future__ import print_function +from collections import Counter + +given_array = [2,2,3,7,5,7,7,7,4,7,2,7,4,5,6,7,7,8,6,7,7,8,10,12,29,30,19,10,7,7,7,7,7,7,7,7,7] + +def find_major(array): + counted = Counter(array) + return counted.most_common(1)[0][0] + + +print(find_major(given_array)) From d81c93bc033e6a102ad6cc116a28475473fc6d2a Mon Sep 17 00:00:00 2001 From: whiterd Date: Tue, 10 Jan 2017 21:53:45 -0600 Subject: [PATCH 5/7] [Python] Challenge 4 (Unreviewed) --- challenge_4/python/whiterd/README.md | 7 +++++++ challenge_4/python/whiterd/src/invert_bin.py | 13 +++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 challenge_4/python/whiterd/README.md create mode 100644 challenge_4/python/whiterd/src/invert_bin.py diff --git a/challenge_4/python/whiterd/README.md b/challenge_4/python/whiterd/README.md new file mode 100644 index 000000000..90a661a0e --- /dev/null +++ b/challenge_4/python/whiterd/README.md @@ -0,0 +1,7 @@ +# Invert Binary Tree + +* Take in a binary tree as a nested list. + +* For each level in the tree, reverse the order of the items in that level. + +* Output the resulting binary tree. \ No newline at end of file diff --git a/challenge_4/python/whiterd/src/invert_bin.py b/challenge_4/python/whiterd/src/invert_bin.py new file mode 100644 index 000000000..96cee1a5e --- /dev/null +++ b/challenge_4/python/whiterd/src/invert_bin.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python + +''' + Input: An object consisting of a binary tree. + + Output: The same tree with values mirrored. +''' + +def mirror(node): + if node: + mirror(node.left) + mirror(node.right) + node.left, node.right = node.right, node.left From c11515ba70832d3b2f0ea18e7c808caa1efe4f44 Mon Sep 17 00:00:00 2001 From: whiterd Date: Wed, 11 Jan 2017 08:49:03 -0600 Subject: [PATCH 6/7] [Python] Challenge 4 (Unreviewed) --- challenge_4/python/whiterd/README.md | 6 +++--- challenge_4/python/whiterd/src/invert_bin.py | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/challenge_4/python/whiterd/README.md b/challenge_4/python/whiterd/README.md index 90a661a0e..da7eb3581 100644 --- a/challenge_4/python/whiterd/README.md +++ b/challenge_4/python/whiterd/README.md @@ -1,7 +1,7 @@ # Invert Binary Tree -* Take in a binary tree as a nested list. +* Take in a binary tree object. -* For each level in the tree, reverse the order of the items in that level. +* For each level in the tree, reverse the order of the items in that level (mirror). -* Output the resulting binary tree. \ No newline at end of file +* Return the node object. \ No newline at end of file diff --git a/challenge_4/python/whiterd/src/invert_bin.py b/challenge_4/python/whiterd/src/invert_bin.py index 96cee1a5e..194128418 100644 --- a/challenge_4/python/whiterd/src/invert_bin.py +++ b/challenge_4/python/whiterd/src/invert_bin.py @@ -11,3 +11,5 @@ def mirror(node): mirror(node.left) mirror(node.right) node.left, node.right = node.right, node.left + + return node From 394589999036de9d9cb8465d8f38c698fa9ef1de Mon Sep 17 00:00:00 2001 From: whiterd Date: Wed, 11 Jan 2017 10:04:48 -0600 Subject: [PATCH 7/7] [Python] Challenge 5 (Unreviewed) --- challenge_5/python/whiterd/README.md | 7 ++++ challenge_5/python/whiterd/src/find_diff.py | 42 +++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 challenge_5/python/whiterd/README.md create mode 100644 challenge_5/python/whiterd/src/find_diff.py diff --git a/challenge_5/python/whiterd/README.md b/challenge_5/python/whiterd/README.md new file mode 100644 index 000000000..7125304d5 --- /dev/null +++ b/challenge_5/python/whiterd/README.md @@ -0,0 +1,7 @@ +# Find the Difference + +* Assume: two given strings ("s" and "t") of lowercase letters + * "s" is a series of letters (e.g. s = "abcd") + * "t" is an unknow ordering of "s", plus one extra character + +* Output: the added character present in "t" that is not in "s". \ No newline at end of file diff --git a/challenge_5/python/whiterd/src/find_diff.py b/challenge_5/python/whiterd/src/find_diff.py new file mode 100644 index 000000000..494e6332b --- /dev/null +++ b/challenge_5/python/whiterd/src/find_diff.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +''' + Input: string (series of characters) + + Process: string input is randomly sorted and and additional character + is added. + + Output: the additional character that is not present in the input. +''' + +from __future__ import print_function +from random import shuffle, choice +from string import ascii_lowercase + +def get_input(): + """Asks user for input and returns a string.""" + return str(input('Enter a series of unique characters: ')) + +def add_char(s): + """Takes a string as input, adds a char, shuffles it, and returns + the new string.""" + t = list(s) + other_letters = set(ascii_lowercase) - set(t) + t.append(choice(list(other_letters))) + shuffle(t) + return ''.join(t) + +def find_diff(s, t): + """Takes two strings as input and returns the differing character.""" + for i in t: + if i not in s: + return i + + +if __name__ == '__main__': + + s = get_input() + print('You entered: ' + s) + t = add_char(s) + print('The newly generated string is: ' + t) + print('The different character generated is: ' + find_diff(s, t))