Skip to content

Instantly share code, notes, and snippets.

@Otumian-empire
Last active July 6, 2019 10:52
Show Gist options
  • Save Otumian-empire/d4461e9060b46e1ebd6eb0869c624664 to your computer and use it in GitHub Desktop.
Save Otumian-empire/d4461e9060b46e1ebd6eb0869c624664 to your computer and use it in GitHub Desktop.
Given an out string of lenghth, 4 such as "<<>>" and a word. Return a new string where by word is in the middle of the out string. Example, <<word>>
# a program that inserts a str, "word" in the middle of another str, "out"
# this code make_out_word is very limit, i'd say won't even work
def make_out_word(out, word):
nstr = ""
leno = len(out)
if len(out) == 4: # replace with len(out) % 2 == 0
for i in range(leno):
if i == 2: # replace with i == leno // 2
nstr += str(word)
nstr += out[i]
print(nstr)
# this works for only even numbers of "out"
def make_out_word_1(out, word):
nstr = ""
leno = len(out)
if len(out) % 2 == 0:
for i in range(leno):
if i == leno // 2:
nstr += str(word)
nstr += out[i]
print(nstr)
# A very simple version of the above code and works for all length of out
def make_out_word_2(out, word):
new_str = ""
len_of_out = len(out)
new_str = out[0:len_of_out // 2] + word + out[len_of_out // 2 :]
print(new_str)
# change the string "out" to test your output
out = "<<>#<>>"
word = 'word'
make_out_word(out, word)
make_out_word_1(out, word)
make_out_word_2(out, word)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment