✨ Finish Elementary
This commit is contained in:
parent
08b4100a7e
commit
a615255dfa
20
Elementary/absolute-sorting.py
Normal file
20
Elementary/absolute-sorting.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
def checkio(numbers_array: tuple) -> list:
|
||||||
|
return sorted(numbers_array, key=lambda o: abs(o))
|
||||||
|
|
||||||
|
|
||||||
|
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('Example:')
|
||||||
|
print(list(checkio((-20, -5, 10, 15))))
|
||||||
|
|
||||||
|
|
||||||
|
def check_it(array):
|
||||||
|
if not isinstance(array, (list, tuple)):
|
||||||
|
raise TypeError("The result should be a list or tuple.")
|
||||||
|
return list(array)
|
||||||
|
|
||||||
|
|
||||||
|
assert check_it(checkio((-20, -5, 10, 15))) == [-5, 10, 15, -20], "Example" # or (-5, 10, 15, -20)
|
||||||
|
assert check_it(checkio((1, 2, 3, 0))) == [0, 1, 2, 3], "Positive numbers"
|
||||||
|
assert check_it(checkio((-1, -2, -3, 0))) == [0, -1, -2, -3], "Negative numbers"
|
||||||
|
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
|
18
Elementary/best-stock.py
Normal file
18
Elementary/best-stock.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
def best_stock(a):
|
||||||
|
max_value = 0
|
||||||
|
best = None
|
||||||
|
for k, v in a.items():
|
||||||
|
if v > max_value:
|
||||||
|
max_value = v
|
||||||
|
best = k
|
||||||
|
return best
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print("Example:")
|
||||||
|
print(best_stock({"CAC": 10.0, "ATX": 390.2, "WIG": 1.2}))
|
||||||
|
|
||||||
|
# These "asserts" are used for self-checking and not for an auto-testing
|
||||||
|
assert best_stock({"CAC": 10.0, "ATX": 390.2, "WIG": 1.2}) == "ATX"
|
||||||
|
assert best_stock({"CAC": 91.1, "ATX": 1.01, "TASI": 120.9}) == "TASI"
|
||||||
|
print("Coding complete? Click 'Check' to earn cool rewards!")
|
20
Elementary/between-markers-simplified.py
Normal file
20
Elementary/between-markers-simplified.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def between_markers(text: str, begin: str, end: str) -> str:
|
||||||
|
"""
|
||||||
|
returns substring between two given markers
|
||||||
|
"""
|
||||||
|
return re.findall(r"{}(.*){}".format(re.escape(begin), re.escape(end)), text)[0]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('Example:')
|
||||||
|
print(between_markers('What is >apple<', '>', '<'))
|
||||||
|
|
||||||
|
# These "asserts" are used for self-checking and not for testing
|
||||||
|
assert between_markers('What is >apple<', '>', '<') == "apple"
|
||||||
|
assert between_markers('What is [apple]', '[', ']') == "apple"
|
||||||
|
assert between_markers('What is ><', '>', '<') == ""
|
||||||
|
assert between_markers('>apple<', '>', '<') == "apple"
|
||||||
|
print('Wow, you are doing pretty good. Time to check it!')
|
28
Elementary/between-markers.py
Normal file
28
Elementary/between-markers.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def between_markers(text: str, begin: str, end: str) -> str:
|
||||||
|
"""
|
||||||
|
returns substring between two given markers
|
||||||
|
"""
|
||||||
|
if text.count(begin) == 0:
|
||||||
|
text = begin+text
|
||||||
|
if text.count(end) == 0:
|
||||||
|
text = text+end
|
||||||
|
found = re.findall(r"{}(.*){}".format(re.escape(begin), re.escape(end)), text)
|
||||||
|
return found[0] if len(found) > 0 else ""
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('Example:')
|
||||||
|
print(between_markers('What is >apple<', '>', '<'))
|
||||||
|
|
||||||
|
# These "asserts" are used for self-checking and not for testing
|
||||||
|
assert between_markers('What is >apple<', '>', '<') == "apple", "One sym"
|
||||||
|
assert between_markers("<head><title>My new site</title></head>",
|
||||||
|
"<title>", "</title>") == "My new site", "HTML"
|
||||||
|
assert between_markers('No[/b] hi', '[b]', '[/b]') == 'No', 'No opened'
|
||||||
|
assert between_markers('No [b]hi', '[b]', '[/b]') == 'hi', 'No close'
|
||||||
|
assert between_markers('No hi', '[b]', '[/b]') == 'No hi', 'No markers at all'
|
||||||
|
assert between_markers('No <hi>', '>', '<') == '', 'Wrong direction'
|
||||||
|
print('Wow, you are doing pretty good. Time to check it!')
|
37
Elementary/bigger-price.py
Normal file
37
Elementary/bigger-price.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
def bigger_price(limit: int, data: list) -> list:
|
||||||
|
"""
|
||||||
|
TOP most expensive goods
|
||||||
|
"""
|
||||||
|
data = sorted(data, key=lambda o: o["price"])
|
||||||
|
data.reverse()
|
||||||
|
return data[0: limit]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
from pprint import pprint
|
||||||
|
print('Example:')
|
||||||
|
pprint(bigger_price(2, [
|
||||||
|
{"name": "bread", "price": 100},
|
||||||
|
{"name": "wine", "price": 138},
|
||||||
|
{"name": "meat", "price": 15},
|
||||||
|
{"name": "water", "price": 1}
|
||||||
|
]))
|
||||||
|
|
||||||
|
# These "asserts" using for self-checking and not for auto-testing
|
||||||
|
assert bigger_price(2, [
|
||||||
|
{"name": "bread", "price": 100},
|
||||||
|
{"name": "wine", "price": 138},
|
||||||
|
{"name": "meat", "price": 15},
|
||||||
|
{"name": "water", "price": 1}
|
||||||
|
]) == [
|
||||||
|
{"name": "wine", "price": 138},
|
||||||
|
{"name": "bread", "price": 100}
|
||||||
|
], "First"
|
||||||
|
|
||||||
|
assert bigger_price(1, [
|
||||||
|
{"name": "pen", "price": 5},
|
||||||
|
{"name": "whiteboard", "price": 170}
|
||||||
|
]) == [{"name": "whiteboard", "price": 170}], "Second"
|
||||||
|
|
||||||
|
print('Done! Looks like it is fine. Go and check it')
|
14
Elementary/even-last.py
Normal file
14
Elementary/even-last.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
def checkio(array):
|
||||||
|
return sum([x for i, x in enumerate(array) if i % 2 == 0]) * array[-1:][0] if len(array) > 0 else 0
|
||||||
|
|
||||||
|
|
||||||
|
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('Example:')
|
||||||
|
print(checkio([0, 1, 2, 3, 4, 5]))
|
||||||
|
|
||||||
|
assert checkio([0, 1, 2, 3, 4, 5]) == 30, "(0+2+4)*5=30"
|
||||||
|
assert checkio([1, 3, 5]) == 30, "(1+5)*5=30"
|
||||||
|
assert checkio([6]) == 36, "(6)*6=36"
|
||||||
|
assert checkio([]) == 0, "An empty array = 0"
|
||||||
|
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
|
16
Elementary/first-word-simplified.py
Normal file
16
Elementary/first-word-simplified.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
def first_word(text: str) -> str:
|
||||||
|
"""
|
||||||
|
returns the first word in a given text.
|
||||||
|
"""
|
||||||
|
return text[0:text.find(" ")] if text.find(" ") != -1 else text
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print("Example:")
|
||||||
|
print(first_word("Hello world"))
|
||||||
|
|
||||||
|
# These "asserts" are used for self-checking and not for an auto-testing
|
||||||
|
assert first_word("Hello world") == "Hello"
|
||||||
|
assert first_word("a word") == "a"
|
||||||
|
assert first_word("hi") == "hi"
|
||||||
|
print("Coding complete? Click 'Check' to earn cool rewards!")
|
24
Elementary/first-word.py
Normal file
24
Elementary/first-word.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def first_word(text: str) -> str:
|
||||||
|
"""
|
||||||
|
returns the first word in a given text.
|
||||||
|
"""
|
||||||
|
s = re.findall(r"(\b[a-zA-Z']+)", text)
|
||||||
|
return s[0]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print("Example:")
|
||||||
|
print(first_word("Hello world"))
|
||||||
|
|
||||||
|
# These "asserts" are used for self-checking and not for an auto-testing
|
||||||
|
assert first_word("Hello world") == "Hello"
|
||||||
|
assert first_word(" a word ") == "a"
|
||||||
|
assert first_word("don't touch it") == "don't"
|
||||||
|
assert first_word("greetings, friends") == "greetings"
|
||||||
|
assert first_word("... and so on ...") == "and"
|
||||||
|
assert first_word("hi") == "hi"
|
||||||
|
assert first_word("Hello.World") == "Hello"
|
||||||
|
print("Coding complete? Click 'Check' to earn cool rewards!")
|
28
Elementary/fizz-buzz.py
Normal file
28
Elementary/fizz-buzz.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# Your optional code here
|
||||||
|
# You can import some modules or create additional functions
|
||||||
|
|
||||||
|
|
||||||
|
def checkio(number: int) -> str:
|
||||||
|
result = []
|
||||||
|
if number % 3 == 0:
|
||||||
|
result.append("Fizz")
|
||||||
|
if number % 5 == 0:
|
||||||
|
result.append("Buzz")
|
||||||
|
if len(result) == 0:
|
||||||
|
return str(number)
|
||||||
|
return " ".join(result)
|
||||||
|
|
||||||
|
|
||||||
|
# Some hints:
|
||||||
|
# Convert a number in the string with str(n)
|
||||||
|
|
||||||
|
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('Example:')
|
||||||
|
print(checkio(15))
|
||||||
|
|
||||||
|
assert checkio(15) == "Fizz Buzz", "15 is divisible by 3 and 5"
|
||||||
|
assert checkio(6) == "Fizz", "6 is divisible by 3"
|
||||||
|
assert checkio(5) == "Buzz", "5 is divisible by 5"
|
||||||
|
assert checkio(7) == "7", "7 is not divisible by 3 or 5"
|
||||||
|
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
|
25
Elementary/most-numbers.py
Normal file
25
Elementary/most-numbers.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
def checkio(*args):
|
||||||
|
difference = 0
|
||||||
|
for first_number in args:
|
||||||
|
for second_number in args:
|
||||||
|
difference_between_numbers = abs(first_number - second_number)
|
||||||
|
if difference_between_numbers > difference:
|
||||||
|
difference = difference_between_numbers
|
||||||
|
return difference
|
||||||
|
|
||||||
|
|
||||||
|
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||||
|
if __name__ == '__main__':
|
||||||
|
def almost_equal(checked, correct, significant_digits):
|
||||||
|
precision = 0.1 ** significant_digits
|
||||||
|
return correct - precision < checked < correct + precision
|
||||||
|
|
||||||
|
|
||||||
|
print('Example:')
|
||||||
|
print(checkio(1, 2, 3))
|
||||||
|
|
||||||
|
assert almost_equal(checkio(1, 2, 3), 2, 3), "3-1=2"
|
||||||
|
assert almost_equal(checkio(5, -5), 10, 3), "5-(-5)=10"
|
||||||
|
assert almost_equal(checkio(10.2, -2.2, 0, 1.1, 0.5), 12.4, 3), "10.2-(-2.2)=12.4"
|
||||||
|
assert almost_equal(checkio(), 0, 3), "Empty"
|
||||||
|
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
|
35
Elementary/popular-words.py
Normal file
35
Elementary/popular-words.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
def popular_words(text: str, words: list) -> dict:
|
||||||
|
result = {}
|
||||||
|
text = text.lower()
|
||||||
|
for word in words:
|
||||||
|
# print(text[0:len(word)+1])
|
||||||
|
result[word] = 1 if text[-(len(word)):] == word else 0
|
||||||
|
result[word] += 1 if text[0:len(word)+1] == word+" " else 0
|
||||||
|
result[word] += text.count(" "+word+" ")
|
||||||
|
result[word] += text.count(word+"\n")
|
||||||
|
result[word] += text.count("\n"+word+" ")
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print("Example:")
|
||||||
|
print(popular_words('''
|
||||||
|
When I was One
|
||||||
|
I had just begun
|
||||||
|
When I was Two
|
||||||
|
I was nearly new
|
||||||
|
''', ['i', 'was', 'three', 'near']))
|
||||||
|
|
||||||
|
# These "asserts" are used for self-checking and not for an auto-testing
|
||||||
|
assert popular_words('''
|
||||||
|
When I was One
|
||||||
|
I had just begun
|
||||||
|
When I was Two
|
||||||
|
I was nearly new
|
||||||
|
''', ['i', 'was', 'three', 'near']) == {
|
||||||
|
'i': 4,
|
||||||
|
'was': 3,
|
||||||
|
'three': 0,
|
||||||
|
'near': 0
|
||||||
|
}
|
||||||
|
print("Coding complete? Click 'Check' to earn cool rewards!")
|
17
Elementary/right-to-left.py
Normal file
17
Elementary/right-to-left.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
def left_join(phrases):
|
||||||
|
"""
|
||||||
|
Join strings and replace "right" to "left"
|
||||||
|
"""
|
||||||
|
return ",".join([x.replace("right", "left") for x in phrases])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('Example:')
|
||||||
|
print(left_join(("left", "right", "left", "stop")))
|
||||||
|
|
||||||
|
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||||
|
assert left_join(("left", "right", "left", "stop")) == "left,left,left,stop", "All to left"
|
||||||
|
assert left_join(("bright aright", "ok")) == "bleft aleft,ok", "Bright Left"
|
||||||
|
assert left_join(("brightness wright",)) == "bleftness wleft", "One phrase"
|
||||||
|
assert left_join(("enough", "jokes")) == "enough,jokes", "Nothing to replace"
|
||||||
|
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
|
19
Elementary/second-index.py
Normal file
19
Elementary/second-index.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
def second_index(text: str, symbol: str) -> [int, None]:
|
||||||
|
"""
|
||||||
|
returns the second index of a symbol in a given text
|
||||||
|
"""
|
||||||
|
symbols = [index for index, letter in enumerate(text) if letter == symbol]
|
||||||
|
return symbols[1] if len(symbols) > 1 else None
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('Example:')
|
||||||
|
print(second_index("sims", "s"))
|
||||||
|
|
||||||
|
# These "asserts" are used for self-checking and not for an auto-testing
|
||||||
|
assert second_index("sims", "s") == 3, "First"
|
||||||
|
assert second_index("find the river", "e") == 12, "Second"
|
||||||
|
assert second_index("hi", " ") is None, "Third"
|
||||||
|
assert second_index("hi mayor", " ") is None, "Fourth"
|
||||||
|
assert second_index("hi mr Mayor", " ") == 5, "Fifth"
|
||||||
|
print('You are awesome! All tests are done! Go Check it!')
|
13
Elementary/secret-message.py
Normal file
13
Elementary/secret-message.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
def find_message(text: str) -> str:
|
||||||
|
return "".join([x for x in text if x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('Example:')
|
||||||
|
print(find_message("How are you? Eh, ok. Low or Lower? Ohhh."))
|
||||||
|
|
||||||
|
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||||
|
assert find_message("How are you? Eh, ok. Low or Lower? Ohhh.") == "HELLO", "hello"
|
||||||
|
assert find_message("hello world!") == "", "Nothing"
|
||||||
|
assert find_message("HELLO WORLD!!!") == "HELLOWORLD", "Capitals"
|
||||||
|
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
|
24
Elementary/three-words.py
Normal file
24
Elementary/three-words.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
def checkio(words: str) -> bool:
|
||||||
|
count = 0
|
||||||
|
words = words.split(" ")
|
||||||
|
for word in words:
|
||||||
|
if word[0].isalpha():
|
||||||
|
count += 1
|
||||||
|
if count == 3:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
count = 0
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('Example:')
|
||||||
|
print(checkio("Hello World hello"))
|
||||||
|
|
||||||
|
assert checkio("Hello World hello") == True, "Hello"
|
||||||
|
assert checkio("He is 123 man") == False, "123 man"
|
||||||
|
assert checkio("1 2 3 4") == False, "Digits"
|
||||||
|
assert checkio("bla bla bla bla") == True, "Bla Bla"
|
||||||
|
assert checkio("Hi") == False, "Hi"
|
||||||
|
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
|
Loading…
Reference in New Issue
Block a user