DSA Lab 2
DSA Lab 2
TAXILA
Submitted To:
Engr. Iqra Jabeen
Submitted By:
Amjid Khan (23-TE-86)
Subject:
Data Structure and Algorithms Lab
DEPARTMENT OF TELECOMMUNICATION
ENGINEERING
1
Lab Report: 2
Computation and Implementation of Python code for Inserting an
element at any location in an Array.
Objective:
➢ Modify the array by adding a new element at a specific index.
➢ Maintain the order and integrity of existing elements.
➢ Support dynamic data structure management.
➢ Enable precise control over data insertion.
Equipment Required:
➢ Computer/Laptop with at least 4 GB RAM.
➢ Python 3.x installed and configured.
➢ IDE/Text Editor like PyCharm, VS Code, or Jupyter Notebook.
➢ NumPy library (optional) for advanced array handling.
➢ Internet connection for downloading resources and accessing documentation.
Procedure:
➢ Open Computer System.
➢ Go to all the programs.
➢ Open python.
➢ Create a new file with the extension .py.
➢ Write all the required code and then run the module.
Theory:
Array:
An array is a collection of items stored at contiguous memory locations. The idea is to
store multiple items of the same type together. This makes it easier to calculate the position of
2
each element by simply adding an offset to a base value, i.e., the memory location of the first
element of the array (generally denoted by the name of the array).
Without Alias
Using Alias
import array as arr
Using *
import array as arr
from array import *
Declaring Arrays:
Python provides an array module to declare a set of data as an array.
Method: 1
arrayName = array(typecode, [initializers])
typecode - the codes that are used to define the type of value the array will hold.
3
output:
4
Method#2:
import array as arr
a = arr.array('i',[1,2,3,4,5])
print(a[0])
output:
Method#3:
from array import *
a = array ('i',[1,2,3,4,5])
print(a[0])
output:
output:
5
Looping in an array:
import array as arr
a=arr.array('i',[1,2,3,4,5])
print('Integer array: ',end=" ")
for i in range (0,5):
print(a[i], end=" ")
print()
output:
append (): This function is used to add the value mentioned in its arguments at the end of the
array.
insert(i,x) :This function is used to add the value(x) at the ith position specified in its argument.
index():- This function returns the index of the first occurrence of the value mentioned in the
arguments.
Lab Tasks:
1. Implement python code for insertion at given index in an array and generate the following
output.
import array
arr = array.array('i', [1, 2, 4, 5])
print("Printing array before insertion")
for i in range(len(arr)):
print(f"array[{i}] = {arr[i]}")
arr.insert(2, 3)
print("\nPrinting array after insertion")
for i in range(len(arr)):
print(f"array[{i}] = {arr[i]}")
output:
2. Write a python program that inputs an integer array and then inserts the integer value X
in the first position in the array and generates the following output.
import array
arr = array.array('i', [1, 2, 3, 4, 5, 6])
print("Stored Data in Array ::")
for item in arr:
print(item, end=" ")
print()
X = 0
arr.insert(0, X)
print("New Array is ::")
for item in arr:
8
output:
3. Write a Python program to swap the first and last element of an integer in an array.
import array
arr = array.array('i', [1, 2, 3, 4, 5, 6])
print('Array before swipping:')
for i in arr:
print(i,end=" ")
print("\n")
output:
4. Write a Python program to find the largest and smallest element of an integer in an array.
import array
arr = array.array('i', [10, 3, 45, 7, 22, 99, 1])
print(arr)
print("The largest element of array is: ",max(arr))
print("The smallest element of array is: ",min(arr))
9
output:
5. Write a program in Python to print all even and odd numbers from an array to a new array.
import array
arr = array.array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
even_arr = array.array('i', [x for x in arr if x % 2 == 0])
odd_arr = array.array('i', [x for x in arr if x % 2 != 0])
print("Even numbers array:", even_arr)
print("Odd numbers array:", odd_arr)
output:
6. Write a Python program to generate the following output by keeping the list as an input.
import array
arr = array.array('d', [2.1, 4.5, 3.5, 4.2, 3.3, 5.5])
print(f"First element: {arr[0]}")
print(f"Second element: {arr[1]}")
print(f"Last element: {arr[-1]}")
sub_arr = array.array('d', [arr[2], arr[3], arr[4]])
print(sub_arr)
print(arr)
output:
10
Conclusion:
In this lab, we examined how to insert an element at any location in an array using Python. The
key aspect of this operation involves shifting elements from the insertion point onward to create
space for the new element. This process maintains the order of existing elements while
accommodating the new addition.
We implemented this insertion operation using Python lists, leveraging list slicing and
concatenation to insert the element at the desired index. The code snippet provided illustrates a
straightforward approach to handling this task and considers various edge cases, such as inserting
at the beginning, end, or within the bounds of the list.
In conclusion, the ability to insert elements at specific locations is crucial for a wide range of
applications, including data processing and real-time data management. Python's list operations
simplify this process, making it easy to manipulate arrays dynamically and efficiently.