






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
some basic level python programs for beginners
Typology: Exercises
Uploaded on 02/06/2018
1 / 12
This page cannot be seen from the preview
Don't miss anything!







def fact(x): if x == 0: return 1 return x * fact(x - 1) print fact(x)
OR def fact(x): if x == 0: return 1 else: return x * fact(x - 1) print fact(x)
>>> print fact(4) 24
def power(a,b): if b==0: return 1 elif a==0: return 0 elif b==1: return a else: return a*power(a,b-1) FOR O/P-print(power(3,4))
3-sum of digits def sumDigits(n): if n == 0: return 0 else: return n % 10 + sumDigits(int(n / 10))
>>> print(sumDigits(345))
12 >>> print(sumDigits(45)) 9
>>> print(fibonacci(7)) 13 >>> print(fibonacci(5)) 5 6-SNAKE TO CAMEL >>> def snake_to_camel(word): import re return ''.join(x.capitalize() or '' for x in word.split(''))
>>> print(snake_to_camel('python_exercises')) PythonExercises 7- sum def sum(x) : if x==1: return 1 else : return x + sum (x-1) >print sum(10) [55] Airthmatic mean
def mean(numbers): return float(sum(numbers)) / max(len(numbers), 1) mean((1,2,3,4))
avg = lambda l : reduce(lambda x, y: x + y, l) / float(len(l)) avg([1,2,3,4])
rsum = lambda m, n : reduce(lambda x, y: x + y, range(m, n+1))
rsum(2,10) 54
function
sd = lambda l: (reduce(lambda x, y: x+y, map(lambda x: (x - a(l)) ** 2, l)) / float(len(l))) ** 0.
Binary search def binary_search(array, target): lower = 0 upper = len(array) while lower < upper: x = lower + (upper - lower) // 2 val = array[x] if target == val: return x elif target > val: if lower == x: break lower = x elif target < val: upper = x
>>> binary_search([1,5,8,10], 5) 1 >>> binary_search([1,5,8,10], 0) ////retrun none >>> binary_search([1,5,8,10], 15)
Decimal to binary >>> def decToBin(n):
if n==0: return '' else: return decToBin(n/2) + str(n%2)
>>> decToBin(7) '111'
("coconut")) substitute = lambda x, y, l: map(lambda z: y if z == x else z, l)
SELECTION SORT >>> a = [16, 19, 11, 15, 10, 12, 14] >>> i = 0
>>> while i def merge_sort(array): ret = [] if( len(array) == 1): return array; half = len(array) / 2 lower = merge_sort(array[:half]) upper = merge_sort(array[half:]) lower_len = len(lower) upper_len = len(upper) i = 0 j = 0 while i != lower_len or j != upper_len: if( i != lower_len and (j == upper_len or lower[i] < upper[j])): ret.append(lower[i]) i += 1 else: ret.append(upper[j]) j += 1
return ret
>>> array = [4, 2, 3, 8, 8, 43, 6,1, 0]
>>> ar = merge_sort(array)
>>> print " ".join(str(x) for x in ar) 0 1 2 3 4 6 8 8 43
74 >>> def invertdict(list): """a function invertdict to interchange keys and values in a dictionary. For simplicity, assume that all values are unique >>> invertdict({'x': 1, 'y': 2, 'z': 3}) {1: 'x', 2: 'y', 3: 'z'} """ return dict([(v,k) for k,v in list.items()])
>>> print invertdict({'x': 1, 'y': 2, 'z': 3}) {1: 'x', 2: 'y', 3: 'z'}
75 >>> def anagrams(list): """a program to find anagrams in a given list of words. Two words are called anagrams if one word can be formed by rearranging letters of another. For example 'eat', 'ate' and 'tea' are anagrams. >>> anagrams(['eat', 'ate', 'done', 'tea', 'soup', 'node']) [['eat', 'ate', 'tea'], ['done', 'node'], ['soup']] """ anag = []