From b245bdc90a739277366d517db3a7429878cf5554 Mon Sep 17 00:00:00 2001 From: Meg2tron Date: Sun, 15 Oct 2017 23:00:07 +0530 Subject: [PATCH] added lapindrome checker --- exercises/Lapindrome/example.py | 8 +++++ exercises/Lapindrome/lapindrome.py | 58 ++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 exercises/Lapindrome/example.py create mode 100644 exercises/Lapindrome/lapindrome.py diff --git a/exercises/Lapindrome/example.py b/exercises/Lapindrome/example.py new file mode 100644 index 00000000000..3818a8ad768 --- /dev/null +++ b/exercises/Lapindrome/example.py @@ -0,0 +1,8 @@ +import lapindrome as lap + +st=str(input("Enter String:")) +A=lap.lapindrome(st) +if A.chk_lapindrome(): + print("lapindrome") +else: + print("not lapindrome") diff --git a/exercises/Lapindrome/lapindrome.py b/exercises/Lapindrome/lapindrome.py new file mode 100644 index 00000000000..10c73197cd4 --- /dev/null +++ b/exercises/Lapindrome/lapindrome.py @@ -0,0 +1,58 @@ +class lapindrome(): + # Constructor for accepting string + def __init__(self,string): + self.string=string + pass + #Function to check Lapindrome + def chk_lapindrome(self): + l=len(self.string) + if l == 1: + return None + elif l%2==0: + return self.lapin_for_even(l) + else: + return self.lapin_for_odd(l) + pass + ''' +for length is even string is divided + into two equal parts and + then check the frequency of characters''' + def lapin_for_even(self,length): + l = length // 2 + #slicing the string in two equal half + first= self.string[0 : l] + second= self.string[l:] + #sorted both first and second strings + first_sorted = sorted(first) + sec_sorted = sorted(second) + #list to string conversion + first = "".join(first_sorted) + second = "".join(sec_sorted) + + if first == second: + return True + else: + return False + pass + ''' +for length is odd string is divided + into two parts i.e. first 0 to length/2 and second length/2 + 1 to l + then check the frequency of characters''' + def lapin_for_odd(self,length): + l = length // 2 + first= self.string[0 : l] + second= self.string[l+1:] + + first_sorted = sorted(first) + sec_sorted = sorted(second) + + first = "".join(first_sorted) + second = "".join(sec_sorted) + + if first == second: + return True + else: + return False + pass + + pass