Skip to content

Instantly share code, notes, and snippets.

@Nditah
Created April 2, 2018 10:44
Show Gist options
  • Save Nditah/0aa548490b330ee4ec2bc4ebbf16b718 to your computer and use it in GitHub Desktop.
Save Nditah/0aa548490b330ee4ec2bc4ebbf16b718 to your computer and use it in GitHub Desktop.
GooglesPythonClassString2 created by Nditah - https://repl.it/@Nditah/GooglesPythonClassString2
# F. front_back
# Consider dividing a string into two halves.
# If the length is even, the front and back halves are the same length.
# If the length is odd, we'll say that the extra char goes in the front half.
# e.g. 'abcde', the front half is 'abc', the back half 'de'.
# Given 2 strings, a and b, return a string of the form
# a-front + b-front + a-back + b-back
def front_back(a, b):
af = len(a)//2
if len(a)%2 == 1: af += 1
bf = len(b)//2
if len(b)%2 == 1: bf += 1
print (af , bf)
return a[:af] + b[:bf] + a[af:] + b[bf:]
# Simple provided test() function used in main() to print
# what each function returns vs. what it's supposed to return.
def test(got, expected):
if got == expected:
prefix = ' OK '
else:
prefix = ' X '
print ('%s got: %s expected: %s' % (prefix, repr(got), repr(expected)))
print(front_back("dinner","Pythonian"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment