Python Unit Testing and Data Structures

Another name for unit test in Python?

pyUnit

What is unit testing?

Framework used for testing your code while parallely writing your code and it helps in defining more precisely the intent of the code.

What is the difference between unit test and docstring?

Unit test is a unit testing framework that helps in determining quickly the impact of any modification on the rest of the code. Docstring are used to verify if the docstrings are still relevant to the current version of the code.

Explain what is pdb and how to invoke it?

is an interactive python debugger which can be started off any where in the codebase used to debug complex code. To invoke use python -m filename.py

What is logging used for?

used to report errors and status information from application libraries.

Write unittests for the above function which returns indices of the two numbers that add up to a given target.

import unittest
from two_sum import two_sum
class TestTwoSum(unittest.TestCase):
def setUp(self):
self.in_lists=[[2,3,4,1,7]
[4,6,2,9,1,20]]
self.targets =[10,24]
self.answers=[(1,4)
(0,5)
]
def test_two_sum(self):
for l.t.a in zip(self.in_lists,self.targets,self.answers):
self.assertEqual(two_sun(l,t),a)
unittesst.main()

Given a list check if it contains duplicate elements.

def has_dupl(in_list):
return len(in_list)!=len(set(in_list))
if__name__==”main”
A=[2,55,9,17]
print(has_dup)

Implement a stack data structure.

Class Stack(object):
def _init_(self,limit=10):
self.stack
self.limit=limit
def isEmpty(self):
return len(self.stack)<=0
def push(self,item)
if len(self.stack)>=self.limit
print(“Stack OverFlow”)
else: self.items.append(item)
def pop(self):if len(self.stack)<=0
print(“Stack UnderFlow”)
return=0
else:
return self.stack.pop()
def top(self):
if len(self.stack)<=0:
print(“Stack UnderF”)
return 0
else: return self.stack[-1]

Again, consider the above implementation Use such stacks to implement a queue.

Class queue(object):
def_int_ (self):
self.S1=[]
self.S2=[]
def enqueue(self,element):
self.S1.append(element)
def dequeue(self):
if not self.S2:
while self.S1:
self.S2.append(self.S1.pop())
return self.S2.pop()

write a program which checks if the two input numbers have the same prime division

def gcd(a,b):
if b==0
return a
return gcd(b,a%b)
def same_factor(a,b):
if a==b==0
return true
denominator = gcd(a,b)
while (a!=1):
a_gcd=gcd(a,denominator)
if a_gcd==1:
breack
a/=a_gcd
else: while (b!=1):
b_gcd=gcd(d,denominator)
if b_gcd==1
break
b/=b_gcd
else:
return true
return false

given an input list of ints write a func that prints all the even ints before the odd ones

def evenO(A):
left=0
right=len(A)-1
while(left while(A[left]%2==0 and left left+=1
while(A[right]%2==1 and left if(left A[left],A[right]=A[right],A[left]
left+=1
right-=1

write a program to randomly shuffle elements of an input list.

import random
def shuffle(in_list):
n=len(in_list)
for i in range (n-1,0-1):
rand_index=int(random.random()*i)
in_list[rand_index],in_list[i],in_list[rand_index]

write the code to implement quick sort

def partition(in_list):
p=in_list[-1]
left[]
right=[]
for v in in_list[:-1]:
if v <=p: left.append(v)
else: right.append(v)
return left,p,right
def quickSort(in_list):
if len(in_list)<=1: return in_list
left,p,right = partition(in_list)
s_left=quickSort(left)
s_right=quickSort(right)
return s_left +[p]+s_right

Use the queue write a program which takes an integer K and a queue of ints as the input and reverse it.

from datastructures import Stack
from data structures import Queue
def reverse(que,k):
stack=Stack()
if q is None or k >q.size():
return
for i in range(0,k)
stack.push(q.dequeue())
while not stack.isEmpty():
q.enqueue(Stack.pop())
for i in range (0,q.size()-k):
q.enqueue(q.enqueue())
q=Queue()
for i in range(11,20)
q.enqueue(i) reversek(q,4) while not q.isEmpty(): print(q.dequeue())