Skip to content

Instantly share code, notes, and snippets.

@olekthunder
Last active June 17, 2018 15:11
Show Gist options
  • Select an option

  • Save olekthunder/1ddf41c2c9795bb2e25d8fd047798071 to your computer and use it in GitHub Desktop.

Select an option

Save olekthunder/1ddf41c2c9795bb2e25d8fd047798071 to your computer and use it in GitHub Desktop.
Print squared words to stdout
#!/usr/bin/python3
def print_spaced(string, reversed=False):
char_list = list(string)
if reversed:
char_list.reverse()
print(" ".join(char_list))
def main(string):
words = string.split()
for w in words:
word_len = (len(w) - 1) * 2
# Print first line
print_spaced(w)
# Print body of square (letter + spaces + letter)
for idx in range(1, len(w)-1):
print(w[idx] + " " * (word_len - 1) + w[-idx-1])
# Print last line
print_spaced(w, reversed=True)
# Print newline to indent words
print()
if __name__ == "__main__":
main("hello world")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment