Posts

Swapping variables in python

 Swapping variables in python Explanation: Assignment(=) in any programming language takes place from right to left so what happens behind the scene? Here in line 9 ; x,y = y,x the swapping takes place the right side i.e left of = signs are evaluated first so y which has the value of second number is assigned to x and the variable x which has the value of first number gets assigned to y.

SQL Basics

Image
SQL Basics Viewing existing databases: To view existing databases use : SHOW DATABASES;               Creating A Database:    To create a new database use: CREATE DATABASE [IF NOT EXISTS]  database_name ; The database_name is the name of the desired database we want to create, which should be meaningful and descriptive. The IF NOT EXISTS is an optional statement which gives an error while creating a new database if it already exists. (But if the IF NOT EXISTS clause is used in MariaDB it will return a warning instead of an error if the specified database already exists.) Eg. CREATE DATABASE JOHNDRIPPER;  Now if we run SHOW DATABASES; again we will see the new database johndripper there. Selecting a Database: To select a database use the query: USE database_name; Eg. USE JOHNDRIPPER;   Removing a Database: Removing a database deletes the entire data and all its related objects associated with it permanently therefore us...

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...

Chapter 05 - File Handling

Chapter 05 - File Handling Type C: Programming Practice/ Knowledge based Questions 1. Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space. 2. A file sports.dat contains information in following format: Event ~ Participant. Write a function that would read contents from file sports.dat and create a file named Atheletic.dat copying only those records from sports.dat where the event name is "Athletics". 3. A file contains a list of telephone numbers in the following form: Arvind 7258031 Sachin 7259197 The names contain only one word the names and telephone numbers are separated by white spaces. Write a program to read a file and display its contents in two columns. 4. Write a program to count the words 'to' and 'the' present in a text file "Poem.txt". 5.Write a program to count the number of upper-case alphabets ...

Chapter 03 - Working With Functions

Chapter 03 - Working With Functions 1. Write a function that takes amount in dollars and dollar to rupees conversion price; it then returns the amount converted to rupees. Create the function in both void and non-void forms. YOUR CODE HERE 2.Write a function to calculate volume of a box with appropriate default values for its parameters. Your function should have the following input parameters: (a) length of box; (b) width of box; (c) height of box. Test it by writing complete program to invoke it. YOUR CODE HERE 3. Write a program to have following functions: (i) a function that takes a number as argument and calculates cube for it. The function does not return a value. If there is no value passed to the function in function call, the function should calculate cube of 2. def CubeOfNumber(user_input=2): print('Cube of',user_input,'is',user_input**3) user_input=eval(input("Please inse...