Day 22
Practicing Python from Basics
Arrays
1. one dimensional array
In [ ]: arr = [1, 2, 3, 4, 5]
In [ ]: first_element = arr[0]
print(first_element)
2. Multidimensional Array
array of array with same length is 2D arrays.
In [ ]: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [ ]: first_row = matrix[0]
print(first_row)
[1, 2, 3]
In [ ]: # all rows
for row in matrix:
print(row)
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
3. Jagged arrays:
Arrays of arrays where the sub-arrays can have different lengths.
In [ ]: jagged = [[1, 2], [3, 4, 5], [6]]
In [ ]: # elements from array
for element in jagged:
print(element)
[1, 2]
[3, 4, 5]
[6]
Array Exercises
1. Create an array of 10 integers and print the array.
In [ ]: array = [1,2,3,3,4,5,10,32,12,11]
# length of array
length = len(array)
print('Length of array is :: ',length)
# print array
print(array)
Length of array is :: 10
[1, 2, 3, 3, 4, 5, 10, 32, 12, 11]
2. Find the maximum and minimum values in an array.
In [ ]: # maximum value of array
maximum = max(array)
print('Maximum :',maximum)
# minimum value of array
minimum = min(array)
print('Minimum :',minimum)
Maximum : 32
Minimum : 1
3. Implement a function to reverse an array.
In [ ]: def reverse_array(arr):
# using slicing to reverse array
reversed_arr = arr[::-1]
return reversed_arr
# original array
print("Original Array : ",array)
# calling function to reverse array
print("Reversed Array : ",reverse_array(array))
Original Array : [1, 2, 3, 3, 4, 5, 10, 32, 12, 11]
Reversed Array : [11, 12, 32, 10, 5, 4, 3, 3, 2, 1]
Linked Lists
Definition:
A linked list is a linear data structure where elements are stored in nodes.
Each node contains the data and a reference (link) to the next node in the sequence.
Implementation
In [ ]: # creating node class to hold linked list node value and next reference.
class Node:
def __init__(self,data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self,data):
# creating object for node
new_node = Node(data)
# checking if head has value of not
if not self.head:
self.head = new_node
else:
last = self.head
while last.next:
last = last.next
last.next = new_node
def display(self):
current = self.head
while current:
print(current.data, end='->')
current = current.next
print()
In [ ]: # Adding values to linked list
ll = LinkedList()
ll.append(1)
ll.append(2)
ll.append(3)
ll.append(4)
ll.append(5)
ll.append(6)
ll.append(7)
In [ ]: # displaying linked list
ll.display()
1->2->3->4->5->6->7->
Above implemention is also known as singly linked list.
Each node points to the next node, and the last node points to None .
Exercise
1. Implement a function to find the length of a singly linked list.
In [ ]: # definig function for calculating lenght of linked list
def len_of_ll(ll_obj):
# creating temporary variable to traverse list.
current = ll_obj.head
length = 0
# while loop to traverse all list.
while current:
current = current.next
length += 1
return length
# calling function with previously created object
length = len_of_ll(ll)
# printing result
print(f"length of given linked list is :: {length}")
length of given linked list is :: 7
2. Write a function to search a value linked list.
In [ ]: # defining function to search for given value in linked list
def search_ll(ll_obj, data):
# temporary variable to traverse list
current = ll_obj.head
position = 0
# checking if data is at head node.
if current.data == data:
return f"search successful, Found at position {position+1}"
else:
while current:
position += 1
if current.data == data:
return f"search successful, Found at position {position}"
current = current.next
#value to search
search = 4
# calling
print(search_ll(ll,search))
search successful, Found at position 4
3. Create a function to delete a node from linked list.
In [ ]: def delete_node(ll_obj, data):
# creating temporary variable to hold head
temp = ll_obj.head
# message
msg = None
if temp.data == data:
ll_obj.head = temp.next
temp = None
return msg
else:
# variable to hold previos node
prev = None
# searching the node
while temp:
if temp.data == data:
msg = "Node Deleted Successfully."
break
prev = temp
temp = temp.next
# node not found
if temp is None:
return "Node Not found!."
# now deleting/ unlinking the node
prev.next = temp.next
# deleting temp
temp = None
# msg
return msg
Deletting
In [ ]: # value of node to be deleted
node_data = 4
# calling to delete
msg = delete_node(ll,node_data) # here ll is a previously created object of link
# message
print(msg)
# printing list to see
ll.display()
Node Not found!.
1->2->3->5->6->7->
In [ ]: # value of node to be deleted
node_data = 6
# calling to delete
msg = delete_node(ll,node_data) # here ll is a previously created object of link
# message
print(msg)
# printing list to see
ll.display()
None
1->2->3->5->7->
In [ ]: # value of node to be deleted
node_data = 2
# calling to delete
msg = delete_node(ll,node_data) # here ll is a previously created object of link
# message
print(msg)
# printing list to see
ll.display()
Node Deleted Successfully.
1->3->5->7->