From 59c0da9f0ef441a18187fc37395e620e1b9facbf Mon Sep 17 00:00:00 2001 From: Yohaan Date: Tue, 20 Oct 2020 03:54:42 +0000 Subject: [PATCH] Palindrome problem --- day2_palindrome.py | 16 ++++++++++++++++ main.py | 28 +++++++++++++--------------- 2 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 day2_palindrome.py diff --git a/day2_palindrome.py b/day2_palindrome.py new file mode 100644 index 0000000..ed8987c --- /dev/null +++ b/day2_palindrome.py @@ -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 diff --git a/main.py b/main.py index 5dc0cac..ed8987c 100644 --- a/main.py +++ b/main.py @@ -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) \ No newline at end of file +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