Finishing 13 elements

This commit is contained in:
2020-01-02 17:30:27 +01:00
parent a615255dfa
commit f81e9d7ae3
13 changed files with 436 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
def count_inversion(sequence):
"""
Count inversions in a sequence of numbers
"""
sequence = list(sequence)
ordered_sequence = sorted(sequence)
inversions = 0
for index, number in enumerate(ordered_sequence):
if sequence.index(number) == index:
continue
inversions += sequence.index(number) - index
sequence.insert(index, sequence.pop(sequence.index(number)))
return inversions
if __name__ == '__main__':
print("Example:")
print(count_inversion([1, 2, 5, 3, 4, 7, 6]))
# These "asserts" using only for self-checking and not necessary for auto-testing
assert count_inversion((1, 2, 5, 3, 4, 7, 6)) == 3, "Example"
assert count_inversion((0, 1, 2, 3)) == 0, "Sorted"
assert count_inversion((5, 3, 2, 1, 0)) == 10, "Reversed"
assert count_inversion((99, -99)) == 1, "Two numbers"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

20
IceBase/cut-sentence.py Normal file
View File

@@ -0,0 +1,20 @@
def cut_sentence(line, length):
"""
Cut a given sentence, so it becomes shorter than or equal to a given length.
"""
if length >= len(line):
return line
if line[length] == " ":
return line[:length]+"..."
line = line[:length].split(" ")
line.pop()
return " ".join(line)+"..."
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert cut_sentence("Hi my name is Alex", 4) == "Hi...", "First"
assert cut_sentence("Hi my name is Alex", 8) == "Hi my...", "Second"
assert cut_sentence("Hi my name is Alex", 18) == "Hi my name is Alex", "Third"
assert cut_sentence("Hi my name is Alex", 20) == "Hi my name is Alex", "Fourth"
print('Done! Do you like it? Go Check it!')

View File

@@ -0,0 +1,38 @@
def steps_to_convert(line1, line2):
len_line1 = len(line1)
len_line2 = len(line2)
if line1 == "" or line2 == "":
return len_line1 if len_line1 != 0 else len_line2
similarities = has_similar(line1, line2)
if len(similarities) > 0:
return abs(max(len_line1, len_line2) - len(similarities))
else:
if len_line1 < len_line2:
if line1 in line2:
return len_line2 - len_line1
else:
if line2 in line1:
return len_line1 - len_line2
return 0
def has_similar(line1, line2):
similarities = []
for index, letter in enumerate(line1):
if letter == line2[index]:
similarities.append(index)
return similarities
if __name__ == "__main__":
# These "asserts" using only for self-checking and not necessary for auto-testing
assert steps_to_convert('line1', 'line1') == 0, "eq"
assert steps_to_convert('line1', 'line2') == 1, "2"
assert steps_to_convert('line', 'line2') == 1, "none to 2"
assert steps_to_convert('ine', 'line2') == 2, "need two more"
assert steps_to_convert('line1', '1enil') == 4, "everything is opposite"
assert steps_to_convert('', '') == 0, "two empty"
assert steps_to_convert('l', '') == 1, "one side"
assert steps_to_convert('', 'l') == 1, "another side"
print("You are good to go!")