Posts

Showing posts from November, 2019

Chapter 10: Data Structures - II: Stacks and Queues

Chapter 10: Data Structures - II: Stacks and Queues Type C: Programming Practice/ Knowledge based Questions 1. Write a program that depending upon user's choice, either pushes or pops an element in a stack. 2. A line of text is read from the input terminal into a stack. Write a program to output the string in the reverse order, each character appearing twice. ( e.g., the string a b c d e should be changed to ee dd cc bb aa ) 3. Write a program that depending upon user's choice, either pushes or pops an element in a stack. The elements are shifted towards right so that top always remains at 0th (zeroth) index. (N.B: If the elements are shifted downwards or towards right side, then unused free spaces are available at the beginning of the list otherwise free space is available at the end of the list) 4. Write a program to insert or delete an element from a queue depending upon user's choice. The elements are not shifted after insertion or d...

Chapter 6: Recursion

Chapter 6: Recursion Type C: Programming Practice/Knowledge based Questions 1. Write a function that takes a number and tests if it is a prime number using recursion technique. def primeOrNot(num,n): if n>=2: if (num%n)==0: return False else: return primeOrNot(number,n-1) elif num==1: return False else: return True number=eval(input("Enter a number: ")) if primeOrNot(number,number-1): print("The number is a prime number") else: print("The number is not a prime number") 2. Implement a function product() to multiply 2 numbers recursively using + and - operators only. def product( x , y ): # if x is less than y swap # the numbers if x < y: return product(y, x) # iteratively calculate y # times sum of x elif y != 0: return (x + product(x, y - 1)) # if any of the two nu...

Chapter 09: Data Structures - I: Linear Lists

Chapter 09: Data Structures - I: Linear Lists Type C: Programming Practice/Knowledge based Questions 1. Write a program that uses a function called find_in_list() to check for the position of the first occurrence of v in the list passed as parameter(lst) or -1 if not found. The header for the function is given below: def find_in_list(lst, v): """ lst - a list       v   - a value that may or may not be in the list""" def find_in_list(lst, v): i=0 for i in range(len(lst)): if lst[i]==v: return i else: return -1 n=int(input('Enter desired linear-list size(max 50) ')) print('enter elements for linear list') lst=[0]*n for i in range(n): lst[i]=int(input('Element '+str(i)+': ')) item=int(input('Pls insert item to search ')) index=find_in_list(lst,item) if index>0: print('item found at index ',index) else: print('item not found...