Skip to content

Commit 2ae096c

Browse files
author
Amogh Singhal
authored
Update Python_Programming_Quiz.md
1 parent ba31036 commit 2ae096c

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Python_Programming_Quiz.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,51 @@ Note: __Use lambda functions when an anonymous function is required for a short
435435
#### 27. What are iterators in Python ?
436436

437437
#### 28. What is the difference between an iterator and a generator ?
438+
439+
#### 29. What do you know about palindromes? Can you implement one in Python?
440+
441+
A palindrome is a phrase, a word, or a sequence that reads the same forward and backward. <br>
442+
One such example will be _pip_ An example of such a phrase will be _‘nurses run’_.
443+
444+
__Normal Function Implementation:__
445+
446+
```
447+
def isPalindrome(string):
448+
left, right = 0, len(string)-1
449+
while right >= left:
450+
if not string[left] == string[right]:
451+
return False
452+
left+=1;right-=1
453+
return True
454+
455+
isPalindrome('redrum murder')
456+
# returns True
457+
458+
isPalindrome('CC.')
459+
# returns False
460+
```
461+
462+
__Iterator Implementation:__
463+
464+
```
465+
def isPalindrome(string):
466+
left, right = iter(string), iter(string[::-1])
467+
i=0
468+
while i < len(string)/2:
469+
if next(left)!=next(right):
470+
return False
471+
i+=1
472+
return True
473+
474+
isPalindrome('redrum murder')
475+
# prints True
476+
477+
isPalindrome('CC.')
478+
# prints False
479+
480+
isPalindrome('CCC.')
481+
# prints False
482+
483+
isPalindrome('CCC')
484+
# prints True
485+
```

0 commit comments

Comments
 (0)