-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord.py
67 lines (59 loc) · 2.14 KB
/
Word.py
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
64
65
66
67
"""
TODO: Make a class that will identify a word that is a palindrome. For example:
HANNAH is a palindrome since it is spelled the same way backwards as forwards.
DAMIEN is not. That's cause I'm cooler :P (jk)
"""
class Word():
"""
This is a Word class. You can make mulitple instances/objects of it (aka multiple words).
For example:
>>> hannah = Word('hannah')
>>> damien = Word('damien')
>>> hannah.reverse()
>>> damien.reverse()
>>> print hannah.getWord()
hannah
>>> print damien.getWord()
neimad
Classes have public methods and private methods. Private methods are denoted by the underscore on the front (aka _).
Private methods should not be called outside of the class. So notice in the above example we didn't call
hannah._reverse('hannah') because that's a private method. Notice that reverse() is allowed to call _reverse() because
it's inside the class.
"""
def __init__(self, word):
"""
This takes in a string and stores it in the private attribute _word.
"""
self._word = word
def getWord(self):
return self._word
def _reverse(self, word):
"""
This reverses the order of the letters in a word and returns the reversed word.
"""
reversed_word = ""
for position in range(len(word)-1, -1, -1):
reversed_word = reversed_word + word[position]
return reversed_word
def reverse(self):
"""
This reverses the order of the letters in the word.
"""
self._word = self._reverse(self._word)
def is_palindrom(self):
"""
This returns True if the word is a palindrome and False otherwise.
"""
# WRITE YOUR CODE HERE
# if palindrome
return True
# else
return False
if __name__ == '__main__':
first_name = input("Please enter first name:")
name = Word(first_name)
name.reverse()
print("Did you know your name spelled backwards is {}?".format(name.getWord()))
name.reverse() # reverse it back
if name.is_palindrom():
print("Did you know your name is a palindrome?")