-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathzeroCollect.py
More file actions
32 lines (27 loc) · 911 Bytes
/
zeroCollect.py
File metadata and controls
32 lines (27 loc) · 911 Bytes
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
########## lesson learnt:
* Had already done this question but nothing clicked for a while - reduce nervousness while writing the code, donot care of the person sitting on the other side
* think clearly in both the decision loops always, do not look into one specific loop always, look around for it... the solution is present in front or where you are not looking
#N2 Logic
def zeroCollect(a):
for i in range(len(a)):
if a[i] == 0:
for j in range(0,i):
if a[j] != 0:
a[i] = a[j]
a[j] = 0
break
print a
#N Logic
def zeroCollect2(a):
temp = 0
for i in range(len(a)):
if a[i] == 0:
a[i] = a[temp]
a[temp] = 0
temp = temp + 1
else:
if temp == 0:
temp = i
print a
zeroCollect([1,0,9,2,4,0,0,5,0])
zeroCollect2([1,0,9,2,4,0,0,5,0])