aboutsummaryrefslogtreecommitdiff
path: root/challenge-253/zapwai/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-253/zapwai/python/ch-2.py')
-rw-r--r--challenge-253/zapwai/python/ch-2.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-253/zapwai/python/ch-2.py b/challenge-253/zapwai/python/ch-2.py
new file mode 100644
index 0000000000..5959d5bede
--- /dev/null
+++ b/challenge-253/zapwai/python/ch-2.py
@@ -0,0 +1,53 @@
+def is_weaker(m, i, j):
+ rowi = m[i][:]
+ rowj = m[:][j]
+ (numi, numj) = (0, 0)
+ for u in rowi:
+ if u == 1:
+ numi += 1
+ for v in rowj:
+ if v == 1:
+ numj += 1
+ if numi < numj:
+ return 1
+ elif (numi == numj and i < j):
+ return 1
+ return 0
+
+def mylist(length):
+ alist = list(range(length))
+ while(1):
+ cnt = 0
+ for i in range(length-1):
+ if not is_weaker(m, alist[i], alist[i+1]):
+ cnt += 1
+ tmp = alist[i]
+ alist[i] = alist[i+1]
+ alist[i+1] = tmp
+ if cnt == 0:
+ break
+ return alist
+
+def proc(m):
+ print("Input: m = ")
+ for i in range(len(m)):
+ print("\t",m[i])
+ list = mylist(len(m))
+ print("\nOutput:", list,"\n")
+
+m = [
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 0],
+ [1, 0, 0, 0, 0],
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 1]
+]
+proc(m)
+
+m = [
+ [1, 0, 0, 0],
+ [1, 1, 1, 1],
+ [1, 0, 0, 0],
+ [1, 0, 0, 0]
+]
+proc(m)