Posts

Showing posts from February, 2019

Calculate average value of dictionaries

Question:- We have a list of annual rainfall recordings of cities. Each element in the list is of the form  (c,r)  where  c  is the city and  r  is the annual rainfall for a particular year. The list may have multiple entries for the same city, corresponding to rainfall recordings in different years. Write a Python function  rainaverage(l)  that takes as input a list of rainfall recordings and computes the avarage rainfall for each city. The output should be a list of pairs  (c,ar)  where  c  is the city and  ar  is the average rainfall for this city among the recordings in the input list. Note that  ar  should be of type float. The output should be sorted in dictionary order with respect to the city name. Here are some examples to show how  rainaverage(l)  should work. >>> rainaverage([(1,2),(1,3),(2,3),(1,1),(3,8)]) [(1, 2.0), (2, 3.0), (3, 8.0)] >>> rainaverage([('Bomb...

Alternating prime and square

Question:- Write a Python function  squareprime(l)  that takes a nonempty list of integers and returns  True  if the elements of  l alternate between perfect squares and prime numbers, and returns  False  otherwise. Note that the alternating sequence of squares and primes may begin with a square or with a prime. Here are some examples to show how your function should work. >>> primesquare([4]) True >>> primesquare([4,5,16,101,64]) True >>> primesquare([5,16,101,36,27]) False Solution:- import math def prime_check (n): flag = 0 for i in range ( 2 ,n - 1 ): if n % i == 0 : flag = 1 if flag == 0 : return True else : return False def is_square (n): if math . sqrt(n) % 1 == 0 : return True else : return False def primesquare (l): flag = 0 if len (l) == 1 : if is_square(l[ 0 ]) or prime_check(...

Flip the matrix

Question. A two dimensional matrix can be represented in Python row-wise, as a list of lists: each inner list represents one row of the matrix. For instance, the matrix 1 2 3 4 5 6 7 8 9 would be represented as  [[1, 2, 3], [4, 5, 6], [7, 8, 9]] . A horizonatal flip reflects each row. For instance, if we flip the previous matrix horizontally, we get 3 2 1 6 5 4 9 8 7 which would be represented as  [[3, 2, 1], [6, 5, 4], [9, 8, 7]] . A vertical flip reflects each column. For instance, if we flip the previous matrix that has already been flipped horizontally, we get 9 8 7 6 5 4 3 2 1 which would be represented as  [[9, 8, 7], [6, 5, 4], [3, 2, 1]] . Write a Python function  matrixflip(m,d)  that takes as input a two dimensional matrix  m  and a direction  d , where  d  is either  'h'  or  'v' . If  d == 'h' , the function should return the matrix flipped horizontally. If  d ==...

Rotating a list

Question. A list rotation consists of taking the first element and moving it to the end. For instance, if we rotate the list  [1,2,3,4,5] , we get  [2,3,4,5,1] . If we rotate it again, we get  [3,4,5,1,2] . Write a Python function  rotatelist(l,k)  that takes a list  l  and a positive integer  k  and returns the list  l  after  k  rotations. If  k  is not positive, your function should return  l  unchanged. Note that your function should not change  l  itself, and should return the rotated list. Here are some examples to show how your function should work. >>> rotatelist([1,2,3,4,5],1) [2, 3, 4, 5, 1] >>> rotatelist([1,2,3,4,5],3) [4, 5, 1, 2, 3] >>> rotatelist([1,2,3,4,5],12) [3, 4, 5, 1, 2] Solution:- If the number of times to be rotated is greater than the length of list then we start iterating from n % size value till the end and append the el...

Maximum depth of nested brancket

Question. Write a function  nestingdepth(s)  that takes as input a string  s  and computes the maximum nesting depth of brackets if  s  has properly nested brackets. If the string is not properly matched, your function should return  -1 . Hint: Use the function  matched()  from the practice assignment. Here are some examples to show how your function should work. >>> nestingdepth("zb%78")  0   >>> nestingdepth("(7)(a")  -1   >>> nestingdepth("a)*(?")  -1  >>> nestingdepth("((jkl)78(A)&l(8(dd(FJI:),):)?)")  4 Solution:- If the string has valid brackets, I'm checking for the maximun number of "(". def matched (s): a = 0 for i in range ( 0 , len (s)): if s[i] == '(' : if a >= 0 : a = a + 1 elif s[i] == ')' : a = a - 1 if a == 0 : return True else : return ...

Prime partitioning a number

Question:- A positive integer m can be partitioned as primes if it can be written as p + q where p > 0, q > 0 and both p and q are prime numbers. Write a Python function  primepartition(m)  that takes an integer  m  as input and returns  True  if  m  can be partitioned as primes and  False  otherwise. (If  m  is not positive, your function should return  False .) Here are some examples of how your function should work. >>> primepartition(7) True >>> primepartition(185) False >>> primepartition(3432) True Solution:- The idea behind the solution is that I'm computing all the prime numbers till the given number and appending to a list . Then I'm iterating over the list and subtracting it from the given number, if the subtracted value is prime then I'm returning True, otherwise after the loop ends returning False def primes (n): l = [] for i in range ( 2 ,n)...