-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_matching.py
50 lines (39 loc) · 1.73 KB
/
array_matching.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
#Have the function ArrayMatching(strArr) read the array of strings stored in strArr which will contain only two elements,
#both of which will represent an array of positive integers.
#For example: if strArr is ["[1, 2, 5, 6]", "[5, 2, 8, 11]"],
#then both elements in the input represent two integer arrays,
#and your goal for this challenge is to add the elements in corresponding locations from both arrays.
#For the example input, your program should do the following additions: [(1 + 5), (2 + 2), (5 + 8), (6 + 11)] which then equals [6, 4, 13, 17].
#Your program should finally return this resulting array in a string format with each element separated by a hyphen: 6-4-13-17.
#If the two arrays do not have the same amount of elements,
#then simply append the remaining elements onto the new array (example shown below).
#Both arrays will be in the format: [e1, e2, e3, ...] where at least one element will exist in each array.
def ArrayMatching(strArr):
arr1= eval(strArr[0])
arr2= eval(strArr[1])
result =[]
min_len = min(len(arr1),len(arr2))
for i in range(min_len):
result.append(arr1[i]+arr2[i])
if len(arr1)>min_len:
result.extend(arr1[min_len:])
if len(arr2)>min_len:
result.extend(arr2[min_len:])
# code goes here
return "-".join(str(i) for i in result)
# keep this function call here
print(ArrayMatching(input()))
def ArrayMatching(strArr):
x = [int(i) for i in strArr[0][1:-1].split(',')]
y = [int(i) for i in strArr[1][1:-1].split(',')]
sums = []
for i in range(max(len(x), len(y))):
total = 0
if i < len(x):
total += x[i]
if i < len(y):
total += y[i]
sums.append(str(total))
return '-'.join(sums)
# keep this function call here
print(ArrayMatching(input()))