diff options
Diffstat (limited to 'challenge-271/zapwai/python/ch-2.py')
| -rw-r--r-- | challenge-271/zapwai/python/ch-2.py | 44 |
1 files changed, 44 insertions, 0 deletions
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) |
