Created
September 1, 2015 11:55
-
-
Save kodekracker/fbf2c0d2599011b3dc83 to your computer and use it in GitHub Desktop.
Screening Solutions
This file contains hidden or 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
| #! /usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # 1) Write a program to illustrate recursion for the following: | |
| # * Whether a word is a pallindrome or not. | |
| def isWordPalindrome(word): | |
| """ | |
| Checks whether a word is palindrome or not | |
| """ | |
| if len(word) < 2: | |
| return True | |
| if word[0] != word[-1]: | |
| return False | |
| return isWordPalindrome(word[1:-1]) | |
| if __name__=='__main__': | |
| word = str(raw_input()) | |
| if isWordPalindrome(word): | |
| print "Entered word is palindrome." | |
| else: | |
| print "Entered word is not palindrome." | |
| # * Calculate the factorial of every digit of your birth date written in | |
| # dd.mm.yy | |
| # ex: for 01.05.91 the result should be 11.1120.3628801 | |
| def factorial(n): | |
| """ | |
| Return a factorial of a number | |
| """ | |
| return 1 if (n < 1) else n * factorial(n-1) | |
| def getDigitFactorialString(number): | |
| """ | |
| Return a factorial string of each digit in a number | |
| """ | |
| digits_fact = [str(factorial(int(n))) for n in str(number)] | |
| return ''.join(digits_fact) | |
| if __name__=='__main__': | |
| # get Birthdate in dd.mm.yy format | |
| birthDate = str(raw_input()) | |
| dd, mm, yy = birthDate.split('.') | |
| result = '.'.join([ | |
| getDigitFactorialString(dd), | |
| getDigitFactorialString(mm), | |
| getDigitFactorialString(yy) | |
| ]) | |
| print result | |
| # 2) Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for | |
| # the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". | |
| def main(): | |
| for i in range(1, 101): | |
| print i, | |
| if i%3==0 and i%5==0: | |
| print 'FizzBuzz' | |
| elif i%3==0: | |
| print 'Fizz' | |
| elif i%5==0: | |
| print 'Buzz' | |
| else: | |
| print i | |
| if __name__=='__main__': | |
| main() | |
| # 3) Write a function that determines if a string starts with an upper-case letter A-Z. | |
| def checksString(s): | |
| """ | |
| Checks string starts with an upper-case letter A-Z or not | |
| """ | |
| return s.split(' ')[0].istitle() | |
| if __name__=='__main__': | |
| word = str(raw_input()) | |
| if checksString(word): | |
| print 'String starts with an upper-case letter A-Z' | |
| else: | |
| print 'String does not starts with an upper-case letter A-Z' | |
| # 4) Write code, that will flatten an array of arbitrarily nested arrays of | |
| # integers into a flat array of integers. | |
| # ex: [[1,2,[3]],4] -> [1,2,3,4]. | |
| from compiler.ast import flatten | |
| def flattenNestedArray(arr): | |
| """ | |
| Flatten a nested array into a single list | |
| """ | |
| return flatten(arr) | |
| if __name__=='__main__': | |
| arr_ex = [0, [1, 2], [3, 4, [5, 6]], 7] | |
| print flattenNestedArray(arr_ex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment