-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy patharrayCompare.py
More file actions
37 lines (24 loc) · 1.28 KB
/
arrayCompare.py
File metadata and controls
37 lines (24 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'''Comparing arrays'''
'''This problem helps one to understand the key concepts of an array(list) in Python.
Two arrays are said to be the same if they contain the same elements and in the same order.
However, in this problem, we will compare two arrays to see if they are same, but with a slight twist.
Here, two arrays are the same if the elements of one array are squares of elements of other arrays and regardless of the order.
Consider two arrays a and b.
'''
# function to compare the arrays
def comp(array1, array2):
# Handle edge cases where one or both arrays could be None
if array1 is None or array2 is None:
return array1 == array2 # Return True only if both are None
# Check if sorted squares of array1 are equal to sorted elements of array2
return (sorted(array1) == sorted([i ** 2 for i in array2])) or (sorted(array2) == sorted([i ** 2 for i in array1]))
# Example usage:
print(comp([1, 2, 3, 4], [1, 4, 9, 16]))
if array1 is None and array2 is not None:
return False
if (sorted(array1) == sorted([i ** 2 for i in array2])) or (sorted(array2) == sorted([i ** 2 for i in array1])):
return True
return False
lis1=eval(input("Enter list 1: "))
lis2=eval(input("Enter list 2: "))
print(comp(lis1,lis2))