# Array is a collection of multiple data in a single variable of same data type
#import array
# import array as arr
# def pr(arr,len):
# for i in range(len):
# print(arr[i], end=" ")
# print()
# # Create an errror
# a = arr.array('i',[2,5,3])
# #insert functin insert(index,element)
# a.insert(1,4)
# pr(a,4)
#
# # append functoion append(element)
# a.append(7)
# pr(a,5)
#
# # -------------------------------------
# # Remove the elements from the array
# # remove() , pop()
# print('Removing')
# a.remove(2) # -> remove the first search ocurence
# pr(a,4)
#
# a.pop()
# pr(a,3)
# Question 1.
# Pair Sum
# from collections import *
# def pairSum(arr,s):
# n = len(arr)
# ans = []
# for i in range(n):
# for j in range(i+1,n):
# if (arr[i] + arr[j] == s):
# pair = [-1 for i in range(2)]
# pair[0] = arr[i]
# pair[1] = arr[j]
# ans.append(pair)
# res = [[-1 for i in range(2)]for i in range(len(ans))]
# for i in range(len(ans)):
# a = ans[1][0]
# b = ans[1][1]
# res[i][0] =min(a,b)
# res[i][1] = max(a,b)
#
# res = sorted(res, key = lambda x: x[0])
# return res
# print(pairSum([2,-3,3,3,-2],0))
# import math
#List(Arrays) Operations on Pyhton
# arr = [5,3,2,6,7,-1]
# ans = math.inf
# print(ans)
# for i in arr:
# if i < ans:
# ans = i
# print(ans)
# ans = min(arr) #Finding Minimum in the List
# print(ans)
# #Maxmium operation on Lists
# ans = max(arr)
# print(ans)
# import array as arr
# # Question 1.
# def FindSumMaxMin(arr):
# mx = max(arr)
# mn = min(arr)
# return mx + mn
#
# ar = arr.array('i', [1,2,3,4,5,6,7,-1])
# print(FindSumMaxMin(ar))
# #Sort Operatin on Python
# arr = [5,3,2,6,7,-1]
# arr.sort() #By degault sort in ascending order
# arr.sort(reverse=True) #Descending order sorting
# print(arr)
#Reverse operatin on array
# arr = [5,3,2,6,7,-1]
# arr.reverse()
# print(arr)
#Question 2.
# def revereseString(str):
# l = str.split()
# res = ""
# l.reverse()
# for i in l:
# res+=i
# res += " "
# return res
#
# str = " Hellow World "
# print(revereseString(str))
#Resize operation on list
#Slicing
# arr = [5,10,2,3,4,5]
# arr = arr[2:5]
# print(arr)
##2D Arrays[List] in Python##
# arr = [[1,2,3],[4,5,6],[7,8,9]]
# print("element at [1][0] = ",arr[1][0])
# print("Element at [2] = ",arr[2])
#Traversing value in python 2d array
# arr = [[1,2,3],[4,5,6],[7,8,9]]
# for i in arr:
# for j in i:
# print(j,end=" ")
# print()
#Inserting Values in Python 2-d Array
# arr = [[1,2,3],[4,5,6],[7,8,9]]
# # arr.insert(2,[11,12,13])
# # arr.insert(2,[11])
# # arr.insert(2,11) #but its not iterble
# print("Originl Array")
# for i in arr:
# for j in i:
# print(j,end=" ")
# print()
# #Updation
# arr[0][0] = 9
# arr[2] = [1,2,3]
# print("updated array")
# for i in arr:
# for j in i:
# print(j,end=" ")
# print()
##Deletion in 2-D array
# arr = [[1,2,3],[4,5,6],[7,8,9]]
# print("Original array")
# for i in arr:
# for j in i:
# print(j,end=" ")
# print()
# del(arr[1])
# print("Modified Array")
# for i in arr:
# for j in i:
# print(j,end=" ")
# print()
# --------------------------------------
##Questions on Array on Python##
# -------------------------------------
# Question1. : Reverse an Array
# def reverses(arr,m):
# # new_l = arr[0:m+1]
# # # del(arr[0:m+1])
# # # print(arr)
# # arr = arr[:m:-1]
# # # for i in range(int(len(arr)/2)):
# # # temp = arr[i]
# # # arr[i] = arr[len(arr)-1-i]
# # # arr[len(arr)-1-i] = temp
# # new_l = new_l + arr
# # return new_l
#
# # solution 2
# return (arr[0:m+1] + arr[:m:-1])
# arr = [1,2,3,4,5,6]
# print(reverses(arr,3))
#Question 2.
# # Intersection of Two sorted Array
# def findIntersection(arr,n,brr,m):
# res = []
# m = dict()
# for i in arr:
# m[i] = m.get(i,0) + i
#
# for j in brr:
# if j in m and m[j]>0:
# res.append(j)
# m[j] -= 1
# return res
# arr = [1,2,3,4,5,6,2,3,4]
# brr = [1,1,2,2,3,3,4]
# m = len(arr)
# n = len(brr)
# print (findIntersection(arr,m,brr,n))
# #Question 3. sort 0 1 2
# def sortt(arr,n):
# a = 0
# b = 0
# c = n-1
# while(b<=c):
# if arr[b] == 1:
# b+=1
# elif arr[b] ==0:
# temp = arr[a]
# arr[a] = arr[b]
# arr[b] = temp
# a+=1
# b+=1
# else:
# temp = arr[b]
# arr[b] = arr[c]
# arr[c] = temp
# c -= 1
# return arr
# arr = [0,1,0,1,1,2,0,1,0,2]
# n = len(arr)
# print(sortt(arr,n))
##Copying an array list in python##
# import array as a
from array import *
# nums = array('I',[1,2,3,4,5])
# nums.append(12)
# nums.remove(2)
# nums.reverse()
# print(nums[2])
# print(nums)
# sum = 0
# for i in nums:
# print(i,end=" ")
# sum += i
# print(sum)
# print(nums.typecode)#Showing what kind of data you're storing
# print(nums.buffer_info())#showing id or size
# #copy elements in an array
# newArr = array(nums.typecode,(ele for ele in nums))
# print(newArr)
# print(nums)
# Input Taking from user
# arr = array('i',[])
# n = int(input("Enter number of values taht you want to insert: "))
# for i in range(n):
# x = int(input(f"Enter {i+1}th Number : "))
# arr.append(x)
#
# print(arr)
# #Searching
# nums = array('I',[1,2,3,4,5])
# print(type(nums))
# # idx = 0
# # for ele in nums:
# # if ele == 2:
# # print(idx)
# # break
# # idx += 1
# print(nums.index(2))
##Installing Numpy on the System##
# vals = array('i',[1,2,3],[4,5,6]) #it is giving array because array only take 2 arguments
#For making the multi dimensional arrray will use numpy
from numpy import *
# vals = array([1,2,3,4,5,6],int)#Data type is optional
# print(vals)
# vals = array([2,4,6,3.6,6], int)
# print(vals.dtype)
# print(vals)/
# #linspace
# print(linspace(1,2,5))
# #LogSpace
# vals = logspace(1,40,5)
# # print(vals)
# print(vals[4])
# print('%.2f' %vals[4])#exact Value
# # Arrange
# vals = arange(1,20,3)
# print(vals)
# #Zeros and ones
# vals = zeros(6,int)
# print(vals)
# vals = ones(6,int)
# print(vals)
# arr1 = array([1,2,3])
# arr2 = array([4,5,6])
# # arr3 = arr1 * 7
# arr3 = arr1 + arr2
# print(arr3)
# #Conetanation of arrays
# vals = concatenate([arr1,arr2])
# print(vals)
# Copying a array
# #Shallow Copy
# arr1 = array([1,2,3])
# arr2 = arr1.view()
#
# print(id(arr1))
# print(id(arr2))
#
# print(arr1)
# print(arr2)
# #if we are making any changes in one of the array the changes reflect on both arrays because hte are linked
# arr1[2] = 6
# print(arr2)
# #Deep Copy
# arr1 = array([1,2,3])
# arr2 = arr1.copy()
# arr1[2] = 5
# print(arr1)
# print(arr2)
##working with arrays in Python##
# import array as arr
# l = [1,2,3,4,5,6,7,8,9,10,2]
# a = arr.array('i',l)
# print("Initial array")
# for i in a:
# print(i,end=" ")
# print()
# #Slicing from range 2-5
# slice1 = a[2:5]
# print(slice1)
# #slicing with gaps
# slice2 = a[2:5:2]
# print(slice2)
# #slicing with negative values
# slice3 = a[-1:-4:-1]
# print(slice3)
# #Slice alll elements
# slice4 = a[:]
# print(slice4)
# #reverse the array
# slice5 = a[::-1]
# print(slice5)
# #Searching
# print(a.index(7))
# print(a.index(2))
# #Update
# a[10] = 11
# print(a)
# #sort
# a = sorted(a)
# print(a)
##Working with matrices in Python##
# def change(arr):
# n = len(arr)#lenght of rows
# m = len(arr[0])#length of colums
#
# for i in range(n):
# sum = 0
# for j in range(m-1,-1,-1):
# temp = arr[i][j]
# arr[i][j] = sum
# sum += temp
# return arr
#
# arr = [[1,2,3],[4,5,6],[7,8,9]]
# print(change(arr))
##Set matrix 0
# #if any element of matrix is 0 make the row and col of that element 0
# def change(arr):
# n = len(arr)
# m = len(arr[0])
# colZero = [False for _ in range(m)]
# rowZero = [False for _ in range(n)]
# for i in range(n):
# for j in range(m):
# if arr[i][j] == 0:
# colZero[j] = True
# rowZero[i] = True
# for i in range(n):
# for j in range(m):
# if colZero[j] or rowZero[i]:
# arr[i][j] = 0
# return arr
# arr = [[7,19,3],[4,21,0]]
# print(change(arr))
0 Comments