Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Nothing Special solution in Clear category for Split Pairs by undead2k
'''
My own thinking and notes, if wrong, please correct me! Thanks! :D
Notes:
- if our input looks like; a = "abcde"
if not len(a) % 2 == 0:
#Odd number
a = a + "_"
- We check if the length of "a" can not be divided by 2 without any remainders. If it has remainders, this makes it an Odd number
- if it is an odd number, we add the required underscore to the end of the string to make it an even number
for i in range(0,len(a),2):
- range = 0, 6 (length of "a" with the underscore), 2 (number of jumps for the next iteration)
- this makes i = 0, 2, 4 (based on each iteration)
- Stops at 4 because the next iteration is the stopping point which it doesnt action.
yield a[i:i+2]
- we use a slice to capture just what we need;
- because of the for loop above, i increments on each iteration by 2 and stops at 4
- so we have a[0, a[2 and a[4
- then we manually +2 to i at the end of the slicing which gives us; :2], :4], :6]
- yield allows us to resume where we were after each iteration
'''
def split_pairs(a):
if not len(a) % 2 == 0:
# Odd number
a = a + "_"
for i in range(0, len(a), 2):
yield a[i:i+2]
April 27, 2020
Comments: