-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoderbyte.py
More file actions
126 lines (111 loc) · 3.74 KB
/
Coderbyte.py
File metadata and controls
126 lines (111 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# solution to First Factorial challenge from Coderbyte
# https://coderbyte.com/results/mich100:First%20Factorial:Python3
def FirstFactorial(num):
i = num - 1
while i > 1:
num *= i
i -= 1
return num
# solution to First Reverse challenge from Coderbyte
# https://coderbyte.com/results/mich100:First%20Reverse:Python3
def FirstReverse(strParam):
i = len(strParam) - 1
reverse = ''
while i >= 0:
reverse += strParam[i]
i -= 1
return reverse
# solution to Longest Word challenge from Coderbyte
# https://coderbyte.com/results/mich100:Longest%20Word:Python3
import string
def LongestWord(sen):
s = ''
for i in sen:
if i not in string.punctuation:
s += i
longest = ''
for word in s.split():
if len(word) > len(longest):
longest = word
return longest
# solution to Tree Constructor challenge from Coderbyte
# https://coderbyte.com/results/mich100:Tree%20Constructor:Python3
def TreeConstructor(strArr):
children = list()
parents = list()
root = None
for i in strArr:
numbers = i[1:-1].split(',')
num1 = int(numbers[0])
num2 = int(numbers[1])
children.append(num1)
if children.count(num1) > 1: # can be only one parent for any children
return False
parents.append(num2)
if parents.count(num2) > 2: # can be at the most 2 children to parent
return False
for i in parents: # find the root
counter = children.count(i)
if counter == 0:
if root is None:
root = i
elif root != i: # can be only one without parent
return False
return True
# solution to Bracket Combinations challenge from Coderbyte
# the function FirstFactorial is the first function in this file
# https://coderbyte.com/results/mich100:Bracket%20Combinations:Python3
def BracketCombinations(num):
if num == 0:
return 1
fact1 = FirstFactorial(num)
fact2 = FirstFactorial(2 * num)
return fact2 // (fact1 * fact1 * (num + 1))
# solution to problem from Coderbyte task
# check if it is possible to make palindrome (with length higher than 3) from string
# by removing at the more 2 characters
def string_challenge(strParam):
if check_poly(strParam):
return "palindrome"
i = 0
while i < len(strParam):
temp = strParam[:i] + strParam[i + 1:]
if check_poly(temp):
return strParam[i]
j = i
while j < len(temp):
temp2 = temp[:j] + temp[j + 1:]
if check_poly(temp2):
return strParam[i] + temp[j]
j += 1
i += 1
return "not possible"
def check_poly(s):
if len(s) < 3:
return False
i = 0
while i < len(s) // 2:
if s[i] != s[len(s) - 1]:
return False
i += 1
return True
# solution to problem from Coderbyte task
# get list of integers stored in the following format: [K, r1, r2, r3, ...]
# where K represents the number of desks in a classroom,
# and the rest of the integers in the array will be in sorted order and will represent the desks that are already occupied.
# All of the desks will be arranged in 2 columns,
# where desk #1 is at the top left, desk #2 is at the top right, desk #3 is below #1, desk #4 is below #2, etc.
# The program should the number of ways 2 students can be seated next to each other.
# This means 1 student is on the left and 1 student on the right, or 1 student is directly above or below the other student.
def ArrayChallenge(arr):
K = arr[0]
desks = arr[1:]
i = 1
counter = 0
while i < K:
if i % 2 == 1 and desks.count(i) == 0 and desks.count(i + 1) == 0:
counter += 1
if i + 2 <= K and desks.count(i) == 0 and desks.count(i + 2) == 0:
counter += 1
i += 1
return counter