Finishing 12 new elements

This commit is contained in:
2020-01-03 14:31:18 +01:00
parent f81e9d7ae3
commit da0609f315
12 changed files with 450 additions and 0 deletions

32
OReilly/cipher-map2.py Normal file
View File

@@ -0,0 +1,32 @@
def recall_password(cipher_grille, ciphered_password):
password = ""
for rotation in range(4):
for i, row in enumerate(cipher_grille):
for j, cell in enumerate(row):
if cell == "X":
password += ciphered_password[i][j]
cipher_grille = list(zip(*cipher_grille[::-1]))
return password
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert recall_password(
('X...',
'..X.',
'X..X',
'....'),
('itdf',
'gdce',
'aton',
'qrdi')) == 'icantforgetiddqd', 'First example'
assert recall_password(
('....',
'X..X',
'.X..',
'...X'),
('xhwc',
'rsqx',
'xqzz',
'fyzr')) == 'rxqrwsfzxqxzhczy', 'Second example'

View File

@@ -0,0 +1,29 @@
def create_intervals(data: set):
"""
Create a list of intervals out of set of ints.
"""
if len(data) == 0:
return []
results = []
data = sorted(data)
start = None
current_number = None
for number in data:
if start is None:
start = number
current_number = start
continue
if number != start and number != (current_number + 1):
results.append((start, current_number))
start = number
current_number = number
results.append((start, current_number))
return results
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert create_intervals({1, 2, 3, 4, 5, 7, 8, 12}) == [(1, 5), (7, 8), (12, 12)], "First"
assert create_intervals({1, 2, 3, 6, 7, 8, 4, 5}) == [(1, 8)], "Second"
assert create_intervals({7, 9, 10, 11, 12}) == [(7, 7), (9, 12)], "Third"
print('Almost done! The only thing left to do is to Check it!')

21
OReilly/days-diff.py Normal file
View File

@@ -0,0 +1,21 @@
from datetime import datetime
def days_diff(a, b):
[a_year, a_month, a_days] = list(map(str, a))
[b_year, b_month, b_days] = list(map(str, b))
d1 = datetime.strptime("{}-{}-{}".format(a_year.rjust(4, "0"), a_month.rjust(2, "0"), a_days.rjust(2, "0")), "%Y-%m-%d")
d2 = datetime.strptime("{}-{}-{}".format(b_year.rjust(4, "0"), b_month.rjust(2, "0"), b_days.rjust(2, "0")), "%Y-%m-%d")
return abs((d2 - d1).days)
if __name__ == '__main__':
print("Example:")
print(days_diff((1982, 4, 19), (1982, 4, 22)))
# These "asserts" are used for self-checking and not for an auto-testing
assert days_diff((1982, 4, 19), (1982, 4, 22)) == 3
assert days_diff((2014, 1, 1), (2014, 8, 27)) == 238
assert days_diff((2014, 8, 27), (2014, 1, 1)) == 238
assert days_diff([1, 1, 1], [9999, 12, 31]) == 3652058
print("Coding complete? Click 'Check' to earn cool rewards!")

30
OReilly/median.py Normal file
View File

@@ -0,0 +1,30 @@
from typing import List
import math
def checkio(data: List[int]) -> [int, float]:
data.sort()
data_len = len(data)
if data_len % 2 == 0:
if data_len != 2:
cut = int(data_len / 2 - 1)
data = data[cut:-cut]
return sum(data) / len(data)
middle = math.floor(data_len / 2)
return data[middle]
# These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
print("Example:")
print(checkio([1, 2, 3, 4, 5]))
assert checkio([1, 2, 3, 4, 5]) == 3, "Sorted list"
assert checkio([3, 1, 2, 5, 3]) == 3, "Not sorted list"
assert checkio([1, 300, 2, 200, 1]) == 2, "It's not an average"
assert checkio([3, 6, 20, 99, 10, 15]) == 12.5, "Even length"
print("Start the long test")
assert checkio(list(range(1000000))) == 499999.5, "Long."
assert checkio([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) == 5, "5."
assert checkio([999999, 1]) == 5, "5."
print("Coding complete? Click 'Check' to earn cool rewards!")

51
OReilly/text-editor.py Normal file
View File

@@ -0,0 +1,51 @@
import copy
class Text:
def __init__(self):
self.lines = ""
self.font = ""
def write(self, lines):
self.lines += lines
def set_font(self, font_name):
self.font = font_name
def show(self):
return "[{}]{}[{}]".format(self.font, self.lines, self.font) if self.font != "" else self.lines
def restore(self, version):
self.font = version.font
self.lines = version.lines
class SavedText:
def __init__(self):
self.texts = []
def save_text(self, texts):
self.texts.append(copy.deepcopy(texts))
def get_version(self, version):
return self.texts[version]
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
text = Text()
saver = SavedText()
text.write("At the very beginning ")
saver.save_text(text)
text.set_font("Arial")
saver.save_text(text)
text.write("there was nothing.")
assert text.show() == "[Arial]At the very beginning there was nothing.[Arial]"
text.restore(saver.get_version(0))
assert text.show() == "At the very beginning "
print("Coding complete? Let's try tests!")

33
OReilly/worth-of-words.py Normal file
View File

@@ -0,0 +1,33 @@
VALUES = {'e': 1, 'a': 1, 'i': 1, 'o': 1, 'n': 1, 'r': 1,
't': 1, 'l': 1, 's': 1, 'u': 1, 'd': 2, 'g': 2,
'b': 3, 'c': 3, 'm': 3, 'p': 3, 'f': 4, 'h': 4,
'v': 4, 'w': 4, 'y': 4, 'k': 5, 'j': 8, 'x': 8,
'q': 10, 'z': 10}
def worth_of_words(words):
highest_word = None
highest_score = 0
for word in words:
score = word_score(word)
if score > highest_score:
highest_score = score
highest_word = word
return highest_word
def word_score(word):
score = 0
for letter in word:
score += VALUES[letter]
return score
if __name__ == '__main__':
print("Example:")
print(worth_of_words(['hi', 'quiz', 'bomb', 'president']))
# These "asserts" using only for self-checking and not necessary for auto-testing
assert worth_of_words(['hi', 'quiz', 'bomb', 'president']) == 'quiz'
assert worth_of_words(['zero', 'one', 'two', 'three', 'four', 'five']) == 'zero'
print("Coding complete? Click 'Check' to earn cool rewards!")