✨ Finishing 13 elements
This commit is contained in:
28
ElectronicStation/brackets.py
Normal file
28
ElectronicStation/brackets.py
Normal file
@@ -0,0 +1,28 @@
|
||||
def checkio(expression):
|
||||
OPENING_BRACKETS = ["(", "[", "{"]
|
||||
CLOSING_BRACKETS = [")", "]", "}"]
|
||||
current_bracket = []
|
||||
for letter in expression:
|
||||
if letter in CLOSING_BRACKETS:
|
||||
if len(current_bracket) == 0:
|
||||
return False
|
||||
else:
|
||||
last_bracket = current_bracket[-1:][0]
|
||||
if OPENING_BRACKETS.index(last_bracket) == CLOSING_BRACKETS.index(letter):
|
||||
current_bracket.pop()
|
||||
else:
|
||||
return False
|
||||
if letter in OPENING_BRACKETS:
|
||||
current_bracket.append(letter)
|
||||
return len(current_bracket) == 0
|
||||
|
||||
|
||||
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||
if __name__ == '__main__':
|
||||
assert checkio("((5+3)*2+1)") == True, "Simple"
|
||||
assert checkio("{[(3+1)+2]+}") == True, "Different types"
|
||||
assert checkio("(3+{1-1)}") == False, ") is alone inside {}"
|
||||
assert checkio("[1+1]+(2*2)-{3/3}") == True, "Different operators"
|
||||
assert checkio("(({[(((1)-2)+3)-3]/3}-3)") == False, "One is redundant"
|
||||
assert checkio("2+3") == True, "No brackets, no problem"
|
||||
assert checkio("(((([[[{{{3}}}]]]]))))") == False, "No brackets, no problem"
|
26
ElectronicStation/count-consecutive-summers.py
Normal file
26
ElectronicStation/count-consecutive-summers.py
Normal file
@@ -0,0 +1,26 @@
|
||||
def count_consecutive_summers(num):
|
||||
index = num
|
||||
number_of_consecutive_summers = 1
|
||||
while index > 0:
|
||||
index -= 1
|
||||
sums = index
|
||||
count = 1
|
||||
while sums <= num and index - count >= 0:
|
||||
if sums == num:
|
||||
number_of_consecutive_summers += 1
|
||||
break
|
||||
sums += index - count
|
||||
count += 1
|
||||
return number_of_consecutive_summers
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("Example:")
|
||||
print(count_consecutive_summers(42))
|
||||
|
||||
# These "asserts" are used for self-checking and not for an auto-testing
|
||||
assert count_consecutive_summers(42) == 4
|
||||
assert count_consecutive_summers(99) == 6
|
||||
assert count_consecutive_summers(1) == 1
|
||||
assert count_consecutive_summers(15) == 4
|
||||
print("Coding complete? Click 'Check' to earn cool rewards!")
|
35
ElectronicStation/date-and-time-converter.py
Normal file
35
ElectronicStation/date-and-time-converter.py
Normal file
@@ -0,0 +1,35 @@
|
||||
def date_time(time: str) -> str:
|
||||
[date, time] = time.split(" ")
|
||||
return "{} {}".format(parse_date(date), parse_time(time))
|
||||
|
||||
|
||||
def parse_date(date: str) -> str:
|
||||
MONTHS = [
|
||||
"", "January", "February", "March", "April",
|
||||
"May", "June", "July", "August", "September",
|
||||
"October", "November", "December"
|
||||
]
|
||||
[day, month, year] = date.split(".")
|
||||
day = int(day)
|
||||
return "{} {} {} year".format(int(day), MONTHS[int(month)], year)
|
||||
|
||||
|
||||
def parse_time(time: str) -> str:
|
||||
[hours, minutes] = time.split(":")
|
||||
return "{} hour{} {} minute{}".format(
|
||||
int(hours),
|
||||
"s" if int(hours) != 1 else "",
|
||||
int(minutes),
|
||||
"s" if int(minutes) != 1 else ""
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("Example:")
|
||||
print(date_time('01.01.2000 00:00'))
|
||||
|
||||
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||
assert date_time("01.01.2000 00:00") == "1 January 2000 year 0 hours 0 minutes", "Millenium"
|
||||
assert date_time("09.05.1945 06:30") == "9 May 1945 year 6 hours 30 minutes", "Victory"
|
||||
assert date_time("20.11.1990 03:55") == "20 November 1990 year 3 hours 55 minutes", "Somebody was born"
|
||||
print("Coding complete? Click 'Check' to earn cool rewards!")
|
33
ElectronicStation/largest-histogram.py
Normal file
33
ElectronicStation/largest-histogram.py
Normal file
@@ -0,0 +1,33 @@
|
||||
def largest_histogram(histogram):
|
||||
stack = list()
|
||||
|
||||
max_area = 0
|
||||
|
||||
index = 0
|
||||
while index < len(histogram):
|
||||
if (not stack) or (histogram[stack[-1]] <= histogram[index]):
|
||||
stack.append(index)
|
||||
index += 1
|
||||
else:
|
||||
top_of_stack = stack.pop()
|
||||
area = (histogram[top_of_stack] *
|
||||
((index - stack[-1] - 1)
|
||||
if stack else index))
|
||||
max_area = max(max_area, area)
|
||||
while stack:
|
||||
top_of_stack = stack.pop()
|
||||
area = (histogram[top_of_stack] *
|
||||
((index - stack[-1] - 1)
|
||||
if stack else index))
|
||||
max_area = max(max_area, area)
|
||||
return max_area
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||
assert largest_histogram([5]) == 5, "one is always the biggest"
|
||||
assert largest_histogram([5, 3]) == 6, "two are smallest X 2"
|
||||
assert largest_histogram([1, 1, 4, 1]) == 4, "vertical"
|
||||
assert largest_histogram([1, 1, 3, 1]) == 4, "horizontal"
|
||||
assert largest_histogram([2, 1, 4, 5, 1, 3, 3]) == 8, "complex"
|
||||
print("Done! Go check it!")
|
43
ElectronicStation/multicolored-lamp.py
Normal file
43
ElectronicStation/multicolored-lamp.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import abc
|
||||
|
||||
|
||||
class State(object, metaclass=abc.ABCMeta):
|
||||
def __init__(self):
|
||||
self.color_index = 0
|
||||
|
||||
@abc.abstractmethod
|
||||
def light(self):
|
||||
COLORS = ["Green", "Red", "Blue", "Yellow"]
|
||||
state_color = COLORS[self.color_index]
|
||||
self.color_index += 1
|
||||
if self.color_index == len(COLORS):
|
||||
self.color_index = 0
|
||||
return state_color
|
||||
|
||||
|
||||
class Lamp(State):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.color = None
|
||||
|
||||
def light(self):
|
||||
self.color = super().light()
|
||||
return self.color
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||
|
||||
lamp_1 = Lamp()
|
||||
lamp_2 = Lamp()
|
||||
|
||||
lamp_1.light() # Green
|
||||
lamp_1.light() # Red
|
||||
lamp_2.light() # Green
|
||||
|
||||
assert lamp_1.light() == "Blue"
|
||||
assert lamp_1.light() == "Yellow"
|
||||
assert lamp_1.light() == "Green"
|
||||
assert lamp_2.light() == "Red"
|
||||
assert lamp_2.light() == "Blue"
|
||||
print("Coding complete? Let's try tests!")
|
36
ElectronicStation/restricted-sum.py
Normal file
36
ElectronicStation/restricted-sum.py
Normal file
@@ -0,0 +1,36 @@
|
||||
def checkio(data):
|
||||
result = 0
|
||||
data_length = len(data)
|
||||
if data_length > 0:
|
||||
result += data[0]
|
||||
if data_length > 1:
|
||||
result += data[1]
|
||||
if data_length > 2:
|
||||
result += data[2]
|
||||
if data_length > 3:
|
||||
result += data[3]
|
||||
if data_length > 4:
|
||||
result += data[4]
|
||||
if data_length > 5:
|
||||
result += data[5]
|
||||
if data_length > 6:
|
||||
result += data[6]
|
||||
if data_length > 7:
|
||||
result += data[7]
|
||||
if data_length > 8:
|
||||
result += data[8]
|
||||
if data_length > 9:
|
||||
result += data[9]
|
||||
if data_length > 10:
|
||||
result += data[10]
|
||||
if data_length > 11:
|
||||
result += data[11]
|
||||
if data_length > 12:
|
||||
result += data[12]
|
||||
if data_length > 13:
|
||||
result += data[13]
|
||||
return result
|
||||
|
||||
|
||||
assert checkio([43, -10, 68, 84, 91, 71, -10, -80, 38]) == 295
|
||||
assert checkio([1, 37, -64, 57, -78, 57, 64, -38, -91, 61, 53, -89, 41]) == 295
|
71
ElectronicStation/reverse-roman-numerals.py
Normal file
71
ElectronicStation/reverse-roman-numerals.py
Normal file
@@ -0,0 +1,71 @@
|
||||
def reverse_roman(roman_string):
|
||||
mille = ['', 'M', 'MM', 'MMM']
|
||||
centaine = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
|
||||
dixaine = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC']
|
||||
unite = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']
|
||||
number = 0
|
||||
roman_string_length = len(roman_string)
|
||||
index = 0
|
||||
while True:
|
||||
letter = roman_string[index]
|
||||
current_number = letter
|
||||
is_multiple = False
|
||||
if current_number in mille:
|
||||
while current_number in mille and index < roman_string_length - 1:
|
||||
index += 1
|
||||
current_number += roman_string[index]
|
||||
if current_number not in mille:
|
||||
is_multiple = True
|
||||
current_number = current_number[0:-1]
|
||||
number += (mille.index(current_number) * 1000)
|
||||
elif current_number in centaine:
|
||||
while current_number in centaine and index < roman_string_length - 1:
|
||||
index += 1
|
||||
current_number += roman_string[index]
|
||||
if current_number not in centaine:
|
||||
is_multiple = True
|
||||
current_number = current_number[0:-1]
|
||||
number += 100 * (centaine.index(current_number))
|
||||
elif current_number in dixaine:
|
||||
while current_number in dixaine and index < roman_string_length - 1:
|
||||
index += 1
|
||||
current_number += roman_string[index]
|
||||
if current_number not in dixaine:
|
||||
is_multiple = True
|
||||
current_number = current_number[0:-1]
|
||||
number += 10 * (dixaine.index(current_number))
|
||||
elif current_number in unite:
|
||||
while current_number in unite and index < roman_string_length - 1:
|
||||
index += 1
|
||||
current_number += roman_string[index]
|
||||
if current_number not in unite:
|
||||
is_multiple = True
|
||||
current_number = current_number[0:-1]
|
||||
number += unite.index(current_number)
|
||||
|
||||
if index == roman_string_length - 1:
|
||||
if is_multiple:
|
||||
letter = roman_string[index]
|
||||
if letter in mille:
|
||||
number += (mille.index(letter)) * 1000
|
||||
elif letter in centaine:
|
||||
number += 100 * (centaine.index(letter))
|
||||
elif letter in dixaine:
|
||||
number += 10 * (dixaine.index(letter))
|
||||
elif letter in unite:
|
||||
number += unite.index(letter)
|
||||
break
|
||||
|
||||
return number
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||
assert reverse_roman('VI') == 6, '6'
|
||||
assert reverse_roman('LXXVI') == 76, '76'
|
||||
assert reverse_roman('CDXCIX') == 499, '499'
|
||||
assert reverse_roman('MMMDCCCLXXXVIII') == 3888, '3888'
|
||||
assert reverse_roman("I") == 1, '1'
|
||||
assert reverse_roman("X") == 10, '10'
|
||||
assert reverse_roman("MMMDCCCX") == 3810, '3810'
|
||||
print('Great! It is time to Check your code!')
|
26
ElectronicStation/time-converter-12h-to-24h.py
Normal file
26
ElectronicStation/time-converter-12h-to-24h.py
Normal file
@@ -0,0 +1,26 @@
|
||||
def time_converter(time):
|
||||
[time, apm] = time.split(" ")
|
||||
time_int = int(time.replace(':', ''))
|
||||
if apm == "a.m.":
|
||||
if time_int < 1200:
|
||||
return time.rjust(5, "0")
|
||||
else:
|
||||
[hours, minutes] = time.split(":")
|
||||
return "00:{}".format(minutes)
|
||||
else:
|
||||
if time_int < 1200:
|
||||
[hours, minutes] = time.split(":")
|
||||
return "{}:{}".format(int(hours) + 12, minutes)
|
||||
else:
|
||||
return time
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("Example:")
|
||||
print(time_converter('12:30 p.m.'))
|
||||
|
||||
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||
assert time_converter('12:30 p.m.') == '12:30'
|
||||
assert time_converter('9:00 a.m.') == '09:00'
|
||||
assert time_converter('11:15 p.m.') == '23:15'
|
||||
print("Coding complete? Click 'Check' to earn cool rewards!")
|
Reference in New Issue
Block a user