Skip to content

Instantly share code, notes, and snippets.

@Komet-Kazi
Created January 19, 2021 02:31
Show Gist options
  • Select an option

  • Save Komet-Kazi/2e59a0cad3e9f762b78be0451b5dbe87 to your computer and use it in GitHub Desktop.

Select an option

Save Komet-Kazi/2e59a0cad3e9f762b78be0451b5dbe87 to your computer and use it in GitHub Desktop.
Checkio
# split_pairs.py --> Split the string into pairs of two characters.
# If the string contains an odd number of characters, then the missing second character of the final pair should be replaced with an underscore ('_').
# Input: A string.
# Output: An iterable of strings.
# Precondition: 0<=len(str)<=100
def split_pairs(a: str):
# find length of string. check if odd. if odd append "_" to the list
stringSize = len(a)
isOdd = bool(stringSize % 2)
# add a "_" to string if odd else just assign string
balanced = list(a + "_") if isOdd else list(a)
# Build list of successive 2-letter substrings
pairs = []
for chi in range(0, len(balanced), 2):
pair = "".join(balanced[chi : chi + 2])
pairs.append(pair)
return pairs
if __name__ == "__main__":
print("Example:")
print(list(split_pairs("asfsdfabd")))
# Check the list length is always even
# These "asserts" are used for self-checking and not for an auto-testing
assert list(split_pairs("abcd")) == ["ab", "cd"]
assert list(split_pairs("abc")) == ["ab", "c_"]
assert list(split_pairs("abcdf")) == ["ab", "cd", "f_"]
assert list(split_pairs("a")) == ["a_"]
assert list(split_pairs("")) == []
print("Coding complete? Click 'Check' to earn cool rewards!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment