Created
August 12, 2021 15:20
-
-
Save hodunov/ac8d7e851a2f6ce6dc6905adcbcdd62f to your computer and use it in GitHub Desktop.
Homework 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from timeit import timeit | |
print("list() = ", timeit("list()")) | |
print("[]", timeit("[]")) | |
my_list = list() | |
my_second_list = [] | |
my_dict = dict() | |
my_second_dict = {} | |
print("*" * 10) | |
# 8. Дана строка my_str. Разделите эту строку на пары из двух символов и поместите эти пары в список. | |
# Если строка содержит нечетное количество символов, пропущенный второй символ последней пары должен | |
# быть заменен подчеркиванием ('_'). Примеры: 'abcd' -> ['ab', 'cd'], 'abcde' -> ['ab', 'cd', e_'] | |
# (используйте срезы длинны 2) | |
# EASY | |
my_str = "abcdefg" | |
result = [] | |
for index, value in enumerate(my_str): | |
if index % 2 == 0: | |
couple = my_str[index:index+2] | |
if len(couple)>1: | |
result.append(couple) | |
else: | |
result.append(value+"_") | |
print("EASY", result) | |
# MEDIUM | |
example = 'abcde' | |
solution = [example[index:index+2].ljust(2, '_') for index in range(0,len(example),2)] | |
print("MEDIUM", solution) | |
# EXTRA HARD | |
res = list(map(''.join, zip(*[iter(my_str + '_')]*2))) | |
print("HARD", res) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment