Problem:
Write an algorithm, to find the union and intersection sorted in ascending order, between the elements of two arrays
Input Format
The first line contains an integer, n1 , denoting the size of the first array.
The second line contains n1 space-separated integers representing elements of the first array.
The third line contains an integer, n2 , denoting the size of the second array.
The fourth line contains n2 space-separated integers representing elements of the second array.
Constraints
Apart from Array, no other data structure can be used Output elements should be sorted in ascending order Array should be sorted using Bubble Sort
Output Format
First line, union of space-separated elements of both array elements, sorted in ascending order
Second line, intersection of space-separated elements of both arrays elements, sorted in ascending order
Solution:
"""
1) Sort two arrays
2) Find union and intersecton of two sorted arrays
"""
def swap(array, indexI, indexJ):
temp = array[indexI]
array[indexI] = array[indexJ]
array[indexJ] = temp
def bubbleSort(array):
n = len(array)
for indexI in range(n):
exchanges = 0
for indexJ in range(n-1, indexI, -1):
if array[indexJ] < array[indexJ-1]:
swap(array, indexJ, indexJ-1)
exchanges += 1
if exchanges == 0:
break
def intersection(arrayOne, arrayTwo):
arrayOneIndex = 0
arrayTwoIndex =0
sizeOfArrayOne = len(arrayOne)
sizeOfArrayTwo = len(arrayTwo)
array = []
while arrayOneIndex < sizeOfArrayOne and arrayTwoIndex < sizeOfArrayTwo:
if arrayOne[arrayOneIndex] < arrayTwo[arrayTwoIndex]:
arrayOneIndex += 1
elif arrayOne[arrayOneIndex] > arrayTwo[arrayTwoIndex]:
arrayTwoIndex += 1
else:
array.append(arrayOne[arrayOneIndex])
arrayOneIndex += 1
arrayTwoIndex += 1
return array
def union(arrayOne, arrayTwo):
arrayOneIndex = 0
arrayTwoIndex =0
sizeOfArrayOne = len(arrayOne)
sizeOfArrayTwo = len(arrayTwo)
array = []
while arrayOneIndex < sizeOfArrayOne and arrayTwoIndex < sizeOfArrayTwo:
if arrayOne[arrayOneIndex] < arrayTwo[arrayTwoIndex]:
array.append(arrayOne[arrayOneIndex])
arrayOneIndex += 1
elif arrayOne[arrayOneIndex] > arrayTwo[arrayTwoIndex]:
array.append(arrayTwo[arrayTwoIndex])
arrayTwoIndex += 1
else:
array.append(arrayOne[arrayOneIndex])
arrayOneIndex += 1
arrayTwoIndex += 1
if arrayOneIndex == sizeOfArrayOne:
while arrayTwoIndex < sizeOfArrayTwo:
array.append(arrayTwo[arrayTwoIndex])
arrayTwoIndex += 1
else:
while arrayOneIndex < sizeOfArrayOne:
array.append(arrayOne[arrayOneIndex])
arrayOneIndex += 1
return array
if __name__ == '__main__':
sizeOne = int(input())
try:
arrayOne = [int(x) for x in input().split()]
except:
print('Invalid input')
sys.exit()
sizeTwo = int(input())
try:
arrayTwo = [int(x) for x in input().split()]
except:
print('Invalid syntax')
sys.exit()
bubbleSort(arrayOne)
bubbleSort(arrayTwo)
intersectionArray = intersection(arrayOne, arrayTwo)
unionArray = union(arrayOne, arrayTwo)
print(*unionArray, sep=" ")
print(*intersectionArray, sep=" ")
Correctness
Input
3
4 5 7
5
6 9 5 7 1
Output
1 4 5 6 7 9
5 7
Input
6
1 1 8 9 0 5
4
0 7 3 4
Output
0 1 3 4 5 7 8 9
0
Question:
0) How to avoid such syntax? while arrayOneIndex < sizeOfArrayOne and arrayTwoIndex < sizeOfArrayTwo
1) Does the code require better error handling?
2) Can this code get more pythonic?