Last active
March 18, 2016 00:04
-
-
Save arlk/4451a3b0576d1bc1f718 to your computer and use it in GitHub Desktop.
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
__author__ = "Arun Lakshmanan" | |
class fibbonaci(object): | |
def __init__(self): | |
""" This class generates the first n Fibbonnaci numbers F(n) and modifies it in the following ways: | |
- ... "Buzz" when F(n) is divisible by 3. | |
- ... "Fizz" when F(n) is divisible by 5. | |
- ... "BuzzFizz" when F(n) is prime. | |
- ... the value F(n) otherwise. | |
Example to generate the first 10 Fibbonaci numbers with the modifications mentioned above: | |
>>seqFoo = fibbonaci() | |
>>seqFoo.populate_seq(10) | |
>>newFoo = seqFoo.find_result() | |
""" | |
self.fibbSeq = [0,1] | |
def populate_seq(self, n): | |
""" Generate a Fibbonaci sequence of length n""" | |
for i in range(n-2): | |
self.fibbSeq.append(self.fibbSeq[-1]+self.fibbSeq[-2]) | |
def _is_prime(self,val): | |
""" Return | |
- ... True for prime numbers | |
- ... False for composite numbers """ | |
for div in range(2, 1+int(val**0.5)): | |
if val % div == 0: | |
return False | |
return True | |
def _check_prime(self): | |
""" Populate a boolean list by checking for primes in the fibbonaci sequence """ | |
primes = [] | |
flag = False | |
for elem in self.fibbSeq: | |
if elem >= 2: | |
flag=self._is_prime(elem) | |
primes.append(flag) | |
return primes | |
def _check_div_3(self): | |
""" Populate a boolean list by checking for numbers divisible by 3 in the fibbonaci sequence """ | |
return list(map(lambda x: True if x%3==0 else False, self.fibbSeq)) | |
def _check_div_5(self): | |
""" Populate a boolean list by checking for numbers divisible by 5 in the fibbonaci sequence """ | |
return list(map(lambda x: True if x%5==0 else False, self.fibbSeq)) | |
def find_result(self): | |
""" Return a list with required modifications """ | |
resultSeq = self.fibbSeq | |
resultSeq = ["Buzz" if flag else resultSeq[i] for i,flag in enumerate(self._check_div_3())] | |
resultSeq = ["Fizz" if flag else resultSeq[i] for i,flag in enumerate(self._check_div_5())] | |
resultSeq = ["BuzzFizz" if flag else resultSeq[i] for i,flag in enumerate(self._check_prime())] | |
return resultSeq | |
if __name__=='__main__': | |
mySeq = fibbonaci() | |
length = int(input("What length of Fibbonacci sequence do you require?")) | |
mySeq.populate_seq(length) | |
print(mySeq.find_result()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment