-
Notifications
You must be signed in to change notification settings - Fork 311
/
ch-2.py
134 lines (98 loc) · 3.17 KB
/
ch-2.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
### https://theweeklychallenge.org/blog/perl-weekly-challenge-269/
"""
Task 2: Distribute Elements
Submitted by: [47]Mohammad Sajid Anwar
__________________________________________________________________
You are given an array of distinct integers, @ints.
Write a script to distribute the elements as described below:
1) Put the 1st element of the given array to a new array @arr1.
2) Put the 2nd element of the given array to a new array @arr2.
Once you have one element in each arrays, @arr1 and @arr2, then follow
the rule below:
If the last element of the array @arr1 is greater than the last
element of the array @arr2 then add the first element of the
given array to @arr1 otherwise to the array @arr2.
When done distribution, return the concatenated arrays. @arr1 and
@arr2.
Example 1
Input: @ints = (2, 1, 3, 4, 5)
Output: (2, 3, 4, 5, 1)
1st operation:
Add 1 to @arr1 = (2)
2nd operation:
Add 2 to @arr2 = (1)
3rd operation:
Now the last element of @arr1 is greater than the last element
of @arr2, add 3 to @arr1 = (2, 3).
4th operation:
Again the last element of @arr1 is greate than the last element
of @arr2, add 4 to @arr1 = (2, 3, 4)
5th operation:
Finally, the last element of @arr1 is again greater than the last
element of @arr2, add 5 to @arr1 = (2, 3, 4, 5)
Mow we have two arrays:
@arr1 = (2, 3, 4, 5)
@arr2 = (1)
Concatenate the two arrays and return the final array: (2, 3, 4, 5, 1).
Example 2
Input: @ints = (3, 2, 4)
Output: (3, 4, 2)
1st operation:
Add 1 to @arr1 = (3)
2nd operation:
Add 2 to @arr2 = (2)
3rd operation:
Now the last element of @arr1 is greater than the last element
of @arr2, add 4 to @arr1 = (3, 4).
Mow we have two arrays:
@arr1 = (3, 4)
@arr2 = (2)
Concatenate the two arrays and return the final array: (3, 4, 2).
Example 3
Input: @ints = (5, 4, 3 ,8)
Output: (5, 3, 4, 8)
1st operation:
Add 1 to @arr1 = (5)
2nd operation:
Add 2 to @arr2 = (4)
3rd operation:
Now the last element of @arr1 is greater than the last element
of @arr2, add 3 to @arr1 = (5, 3).
4th operation:
Again the last element of @arr2 is greate than the last element
of @arr1, add 8 to @arr2 = (4, 8)
Mow we have two arrays:
@arr1 = (5, 3)
@arr2 = (4, 8)
Concatenate the two arrays and return the final array: (5, 3, 4, 8).
__________________________________________________________________
Last date to submit the solution 23:59 (UK Time) Sunday 19th May 2024.
__________________________________________________________________
SO WHAT DO YOU THINK ?
"""
### solution by pokgopun@gmail.com
def distElem(ints: tuple):
l = len(ints)
x,y,z = 0, 1, 2
ans = list(ints[:2])
while z < l:
#print(ints[x],"vs",ints[y])
if ans[x] > ans[y]:
x += 1
ans.insert(x,ints[z])
else:
ans.append(ints[z])
y += 1
z += 1
#print(ans)
return tuple(ans)
import unittest
class TestDistElem(unittest.TestCase):
def test(self):
for ints, ans in {
(2, 1, 3, 4, 5): (2, 3, 4, 5, 1),
(3, 2, 4): (3, 4, 2),
(5, 4, 3 ,8): (5, 3, 4, 8),
}.items():
self.assertEqual(distElem(ints),ans)
unittest.main()