Python Interview Coding Question

 



Basic program using python 

1)Program to print multiplication table for given no.

Solution-

# Program published on https://beginnersbook.com


# Python Program to Print Multiplication Table of a Number


num = int(input("Enter the number: "))


print("Multiplication Table of", num)

for i in range(1, 11):

   print(num,"X",i,"=",num * i)


Output:

Enter the number: 6

Multiplication Table of 6

6 X 1 = 6

6 X 2 = 12

6 X 3 = 18

6 X 4 = 24

6 X 5 = 30

6 X 6 = 36

6 X 7 = 42

6 X 8 = 48

6 X 9 = 54

6 X 10 = 60



2) )Program to check whether the given no is prime or not

Solution-

# Python program to check if the input number is prime or not

num = 407

# take input from the user

# num = int(input("Enter a number: "))

# prime numbers are greater than 1

if num > 1:

   # check for factors

   for i in range(2,num):

       if (num % i) == 0:

           print(num,"is not a prime number")

           print(i,"times",num//i,"is",num)

           break

   else:

       print(num,"is a prime number")

    

# if input number is less than

# or equal to 1, it is not prime

else:

   print(num,"is not a prime number")

Output

407 is not a prime number

11 times 37 is 407



3)Program to find factorial of the given no 

Solution-


# Python program to find the factorial of a number provided by the user.

# change the value for a different result

num = 7

# uncomment to take input from the user

#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero

if num < 0:

   print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

   print("The factorial of 0 is 1")

else:

   for i in range(1,num + 1):

       factorial = factorial*i

   print("The factorial of",num,"is",factorial)

Output

The factorial of 7 is 5040



4)Program to Illusrate different set operations

Solution-

# Program to perform different set operations like in mathematics

# define three sets

E = {0, 2, 4, 6, 8};

N = {1, 2, 3, 4, 5};

# set union

print("Union of E and N is",E | N)

# set intersection

print("Intersection of E and N is",E & N)

# set difference

print("Difference of E and N is",E - N)

# set symmetric difference

print("Symmetric difference of E and N is",E ^ N)

Output

Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8}

Intersection of E and N is {2, 4}

Difference of E and N is {8, 0, 6}

Symmetric difference of E and N is {0, 1, 3, 5, 6, 8}



5)Program to implement  Depth first Search Traversal

Solution-

# Python3 program to print DFS traversal  

# from a given given graph 

from collections import defaultdict 

  

# This class represents a directed graph using 

# adjacency list representation 

class Graph: 

  

    # Constructor 

    def __init__(self): 

  

        # default dictionary to store graph 

        self.graph = defaultdict(list) 

  

    # function to add an edge to graph 

    def addEdge(self, u, v): 

        self.graph[u].append(v) 

  

    # A function used by DFS 

    def DFSUtil(self, v, visited): 

  

        # Mark the current node as visited  

        # and print it 

        visited[v] = True

        print(v, end = ' ') 

  

        # Recur for all the vertices  

        # adjacent to this vertex 

        for i in self.graph[v]: 

            if visited[i] == False: 

                self.DFSUtil(i, visited) 

  

    # The function to do DFS traversal. It uses 

    # recursive DFSUtil() 

    def DFS(self, v): 

  

        # Mark all the vertices as not visited 

        visited = [False] * (len(self.graph)) 

  

        # Call the recursive helper function  

        # to print DFS traversal 

        self.DFSUtil(v, visited) 

  

# Driver code 

  

# Create a graph given  

# in the above diagram 

g = Graph() 

g.addEdge(0, 1) 

g.addEdge(0, 2) 

g.addEdge(1, 2) 

g.addEdge(2, 0) 

g.addEdge(2, 3) 

g.addEdge(3, 3) 

  

print("Following is DFS from (starting from vertex 2)") 

g.DFS(2)


Output:

Following is Depth First Traversal (starting from vertex 2)

2 0 1 3

 



6)Program to Implement Breadth first search Transversal

Solution-

# Python3 Program to print BFS traversal 

# from a given source vertex. BFS(int s) 

# traverses vertices reachable from s. 

from collections import defaultdict 

  

# This class represents a directed graph 

# using adjacency list representation 

class Graph: 

  

    # Constructor 

    def __init__(self): 

  

        # default dictionary to store graph 

        self.graph = defaultdict(list) 

  

    # function to add an edge to graph 

    def addEdge(self,u,v): 

        self.graph[u].append(v) 

  

    # Function to print a BFS of graph 

    def BFS(self, s): 

  

        # Mark all the vertices as not visited 

        visited = [False] * (len(self.graph)) 

  

        # Create a queue for BFS 

        queue = [] 

  

        # Mark the source node as  

        # visited and enqueue it 

        queue.append(s) 

        visited[s] = True

  

        while queue: 

  

            # Dequeue a vertex from  

            # queue and print it 

            s = queue.pop(0) 

            print (s, end = " ") 

  

            # Get all adjacent vertices of the 

            # dequeued vertex s. If a adjacent 

            # has not been visited, then mark it 

            # visited and enqueue it 

            for i in self.graph[s]: 

                if visited[i] == False: 

                    queue.append(i) 

                    visited[i] = True

  

# Driver code 

  

# Create a graph given in 

# the above diagram 

g = Graph() 

g.addEdge(0, 1) 

g.addEdge(0, 2) 

g.addEdge(1, 2) 

g.addEdge(2, 0) 

g.addEdge(2, 3) 

g.addEdge(3, 3) 

  

print ("Following is Breadth First Traversal"

                  " (starting from vertex 2)") 

g.BFS(2) 


Output:

Following is Breadth First Traversal (starting from vertex 2)

2 0 3 1


  

7)Program to Implement water jug Problem

Solution-

# This function is used to initialize the  

# dictionary elements with a default value. 

from collections import defaultdict 

  

# jug1 and jug2 contain the value  

# for max capacity in respective jugs  

# and aim is the amount of water to be measured.  

jug1, jug2, aim = 4, 3, 2

  

# Initialize dictionary with  

# default value as false. 

visited = defaultdict(lambda: False) 

  

# Recursive function which prints the  

# intermediate steps to reach the final  

# solution and return boolean value  

# (True if solution is possible, otherwise False). 

# amt1 and amt2 are the amount of water present  

# in both jugs at a certain point of time. 

def waterJugSolver(amt1, amt2):  

  

    # Checks for our goal and  

    # returns true if achieved. 

    if (amt1 == aim and amt2 == 0) or (amt2 == aim and amt1 == 0): 

        print(amt1, amt2) 

        return True

      

    # Checks if we have already visited the 

    # combination or not. If not, then it proceeds further. 

    if visited[(amt1, amt2)] == False: 

        print(amt1, amt2) 

      

        # Changes the boolean value of 

        # the combination as it is visited.  

        visited[(amt1, amt2)] = True

      

        # Check for all the 6 possibilities and  

        # see if a solution is found in any one of them. 

        return (waterJugSolver(0, amt2) or

                waterJugSolver(amt1, 0) or

                waterJugSolver(jug1, amt2) or

                waterJugSolver(amt1, jug2) or

                waterJugSolver(amt1 + min(amt2, (jug1-amt1)), 

                amt2 - min(amt2, (jug1-amt1))) or

                waterJugSolver(amt1 - min(amt1, (jug2-amt2)), 

                amt2 + min(amt1, (jug2-amt2)))) 

      

    # Return False if the combination is  

    # already visited to avoid repetition otherwise 

    # recursion will enter an infinite loop. 

    else: 

        return False

  

print("Steps: ") 

  

# Call the function and pass the 

# initial amount of water present in both jugs. 

waterJugSolver(0, 0) 


Output:

Steps: 

0 0

4 0

4 3

0 3

3 0

3 3

4 2

0 2


8)Program to Implement K nearest Neigbor algorithm

Solution-

import unicodecsv

import random

import operator

import math

 

#getdata() function definition

def getdata(filename):

    with open(filename,'rb') as f:

        reader = unicodecsv.reader(f)

        return list(reader)

 

#random train test data split function definition

def shuffle(i_data):

    random.shuffle(i_data)

    train_data = i_data[:int(0.7*30)]

    test_data = i_data[int(0.7*30):]

    return train_data, test_data

 

def euclideanDist(x, xi):

    d = 0.0

    for i in range(len(x)-1):

        d += pow((float(x[i])-float(xi[i])),2)  #euclidean distance

    d = math.sqrt(d)

    return d

 

#KNN prediction and model training

def knn_predict(test_data, train_data, k_value):

    for i in test_data:

        eu_Distance =[]

        knn = []

        good = 0

 

        bad = 0

        for j in train_data:

            eu_dist = euclideanDist(i, j)

            eu_Distance.append((j[5], eu_dist))

            eu_Distance.sort(key = operator.itemgetter(1))

            knn = eu_Distance[:k_value]

            for k in knn:

                if k[0] =='g':

                    good += 1

                else:

                    bad +=1

        if good > bad:

            i.append('g')

        elif good < bad:

            i.append('b')

        else:

            i.append('NaN')

 

#Accuracy calculation function

def accuracy(test_data):

    correct = 0

    for i in test_data:

        if i[5] == i[6]:

            correct += 1

    accuracy = float(correct)/len(test_data) *100  #accuracy 

    return accuracy

 

dataset = getdata('i_data_sample_30.csv')  #getdata function call with csv file as parameter

train_dataset, test_dataset = shuffle(dataset) #train test data split

K = 5                                          # Assumed K value

knn_predict(test_dataset, train_dataset, K)   

print test_dataset

print "Accuracy : ",accuracy(test_dataset)


 

9)Program to Implement regression algorithm

Solution-

import numpy as np 

import matplotlib.pyplot as plt 

  

def estimate_coef(x, y): 

    # number of observations/points 

    n = np.size(x) 

  

    # mean of x and y vector 

    m_x, m_y = np.mean(x), np.mean(y) 

  

    # calculating cross-deviation and deviation about x 

    SS_xy = np.sum(y*x) - n*m_y*m_x 

    SS_xx = np.sum(x*x) - n*m_x*m_x 

  

    # calculating regression coefficients 

    b_1 = SS_xy / SS_xx 

    b_0 = m_y - b_1*m_x 

  

    return(b_0, b_1) 

  

def plot_regression_line(x, y, b): 

    # plotting the actual points as scatter plot 

    plt.scatter(x, y, color = "m", 

               marker = "o", s = 30) 

  

    # predicted response vector 

    y_pred = b[0] + b[1]*x 

  

    # plotting the regression line 

    plt.plot(x, y_pred, color = "g") 

  

    # putting labels 

    plt.xlabel('x') 

    plt.ylabel('y') 

  

    # function to show plot 

    plt.show() 

  

def main(): 

    # observations 

    x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 

    y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12]) 

  

    # estimating coefficients 

    b = estimate_coef(x, y) 

    print("Estimated coefficients:\nb_0 = {}  \ 

          \nb_1 = {}".format(b[0], b[1])) 

  

    # plotting regression line 

    plot_regression_line(x, y, b) 

  

if __name__ == "__main__": 

    main() 


Output :

Estimated coefficients:

b_0 = -0.0586206896552

b_1 = 1.45747126437


10)Program to Implement Random forest algorithm

Solution-

# Random Forest Algorithm on Sonar Dataset

from random import seed

from random import randrange

from csv import reader

from math import sqrt

 

# Load a CSV file

def load_csv(filename):

dataset = list()

with open(filename, 'r') as file:

csv_reader = reader(file)

for row in csv_reader:

if not row:

continue

dataset.append(row)

return dataset

 

# Convert string column to float

def str_column_to_float(dataset, column):

for row in dataset:

row[column] = float(row[column].strip())

 

# Convert string column to integer

def str_column_to_int(dataset, column):

class_values = [row[column] for row in dataset]

unique = set(class_values)

lookup = dict()

for i, value in enumerate(unique):

lookup[value] = i

for row in dataset:

row[column] = lookup[row[column]]

return lookup

 

# Split a dataset into k folds

def cross_validation_split(dataset, n_folds):

dataset_split = list()

dataset_copy = list(dataset)

fold_size = int(len(dataset) / n_folds)

for i in range(n_folds):

fold = list()

while len(fold) < fold_size:

index = randrange(len(dataset_copy))

fold.append(dataset_copy.pop(index))

dataset_split.append(fold)

return dataset_split

 

# Calculate accuracy percentage

def accuracy_metric(actual, predicted):

correct = 0

for i in range(len(actual)):

if actual[i] == predicted[i]:

correct += 1

return correct / float(len(actual)) * 100.0

 

# Evaluate an algorithm using a cross validation split

def evaluate_algorithm(dataset, algorithm, n_folds, *args):

folds = cross_validation_split(dataset, n_folds)

scores = list()

for fold in folds:

train_set = list(folds)

train_set.remove(fold)

train_set = sum(train_set, [])

test_set = list()

for row in fold:

row_copy = list(row)

test_set.append(row_copy)

row_copy[-1] = None

predicted = algorithm(train_set, test_set, *args)

actual = [row[-1] for row in fold]

accuracy = accuracy_metric(actual, predicted)

scores.append(accuracy)

return scores

 

# Split a dataset based on an attribute and an attribute value

def test_split(index, value, dataset):

left, right = list(), list()

for row in dataset:

if row[index] < value:

left.append(row)

else:

right.append(row)

return left, right

 

# Calculate the Gini index for a split dataset

def gini_index(groups, classes):

# count all samples at split point

n_instances = float(sum([len(group) for group in groups]))

# sum weighted Gini index for each group

gini = 0.0

for group in groups:

size = float(len(group))

# avoid divide by zero

if size == 0:

continue

score = 0.0

# score the group based on the score for each class

for class_val in classes:

p = [row[-1] for row in group].count(class_val) / size

score += p * p

# weight the group score by its relative size

gini += (1.0 - score) * (size / n_instances)

return gini

 

# Select the best split point for a dataset

def get_split(dataset, n_features):

class_values = list(set(row[-1] for row in dataset))

b_index, b_value, b_score, b_groups = 999, 999, 999, None

features = list()

while len(features) < n_features:

index = randrange(len(dataset[0])-1)

if index not in features:

features.append(index)

for index in features:

for row in dataset:

groups = test_split(index, row[index], dataset)

gini = gini_index(groups, class_values)

if gini < b_score:

b_index, b_value, b_score, b_groups = index, row[index], gini, groups

return {'index':b_index, 'value':b_value, 'groups':b_groups}

 

# Create a terminal node value

def to_terminal(group):

outcomes = [row[-1] for row in group]

return max(set(outcomes), key=outcomes.count)

 

# Create child splits for a node or make terminal

def split(node, max_depth, min_size, n_features, depth):

left, right = node['groups']

del(node['groups'])

# check for a no split

if not left or not right:

node['left'] = node['right'] = to_terminal(left + right)

return

# check for max depth

if depth >= max_depth:

node['left'], node['right'] = to_terminal(left), to_terminal(right)

return

# process left child

if len(left) <= min_size:

node['left'] = to_terminal(left)

else:

node['left'] = get_split(left, n_features)

split(node['left'], max_depth, min_size, n_features, depth+1)

# process right child

if len(right) <= min_size:

node['right'] = to_terminal(right)

else:

node['right'] = get_split(right, n_features)

split(node['right'], max_depth, min_size, n_features, depth+1)

 

# Build a decision tree

def build_tree(train, max_depth, min_size, n_features):

root = get_split(train, n_features)

split(root, max_depth, min_size, n_features, 1)

return root

 

# Make a prediction with a decision tree

def predict(node, row):

if row[node['index']] < node['value']:

if isinstance(node['left'], dict):

return predict(node['left'], row)

else:

return node['left']

else:

if isinstance(node['right'], dict):

return predict(node['right'], row)

else:

return node['right']

 

# Create a random subsample from the dataset with replacement

def subsample(dataset, ratio):

sample = list()

n_sample = round(len(dataset) * ratio)

while len(sample) < n_sample:

index = randrange(len(dataset))

sample.append(dataset[index])

return sample

 

# Make a prediction with a list of bagged trees

def bagging_predict(trees, row):

predictions = [predict(tree, row) for tree in trees]

return max(set(predictions), key=predictions.count)

 

# Random Forest Algorithm

def random_forest(train, test, max_depth, min_size, sample_size, n_trees, n_features):

trees = list()

for i in range(n_trees):

sample = subsample(train, sample_size)

tree = build_tree(sample, max_depth, min_size, n_features)

trees.append(tree)

predictions = [bagging_predict(trees, row) for row in test]

return(predictions)

 

# Test the random forest algorithm

seed(2)

# load and prepare data

filename = 'sonar.all-data.csv'

dataset = load_csv(filename)

# convert string attributes to integers

for i in range(0, len(dataset[0])-1):

str_column_to_float(dataset, i)

# convert class column to integers

str_column_to_int(dataset, len(dataset[0])-1)

# evaluate algorithm

n_folds = 5

max_depth = 10

min_size = 1

sample_size = 1.0

n_features = int(sqrt(len(dataset[0])-1))

for n_trees in [1, 5, 10]:

scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features)

print('Trees: %d' % n_trees)

print('Scores: %s' % scores)

print('Mean Accuracy: %.3f%%' % (sum(scores)/float(len(scores))))








  




Post a Comment

0 Comments