File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -435,3 +435,51 @@ Note: __Use lambda functions when an anonymous function is required for a short
435
435
#### 27. What are iterators in Python ?
436
436
437
437
#### 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
+ ```
You can’t perform that action at this time.
0 commit comments