Last active
June 17, 2018 15:11
-
-
Save olekthunder/1ddf41c2c9795bb2e25d8fd047798071 to your computer and use it in GitHub Desktop.
Print squared words to stdout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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