Skip to content

Commit

Permalink
Add checkio project
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicoretti committed Jul 29, 2023
1 parent 8b051b0 commit 79b7f5e
Show file tree
Hide file tree
Showing 18 changed files with 434 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/checkio/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My solutions of the checkio challenges.
5 changes: 5 additions & 0 deletions python/checkio/checkio/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python3

__all__ = ['elementary', 'home', 'electronic_station']
__author__ = 'Nicola Coretti'
__email__ = '[email protected]'
5 changes: 5 additions & 0 deletions python/checkio/checkio/electronic_station/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python3

__all__ = []
__author__ = 'Nicola Coretti'
__email__ = '[email protected]'
6 changes: 6 additions & 0 deletions python/checkio/checkio/elementary/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python3

__all__ = ['absolute_sorting', 'binary_count', 'digits_multiplication', 'even_last', 'fizz_buzz', 'index_power',
'most_numbers', 'number_radix', 'right_to_left', 'secret_message', 'three_words']
__author__ = 'Nicola Coretti'
__email__ = '[email protected]'
31 changes: 31 additions & 0 deletions python/checkio/checkio/elementary/absolute_sorting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3
"""
Let's try some sorting. Here is an array with the specific rules.
The array (a tuple) has various numbers. You should sort it, but sort it by absolute value in ascending order.
For example, the sequence (-20, -5, 10, 15) will be sorted like so: (-5, 10, 15, -20).
Your function should return the sorted list or tuple.
Precondition: The numbers in the array are unique by their absolute values.
Input: An array of numbers , a tuple..
Output: The list or tuple (but not a generator) sorted by absolute values in ascending order.
Addition: The results of your function will be shown as a list in the tests explanation panel.
Precondition: len(set(abs(x) for x in array)) == len(array)
0 < len(array) < 100
all(isinstance(x, int) for x in array)
all(-100 < x < 100 for x in array)
"""


def checkio(numbers_array):
return sorted(numbers_array, key=abs)


if __name__ == '__main__':
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"
9 changes: 9 additions & 0 deletions python/checkio/checkio/elementary/binary_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def checkio(number):
return bin(number).count('1')

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(4) == 1
assert checkio(15) == 4
assert checkio(1) == 1
assert checkio(1022) == 9
26 changes: 26 additions & 0 deletions python/checkio/checkio/elementary/digits_multiplication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python3
"""
You are given a positive integer. Your function should calculate the product of the digits excluding any zeroes.
For example: The number given is 123405. The result will be 1*2*3*4*5=120 (don't forget to exclude zeroes).
Input: A positive integer.
Output: The product of the digits as an integer.
Precondition: 0 < number < 106
"""


def checkio(number):
number_as_string = "{}".format(number)
number = [int(digit, 10) for digit in number_as_string]
result = 1
for digit in number:
if digit != 0:
result *= digit
return result


if __name__ == '__main__':
assert checkio(123405) == 120
assert checkio(999) == 729
assert checkio(1000) == 1
assert checkio(1111) == 1
28 changes: 28 additions & 0 deletions python/checkio/checkio/elementary/even_last.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
"""
You are given an array of integers. You should find the sum of the elements with even indexes (0th, 2nd, 4th...)
then multiply this summed number and the final element of the array together. Don't forget that the first element
has an index of 0. For an empty array, the result will always be 0 (zero).
Input: A list of integers.
Output: The number as an integer.
Precondition: 0 ≤ len(array) ≤ 20
all(isinstance(x, int) for x in array)
all(-100 < x < 100 for x in array)
"""


def checkio(array):
"""
sums even-indexes elements and multiply at the last
"""
if len(array) == 0:
return 0
return sum([element[1] for element in enumerate(array) if (element[0] % 2) == 0]) * array[-1]


if __name__ == '__main__':
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"
39 changes: 39 additions & 0 deletions python/checkio/checkio/elementary/fizz_buzz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
"""
"Fizz buzz" is a word game we will use to teach the robots about division. Let's learn computers.
You should write a function that will receive a positive integer and return:
"Fizz Buzz" if the number is divisible by 3 and by 5;
"Fizz" if the number is divisible by 3;
"Buzz" if the number is divisible by 5;
The number as a string for other cases.
Input: A number as an integer.
Output: The answer as a string.
Precondition: 0 < number ≤ 1000
"""


def is_devisible_by(divisor, value):
return (value % divisor) == 0


def checkio(number):
format_str = "{}"
result = number
is_divisible_by_5 = is_devisible_by(5, number)
is_divisible_by_3 = is_devisible_by(3, number)
if is_divisible_by_5 and is_divisible_by_3:
result = "Fizz Buzz"
elif is_divisible_by_5:
result = "Buzz"
elif is_divisible_by_3:
result = "Fizz"
return format_str.format(result)


if __name__ == '__main__':
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"
33 changes: 33 additions & 0 deletions python/checkio/checkio/elementary/index_power.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
"""
You are given an array with positive numbers and a number N. You should find the N-th power of the element in the array
with the index N. If N is outside of the array, then return -1. Don't forget that the first element has the index 0.
Let's look at a few examples:
- array = [1, 2, 3, 4] and N = 2, then the result is 32 == 9;
- array = [1, 2, 3] and N = 3, but N is outside of the array, so the result is -1.
Input: Two arguments. An array as a list of integers and a number as a integer.
Output: The result as an integer.
Precondition: 0 < len(array) ≤ 10
0 ≤ N
all(0 ≤ x ≤ 100 for x in array)
"""


def index_power(array, n):
"""
Find Nth power of the element with index N.
"""
result = -1
try:
result = array[n] ** n
except IndexError as ex:
result = -1
return result


if __name__ == '__main__':
assert index_power([1, 2, 3, 4], 2) == 9, "Square"
assert index_power([1, 3, 10, 100], 3) == 1000000, "Cube"
assert index_power([0, 1], 0) == 1, "Zero power"
assert index_power([1, 2], 3) == -1, "IndexError"
33 changes: 33 additions & 0 deletions python/checkio/checkio/elementary/most_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
"""
Let's work with numbers.
You are given an array of numbers (floats). You should find the difference between the maximum and minimum element.
Your function should be able to handle an undefined amount of arguments. For an empty argument list,
the function should return 0. Floating-point numbers are represented in computer hardware as base 2 (binary) fractions.
So we should check the result with ±0.001 precision.
Think about how to work with an arbitrary number of arguments.
Input: An arbitrary number of arguments as numbers (int, float).
Output: The difference between maximum and minimum as a number (int, float).
Precondition: 0 ≤ len(args) ≤ 20
all(-100 < x < 100 for x in args)
all(isinstance(x, (int, float)) for x in args)
"""


def checkio(*args):
if not args:
return 0
return max(args) - min(args)


if __name__ == '__main__':
def almost_equal(checked, correct, significant_digits):
precision = 0.1 ** significant_digits
return correct - precision < checked < correct + precision


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"
30 changes: 30 additions & 0 deletions python/checkio/checkio/elementary/number_radix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python3
"""
Do you remember the radix and Numeral systems from math class? Let's practice with it.
You are given a positive number as a string along with the radix for it. Your function should convert it into decimal form.
The radix is less than 37 and greater than 1. The task uses digits and the letters A-Z for the strings.
Watch out for cases when the number cannot be converted. For example: "1A" cannot be converted with radix 9.
For these cases your function should return -1.
Input: Two arguments. A number as string and a radix as an integer.
Output: The converted number as an integer.
Precondition:
re.match("\A[A-Z0-9]\Z", str_number)
0 < len(str_number) ≤ 10
2 ≤ radix ≤ 36
"""


def checkio(str_number, radix):
try:
return int(str_number, radix)
except ValueError as ex:
return -1


if __name__ == '__main__':
assert checkio("AF", 16) == 175, "Hex"
assert checkio("101", 2) == 5, "Bin"
assert checkio("101", 5) == 26, "5 base"
assert checkio("Z", 36) == 35, "Z base"
assert checkio("AB", 10) == -1, "B > A > 10"
27 changes: 27 additions & 0 deletions python/checkio/checkio/elementary/right_to_left.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python3
"""
One of the robots is charged with a simple task: to join a sequence of strings into one sentence to produce
instructions on how to get around the ship. But this robot is left-handed and has a tendency to joke around and confuse
its right-handed friends. You are given a sequence of strings. You should join these strings into chunk of text where
the initial strings are separated by commas. As a joke on the right handed robots, you should replace all cases of the
words "right" with the word "left", even if it's a part of another word. All strings are given in lowercase.
Input: A sequence of strings as a tuple of strings (unicode).
Output: The text as a string.
Precondition:
0 < len(phrases) < 42
"""


def left_join(phrases):
"""
Join strings and replace "right" to "left"
"""
return ",".join(phrases).replace("right", "left")


if __name__ == '__main__':
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"
26 changes: 26 additions & 0 deletions python/checkio/checkio/elementary/secret_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python3
"""
Ever tried to send a secret message to someone without using the postal service? You could use newspapers to
tell your secret. Even if someone finds your message, it's easy to brush them off and that its paranoia and
a bogus conspiracy theory. One of the simplest ways to hide a secret message is to use capital letters.
Let's find some of these secret messages. You are given a chunk of text. Gather all capital letters in one word
in the order that they appear in the text. For example: text = "How are you? Eh, ok. Low or Lower? Ohhh.",
if we collect all of the capital letters, we get the message "HELLO".
Input: A text as a string (unicode).
Output: The secret message as a string or an empty string.
Precondition: 0 < len(text) ≤ 1000
all(ch in string.printable for ch in text)
"""


def find_message(text):
"""Find a secret message"""
result = [character for character in text if character.isupper()]
return "".join(result)


if __name__ == '__main__':
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"
40 changes: 40 additions & 0 deletions python/checkio/checkio/elementary/three_words.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
"""
Let's teach the Robots to distinguish words and numbers.
You are given a string with words and numbers separated by whitespaces (one space). The words contains only letters.
You should check if the string contains three words in succession. For example, the string "start 5 one two three 7 end"
contains three words in succession.
Input: A string with words.
Output: The answer as a boolean.
Precondition: The input contains words and/or numbers. There are no mixed words (letters and digits combined).
0 < len(words) < 100
"""


def is_word(string):
return not string.isdigit()


def checkio(words):
words = words.split(" ")
found_in_succession = 0
if len(words) < 3:
return False
for word in words:
if is_word(word):
found_in_succession += 1
if found_in_succession >= 3:
return True
else:
found_in_succession = 0
return False


if __name__ == '__main__':
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"

5 changes: 5 additions & 0 deletions python/checkio/checkio/home/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python3

__all__ = ['min_and_max', 'none_unique_elements']
__author__ = 'Nicola Coretti'
__email__ = '[email protected]'
Loading

0 comments on commit 79b7f5e

Please sign in to comment.