Add solution fo 1470 leetcode task
This commit is contained in:
parent
04948814fe
commit
dd04d90473
1 changed files with 48 additions and 0 deletions
48
leetcode/static_arrays/1470_shuffle_the_array.py
Normal file
48
leetcode/static_arrays/1470_shuffle_the_array.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
|
class SolutionOne:
|
||||||
|
def shuffle(self, nums: List[int], n: int) -> List[int]:
|
||||||
|
n_a = []
|
||||||
|
l = 0
|
||||||
|
for i in range(0, len(nums)):
|
||||||
|
if i % 2 == 0:
|
||||||
|
n_a.append(nums[l])
|
||||||
|
l += 1
|
||||||
|
else:
|
||||||
|
n_a.append(nums[n])
|
||||||
|
n += 1
|
||||||
|
return n_a
|
||||||
|
|
||||||
|
|
||||||
|
class SolutionTwo:
|
||||||
|
def shuffle(self, nums: List[int], n: int) -> List[int]:
|
||||||
|
lst = []
|
||||||
|
for i in range(n):
|
||||||
|
lst += [nums[i]]
|
||||||
|
lst += [nums[i + n]]
|
||||||
|
return lst
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
nums = [2, 5, 1, 3, 4, 7]
|
||||||
|
n = 3
|
||||||
|
sol = SolutionTwo
|
||||||
|
print(nums)
|
||||||
|
print(sol().shuffle(nums, n))
|
||||||
|
print("-" * 60)
|
||||||
|
nums = [1, 2, 3, 4, 4, 3, 2, 1]
|
||||||
|
n = 4
|
||||||
|
print(nums)
|
||||||
|
print(sol().shuffle(nums, n))
|
||||||
|
print("-" * 60)
|
||||||
|
nums = [1, 1, 2, 2]
|
||||||
|
n = 2
|
||||||
|
print(nums)
|
||||||
|
print(sol().shuffle(nums, n))
|
||||||
|
print("-" * 60)
|
||||||
|
nums = [1, 2]
|
||||||
|
n = 1
|
||||||
|
print(nums)
|
||||||
|
print(SolutionOne().shuffle(nums, n))
|
||||||
|
print("-" * 60)
|
Loading…
Reference in a new issue