Skip to content

Commit

Permalink
Palindrome problem
Browse files Browse the repository at this point in the history
  • Loading branch information
yozaam committed Oct 20, 2020
1 parent 73e1fac commit 59c0da9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
16 changes: 16 additions & 0 deletions day2_palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Required concepts: list slicing https://www.w3schools.com/python/ref_func_slice.asp

print('Question: check if a string is a palindrome')

word1 = "hello"
word2 = "level"
#now I want the reverse word
reverse_word1 = word1[::-1]
reverse_word2 = word2[::-1]
#i got the reverse by slicing with a step of -1, so basically from the last element upto the first, negative step value means right to left
#like for(int i = n ; i>=0 ; i--)

print('Answer:')
print(word1,' is ',word1 == reverse_word1)
print(word2,' is ',word2 == reverse_word2)
#If you are same as your reverse, you are a palindrome
28 changes: 13 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
countOfChar = {
#store the count of all chars
#as a pair
#eg. 'a' : 5
# 'b' : 6
}
# Required concepts: list slicing https://www.w3schools.com/python/ref_func_slice.asp

string = "hello rashil"
print('Question: check if a string is a palindrome')

for char in string:
if char in countOfChar:
countOfChar[char]+=1
#he has already been found just increment him
else:
countOfChar[char] = 1
#he was not found so i initilized him
word1 = "hello"
word2 = "level"
#now I want the reverse word
reverse_word1 = word1[::-1]
reverse_word2 = word2[::-1]
#i got the reverse by slicing with a step of -1, so basically from the last element upto the first, negative step value means right to left
#like for(int i = n ; i>=0 ; i--)

print(countOfChar)
print('Answer:')
print(word1,' is ',word1 == reverse_word1)
print(word2,' is ',word2 == reverse_word2)
#If you are same as your reverse, you are a palindrome

0 comments on commit 59c0da9

Please sign in to comment.