DAL Lab Manual

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

List of Experiment with enhancement by The Institute

Sl. No. List ofPrograms


PYTHON
1 To write a Python program to find GCD of two numbers.
2 To write a Python Program to find the square root of a number by Newton’s
Method.
3 To write a Python program to find the exponentiation of a number.
4 To write a Python Program to find the maximum from a list of numbers.
5 To write a Python Program to perform Linear Search
6 To write a Python Program to perform binary search.
7 To write a Python Program to perform selection sort.
8 To write a Python Program to perform insertion sort.
9 To write a Python Program to perform Merge sort.
10 To write a Python program to find first n prime numbers.
R-Program
1 Write a R program to take input from the user (name and age) and display the
values. Also print the version of R installation. Write a R program to get the
details of the objects in memory.
2 Write a R program to create a sequence of numbers from 20 to 50 and find the
mean of numbers from 20 to 60 and sum of numbers from 51 to 91.
3 Write a R program to convert a given matrix to a 1 dimensional array
4 Write a R program to create an array of two 3x3 matrices each with 3 rows
and 3 columns from two given two vectors.
5 Write a R program to create an empty data frame
6 Write a R program to create a data frame from four given vectors
7 Write a R program to get the structure of a given data frame.
8 Write a R program to get the statistical summary and nature of the data of a
given data frame.
9 Write a R program to get the statistical summary and nature of the data of a
given data frame.
10 Write a R program to create a matrix taking a given vector of numbers as
input. Display the matrix.
11 Write a R program to create a matrix taking a given vector of numbers as
input and define the column and row names. Display the matrix.
12 Write a R program to create a vector of a specified type and length. Create
vector of numeric, complex, logical and character types of length 6.
13 Write a R program to add two vectors of integers type and length 3.
14 Write a R program to append value to a given empty vector.
15 Write a R program to multiply two vectors of integers type and length 3.
16 Write a R program to create a list containing strings, numbers, vectors and a
logical values.
17 Write a R program to create a list containing a vector, a matrix and a list and
give names to the elements in the list. Access the first and second element of
the list.
18 Write a R program to find the levels of factor of a given vector.
19 Write a R program to change the first level of a factor with another level of a
given factor.
20 Write a R program to create an ordered factor from data consisting of the
names of months.
Program1: Write aPython programto find GCDof twonumbers.

Aim:
To write aPython program to find GCD oftwo numbers.

Algorithm:
1. Defineafunction named compute GCD()
2. Find thesmallest amongthe two inputs xandy
3. Perform the following step tillsmaller+1
Check if ((x% i ==0)and (y%i ==0)), then assign GCD=i
4. Print thevalue ofgcd

Program:
defcomputeGCD(x,y):
if x>y:
smaller =y
else:
smaller =x
fori in range(1, smaller+1):
if((x% i ==0) and(y%i==0)):
gcd=i
returngcd
num1 = 54
num2 = 24
# takeinputfrom the user
# num1 = int(input("Enter first number:"))
# num2 = int(input("Enter second number: "))
print("TheGCD. of", num1,"and", num2,"is", computeGCD(num1, num2))

Sample Output:
$pythonmain.py
('TheGCD. of', 54,'and',24, 'is', 6)
Program2:WriteaPythonProgramtofindthesquarerootofanumberby
Newton’sMethod
Aim:
To write aPython Program to find thesquare rootofanumberbyNewton’sMethod.
Algorithm:
1. Defineafunction named newtonSqrt().
2.Initialize approxas 0.5*n and better as 0.5*(approx.+n/approx.)
3. Useawhile loop witha condition better!=approxto perform the following, i.
Set approx.=better
ii. Better=0.5*(approx.+n/approx.)
4. Print thevalue ofapprox..

Program:
defnewtonSqrt(n):
approx=0.5 * n
better =0.5 * (approx+n/approx)
while better != approx:
approx=better
better =0.5 * (approx+n/approx)
returnapprox
print('Thesquare root is' ,newtonSqrt(100))

Sample Output:
Thesquare root is 10
Program3: Write aPython programto find theexponentiation ofanumber.
Aim:
To write aPython program to find the exponentiation of anumber.
Algorithm:
1. Defineafunction named power()
2. Read the values ofbase and exp
3. Use ‘if’to check ifexp is equal to 1 ornot
i. if exp is equal to 1, then return base
ii.ifexp is not equal to 1,then return (base*power(base,exp-1))
4. Print the result.

Program:
def power(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*power(base,exp-1))
base=int(input("Enter base: "))
exp=int(input("Enter exponential value: "))
print("Result:",power(base,exp))

Sample Output:
Enter base: 7
Enter exponential value: 2
Result:49
Program 4: Write a Python Program to find the maximum from a list of numbers.
Aim:
To write aPython Program to find themaximum from a list of numbers.
Algorithm:
1. Create anemptylist named l
2. Read the value of n
3. Read the elements of thelistuntil n
4. Assign l[0]as maxno
5.If l[i]>maxno then set maxno=l[i]
6.Increment iby1
7. Repeat steps 5-6 until i<n
8. Print thevalue ofmaximum number
Program:
l=[]
n=int(input("enter theupper limit"))
fori in range(0,n):
a=int(input("enterthenumbers"))
l.append(a)
maxno=l[0]
fori in range(0,len(l)):
if l[i]>maxno:
maxno=l[i]
print("Themaximum numberis %d"%maxno)

Sample Output: Enterthe upper limit 3


Enter thenumbers 6
Enter thenumbers 9
Enter thenumbers 90
Themaximum number is 90
Program5: Write aPython ProgramtoperformLinear Search
Aim:
To write aPython Program to performLinear Search
Algorithm:
1. Read n elements into thelist
2. Read the element to besearched
3.If alist[pos]==item, then print theposition ofthe item
4. else increment theposition and repeat step 3 untilpos reaches the length of the list
Program:
items = [5, 7, 10, 12, 15]
print("list of items is", items)
x=int(input("enteritemto search:")
i = flag=0
while i <len(items):
if items[i]==x:
flag =1
break
i = i +1
ifflag==1:
print("item found at position:", i + 1)
else:
print("item notfound")

SampleOutput:

$pythonmain.py
(list of items is: [5, 7, 10, 12, 15])
enteritem to search:7 (item
found at position:, 2)
Program6: Write aPythonProgramtoperformBinarySearch
Aim:
To write aPython Program to perform binarysearch.
Algorithm:
1. Read the search element
2. Find themiddle element in the sorted list
3. Comparethe searchelement with the middle element
i. if both arematching, printelement found
ii. else then check if thesearchelement is smallerorlarger than the middleelement
4.If thesearchelement is smaller than the middle element, then repeat steps 2 and 3 for the
left sublistof themiddle element
5.If thesearchelement is larger than themiddleelement, then repeat steps2 and 3 forthe
right sublist of the middle element
6. Repeat theprocess untilthe searchelement if found in thelist
7.If element is not found, loop terminates
Program:
# Pythoncodeto implement iterativeBinarySearch.
#Itreturns location ofxin givenarrayarr
# if present, elsereturns-1

defbinarySearch(arr, l, r, x):
while l <= r:
mid =l + (r -l)/2;
# Check if xis present at mid
if arr[mid]==x:
return mid
#If xis greater, ignoreleft half
elifarr[mid]<x:
l = mid + 1
#If xis smaller, ignoreright half
else:
r =mid -1
#Ifwereach here, then the element
# was not present
return -1
# Test array
arr=[2, 3, 4, 10, 40 ]
x=4
# Function call
result=binarySearch(arr, 0, len(arr)-1, x)
ifresult!= -1:
print"Element is present at index% d"% result
else:
print"Element is not present in array"

Sample Output:

$pythonmain.py
Element is present at index 2
Program7: Write aPython Programtoperformselection sort.
Aim:
To write aPython Program to perform selection sort.
Algorithm:
1. Create afunction named selectionsort
2.Initialisepos=0
3.If alist[location]>alist[pos]then perform the followingtilli+1,
4. Set pos=location
5. Swap alist[i]and alist[pos]
6. Print thesorted list
Program:
defselectionSort(alist):
fori in range(len(alist)-1,0,-1):
pos=0
forlocation in range(1,i+1):
ifalist[location]>alist[pos]:
pos=location
temp =alist[i]
alist[i]=alist[pos]
alist[pos]=temp
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)

SampleOutput:

$pythonmain.py
[17, 20, 26, 31, 44, 54, 55, 77, 93]
Program8: Write aPython Programtoperforminsertionsort.
Aim:
To write aPython Program to perform insertion sort.
Algorithm:
1. Create afunction named insertionsort
2.Initialisecurrentvalue=alist[index]and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the followingtilllen(alist)
4. alist[position]=alist[position-1]
5. position =position-1
6. alist[position]=currentvalue
7. Print thesorted list
Program:
definsertionSort(alist):
forindexin range(1,len(alist)):
currentvalue =alist[index]
position =index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position =position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)
Sample Output:

$python main.py
[20, 54, 54, 54, 54, 54, 93, 93, 93]
Program9: Write aPython ProgramtoperformMerge sort.
Aim:
To write aPython Program to perform Mergesort.
Algorithm:
1. Create afunction named mergesort
2. Find themid of thelist
3. Assign lefthalf =alist[:mid]and righthalf=alist[mid:]
4.Initialise i=j=k=0
5. while i <len(lefthalf)and j <len(righthalf), perform the following if
lefthalf[i]<righthalf[j]:
alist[k]=lefthalf[i]
Increment i
else
alist[k]=righthalf[j]
Increment j
Increment k
6. while i <len(lefthalf),perform thefollowing
alist[k]=lefthalf[i]
Increment i
Increment k
7. while j <len(righthalf), perform thefollowing
alist[k]=righthalf[j]
Increment j
Increment k
8. Print thesorted list

Program:
# Python programforimplementation of MergeSort
# Merges two subarraysofarr[].
# First subarrayis arr[l..m]
# Second subarrayis arr[m+1..r]
def merge(arr, l, m, r):
n1 =m -l + 1
n2 = r-m

# createtemp arrays
L=[0]* (n1) R
=[0]* (n2)

# Copydata to temp arraysL[]and R[]


fori in range(0 , n1):
L[i]=arr[l + i]
forj in range(0 , n2):
R[j]=arr[m + 1 +j]

# Mergethe temparraysback into arr[l..r]


i = 0 #Initial indexof first subarray
j = 0 #Initial indexof secondsubarray k
=l #Initial indexof mergedsubarray

while i <n1 and j < n2 :


ifL[i]<=R[j]:
arr[k]=L[i] i
+=1
else:
arr[k]=R[j]
j +=1
k +=1

# Copythe remaining elements ofL[], if there


# areany
while i <n1:
arr[k]=L[i]
i +=1
k +=1

# Copythe remaining elements of R[],if there


# areany
while j <n2:
arr[k]=R[j]
j +=1
k +=1

# lis forleft indexand r is right indexof the


# sub-arrayof arrto be sorted
defmergeSort(arr,l,r):
if l < r:
# Sameas (l+r)/2, but avoids overflow for
# largel and h
m = (l+(r-1))/2

# Sort first andsecond halves


mergeSort(arr, l, m)
mergeSort(arr, m+1, r)
merge(arr, l, m, r)

# Driver codeto test above


arr=[12, 11, 13, 5, 6, 7]
n =len(arr)
print("Givenarrayis")
fori in range(n):
print("%d"%arr[i]),

mergeSort(arr,0,n-1)
print("\n\nSortedarrayis")
fori in range(n):
print("%d"%arr[i]),

Sample Output:

$python main.py
Given arrayis
12 11 13 5 6 7

Sorted arrayis
5 6 7 11 12 13
Experiment 10
Object: To write aPython program to find first n primenumbers

Algorithm:

1. Read the value of n


.
2. fornum in range(0,n + 1), perform the following
3. ifnum%i is 0 then break
else printthe value ofnum
4. Repeat step 3 for i in range(2,num)
Program:
n =int(input("Enter theupper limit: "))
print("Primenumbersare")
fornum in range(0,n +1):
# primenumbers aregreater than
1 if num >1:
fori in
range(2,num):
if(num % i) ==0:
brea
k
els
e:
print(num)

SampleOutput:
$python main.py
Enter theupper limit:20
Primenumbers are
2
3
5
7
11
13
17
19

R program
Program 1: Write a R program to take input from the user (name and
age) and display the values. Also print the version of R installation.
Write a R program to get the details of the objects in memory.

R Programming Code

Sample Output:

Program 2: Write a R program to create a sequence of numbers from


20 to 50 and find the mean of numbers from 20 to 60 and sum of
numbers from 51 to 91.

R Programming Code

Sample Output:
Program 3: Write a R program to convert a given matrix to a 1
dimensional array.
R Programming Code

Sample Output:

Program 4:Write a R program to create an array of two 3x3 matrices


each with 3 rows and 3 columns from two given two vectors.
R Programming Code

Sample Output:
Program 5:Write a R program to create an empty data frame.
R Programming Code

Sample Output:
Program 6: Write a R program to create a data frame from four given
vectors.
R Programming Code

Sample Output:

Program 7: Write a R program to get the structure of a given data


frame.
R Programming Code
Sample Output:

Program 8: Write a R program to get the statistical summary and


nature of the data of a given data frame.
R Programming Code
Sample Output:

Program 9: Write a R program to extract specific column from a data


frame using column name.
R Programming Code
Sample Output:

Program 10: Write a R program to create a matrix taking a given


vector of numbers as input. Display the matrix.
R Programming Code
Sample Output:

Program 11: Write a R program to create a matrix taking a given


vector of numbers as input and define the column and row names.
Display the matrix.
R Programming Code

Sample Output:
Program 12: Write a R program to create a vector of a specified type
and length. Create vector of numeric, complex, logical and character
types of length 6.
R Programming Code

Sample Output:
Program 13: Write a R program to add two vectors of integers type
and length 3.
R Programming Code

Sample Output:
Program 14: Write a R program to append value to a given empty
vector.
R Programming Code

Sample Output:

Program 15: Write a R program to multiply two vectors of integers


type and length 3.
R Programming Code
Sample Output:

Program 16: Write a R program to create a list containing strings,


numbers, vectors and a logical values.
R Programming Code

Sample Output:
Program 17: Write a R program to create a list containing a vector, a
matrix and a list and give names to the elements in the list. Access the
first and second element of the list.
R Programming Code

Sample Output:
[1] "List:"
[[1]]
[1] "Red" "Green" "Black"

[[2]]
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11

[[3]]
[[3]][[1]]
[1] "Python"

[[3]][[2]]
[1] "PHP"

[[3]][[3]]
[1] "Java"

[1] "List with column names:"


$Color
[1] "Red" "Green" "Black"

$`Odd numbers`
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11

$`Language(s)`
$`Language(s)`[[1]]
[1] "Python"

$`Language(s)`[[2]]
[1] "PHP"

$`Language(s)`[[3]]
[1] "Java"

[1] "1st element:"


$Color
[1] "Red" "Green" "Black"

[1] "2nd element:"


$`Odd numbers`
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11

Program 18: Write a R program to find the levels of factor of a given


vector.
R Programming Code

Sample Output:

Program 19: Write a R program to change the first level of a factor


with another level of a given factor.
R Programming Code
Sample Output:

Program 20: Write a R program to create an ordered factor from data


consisting of the names of months.
R Programming Code

Sample Output:

You might also like