diff --git a/Elementary/absolute-sorting.py b/Elementary/absolute-sorting.py new file mode 100644 index 0000000..59b1513 --- /dev/null +++ b/Elementary/absolute-sorting.py @@ -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!") diff --git a/Elementary/best-stock.py b/Elementary/best-stock.py new file mode 100644 index 0000000..f75f0b9 --- /dev/null +++ b/Elementary/best-stock.py @@ -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!") diff --git a/Elementary/between-markers-simplified.py b/Elementary/between-markers-simplified.py new file mode 100644 index 0000000..59a4e9a --- /dev/null +++ b/Elementary/between-markers-simplified.py @@ -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!') \ No newline at end of file diff --git a/Elementary/between-markers.py b/Elementary/between-markers.py new file mode 100644 index 0000000..059a739 --- /dev/null +++ b/Elementary/between-markers.py @@ -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("My new site", + "", "") == "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 ', '>', '<') == '', 'Wrong direction' + print('Wow, you are doing pretty good. Time to check it!') \ No newline at end of file diff --git a/Elementary/bigger-price.py b/Elementary/bigger-price.py new file mode 100644 index 0000000..1d18d7d --- /dev/null +++ b/Elementary/bigger-price.py @@ -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') diff --git a/Elementary/even-last.py b/Elementary/even-last.py new file mode 100644 index 0000000..d0f2697 --- /dev/null +++ b/Elementary/even-last.py @@ -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!") diff --git a/Elementary/first-word-simplified.py b/Elementary/first-word-simplified.py new file mode 100644 index 0000000..53b7f7f --- /dev/null +++ b/Elementary/first-word-simplified.py @@ -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!") \ No newline at end of file diff --git a/Elementary/first-word.py b/Elementary/first-word.py new file mode 100644 index 0000000..832f709 --- /dev/null +++ b/Elementary/first-word.py @@ -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!") \ No newline at end of file diff --git a/Elementary/fizz-buzz.py b/Elementary/fizz-buzz.py new file mode 100644 index 0000000..3a63f8d --- /dev/null +++ b/Elementary/fizz-buzz.py @@ -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!") diff --git a/Elementary/most-numbers.py b/Elementary/most-numbers.py new file mode 100644 index 0000000..893bb5c --- /dev/null +++ b/Elementary/most-numbers.py @@ -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!") diff --git a/Elementary/popular-words.py b/Elementary/popular-words.py new file mode 100644 index 0000000..c09e7f0 --- /dev/null +++ b/Elementary/popular-words.py @@ -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!") \ No newline at end of file diff --git a/Elementary/right-to-left.py b/Elementary/right-to-left.py new file mode 100644 index 0000000..8e639fc --- /dev/null +++ b/Elementary/right-to-left.py @@ -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!") diff --git a/Elementary/second-index.py b/Elementary/second-index.py new file mode 100644 index 0000000..2839b65 --- /dev/null +++ b/Elementary/second-index.py @@ -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!') \ No newline at end of file diff --git a/Elementary/secret-message.py b/Elementary/secret-message.py new file mode 100644 index 0000000..95b9408 --- /dev/null +++ b/Elementary/secret-message.py @@ -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!") diff --git a/Elementary/three-words.py b/Elementary/three-words.py new file mode 100644 index 0000000..1d8c6d0 --- /dev/null +++ b/Elementary/three-words.py @@ -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!")