TYL Arrays
Question 1
►Create an array of 5 integers [2,3,3,3,4]. Access the first individual item through indexesarray(data type, value list)
►Append a new item, 8 to the end of the array.
►Reverse the items in the array
arrays are mutable so we can use either
obj.reverse() or obj.sort(reverse=True)
►Output the length in bytes of one array item in the internal representation
itemsize
►Output the current memory address and the length in elements of the buffer used to hold an array.
arrObject.buffer_info()
►Find number of occurrences of 3 in the array.
arrObject.count(item) returns the number of occurrences in the array
►Append the iterable lst = [8,6,48] to the array
►Create another integer array with values [14,15,16] and append to array
array.append(x) : adds a new item with value x to the end of the array
array.extend(iterable) : to add more than 1 element to the end of the array.
-- If iterable is another array, it should have have the exact same type code
-- If iterable is not an array, its elements must be of the right type to be appended to the array.
►Insert 1 before the 1st element in the array.
In this case, we insert at position 0.
array.insert(i,x) : inserts element at respective index in the array
►Remove the first occurrence of 3 from the array.
►Pop the element with index, 4 from the array.
array.pop([i]) – takes an optional argument, the index value
--When no parameter is passed, it returns the last value in the array
--When index is specified, it returns the exact element.
-- array.remove(x) – removes the first occurrence of the value x from the array.
--If you pass the index, it will throw an error
►Print the array.
Program:
import array as ar
a = ar.array('i', (2,3,3,3,4) )
print("first item",a[0])
a.append(8)
a.reverse()
print("\nLength in bytes of one array item in the internal representation")
print(a.itemsize)
print("\nCurrent memory address and the length in elements of the buffer")
print(a.buffer_info() )
print("\nNumber of occurrences of 3 in the array")
print(a.count(3) )
lst = [8,6,48]
a.extend(lst)
b= ar.array('i', [14,15,16])
a.extend(b)
a.insert(0,1)
a.remove(3)
a.pop(4)
print("\n Final contents of array")
print(a)
Output:
first item 2Length in bytes of one array item in the internal representation
4
Current memory address and the length in elements of the buffer
(68843624, 6)
Number of occurrences of 3 in the array
3
Final contents of array
array('i', [1, 8, 4, 3, 2, 8, 6, 48, 14, 15, 16])
Question 2
Write a Python program to find whether a given array of integers contains any duplicate element. Return true if any value appears at least twice in the said array and return false if every element is distinct.Either convert the array to a set and check the lengths or count each item and check for duplicity.
Program:
import array as ar
def duplicates_present(a):
a_set = set(a)
if len(a_set) == len(a):
return False
else:
return True
a = ar.array('i',[2,3,3,3,4])
print("duplicates present in array a:",duplicates_present(a))
b = ar.array('i',[2,3,4])
print("duplicates present in array b:",duplicates_present(b))
Alternate solution without using sets:
def duplicates_present_c(a):for item in a:
if a.count(item)>1:
return True
return False
Output:
python tyl_arr_dup.py
duplicates present in array a: True
duplicates present in array b: False
Question 3
Write a Python program to find the first duplicate element in a given array of integers. Return -1 If there are no such elements.
Use array.count(item) to count the number of occurrences and return the first item whose count is more than 1
Program:
import array as ardef firstDuplicate(a):
for item in a:
if a.count(item)>1:
return item
return -1
a = ar.array('i',[2,3,3,3,4])
print("First duplicate value in array a:",firstDuplicate(a))
b = ar.array('i',[2,3,4])
print("First duplicate value in array b:",firstDuplicate(b))
Output:
python tyl_arr_first_dup.pyFirst duplicate value in array a: 3
First duplicate value in array b: -1
Comments
Post a Comment