summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormathiasmagnusson <mathiasmagnussons@gmail.com>2022-02-28 19:10:54 +0100
committermathiasmagnusson <mathiasmagnussons@gmail.com>2022-02-28 19:10:54 +0100
commitd76ab9f13f60e9ca8a0e578d9cb209064a9a790d (patch)
tree9be7b73fd162d9668f56993f798768a339714450
parentd5473756b304ebda9eeee6592e4555d15c910ee2 (diff)
downloadprogramming-problem-solving-d76ab9f13f60e9ca8a0e578d9cb209064a9a790d.tar.gz
Minor input format degeneralization
-rw-r--r--kattis-kth-alginda-quicksort/gen.py5
-rw-r--r--kattis-kth-alginda-quicksort/radix.c2
-rw-r--r--kattis-kth-alginda-quicksort/radix64.c31
-rwxr-xr-xkattis-kth-alginda-quicksort/test10
-rwxr-xr-xkattis-kth-alginda-quicksort/test.fish10
5 files changed, 26 insertions, 32 deletions
diff --git a/kattis-kth-alginda-quicksort/gen.py b/kattis-kth-alginda-quicksort/gen.py
index 37c4095..2e5067b 100644
--- a/kattis-kth-alginda-quicksort/gen.py
+++ b/kattis-kth-alginda-quicksort/gen.py
@@ -3,9 +3,8 @@ from sys import argv
lo = int(argv[1])
hi = int(argv[2])
-n = int(argv[3])
+n = int(argv[3]) if len(argv) > 3 else r(1, 600000)
xs = [r(lo, hi) for _ in range(n)]
-print(len(xs), " ")
-print(' \n '.join(str(x) for x in xs))
+print(len(xs), *(str(x) for x in xs))
diff --git a/kattis-kth-alginda-quicksort/radix.c b/kattis-kth-alginda-quicksort/radix.c
index b297d6f..b195d57 100644
--- a/kattis-kth-alginda-quicksort/radix.c
+++ b/kattis-kth-alginda-quicksort/radix.c
@@ -55,7 +55,7 @@ int main() {
char *p = buffer;
char *end = &buffer[len];
int n = 0;
- while (*p != ' ' && *p != '\n') {
+ while (*p != ' ') {
n *= 10;
n += *p - '0';
p++;
diff --git a/kattis-kth-alginda-quicksort/radix64.c b/kattis-kth-alginda-quicksort/radix64.c
index 4113a94..54b0155 100644
--- a/kattis-kth-alginda-quicksort/radix64.c
+++ b/kattis-kth-alginda-quicksort/radix64.c
@@ -18,7 +18,7 @@ void radix_sort(int n) {
long *in = tmp;
// we loop an even amount of times so the completely sorted array
// will be in xs, not tmp
- for (int m = 0; m < 44; m += 8) {
+ for (int m = 0; m < 40; m += 8) {
long *tmp = out;
out = in;
in = tmp;
@@ -39,12 +39,13 @@ void radix_sort(int n) {
}
}
}
+
int main() {
ssize_t len = read(0, buffer, BUFFER_MAX);
char *p = buffer;
int n = 0;
- while (*p != ' ' && *p != '\n') {
+ while (*p != ' ') {
n *= 10;
n += *p - '0';
p++;
@@ -57,20 +58,17 @@ int main() {
long x = 0;
bool c = true;
- if (c) x += (long)(*p-- - '0') << 0; if (*p == ' ' || *p == '-') c = false;
- if (c) x += (long)(*p-- - '0') << 4; if (*p == ' ' || *p == '-') c = false;
- if (c) x += (long)(*p-- - '0') << 8; if (*p == ' ' || *p == '-') c = false;
- if (c) x += (long)(*p-- - '0') << 12; if (*p == ' ' || *p == '-') c = false;
- if (c) x += (long)(*p-- - '0') << 16; if (*p == ' ' || *p == '-') c = false;
- if (c) x += (long)(*p-- - '0') << 20; if (*p == ' ' || *p == '-') c = false;
- if (c) x += (long)(*p-- - '0') << 24; if (*p == ' ' || *p == '-') c = false;
- if (c) x += (long)(*p-- - '0') << 28; if (*p == ' ' || *p == '-') c = false;
- if (c) x += (long)(*p-- - '0') << 32; if (*p == ' ' || *p == '-') c = false;
+ if (c) x += (long)(*p-- - '0') << 0; if (*p == ' ' || *p == '\n') c = false;
+ if (c) x += (long)(*p-- - '0') << 4; if (*p == ' ' || *p == '\n') c = false;
+ if (c) x += (long)(*p-- - '0') << 8; if (*p == ' ' || *p == '\n') c = false;
+ if (c) x += (long)(*p-- - '0') << 12; if (*p == ' ' || *p == '\n') c = false;
+ if (c) x += (long)(*p-- - '0') << 16; if (*p == ' ' || *p == '\n') c = false;
+ if (c) x += (long)(*p-- - '0') << 20; if (*p == ' ' || *p == '\n') c = false;
+ if (c) x += (long)(*p-- - '0') << 24; if (*p == ' ' || *p == '\n') c = false;
+ if (c) x += (long)(*p-- - '0') << 28; if (*p == ' ' || *p == '\n') c = false;
+ if (c) x += (long)(*p-- - '0') << 32; if (*p == ' ' || *p == '\n') c = false;
if (c) x += (long)(*p-- - '0') << 36;
- if (*p == '-') p--;
- else x |= 1l << 40;
-
xs[i] = x;
}
@@ -81,9 +79,7 @@ int main() {
*p-- = '\n';
for (int i = n - 1; i >= 0; i--) {
long x = xs[i];
- bool neg = x >> 40 == 0;
- x &= ~(1l << 40);
- *p-- = (x & 0xf) + '0'; x >>= 4;
+ *p-- = (x & 0xf) + '0'; x >>= 4;
if (x) *p-- = (x & 0xf) + '0'; x >>= 4;
if (x) *p-- = (x & 0xf) + '0'; x >>= 4;
if (x) *p-- = (x & 0xf) + '0'; x >>= 4;
@@ -93,7 +89,6 @@ int main() {
if (x) *p-- = (x & 0xf) + '0'; x >>= 4;
if (x) *p-- = (x & 0xf) + '0'; x >>= 4;
if (x) *p-- = (x & 0xf) + '0'; x >>= 4;
- if (neg) *p-- = '-';
if (i > 0) *p-- = '\n';
}
write(1, p + 1, last - p);
diff --git a/kattis-kth-alginda-quicksort/test b/kattis-kth-alginda-quicksort/test
new file mode 100755
index 0000000..4314cbf
--- /dev/null
+++ b/kattis-kth-alginda-quicksort/test
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+echo hej > out_1
+echo hej > out_2
+
+while diff out_1 out_2; do
+ python gen.py -2147483648 2147483647 > max
+ "$1" < max > out_1
+ "$2" < max > out_2
+done
diff --git a/kattis-kth-alginda-quicksort/test.fish b/kattis-kth-alginda-quicksort/test.fish
deleted file mode 100755
index 6aaf60c..0000000
--- a/kattis-kth-alginda-quicksort/test.fish
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/fish
-
-echo hej > out_c
-echo hej > out_r
-
-while diff out_c out_r
- python gen.py -2147483648 2147483647 600000 > max
- time ./radix < max > out_r
- time ~/.cache/target/release/kattis-kth-alginda-quicksort < max > out_c
-end