-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse.py
More file actions
63 lines (49 loc) · 1.48 KB
/
reverse.py
File metadata and controls
63 lines (49 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python
import sys
import getopt
def reverse(word):
"""
Prints out the provided word in reverse.
Example: If provided word = 'fragile', this method should print:
original: fragile
reversed: eligarf
"""
print "original: %s" % word
# Break string into array.
my_list = list(word)
# Initiate reversed list of letters
reversed_list = []
# Get length of word
length = len(my_list);
# Append list in reverse order
for x in range(length):
reversed_list.append(my_list[length-x-1])
# Put array back into string
reversed_word = ''.join(reversed_list)
# Code form original solution
# # Break string into pieces
# my_list = list(word)
# # Reverse order of new list
# reversed_list = my_list[::-1]
# # Rejoin list into string
# new_word = ''.join(reversed_list)
print "reversed: %s" % reversed_word
def main():
# parse command line options
try:
opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
except getopt.error, msg:
print msg
print "for help use --help"
sys.exit(2)
# process options
for o, a in opts:
if o in ("-h", "--help"):
print __doc__
sys.exit(0)
# process arguments
for arg in args:
reverse(arg)
print '-------------------'
if __name__ == "__main__":
main()