forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request YearOfProgramming#102 from ningyuansg/master
[Python] Challenges #0, YearOfProgramming#1, YearOfProgramming#2, YearOfProgramming#3, YearOfProgramming#4
- Loading branch information
Showing
5 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
print('Hello World!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
def reverse_string(original_string): | ||
return original_string[::-1] | ||
|
||
original_string = str(input("Enter a string to be reversed > ")) | ||
print(reverse_string(original_string)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from collections import defaultdict | ||
|
||
def find_unique(sequence): | ||
counter_dict = defaultdict(int) | ||
uniques = [] | ||
|
||
for item in sequence: | ||
counter_dict[item] += 1 | ||
|
||
for item, count in counter_dict.items(): | ||
if count == 1: | ||
uniques.append(item) | ||
|
||
return uniques | ||
|
||
test_sequence_list = [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] | ||
|
||
print(find_unique(test_sequence_list)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from collections import Counter | ||
|
||
test_sequence_list = [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] | ||
print(Counter(test_sequence_list).most_common(1)[0][0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class BTree: | ||
def __init__(self, b_tree_list=list()): | ||
self.b_tree_list = b_tree_list | ||
self.levels = len(b_tree_list) | ||
|
||
def visualise(self): | ||
for index, level in enumerate(self.b_tree_list): | ||
spacing = 2 ** (self.levels - index) - 1 | ||
print(((spacing-1)//2)*' ', end='') | ||
for node in level: | ||
if node is None: | ||
print(' ', end='') | ||
else: | ||
print(node, end='') | ||
print(spacing * ' ', end='') | ||
print('') # newline | ||
|
||
def invert(self): | ||
for level in self.b_tree_list: | ||
level.reverse() | ||
|
||
|
||
example_tree = BTree([ | ||
[4], | ||
[2, 7], | ||
[1, 3, 6, 9]]) | ||
|
||
example_tree.visualise() | ||
example_tree.invert() | ||
example_tree.visualise() |