Skip to content

Commit 967f77d

Browse files
committed
move zero at last and test case
1 parent 48e3748 commit 967f77d

File tree

3 files changed

+86
-22
lines changed

3 files changed

+86
-22
lines changed

geek_for_geeks/challenge/move_zero.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# Best Approach (3rd method)
2+
def pushZerosToEnd(arr):
3+
count = 0
4+
n = len(arr)
5+
for i in range(n):
6+
if arr[i] != 0:
7+
arr[count], arr[i] = arr[i], arr[count]
8+
count += 1
9+
return arr
10+
11+
12+
# T.C = O(n)
13+
# S.C = O(1)
14+
15+
116
def pushZerosToEnd(arr):
217
count = 0
318
n = len(arr)
@@ -9,3 +24,7 @@ def pushZerosToEnd(arr):
924
arr[count] = 0
1025
count += 1
1126
return arr
27+
28+
29+
# T.C = O(2n)
30+
# S.C = O(1)

test/geeks.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

test/test_case.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import sys
3+
4+
sys.path.append(os.path.abspath(".."))
5+
from geek_for_geeks.challenge.move_zero import pushZerosToEnd
6+
from geek_for_geeks.challenge.secondlargest import getSecondLargest
7+
8+
9+
class Test_getSecondLargest:
10+
def test_basic(self):
11+
assert getSecondLargest([2, 3, 6, 6, 5]) == 5
12+
13+
def test_all_same(self):
14+
assert getSecondLargest([4, 4, 4]) == -1
15+
16+
def test_one_element(self):
17+
assert getSecondLargest([10]) == -1
18+
19+
def test_negative_numbers(self):
20+
assert getSecondLargest([-5, -2, -9, -1]) == -2
21+
22+
def test_empty_array(self):
23+
assert getSecondLargest([]) == -1
24+
25+
26+
class TestPushZerosToEnd:
27+
def test_general_case(self):
28+
arr = [1, 0, 2, 0, 4, 0, 5]
29+
expected = [1, 2, 4, 5, 0, 0, 0]
30+
pushZerosToEnd(arr)
31+
assert arr == expected
32+
33+
def test_all_zeros(self):
34+
arr = [0, 0, 0, 0]
35+
expected = [0, 0, 0, 0]
36+
pushZerosToEnd(arr)
37+
assert arr == expected
38+
39+
def test_no_zeros(self):
40+
arr = [1, 2, 3, 4]
41+
expected = [1, 2, 3, 4]
42+
pushZerosToEnd(arr)
43+
assert arr == expected
44+
45+
def test_empty_list(self):
46+
arr = []
47+
expected = []
48+
pushZerosToEnd(arr)
49+
assert arr == expected
50+
51+
def test_single_element_zero(self):
52+
arr = [0]
53+
expected = [0]
54+
pushZerosToEnd(arr)
55+
assert arr == expected
56+
57+
def test_single_element_nonzero(self):
58+
arr = [5]
59+
expected = [5]
60+
pushZerosToEnd(arr)
61+
assert arr == expected
62+
63+
def test_mixed_types(self):
64+
arr = [0, 1, 0, 0, 2, 0, 3]
65+
expected = [1, 2, 3, 0, 0, 0, 0]
66+
pushZerosToEnd(arr)
67+
assert arr == expected

0 commit comments

Comments
 (0)