Functions Code#

  • Please solve the following questions using Python code.  

Question-1#

Write a function that accepts an integer as its parameter and prints an \(n \times n\) square with ‘*’ characters.

Solution


def square_star(n):
    
    for i in range(n):
        print('* '*n)

Question-2#

Write a function that takes a string as its parameter and returns the vowels (‘a, e, i, o, u’) in the given string without repetition as a list.

  • If there are no vowels in the string, return the string ‘No vowels’ .

Solution


def vowel_func(text):
  
  result = list(set(text) & {'a', 'e', 'i', 'o', 'u'})
    
  if len(result) == 0:
    return 'No vowels!'
  else:
    return result

Sample Output

print(vowel_func(‘abbbecido’))
[‘e’, ‘i’, ‘a’, ‘o’]
print(vowel_func(‘bcd’))
No vowels!

Question-3#

Write a function that takes two integers as parameters and returns the powers of the first integer (base) from 0 to the second integer number.

  • Example: parameters are 3, 5

    • Output: \([3^0, 3^1, 3^2, 3^3, 3^4, 3^5]\)

Solution-1

def power_func(a, b):
  return [a**i for i in range(b+1)]

Sample Output

print(power_func(3,5))
[1, 3, 9, 27, 81, 243]

Solution-2

def f(base, exponent):
  ans = [] 
    
  for i in range(exponent+1):
    ans.append(base**i)
      
  return ans

Sample Output

print(f(3,5))
[1, 3, 9, 27, 81, 243]

Question-4#

Write a function with parameters start and end, where it returns a list of all even numbers between start and end, inclusive.

  • If start is greater than end, the output should be in descending order; if start is less than end, the output should be in ascending order.

  • Example:

    • start=11, end=23 —> Output: [12, 14, 16, 18, 20, 22]

    • start=20, end= 5 —> Output: [20, 18, 16, 14, 12, 10, 8, 6]

Solution-1

def even_numbers(start, end):
  even_list = []

  reverse = False
    
  if start > end:
    start, end = end, start
    reverse = True
    
    
  for i in range(start, end+1):
    if i % 2 == 0:
      even_list.append(i)
  
  if reverse:    
    even_list.reverse()
  
  return even_list

Sample Output

print(even_numbers(45, 23))
[44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24]

Solution-2

def even_num(start, end):
    if start > end:
        return list(range(start, end-1, -2))
    else:
        return list(range(start, end+1, 2))

Sample Output

print(even_num(20,5))
[20, 18, 16, 14, 12, 10, 8, 6]

Question-5#

Write a function that takes two tuples as parameters and returns a tuple consisting of elements present in the first tuple but not in the second tuple.

  • Example:

    • tuple1 = [1, 5, 9, 12, 6], tuple2 = [2, 7, 6, 9, 20, 23, 29] —> Output: [1, 5, 12]

    • tuple1 = [1,3], tuple2 = [2, 7] —> Output: [1, 3]

Solution-1

def diff_tuples(tuple1, tuple2):
  list_ = []
    
  for i in tuple1:
    if i not in tuple2:
      list_.append(i)
        
  return list_

Sample Output

print(diff_tuples( (1,2,3,4), (3,4,5,6) ))
[1, 2]

Solution-2


def diff_tuples(tuple1, tuple2):
  set1 = set(tuple1)  # use sets
  set2 = set(tuple2)
  return tuple(set1 - set2)

Sample Output

print(diff_tuples( (1,2,3,4), (3,4,5,6) )) (1, 2)

Question-6#

Write a function that takes a date as a string in the format ‘month/day/year’, where the month is represented by a number.

  • The function should return the date in the format ‘year/month/day’.

  • Example:

    • Input : ‘09/25/1987’

    • Output: ‘1987/09/25’

Solution

def date_conv(date):
    
  d = date.split('/')
  return d[-1] + '/' + d[0] + '/' +d[1]

Sample Output

print(date_conv(‘09/25/1987’))
1987/09/25

Question-7#

Write a function that takes a list as its parameter and returns the standard deviation of its elements.
The standard deviation (std) for a sequence is calculated using the formula: \(\displaystyle std = \sqrt\frac{\sum_{i=1}^{n}(x_i-\bar{x})^2}{n}\)

where \(\bar{x}\) is the mean and \(\sum\) denotes the sum. Here’s a hint for the implementation:

  1. Find the mean.

  2. Subtract mean from each element.

  3. Square the diferences.

  4. Sum the squared differences.

  5. Divide by n

  6. Take square root

  • Find the std of the list: [5,8,2,5,6,1,1,3]

  • Compare the result obtained from your custom standard deviation function with the standard deviation functions provided by the numpy and statistics libraries.

Solution


import numpy as np
import statistics


def std_func(data_list):
  mean = np.mean(data_list)
  deviation = [i-mean for i in data_list]
  dev_square = [i**2 for i in deviation]
  sum_square = sum(dev_square)
  average_sum = sum_square/len(data_list)
  std = np.sqrt(average_sum)
  return std

Sample Output mylist = [5,8,2,5,6,1,1,3]

print(f’Custom Function: {std_func(mylist)}’)
print(f’Numpy : {np.std(mylist)}’)
print(f’Statistics : {statistics.pstdev(mylist)}’)

Custom Function: 2.368411915187052
Numpy : 2.368411915187052
Statistics : 2.368411915187052