diff options
author | Franciszek Malinka <franciszek.malinka@gmail.com> | 2021-10-05 21:49:54 +0200 |
---|---|---|
committer | Franciszek Malinka <franciszek.malinka@gmail.com> | 2021-10-05 21:49:54 +0200 |
commit | c5fcf7179a83ef65c86c6a4a390029149e518649 (patch) | |
tree | d29ffc5b86a0d257453cedcf87d91a13d8bf3b0d /semestr-4/ask/lista11 | |
parent | f8a88b6a4aba1f66d04711a9330eaba49a50c463 (diff) |
Duzy commit ze smieciami
Diffstat (limited to 'semestr-4/ask/lista11')
-rw-r--r-- | semestr-4/ask/lista11/__pycache__/gen.cpython-38.pyc | bin | 0 -> 1323 bytes | |||
-rw-r--r-- | semestr-4/ask/lista11/gen.py | 65 | ||||
-rw-r--r-- | semestr-4/ask/lista11/zad8.c | 23 |
3 files changed, 88 insertions, 0 deletions
diff --git a/semestr-4/ask/lista11/__pycache__/gen.cpython-38.pyc b/semestr-4/ask/lista11/__pycache__/gen.cpython-38.pyc Binary files differnew file mode 100644 index 0000000..e27b7a7 --- /dev/null +++ b/semestr-4/ask/lista11/__pycache__/gen.cpython-38.pyc diff --git a/semestr-4/ask/lista11/gen.py b/semestr-4/ask/lista11/gen.py new file mode 100644 index 0000000..d4de762 --- /dev/null +++ b/semestr-4/ask/lista11/gen.py @@ -0,0 +1,65 @@ +def next_permutation(a): + """Generate the lexicographically next permutation inplace. + + https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order + Return false if there is no next permutation. + """ + # Find the largest index i such that a[i] < a[i + 1]. If no such + # index exists, the permutation is the last permutation + for i in reversed(range(len(a) - 1)): + if a[i] < a[i + 1]: + break # found + else: # no break: not found + return False # no next permutation + + # Find the largest index j greater than i such that a[i] < a[j] + j = next(j for j in reversed(range(i + 1, len(a))) if a[i] < a[j]) + + # Swap the value of a[i] with that of a[j] + a[i], a[j] = a[j], a[i] + + # Reverse sequence from a[i + 1] up to and including the final element a[n] + a[i + 1:] = reversed(a[i + 1:]) + return True + +def perm_to_str(a): + return ''.join(map(str, a)) + +def str_to_perm(s): + return [int(c) for c in s] + +t = [0,1,2,3] +perm_to_idx = dict() + +def bbin(x): + return bin(x)[2:] + +cnt = 23 +while True: + perm_to_idx[perm_to_str(t)] = cnt + cnt -= 1 + if not next_permutation(t): + break + +for p in perm_to_idx.keys(): + for i in range(4): + t = str_to_perm(p) + for j in range(4): + if t[j] > t[i]: + t[j] -= 1 + t[i] = 3 + print(p, 'x', i, '->', perm_to_str(t), ':\t', bbin(perm_to_idx[p]), '\t', bbin(i), '\t', bbin(perm_to_idx[perm_to_str(t)]), end='\t') + print(perm_to_idx[p], '\t', i, '\t', perm_to_idx[perm_to_str(t)]) + +print('\n-----\n') + + +for p in perm_to_idx.keys(): + for i in range(4): + t = str_to_perm(p) + for j in range(4): + if t[j] > t[i]: + t[j] -= 1 + t[i] = 3 + print(p, 'x', i, '->', perm_to_str(t), ':\t', bbin(perm_to_idx[p]), '\t', bbin(i), '\t', bbin(perm_to_idx[perm_to_str(t)]), end='\t') + print(perm_to_idx[p], '\t', i, '\t', perm_to_idx[perm_to_str(t)]) diff --git a/semestr-4/ask/lista11/zad8.c b/semestr-4/ask/lista11/zad8.c new file mode 100644 index 0000000..cd8c2bf --- /dev/null +++ b/semestr-4/ask/lista11/zad8.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <stdint.h> + +uint8_t victim(uint8_t s) { + s |= ((s & 0x55) << 1) | (s & 0xaa >> 1); + return ((s >> 2) && 1) | ((s >> 4) && 2) | ((s >> 6) && 3); +} + +uint8_t update(uint8_t s, uint8_t v) { + int8_t p0 = victim(s); + int8_t p1 = victim(s ^ 0b01010101); + int8_t p2 = victim(s ^ 0b10101010); + int8_t p3 = victim(s ^ 0b11111111); + uint8_t age = (3 << (v << 1)) & s; + + s -= (~((char)(age - 1) >> 7)) & (1 << (p1 << 1)); + s -= (~((char)(age - 2) >> 7)) & (1 << (p2 << 1)); + s -= (~((char)(age - 3) >> 7)) & (1 << (p3 << 1)); + s |= (3 << (v << 1)); + return s; +} + +int main() {}
\ No newline at end of file |