-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
29 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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 |