aboutsummaryrefslogtreecommitdiff
path: root/challenge-271/zapwai/python
diff options
context:
space:
mode:
authorDavid Ferrone <zapwai@gmail.com>2024-05-27 00:30:38 -0400
committerDavid Ferrone <zapwai@gmail.com>2024-05-27 00:30:38 -0400
commitcc7611c673901442ddfac5a71c691350144dcd03 (patch)
tree438179ab3f56053f12951d76701b704b4822a323 /challenge-271/zapwai/python
parent5ea56aa37a9f0b7098302e2acb76c73907c70bde (diff)
downloadperlweeklychallenge-club-cc7611c673901442ddfac5a71c691350144dcd03.tar.gz
perlweeklychallenge-club-cc7611c673901442ddfac5a71c691350144dcd03.tar.bz2
perlweeklychallenge-club-cc7611c673901442ddfac5a71c691350144dcd03.zip
Week 271
Diffstat (limited to 'challenge-271/zapwai/python')
-rw-r--r--challenge-271/zapwai/python/ch-1.py32
-rw-r--r--challenge-271/zapwai/python/ch-2.py44
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-271/zapwai/python/ch-1.py b/challenge-271/zapwai/python/ch-1.py
new file mode 100644
index 0000000000..5cba68f53f
--- /dev/null
+++ b/challenge-271/zapwai/python/ch-1.py
@@ -0,0 +1,32 @@
+def proc(m):
+ print("Input: m = ", m)
+ cnt = []
+ for i in range(len(m)):
+ cnt.append(0)
+ pres = 0
+ for row in m:
+ for entry in row:
+ if entry == 1:
+ cnt[pres] += 1
+ pres += 1
+ max = 0
+ max_index = 0
+ for i in range(len(cnt)):
+ if cnt[i] > max :
+ max_index = i
+ max = cnt[i]
+ print( "Output: row", max_index + 1, " ( count is", max, ")")
+
+matrix = [ [0, 1],
+ [1, 0],
+ ]
+proc(matrix)
+matrix = [ [0, 0, 0],
+ [1, 0, 1],
+ ]
+proc(matrix)
+matrix = [ [0, 0],
+ [1, 1],
+ [0, 0],
+ ]
+proc(matrix)
diff --git a/challenge-271/zapwai/python/ch-2.py b/challenge-271/zapwai/python/ch-2.py
new file mode 100644
index 0000000000..3ba24a81ef
--- /dev/null
+++ b/challenge-271/zapwai/python/ch-2.py
@@ -0,0 +1,44 @@
+def proc(ints):
+ print( "Input:", ints)
+ count = []
+ for i in ints:
+ bina = bin(i)
+ dig = list(bina)
+ cnt = 0
+ for d in dig:
+ if d == '1':
+ cnt += 1
+ count.append(cnt)
+ ords = ints.copy()
+ c = 1
+ while c != 0:
+ c = 0
+ for i in range(len(ords) - 1):
+ if count[i] > count[i+1]:
+ c += 1
+ tmp_cnt = count[i]
+ tmp_int = ords[i]
+ count[i] = count[i+1]
+ count[i+1] = tmp_cnt
+ ords[i] = ords[i+1]
+ ords[i+1] = tmp_int
+ c = 1
+ while c != 0:
+ c = 0
+ for i in range(len(ords) - 1):
+ if count[i] != count[i+1]:
+ continue
+ if ords[i] > ords[i+1]:
+ c += 1
+ tmp_int = ords[i]
+ ords[i] = ords[i+1]
+ ords[i+1] = tmp_int
+ tmp_cnt = count[i]
+ count[i] = count[i+1]
+ count[i+1] = tmp_cnt
+ print("Output:", ords)
+
+ints = [0, 1, 2, 3, 4, 5, 6, 7, 8]
+proc(ints)
+ints = [1024, 512, 256, 128, 64]
+proc(ints)