Skip to content

Commit c0bf780

Browse files
author
joseguilhermefmoura
committed
⬆️ Update files names
1 parent f330b40 commit c0bf780

File tree

17 files changed

+323
-0
lines changed

17 files changed

+323
-0
lines changed

01_Elementary/01.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def mult_two(a: int, b: int) -> int:
2+
return a * b

01_Elementary/02.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def easy_unpack(tpl: tuple) -> tuple:
2+
return (tpl[0], tpl[2], tpl[-2])

01_Elementary/03.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def first_word(line: str) -> str:
2+
return line.split()[0]

01_Elementary/04.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def is_acceptable_password(password:str) -> bool:
2+
return (lambda password : len(password) > 6)(password)

01_Elementary/05.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def number_length(a: int) -> int:
2+
return len(str(a))

01_Elementary/06.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def end_zeros(num: int) -> int:
2+
3+
sum = 0
4+
5+
for number in list(reversed(str(num))):
6+
if int(number) == 0:
7+
sum += 1
8+
else:
9+
return sum
10+
return sum

01_Elementary/07.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
# Backward String
3+
4+
## You should return a given string in reverse order.
5+
6+
- Input: A string.
7+
8+
- Output: A string.
9+
"""
10+
11+
def backward_string(val: str) -> str:
12+
return val [::-1]

01_Elementary/08.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
# Remove All Before
3+
4+
## Not all of the elements are important.
5+
## What you need to do here is to remove from the list all of the elements before the given one.
6+
## For the illustration we have a list [1, 2, 3, 4, 5] and we need to remove all elements
7+
## that go before 3 - which is 1 and 2.
8+
9+
## We have two edge cases here: (1) if a cutting element cannot be found,
10+
## then the list shoudn't be changed. (2) if the list is empty, then it should remain empty.
11+
12+
- Input: List and the border element.
13+
14+
- Output: Iterable (tuple, list, iterator ...).
15+
"""
16+
17+
18+
def remove_all_before(items: list, border: int) -> list:
19+
if border not in items:
20+
return items
21+
else:
22+
return items[items.index(border):]
23+
24+
25+
# These "asserts" are used for self-checking and not for an auto-testing
26+
assert list(remove_all_before([1, 2, 3, 4, 5], 3)) == [3, 4, 5]
27+
assert list(remove_all_before([1, 1, 2, 2, 3, 3], 2)) == [2, 2, 3, 3]
28+
assert list(remove_all_before([1, 1, 2, 4, 2, 3, 4], 2)) == [2, 4, 2, 3, 4]
29+
assert list(remove_all_before([1, 1, 5, 6, 7], 2)) == [1, 1, 5, 6, 7]
30+
assert list(remove_all_before([], 0)) == []
31+
assert list(remove_all_before([7, 7, 7, 7, 7, 7, 7, 7, 7], 7)) == [7, 7, 7, 7, 7, 7, 7, 7, 7]

01_Elementary/09.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
# All Upper I
3+
4+
## Check if a given string has all symbols in upper case.
5+
## If the string is empty or doesn't have any letter in it - function should return True.
6+
7+
- Input: A string.
8+
9+
- Output: a boolean.
10+
"""
11+
12+
13+
def is_all_upper(text: str) -> bool:
14+
return (lambda text : text.upper() == text)(text)
15+
16+
17+
# These "asserts" are used for self-checking and not for an auto-testing
18+
assert is_all_upper('ALL UPPER') == True
19+
assert is_all_upper('all lower') == False
20+
assert is_all_upper('mixed UPPER and lower') == False
21+
assert is_all_upper('') == True

01_Elementary/10.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
# Replace First
3+
4+
## In a given list the first element should become the last one.
5+
## An empty list or list with only one element should stay the same.
6+
7+
- Input: List.
8+
9+
- Output: Iterable.
10+
"""
11+
12+
13+
def replace_first(items: list) -> list:
14+
if not items:
15+
return []
16+
else:
17+
items.append(items[0])
18+
return items[1:]
19+
20+
# These "asserts" are used for self-checking and not for an auto-testing
21+
assert list(replace_first([1, 2, 3, 4])) == [2, 3, 4, 1]
22+
assert list(replace_first([1])) == [1]
23+
assert list(replace_first([])) == []

01_Elementary/11.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
# Max Digit
3+
4+
## You have a number and you need to determine which digit in this number is the biggest.
5+
6+
- Input: A positive int.
7+
8+
- Output: An Int (0-9).
9+
"""
10+
11+
12+
def max_digit(numbers: int) -> int:
13+
return sorted([int(number) for number in str(numbers)])[-1]
14+
15+
16+
# These "asserts" are used for self-checking and not for an auto-testing
17+
assert max_digit(0) == 0
18+
assert max_digit(52) == 5
19+
assert max_digit(634) == 6
20+
assert max_digit(1) == 1
21+
assert max_digit(10000) == 1

01_Elementary/12.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
# Split Pairs
3+
4+
## Split the string into pairs of two characters.
5+
## If the string contains an odd number of characters, then the missing second
6+
## character of the final pair should be replaced with an underscore ('_').
7+
8+
- Input: A string.
9+
10+
- Output: An iterable of strings.
11+
"""
12+
13+
14+
def split_pairs(a):
15+
if len(a) % 2 == 0:
16+
return [a[i : i + 2] for i in range(0, len(a), 2)]
17+
else:
18+
a = a + '_'
19+
return split_pairs(a)
20+
21+
22+
# These "asserts" are used for self-checking and not for an auto-testing
23+
assert list(split_pairs('abcd')) == ['ab', 'cd']
24+
assert list(split_pairs('abc')) == ['ab', 'c_']
25+
assert list(split_pairs('abcdf')) == ['ab', 'cd', 'f_']
26+
assert list(split_pairs('a')) == ['a_']
27+
assert list(split_pairs('')) == []

01_Elementary/13.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
# Beginning Zeros
3+
4+
## You have a string that consist only of digits.
5+
## You need to find how many zero digits ("0") are at the beginning of the given string.
6+
7+
- Input: A string, that consist of digits.
8+
9+
- Output: An Int.
10+
"""
11+
12+
def beginning_zeros(number: str) -> int:
13+
sum = 0
14+
15+
for number in list(str(number)):
16+
if int(number) == 0:
17+
sum += 1
18+
else:
19+
return sum
20+
return sum
21+
22+
23+
# These "asserts" are used for self-checking and not for an auto-testing
24+
assert beginning_zeros('100') == 0
25+
assert beginning_zeros('001') == 2
26+
assert beginning_zeros('100100') == 0
27+
assert beginning_zeros('001001') == 2
28+
assert beginning_zeros('012345679') == 1
29+
assert beginning_zeros('0000') == 4

01_Elementary/14.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
# Nearest Value
3+
4+
## Find the nearest value to the given one.
5+
## You are given a list of values as set form and a value for which you need to find the nearest one.
6+
## For example, we have the following set of numbers: 4, 7, 10, 11, 12, 17,
7+
## and we need to find the nearest value to the number 9.
8+
## If we sort this set in the ascending order, then to the left of number 9 will be number 7 and
9+
## to the right - will be number 10.
10+
## But 10 is closer than 7, which means that the correct answer is 10.
11+
12+
## A few clarifications:
13+
## -> If 2 numbers are at the same distance, you need to choose the smallest one;
14+
## -> The set of numbers is always non-empty, i.e. the size is >=1;
15+
## -> The given value can be in this set, which means that it’s the answer;
16+
## -> The set can contain both positive and negative numbers, but they are always integers;
17+
## -> The set isn’t sorted and consists of unique numbers.
18+
19+
- Input: Two arguments. A list of values in the set form. The sought value is an int.
20+
21+
- Output: Int.
22+
"""
23+
24+
25+
def nearest_value(values: set, one: int) -> int:
26+
if one in values:
27+
return one
28+
29+
minor_distance = 1
30+
31+
while True:
32+
if (one - minor_distance) in values:
33+
return (one - minor_distance)
34+
elif (one + minor_distance) in values:
35+
return (one + minor_distance)
36+
else:
37+
minor_distance += 1
38+
39+
40+
# These "asserts" are used for self-checking and not for an auto-testing
41+
assert nearest_value({4, 7, 10, 11, 12, 17}, 9) == 10
42+
assert nearest_value({4, 7, 10, 11, 12, 17}, 8) == 7
43+
assert nearest_value({4, 8, 10, 11, 12, 17}, 9) == 8
44+
assert nearest_value({4, 9, 10, 11, 12, 17}, 9) == 9
45+
assert nearest_value({4, 7, 10, 11, 12, 17}, 0) == 4
46+
assert nearest_value({4, 7, 10, 11, 12, 17}, 100) == 17
47+
assert nearest_value({5, 10, 8, 12, 89, 100}, 7) == 8
48+
assert nearest_value({-1, 2, 3}, 0) == -1

01_Elementary/15.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
# Between Markers (simplified)
3+
4+
## You are given a string and two markers (the initial one and final).
5+
## You have to find a substring enclosed between these two markers.
6+
7+
## But there are a few important conditions.
8+
## -> The initial and final markers are always different.
9+
## -> The initial and final markers are always 1 char size.
10+
## -> The initial and final markers always exist in a string and go one after another.
11+
12+
- Input: Three arguments. All of them are strings.
13+
The second and third arguments are the initial and final markers.
14+
15+
- Output: A string.
16+
"""
17+
18+
19+
def between_markers(text: str, begin: str, end: str) -> str:
20+
return text[text.find(begin) + 1:text.find(end)]
21+
22+
23+
# These "asserts" are used for self-checking and not for an auto-testing
24+
assert between_markers('What is >apple<', '>', '<') == "apple"
25+
assert between_markers('What is [apple]', '[', ']') == "apple"
26+
assert between_markers('What is ><', '>', '<') == ""
27+
assert between_markers('>apple<', '>', '<') == "apple"

01_Elementary/16.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
# Correct Sentence
3+
4+
## For the input of your function, you will be given one sentence.
5+
## You have to return a corrected version, that starts with a capital letter
6+
## and ends with a period (dot).
7+
8+
## Pay attention to the fact that not all of the fixes are necessary.
9+
## If a sentence already ends with a period (dot), then adding another one will be a mistake.
10+
11+
- Input: A string.
12+
- Output: A string.
13+
14+
>> EXAMPLE:
15+
16+
correct_sentence("greetings, friends") == "Greetings, friends."
17+
correct_sentence("Greetings, friends") == "Greetings, friends."
18+
correct_sentence("Greetings, friends.") == "Greetings, friends."
19+
20+
<<
21+
"""
22+
23+
24+
def correct_sentence(text: str) -> str:
25+
if text[-1] != '.':
26+
text += '.'
27+
return text[0].upper() + text[1:]
28+
else:
29+
return text[0].upper() + text[1:]
30+
31+
32+
# These "asserts" are used for self-checking and not for an auto-testing
33+
assert correct_sentence("greetings, friends") == "Greetings, friends."
34+
assert correct_sentence("Greetings, friends") == "Greetings, friends."
35+
assert correct_sentence("Greetings, friends.") == "Greetings, friends."
36+
assert correct_sentence("hi") == "Hi."
37+
assert correct_sentence("welcome to New York") == "Welcome to New York."

01_Elementary/17.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
# Is Even
3+
4+
## Check if the given number is even or not.
5+
## Your function should return True if the number is even, and False if the number is odd.
6+
7+
- Input: An int.
8+
- Output: A bool.
9+
10+
>> EXAMPLE:
11+
12+
is_even(2) == True
13+
is_even(5) == False
14+
is_even(0) == True
15+
16+
<<
17+
"""
18+
19+
20+
def is_even(num: int) -> bool:
21+
return bool(num % 2 == 0)
22+
23+
24+
# These "asserts" are used for self-checking and not for an auto-testing
25+
assert is_even(2) == True
26+
assert is_even(5) == False
27+
assert is_even(0) == True

0 commit comments

Comments
 (0)