Assignment 36: Functions-5

  1. Write a Python function to remove duplicate elements from a given list. (TSRS)

  2. Write a Python function to count the frequency of each element of a list and store the list elements as keys and their frequencies as values in a dictionary. (TSRS)

  3. Write a Python function to find numbers in a given text, store the numbers in a list and return the list. (TSRS)

  4. Write a Python function to find the largest sorted subsequence in a given list. Return the largest subsequence as a list. (TSRS)

  5. Write a Python function to check if two given lists have the same elements in any order. Return True or False. (TSRS)

def remove_duplicates(x):
  return set(x)
remove_duplicates([10])
{10}
remove_duplicates(10)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_1557/2204634587.py in <cell line: 0>()
----> 1 remove_duplicates(10)

/tmp/ipykernel_1557/3244903296.py in remove_duplicates(x)
      1 def remove_duplicates(x):
----> 2   return set(x)

TypeError: 'int' object is not iterable
remove_duplicates('10')
{'0', '1'}
def remove_duplicates(x):
  return {x}
remove_duplicates('10')
{'10'}
remove_duplicates(10)
{10}
dummy_list = [1,2,3,1,2]
set(dummy_list)
{1, 2, 3}

def dict_num_freq(x):
  dummy_set = set(x)
  return {ke:sum([True for el in dummy_list
                  if el==ke]) for ke in dummy_set}
dict_num_freq(dummy_list)
{1: 2, 2: 2, 3: 1}
text = "5 mangoes and 3 apples"
def extract_nums(any_text):
  return [num for num in text if num.isdigit()]
extract_nums(text)
['5', '3', '3']
text = "53 mangoes and 3 apples"
extract_nums(text)
['5', '3', '3']
text.split()
['53', 'mangoes', 'and', '3', 'apples']
def extract_nums(any_text):
  return [num for num in text.split() if num.isdigit()]
extract_nums(text)
['53', '3']
seq_of_digits = [1,2,3,2,3,4,5,6,7,8,9]
ans_list
[1]
def sorted_seq(seq_of_digits):
  ans_list = []
  ans_list.append(seq_of_digits[0])
  best_list = []
  for ele in seq_of_digits[1:]:
    if ele>ans_list[-1]:
      ans_list.append(ele)
    else:
      best_list = ans_list
      ans_list = [ele]
    if len(ans_list)>len(best_list):
      best_list = ans_list
  return best_list
sorted_seq(seq_of_digits)
[2, 3, 4, 5, 6, 7, 8, 9]
ans_list, best_list
([2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3])
[1,2,3], [3,2,1]
([1, 2, 3], [3, 2, 1])
sorted([3,2,1])==sorted([1,2,3])
True

Assignment 37: Recursion-1

  1. Write a recursive function to print the first N natural numbers.

  2. Write a recursive function to print the first N natural numbers in reverse order.

  3. Write a recursive function to print the first N odd natural numbers.

  4. Write a recursive function to print the first N odd natural numbers in reverse order.

  5. Write a recursive function to print MySirG N times on the screen.

def summation(n):
  return n and n+summation(n-1)
summation(5)
15
def summation(n):
  print(n)
  print(2*n)
  print(2*n+1)
  print(n**2)
  print(n**3)
  print("mysirg")
  n and summation(n-1)
summation(10)
10
20
21
100
1000
mysirg
9
18
19
81
729
mysirg
8
16
17
64
512
mysirg
7
14
15
49
343
mysirg
6
12
13
36
216
mysirg
5
10
11
25
125
mysirg
4
8
9
16
64
mysirg
3
6
7
9
27
mysirg
2
4
5
4
8
mysirg
1
2
3
1
1
mysirg
0
0
1
0
0
mysirg
def summation(n):
  n and summation(n-1)
  if  n%2==0:
    print(n)
  else:
    print(n)

  print(n)
summation(10)
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10

Assignment 38: Recursion-2

  1. Write a recursive function to print the first N even natural numbers.

  2. Write a recursive function to print the first N even natural numbers in reverse order.

  3. Write a recursive function to print squares of the first N natural numbers.

  4. Write a recursive function to print cubes of the first N natural numbers.

  5. Write a recursive function to print the reverse of a given number.

123–>321

int(str(123)[::-1])
321
def rever_num(num):
  num>1 and rever_num(num//10)
  print(num%10,end="")
rever_num(123)
123

Assignment 39: Recursion-3

  1. Write a recursive function to calculate the sum of the first N natural numbers.

  2. Write a recursive function to calculate the sum of the first N odd natural numbers.

  3. Write a recursive function to calculate the sum of the first N even natural numbers.

  4. Write a recursive function to calculate the sum of squares of the first N natural numbers.

  5. Write a recursive function to calculate the sum of cubes of the first N natural numbers.

def summation(n):
  return n and n**3+summation(n-1)
summation(3)
36