From f8a88b6a4aba1f66d04711a9330eaba49a50c463 Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Mon, 24 May 2021 12:40:58 +0200 Subject: update --- Semestr 4/ask/lista11/gen.py | 65 ++++++++++++++++++++++++++++++++++++++++++++ Semestr 4/ask/lista11/zad8.c | 23 ++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 Semestr 4/ask/lista11/gen.py create mode 100644 Semestr 4/ask/lista11/zad8.c (limited to 'Semestr 4/ask/lista11') 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 +#include + +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 -- cgit v1.2.3