From cc7611c673901442ddfac5a71c691350144dcd03 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 27 May 2024 00:30:38 -0400 Subject: Week 271 --- challenge-271/zapwai/python/ch-1.py | 32 +++++++++++++++++++++++++++ challenge-271/zapwai/python/ch-2.py | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 challenge-271/zapwai/python/ch-1.py create mode 100644 challenge-271/zapwai/python/ch-2.py (limited to 'challenge-271/zapwai/python') 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) -- cgit