diff options
| author | Abigail <abigail@abigail.be> | 2021-11-17 16:09:09 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-11-17 19:20:28 +0100 |
| commit | 62167a58d3c55a71f37815381c858fad45825a8d (patch) | |
| tree | c2dab46599fc720673c6f1b4c8378c00e99dfeb2 | |
| parent | 83b91177f7b1aced78863b336bd236a40f7e1aef (diff) | |
| download | perlweeklychallenge-club-62167a58d3c55a71f37815381c858fad45825a8d.tar.gz perlweeklychallenge-club-62167a58d3c55a71f37815381c858fad45825a8d.tar.bz2 perlweeklychallenge-club-62167a58d3c55a71f37815381c858fad45825a8d.zip | |
Solutions for week 139
33 files changed, 1036 insertions, 0 deletions
diff --git a/challenge-139/abigail/awk/ch-1.awk b/challenge-139/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..85b6f4f2b9 --- /dev/null +++ b/challenge-139/abigail/awk/ch-1.awk @@ -0,0 +1,19 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +{ + sorted = 1 + for (i = 2; i <= NF; i ++) { + if ($(i - 1) > $i) { + sorted = 0 + } + } + print sorted +} diff --git a/challenge-139/abigail/awk/ch-2.awk b/challenge-139/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..dcc6f07d4c --- /dev/null +++ b/challenge-139/abigail/awk/ch-2.awk @@ -0,0 +1,35 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +function is_long (number, rest, seen, i) { + rest = 0 + delete seen + for (i = 2; i <= number; i ++) { + rest = (rest * BASE + BASE - 1) % number + if (seen [rest]) {return 0} + seen [rest] = 1 + } + return 1 +} + +BEGIN { + BASE = 10 + COUNT = 5 + number = 1 + + while (COUNT > 0) { + number ++ + if (BASE % number == 0) {continue} + if (is_long(number)) { + print (number) + COUNT -- + } + } +} diff --git a/challenge-139/abigail/bash/ch-1.sh b/challenge-139/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..292191ce6c --- /dev/null +++ b/challenge-139/abigail/bash/ch-1.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +while read -a list +do ((sorted = 1)) + for ((i = 1; i < ${#list[@]}; i ++)) + do if ((${list[$i - 1]} > ${list[$i]})) + then ((sorted = 0)) + fi + done + echo $sorted +done diff --git a/challenge-139/abigail/bash/ch-2.sh b/challenge-139/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..bbcc3c093d --- /dev/null +++ b/challenge-139/abigail/bash/ch-2.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh +# + +set -f + +BASE=10 +COUNT=5 + +function is_long () { + local number=$1 + local rest=0 + local seen=() + + for ((i = 0; i < number; i ++)) + do seen[$i]=0 + done + + for ((i = 2; i <= number; i ++)) + do ((rest = (rest * BASE + BASE - 1) % number)) + if ((${seen[$rest]} == 1)) + then is_long=0 + return + fi + seen[$rest]=1 + done + + is_long=1 +} + +number=1 +while ((COUNT > 0)) +do ((number ++)) + if ((BASE % number != 0)) + then is_long $number + if (($is_long == 1)) + then echo $number + ((COUNT --)) + fi + fi +done diff --git a/challenge-139/abigail/bc/ch-1.bc b/challenge-139/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..4afe2a1c11 --- /dev/null +++ b/challenge-139/abigail/bc/ch-1.bc @@ -0,0 +1,27 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc < input-file +# +# End each line with a 0. +# End the input with a 0 as the first number on a line +# + + +while (1) { + prev = read(); if (prev == 0) break; # End of input + sorted = 1; + while (1) { + next = read() + if (next == 0) { # End of line + sorted + break + } + if (prev > next) { + sorted = 0 + } + prev = next + } +} diff --git a/challenge-139/abigail/bc/ch-2.bc b/challenge-139/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..c51b1ab38d --- /dev/null +++ b/challenge-139/abigail/bc/ch-2.bc @@ -0,0 +1,40 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-2.bc +# + +base = 10 +count = 5 + +define is_long (number) { + for (i = 0; i < number; i ++) { + seen [i] = 0 + } + rest = 0 + for (i = 2; i <= number; i ++) { + rest = (rest * base + base - 1) % number + if (seen [rest] > 0) { + return (0) + } + seen [rest] = 1 + } + return (1) +} + + +number = 1 +for (;count > 0;) { + number = number + 1 + if (base % number == 0) { + continue + } + if (is_long (number) == 1) { + number + count = count - 1 + } +} + +quit diff --git a/challenge-139/abigail/c/ch-1.c b/challenge-139/abigail/c/ch-1.c new file mode 100644 index 0000000000..5a9e1269b7 --- /dev/null +++ b/challenge-139/abigail/c/ch-1.c @@ -0,0 +1,40 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <stdbool.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&line, &len, stdin)) != -1) { + int offset = 0; + int off; + int prev, next; + bool sorted = true; + if (sscanf (line, "%d%n", &prev, &offset) != 1) { + perror ("Failure to scan"); + exit (1); + } + while (sscanf (line + offset, "%d%n", &next, &off) == 1) { + offset += off; + if (prev > next) { + sorted = false; + } + prev = next; + } + printf ("%d\n", sorted ? 1 : 0); + } + free (line); + + return (0); +} diff --git a/challenge-139/abigail/c/ch-2.c b/challenge-139/abigail/c/ch-2.c new file mode 100644 index 0000000000..2a21888dfd --- /dev/null +++ b/challenge-139/abigail/c/ch-2.c @@ -0,0 +1,52 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <stdbool.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +# define BASE 10 +# define COUNT 5 + + +bool is_long (int number) { + int rest = 0; + bool * seen; + if ((seen = (bool *) malloc (number * sizeof (bool))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + for (size_t i = 0; i < number; i ++) { + seen [i] = false; + } + for (int i = 2; i <= number; i ++) { + rest = (rest * BASE + BASE - 1) % number; + if (seen [rest] ++) { + free (seen); + return (0); + } + } + free (seen); + return (1); +} + + +int main (void) { + int count = COUNT; + int number = 1; + while (count) { + number ++; + if (BASE % number == 0) {continue;} + if (is_long (number)) { + printf ("%d\n", number); + count --; + } + } + return (0); +} diff --git a/challenge-139/abigail/go/ch-1.go b/challenge-139/abigail/go/ch-1.go new file mode 100644 index 0000000000..2f3ad22c52 --- /dev/null +++ b/challenge-139/abigail/go/ch-1.go @@ -0,0 +1,44 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" + "bufio" + "os" + "strings" + "strconv" +) + +func main () { + var reader = bufio . NewReader (os. Stdin) + for { + var line, err = reader . ReadString ('\n') + if (err != nil) { + break + } + + line = strings . TrimSuffix (line, "\n") + + list := strings . Split (line, " ") + + sorted := true + for i := 1; sorted && i < len (list); i ++ { + prev, _ := strconv . Atoi (list [i - 1]) + next, _ := strconv . Atoi (list [i]) + sorted = prev <= next + } + + if (sorted) { + fmt . Println (1) + } else { + fmt . Println (0) + } + } +} diff --git a/challenge-139/abigail/go/ch-2.go b/challenge-139/abigail/go/ch-2.go new file mode 100644 index 0000000000..dd2bfa3a10 --- /dev/null +++ b/challenge-139/abigail/go/ch-2.go @@ -0,0 +1,46 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go +// + +import ( + "fmt" +) + +var ( + BASE int = 10 + COUNT int = 5 +) + +func is_long (number int) bool { + seen := make ([] bool, number) + rest := 0 + for i := 2; i <= number; i ++ { + rest = (rest * BASE + BASE - 1) % number + if seen [rest] { + return false + } + seen [rest] = true + } + return true +} + + +func main () { + number := 1 + for COUNT > 0 { + number ++ + if BASE % number == 0 { + continue + } + if is_long (number) { + fmt . Println (number) + COUNT -- + } + } +} diff --git a/challenge-139/abigail/java/ch-1.java b/challenge-139/abigail/java/ch-1.java new file mode 100644 index 0000000000..e5a102cb12 --- /dev/null +++ b/challenge-139/abigail/java/ch-1.java @@ -0,0 +1,25 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file +// + +import java.util.*; + +public class ch1 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNextLine ()) { + String line = scanner . nextLine (); + String [] list = line . split (" "); + boolean sorted = true; + for (int i = 1; sorted && i < list . length; i ++) { + sorted = Integer . parseInt (list [i - 1]) <= + Integer . parseInt (list [i]); + } + System . out . println (sorted ? 1 : 0); + } + } +} diff --git a/challenge-139/abigail/java/ch-2.java b/challenge-139/abigail/java/ch-2.java new file mode 100644 index 0000000000..a7f1c2715f --- /dev/null +++ b/challenge-139/abigail/java/ch-2.java @@ -0,0 +1,45 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2.java; java ch2 +// + +import java.util.*; + +public class ch2 { + public static final int BASE = 10; + public static final int COUNT = 5; + + public static boolean is_long (int number) { + boolean [] seen = new boolean [number]; + for (int i = 0; i < number; i ++) { + seen [i] = false; + } + int rest = 0; + for (int i = 2; i <= number; i ++) { + rest = (rest * BASE + BASE - 1) % number; + if (seen [rest]) { + return false; + } + seen [rest] = true; + } + return true; + } + + public static void main (String [] args) { + int number = 1; + int count = COUNT; + while (count > 0) { + number ++; + if (BASE % number == 0) { + continue; + } + if (is_long (number)) { + System . out . println (number); + count --; + } + } + } +} diff --git a/challenge-139/abigail/lua/ch-1.lua b/challenge-139/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..8a2603a7e8 --- /dev/null +++ b/challenge-139/abigail/lua/ch-1.lua @@ -0,0 +1,21 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + local sorted = 1 + local prev = nil + for next in line : gmatch ("[0-9]+") do + if prev ~= nil and prev > next then + sorted = 0 + end + prev = next + end + print (sorted) +end diff --git a/challenge-139/abigail/lua/ch-2.lua b/challenge-139/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..bc970a0db1 --- /dev/null +++ b/challenge-139/abigail/lua/ch-2.lua @@ -0,0 +1,41 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua +-- + +local BASE = 10 +local COUNT = 5 + +function is_long (number) + local rest = 0 + local seen = {} + + for _ = 2, number do + rest = (rest * BASE + BASE - 1) % number + if (seen [rest] ~= nil) then + return (0) + end + seen [rest] = 1 + end + + return (1) +end + + +local number = 1 +while COUNT > 0 do + number = number + 1 + if BASE % number == 0 then + goto end_loop + end + if is_long (number) == 1 then + print (number) + COUNT = COUNT - 1 + end + ::end_loop:: +end diff --git a/challenge-139/abigail/node/ch-1.js b/challenge-139/abigail/node/ch-1.js new file mode 100644 index 0000000000..f45be6188c --- /dev/null +++ b/challenge-139/abigail/node/ch-1.js @@ -0,0 +1,20 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + console . log (line . match (/[0-9]+/gi) . reduce ((sorted, _, i, list) => { + if (i > 0 && list [i - 1] > list [i]) {sorted = 0} + return sorted}, + 1) + ) +}) + diff --git a/challenge-139/abigail/node/ch-2.js b/challenge-139/abigail/node/ch-2.js new file mode 100644 index 0000000000..88f78d7b99 --- /dev/null +++ b/challenge-139/abigail/node/ch-2.js @@ -0,0 +1,37 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js +// + +let BASE = 10 +let COUNT = 5 + +function is_long (number) { + let seen = Array (number) . fill (0) + let rest = 0 + for (let i = 2; i <= number; i ++) { + rest = (rest * BASE + BASE - 1) % number + if (seen [rest] == 1) { + return 0 + } + seen [rest] = 1 + } + return 1 +} + +let number = 1 +while (COUNT > 0) { + number ++ + if (BASE % number == 0) { + continue + } + if (is_long (number)) { + console . log (number) + COUNT -- + } +} diff --git a/challenge-139/abigail/pascal/ch-1.p b/challenge-139/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..9fe398e6cc --- /dev/null +++ b/challenge-139/abigail/pascal/ch-1.p @@ -0,0 +1,29 @@ +Program ch1; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +var + prev, next: integer; + sorted: integer; + +begin + while not eof do begin + sorted := 1; + read (prev); + while not eoln do begin + read (next); + if prev > next then begin + sorted := 0; + end; + prev := next; + end; + readln (); + writeln (sorted); + end; +end. diff --git a/challenge-139/abigail/pascal/ch-2.p b/challenge-139/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..a5f15ac94e --- /dev/null +++ b/challenge-139/abigail/pascal/ch-2.p @@ -0,0 +1,52 @@ +Program ch2; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out *) +(* *) + +var + BASE: integer = 10; + COUNT: integer = 5; + number: integer = 1; + +function is_long (number: integer): boolean; + var + seen: array of boolean; + rest: integer; + i: integer; + + begin + setlength (seen, number); + for i := 0 to number - 1 do begin + seen [i] := false; + end; + + rest := 0; + + for i := 2 to number do begin + rest := (rest * BASE + BASE - 1) mod number; + if seen [rest] then begin + exit (false); + end; + seen [rest] := true; + end; + + exit (true); + end; + +begin + while COUNT > 0 do begin + inc (number); + if BASE mod number = 0 then begin + continue; + end; + if is_long (number) then begin + writeln (number); + dec (COUNT); + end + end +end. diff --git a/challenge-139/abigail/perl/ch-1.pl b/challenge-139/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..9373cb2d40 --- /dev/null +++ b/challenge-139/abigail/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# + +# +# A list is sorted if it doesn't contain two successive elements +# where the first element is larger than the second. +# + +while (<>) { + my @list = /[0-9]+/g; + say grep ({$list [$_ - 1] > $list [$_]} 1 .. $#list) ? 0 : 1 +} diff --git a/challenge-139/abigail/perl/ch-2.pl b/challenge-139/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..31ed9a671f --- /dev/null +++ b/challenge-139/abigail/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl +# + +# +# A prime p in base b is a long prime if ((b^(p - 1) - 1) / p) isn't repetitive. +# + +my $COUNT = 5; +my $BASE = 10; + +# +# Check whether ($BASE ** ($number - 1) - 1) / $number gives a result +# which repeats or not. We do this by performing long division, +# and check we don't repeat an intermediate result. +# +# We could have just calculated $BASE ** ($number - 1) - 1) / $number using +# big integers. We choose not to, using a method which can easily be ported +# to languages without support for big integers. +# +# Note, we never check whether $number is a prime. If it passes the test, +# it will be a prime (that is, no composite number will repeat with maximum +# period). +# +sub is_long ($number) { + my $rest = 0; + my %seen; + for (2 .. $number) { + return 0 if $seen {$rest = ($rest * $BASE + $BASE - 1) % $number} ++; + } + return 1; +} + + +my $number = 1; +while ($COUNT) { + $number ++; + next if $BASE % $number == 0; # Divisors of $BASE don't repeat. + next unless is_long $number; + say $number; + $COUNT --; +} diff --git a/challenge-139/abigail/python/ch-1.py b/challenge-139/abigail/python/ch-1.py new file mode 100644 index 0000000000..f2a73b2d4a --- /dev/null +++ b/challenge-139/abigail/python/ch-1.py @@ -0,0 +1,19 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + list = line . split (" ") + sorted = 1 + for i in range (1, len (list)): + if list [i - 1] > list [i]: + sorted = 0 + print (sorted) diff --git a/challenge-139/abigail/python/ch-2.py b/challenge-139/abigail/python/ch-2.py new file mode 100644 index 0000000000..509bfa8ee5 --- /dev/null +++ b/challenge-139/abigail/python/ch-2.py @@ -0,0 +1,32 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py +# + +BASE = 10 +COUNT = 5 + +def is_long (number): + seen = [0] * number + rest = 0 + for _ in range (1, number): + rest = (rest * BASE + BASE - 1) % number + if seen [rest] == 1: + return False + seen [rest] = 1 + return True + + +number = 1 +while COUNT > 0: + number = number + 1 + if BASE % number == 0: + continue + if is_long (number): + print (number) + COUNT = COUNT - 1 diff --git a/challenge-139/abigail/r/ch-1.r b/challenge-139/abigail/r/ch-1.r new file mode 100644 index 0000000000..7f9075c75b --- /dev/null +++ b/challenge-139/abigail/r/ch-1.r @@ -0,0 +1,26 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r < input-file +# + +stdin <- file ('stdin', 'r') +repeat { + line <- readLines (stdin, n = 1) + if (length (line) == 0) { + break + } + parts <- strsplit (line, " ") + list <- as.numeric (parts [[1]]) + sorted <- 1 + if (length (list) > 1) { + for (i in 2 : length (list)) { + if (list [i - 1] > list [i]) { + sorted <- 0 + } + } + } + cat (sorted, "\n") +} diff --git a/challenge-139/abigail/r/ch-2.r b/challenge-139/abigail/r/ch-2.r new file mode 100644 index 0000000000..eb6c47281d --- /dev/null +++ b/challenge-139/abigail/r/ch-2.r @@ -0,0 +1,35 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-2.r +# + +BASE <- 10 +COUNT <- 5 + +is_long <- function (number) { + seen <- vector ("numeric", number) + rest <- 0 + for (i in 2 : number) { + rest <- (rest * BASE + BASE - 1) %% number + if (seen [[1 + rest]] > 0) { + return (0) + } + seen [[1 + rest]] = 1 + } + return (1) +} + +number <- 1 +while (COUNT > 0) { + number <- number + 1 + if (BASE %% number == 0) { + next + } + if (is_long (number) == 1) { + cat (number, "\n") + COUNT <- COUNT - 1 + } +} diff --git a/challenge |
