Skip to content

Instantly share code, notes, and snippets.

@mys721tx
Last active December 29, 2017 00:10
Show Gist options
  • Save mys721tx/b311112ac066533cab513ff3ed99d477 to your computer and use it in GitHub Desktop.
Save mys721tx/b311112ac066533cab513ff3ed99d477 to your computer and use it in GitHub Desktop.
Mirror Image

The problem can be solved by using a stack.

  1. We push everything in the stack.
  2. We pop everything off the stack

The running time of this algorithm is bounded by the stack operations. We can achieve O(n).

"""
mirror_image.py
"""
def mirror(string):
"""
mirror takes a str string.
mirror returns a str that is the reversed string.
"""
reverse = []
for character in string:
reverse.append(character)
output = ""
while reverse:
output += reverse.pop()
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment