Finishing 3 elements

master
Clément 12 months ago
parent 10190b0d23
commit 02a38e8d51
  1. 35
      Storage/stick-sawing.py
  2. 31
      Storage/weak-point.py
  3. 24
      Storage/word-pattern.py

@ -0,0 +1,35 @@
def checkio(number):
middle = number // 2
tri_nums = []
i = 1
current = 1
while current <= middle:
current = i * (i + 1) / 2
tri_nums.append(int(current))
i += 1
i = 0
list_len = len(tri_nums)
for num in tri_nums:
j = i
current = [num]
while sum(current) < number:
j += 1
if j >= list_len:
break
current.append(tri_nums[j])
if sum(current) == number:
return current
i += 1
return []
# These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(64) == [15, 21, 28], "1st example"
assert checkio(371) == [36, 45, 55, 66, 78, 91], "1st example"
assert checkio(225) == [105, 120], "1st example"
assert checkio(10) == [1, 3, 6], "10"
assert checkio(882) == [], "1st example"

@ -0,0 +1,31 @@
def calculate_line_sum(matrix):
min_sum = 10000
current_line_index = 0
i = 0
for line in matrix:
current_line_index = i if min_sum > sum(line) else current_line_index
min_sum = sum(line) if min_sum > sum(line) else min_sum
i += 1
return current_line_index
def weak_point(matrix):
return calculate_line_sum(matrix), calculate_line_sum(list(zip(*matrix[::-1]))) # [0, 0]
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert isinstance(weak_point([[1]]), (list, tuple)), "The result should be a list or a tuple"
assert list(weak_point([[7, 2, 7, 2, 8],
[2, 9, 4, 1, 7],
[3, 8, 6, 2, 4],
[2, 5, 2, 9, 1],
[6, 6, 5, 4, 5]])) == [3, 3], "Example"
assert list(weak_point([[7, 2, 4, 2, 8],
[2, 8, 1, 1, 7],
[3, 8, 6, 2, 4],
[2, 5, 2, 9, 1],
[6, 6, 5, 4, 5]])) == [1, 2], "Two weak point"
assert list(weak_point([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])) == [0, 0], "Top left"

@ -0,0 +1,24 @@
def check_command(pattern, command):
text_length = len(command)
binary = "{0:b}".format(pattern).zfill(text_length)
if len(binary) > text_length:
return False
for i, char in enumerate(command):
if char.isnumeric() and binary[i] == "1":
return False
if char.isalpha() and binary[i] == "0":
return False
return True
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert check_command(42, "12a0b3e4") == True, "42 is the answer"
assert check_command(101, "ab23b4zz") == False, "one hundred plus one"
assert check_command(0, "478103487120470129") == True, "Any number"
assert check_command(127, "Checkio") == True, "Uppercase"
assert check_command(7, "Hello") == False, "Only full match"
assert check_command(8, "a") == False, "Too short command"
assert check_command(5, "H2O") == True, "Water"
assert check_command(42, "C2H5OH") == False, "Yep, this is not the Answer"
Loading…
Cancel
Save