diff options
| author | Abigail <abigail@abigail.freedom.nl> | 2021-12-27 18:20:34 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.freedom.nl> | 2021-12-29 20:19:41 +0100 |
| commit | a7cfe19e7988033b8b37d4effedcf01ee203ff0e (patch) | |
| tree | ab78dd124aef81b9f58ec35ca24e9b768e1d3512 | |
| parent | 767f501835ea07823607293b532b6a0520fafc3b (diff) | |
| download | perlweeklychallenge-club-a7cfe19e7988033b8b37d4effedcf01ee203ff0e.tar.gz perlweeklychallenge-club-a7cfe19e7988033b8b37d4effedcf01ee203ff0e.tar.bz2 perlweeklychallenge-club-a7cfe19e7988033b8b37d4effedcf01ee203ff0e.zip | |
Week 145
35 files changed, 972 insertions, 17 deletions
diff --git a/challenge-145/abigail/README.md b/challenge-145/abigail/README.md index 554ccda4b8..9841e920c1 100644 --- a/challenge-145/abigail/README.md +++ b/challenge-145/abigail/README.md @@ -4,35 +4,33 @@ * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) -* [Basic](basic/ch-1.bas) -* [Bc](bc/ch-1.bc) -* [Befunge-93](befunge-93/ch-1.bf93) +* [bc](bc/ch-1.bc) * [C](c/ch-1.c) -* [Cobol](cobol/ch-1.cb) -* [Csh](csh/ch-1.csh) -* [Erlang](lua/ch-1.erl) -* [Forth](lua/ch-1.fs) -* [Fortran](fortran/ch-1.f90) * [Go](go/ch-1.go) * [Java](java/ch-1.java) * [Lua](lua/ch-1.lua) -* [m4](m4/ch-1.m4) -* [MMIX](mmix/ch-1.mms) * [Node.js](node/ch-1.js) -* [OCaml](ocaml/ch-1.ml) * [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) -* [PHP](php/ch-1.php) -* [PostScript](postscript/ch-1.ps) * [Python](python/ch-1.py) * [R](r/ch-1.r) -* [Rexx](rexx/ch-1.rexx) * [Ruby](ruby/ch-1.rb) -* [Scheme](scheme/ch-1.scm) -* [Sed](sed/ch-1.sed) -* [SQL](sql/ch-1.sql) * [Tcl](tcl/ch-1.tcl) +* [Scheme](scheme/ch-1.scm) ## Part 2 +* [AWK](awk/ch-2.awk) +* [Bash](bash/ch-2.sh) +* [C](c/ch-2.c) +* [Go](go/ch-2.go) +* [Java](java/ch-2.java) +* [Lua](lua/ch-2.lua) +* [Node.js](node/ch-2.js) +* [Pascal](pascal/ch-2.p) * [Perl](perl/ch-2.pl) +* [Python](python/ch-2.py) +* [R](r/ch-2.r) +* [Ruby](ruby/ch-2.rb) +* [Tcl](tcl/ch-2.tcl) +* [Scheme](scheme/ch-2.scm) diff --git a/challenge-145/abigail/awk/ch-1.awk b/challenge-145/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..e5dd36fb5e --- /dev/null +++ b/challenge-145/abigail/awk/ch-1.awk @@ -0,0 +1,23 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +{ + for (i = 1; i <= NF; i ++) { + if (NR == 1) { + a [i] = $i + } + else { + sum += a [i] * $i + } + } + if (NR == 2) { + print sum + } +} diff --git a/challenge-145/abigail/awk/ch-2.awk b/challenge-145/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..4158b059c5 --- /dev/null +++ b/challenge-145/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_palindrome (string, l) { + l = length (string) + return l <= 1 ? 1 \ + : substr (string, 1, 1) != substr (string, l, 1) ? 0 \ + : is_palindrome(substr (string, 2, l - 2)) +} + +{ + delete palindromes + count = 0 + for (i = 1; i <= length ($0); i ++) { + for (j = i; j <= length ($0); j ++) { + s = substr ($0, i, j - i + 1) + if (is_palindrome(s)) { + if (palindromes [s] ++ == 0) { + if (count ++) { + printf (" ") + } + printf ("%s", s) + } + } + } + } + printf ("\n") +} diff --git a/challenge-145/abigail/bash/ch-1.sh b/challenge-145/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..f9495e1871 --- /dev/null +++ b/challenge-145/abigail/bash/ch-1.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +read -a a +read -a b +for ((i = 0; i < ${#a[@]}; i ++)) +do ((sum += a[i] * b[i])) +done +echo $sum diff --git a/challenge-145/abigail/bash/ch-2.sh b/challenge-145/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..97bf3f6bdf --- /dev/null +++ b/challenge-145/abigail/bash/ch-2.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +declare -A palindromes + +function is_palindrome () { + local string=$1 + if ((${#string} < 2)) + then + is_palindrome=1 + return + fi + local f=${string:0:1} + local s=${string: -1:1} + if [ $f != $s ] + then is_palindrome=0 + return + fi + is_palindrome ${string:1:$((${#string} - 2))} +} + + + +while read line +do palindromes=() + for ((i = 0; i < ${#line}; i ++)) + do for ((j = i; j < ${#line}; j ++)) + do sub=${line:$i:$((j-i+1))} + is_palindrome $sub + if (($is_palindrome == 1)) + then palindromes[$sub]=1 + fi + done + done + first=1 + for p in "${!palindromes[@]}" + do if ((first == 1)) + then ((first ++)) + else printf " " + fi + printf %s $p + done + echo +done diff --git a/challenge-145/abigail/bc/ch-1.bc b/challenge-145/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..2b1ce8842d --- /dev/null +++ b/challenge-145/abigail/bc/ch-1.bc @@ -0,0 +1,21 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc < input-file +# End first line with a 0. +# + +while (1) { + a = read () + if (a == 0) {break} + a [i] = a + i = i + 1 +} + +for (j = 0; j < i; j ++) { + sum += a [j] * read () +} + +sum diff --git a/challenge-145/abigail/c/ch-1.c b/challenge-145/abigail/c/ch-1.c new file mode 100644 index 0000000000..9aa118bfcd --- /dev/null +++ b/challenge-145/abigail/c/ch-1.c @@ -0,0 +1,35 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +int main (void) { + char * line1 = NULL; + char * line2 = NULL; + size_t len = 0; + long long sum = 0; + long long num1, num2; + int skip1, skip2; + + if (getline (&line1, &len, stdin) == -1 || + getline (&line2, &len, stdin) == -1) { + perror ("Failed to read input"); + exit (1); + } + while (sscanf (line1, "%lld%n", &num1, &skip1) == 1 && + sscanf (line2, "%lld%n", &num2, &skip2) == 1) { + sum += num1 * num2; + line1 += skip1; + line2 += skip2; + } + printf ("%lld\n", sum); + + return (0); +} diff --git a/challenge-145/abigail/c/ch-2.c b/challenge-145/abigail/c/ch-2.c new file mode 100644 index 0000000000..aeb3dc8779 --- /dev/null +++ b/challenge-145/abigail/c/ch-2.c @@ -0,0 +1,87 @@ +# 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 + */ + +int cmp (const void * a, const void * b) { + return strcmp (* (char **) a, * (char **) b); +} + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&line, &len, stdin)) != -1) { + str_len --; /* Don't care about trailing newline */ + /* + * If we have a string of all identical characters + * we have the maximum amount of substrings which + * are palindromes. For a string of length N, this + * will be N * (N + 1) / 2 palindromes. + */ + char ** palindromes; + size_t p_count = 0; + if ((palindromes = (char **) malloc ( + (str_len * (str_len + 1) / 2) * sizeof (char *))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + + for (size_t i = 0; i < str_len; i ++) { + for (size_t j = i; j < str_len; j ++) { + /* + * Now test whether line [i .. j] is a palindrome + */ + bool is_palindrome = true; + for (size_t k = 0; is_palindrome && i + k < j - k; k ++) { + if (line [i + k] != line [j - k]) { + is_palindrome = false; + } + } + if (is_palindrome) { + char * palindrome; + if ((palindrome = (char *) malloc ((j - i + 2) * + sizeof (char))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + strncpy (palindrome, line + i, j - i + 1); + palindrome [j - i + 1] = '\0'; + + palindromes [p_count ++] = palindrome; + } + } + } + + /* + * Sort all the palindromes. Once sorted, it's easy to print + * the uniques once: any unique palindrome is either first, + * of different then the one before it. + */ + qsort (palindromes, p_count, sizeof (char *), cmp); + for (size_t i = 0; i < p_count; i ++) { + if (i == 0 || strcmp (palindromes [i], palindromes [i - 1])) { + if (i) { + printf (" "); + } + printf ("%s", palindromes [i]); + } + free (palindromes [i]); + } + printf ("\n"); + + free (palindromes); + } + + free (line); + return (0); +} diff --git a/challenge-145/abigail/go/ch-1.go b/challenge-145/abigail/go/ch-1.go new file mode 100644 index 0000000000..4ad6f719d0 --- /dev/null +++ b/challenge-145/abigail/go/ch-1.go @@ -0,0 +1,34 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go < input-file +// + +import ( + "fmt" + "bufio" + "os" + "strconv" + "strings" +) + +func main () { + reader := bufio . NewReader (os. Stdin) + a, _ := reader . ReadString ('\n') + b, _ := reader . ReadString ('\n') + aa := strings . Split (strings . Trim (a, "\n"), " ") + bb := strings . Split (strings . Trim (b, "\n"), " ") + sum := 0 + + for i := 0; i < len (aa); i ++ { + a, _ := strconv . Atoi (aa [i]) + b, _ := strconv . Atoi (bb [i]) + sum += a * b + } + + fmt . Println (sum) +} diff --git a/challenge-145/abigail/go/ch-2.go b/challenge-145/abigail/go/ch-2.go new file mode 100644 index 0000000000..9fc4b52c37 --- /dev/null +++ b/challenge-145/abigail/go/ch-2.go @@ -0,0 +1,48 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go +// + +import ( + "fmt" + "bufio" + "os" +) + +func is_palindrome (str string) bool { + runes := [] rune (str) + for i, j := 0, len (runes) - 1; i < j; i, j = i + 1, j - 1 { + if runes [i] != runes [j] { + return false + } + } + return true +} + +func main () { + var reader = bufio . NewReader (os. Stdin) + for { + var text, err = reader . ReadString ('\n') + if (err != nil) { + break + } + palindromes := make (map [string] bool) + for i := 0; i < len (text) - 1; i ++ { + for j := i; j < len (text) - 1; j ++ { + substr := text [i : j + 1] + if is_palindrome (substr) { + palindromes [substr] = true + } + } + } + for k := range palindromes { + fmt . Printf ("%s ", k) + } + fmt . Println ("") + } +} diff --git a/challenge-145/abigail/java/ch-1.java b/challenge-145/abigail/java/ch-1.java new file mode 100644 index 0000000000..9b25253951 --- /dev/null +++ b/challenge-145/abigail/java/ch-1.java @@ -0,0 +1,26 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file +// + +import java.util.*; +import java.util.ArrayList; + +public class ch1 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + ArrayList <Integer> numbers = new ArrayList <Integer> (); + while (scanner . hasNextInt ()) { + numbers . add (scanner . nextInt ()); + } + int size = numbers . size () / 2; + int sum = 0; + for (int i = 0; i < size; i ++) { + sum += numbers . get (i) * numbers . get (size + i); + } + System . out . println (sum); + } +} diff --git a/challenge-145/abigail/java/ch-2.java b/challenge-145/abigail/java/ch-2.java new file mode 100644 index 0000000000..0de7c4b3f8 --- /dev/null +++ b/challenge-145/abigail/java/ch-2.java @@ -0,0 +1,38 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2.java; java ch2 < input-file +// + +import java.util.*; +import java.util.Hashtable; +import java.util.Map; + +public class ch2 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNextLine ()) { + String line = scanner . nextLine (); + + Map <String, Boolean> palindromes = + new Hashtable <String, Boolean> (); + + for (int i = 0; i < line . length (); i ++) { + for (int j = i; j < line . length (); j ++) { + String string = line . substring (i, j + 1); + if (string . equals (new StringBuffer (string) . + reverse () . toString ())) { + if (!palindromes . containsKey (string)) { + palindromes . put (string, true); + System . out . print (string); + System . out . print (" "); + } + } + } + } + System . out . println (""); + } + } +} diff --git a/challenge-145/abigail/lua/ch-1.lua b/challenge-145/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..27ab36961a --- /dev/null +++ b/challenge-145/abigail/lua/ch-1.lua @@ -0,0 +1,30 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +local a = {} +local b = {} + +local a_line = io . read ("*l") +local b_line = io . read ("*l") + +for n in a_line : gmatch ("-?[0-9]+") do + a [#a + 1] = tonumber (n) +end +for n in b_line : gmatch ("-?[0-9]+") do + b [#b + 1] = tonumber (n) +end + +local sum = 0 + +for i = 1, #a do + sum = sum + a [i] * b [i] +end + +print (sum) diff --git a/challenge-145/abigail/lua/ch-2.lua b/challenge-145/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..521f8b0acf --- /dev/null +++ b/challenge-145/abigail/lua/ch-2.lua @@ -0,0 +1,26 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +for line in io . lines () do + local palindromes = {} + local out = "" + for i = 1, #line do + for j = i, #line do + local string = line : sub (i, j) + if string == string : reverse () then + if palindromes [string] == nil + then out = out .. string .. " " + palindromes [string] = 1 + end + end + end + end + print (out : sub (1, #out - 1)) +end diff --git a/challenge-145/abigail/node/ch-1.js b/challenge-145/abigail/node/ch-1.js new file mode 100644 index 0000000000..2d4b94d896 --- /dev/null +++ b/challenge-145/abigail/node/ch-1.js @@ -0,0 +1,15 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + +let fs = require ("fs") +let [a, b] = fs . readFileSync (0) . toString () . trim () . split ("\n") . + map (line => (line . split (" ")) . map (n => +n)) + +console . log (a . reduce ((sum, n, i) => {return sum + a [i] * b [i]}, 0)) diff --git a/challenge-145/abigail/node/ch-2.js b/challenge-145/abigail/node/ch-2.js new file mode 100644 index 0000000000..d9ac4952f2 --- /dev/null +++ b/challenge-145/abigail/node/ch-2.js @@ -0,0 +1,29 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let palindromes = {} + let out = "" + line = line . trim () + for (let i = 0; i < line . length; i ++) { + for (let j = i; j < line . length; j ++) { + let string = line . substring (i, j + 1) + if (string == string . split ("") . reverse () . join ("")) { + if (!(string in palindromes)) { + out = out + string + " " + palindromes [string] = 1 + } + } + } + } + console . log (out) +}) diff --git a/challenge-145/abigail/pascal/ch-1.p b/challenge-145/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..2644e9aa0b --- /dev/null +++ b/challenge-145/abigail/pascal/ch-1.p @@ -0,0 +1,37 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +var + n, size, j, half, sum: integer; + numbers: array of integer; + +begin + (* *) + (* Read in data into a single array *) + (* *) + size := 0; + while not eof do begin + read (n); + inc (size); + setlength (numbers, size); + numbers [size - 1] := n; + end; + + (* *) + (* Calculate the dot product *) + (* *) + sum := 0; + half := (size - 1) div 2; + for j := 0 to half do begin + sum := sum + numbers [j] * numbers [half + j]; + end; + + writeln (sum); +end. diff --git a/challenge-145/abigail/pascal/ch-2.p b/challenge-145/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..f7328d3a0d --- /dev/null +++ b/challenge-145/abigail/pascal/ch-2.p @@ -0,0 +1,38 @@ +Program ch2; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *) +(* *) + +uses + StrUtils, fgl; + +var + line: string; + i, j: integer; + substr: string; + palindromes: specialize TFPGMap <string, boolean>; + +begin + while not eof do begin + palindromes := specialize TFPGMap <string, boolean> . Create; + readln (line); + for i := 1 to length (line) do begin + for j := i to length (line) do begin + substr := copy (line, i, j - i + 1); + if substr = ReverseString (substr) then begin + if palindromes . IndexOf (substr) = -1 then begin + write (substr, ' '); + palindromes . Add (substr, true); + end + end + end + end; + writeln (''); + palindromes . Free; + end +end. diff --git a/challenge-145/abigail/perl/ch-1.pl b/challenge-145/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..712ef7ffbd --- /dev/null +++ b/challenge-145/abigail/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# We want the dot product. Math::Matrix gives us the dotproduct. +# + +# +# Run as: perl ch-1.pl < input-file +# + +use Math::Matrix; +say Math::Matrix:: -> new ([split ' ' => scalar <>]) -> dot_product + (Math::Matrix:: -> new ([split ' ' => scalar <>])); diff --git a/challenge-145/abigail/perl/ch-2.pl b/challenge-145/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..e8452b84e0 --- /dev/null +++ b/challenge-145/abigail/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/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 < input-file +# + +# +# Once again, there is a choice to be made. The maker of the challenge +# gives us two, conflicting, options: +# +# "Do as I say" +# +# and +# +# "Do as I do" +# +# +# The challenge *description* asks us to create a *Palindromic Tree*, +# linking to a blog describing the elaborate graph structure (and the +# algorithm to create it) which is a Palindromic Tree. +# +# The challenge *examples* complete ignores the requirement of a +# palindromic tree, heck, doesn't even bother to create anything +# which even remotely looks like any kind of graph or other form of +# datastructure, and just outputs all palindromic substrings of +# the input, with duplicates removed. +# +# Sigh. +# +# Conflicting challenges like this push me ever further to the point +# of abandoning the weekly challenges. I'm very close to the edge. +# + +# +# We'll be doing the "do as I do" option. +# + +local $, = $"; +while (<>) { + my %seen; + /(.+)(?{$seen {$1} ++ if $1 eq reverse $1})(*FAIL)/; + # + # Do we need to keep the order in which the palindromes + # appear in the word? The examples do, but the description + # is, (obviously, because the description bares no resemblance + # to the examples) silent about that. + # + # We'll judge that the order appearing in the examples is an + # artifact of the algorithm used to calculate the answers, and + # not an requirement. + # + say keys %seen; +} + diff --git a/challenge-145/abigail/python/ch-1.py b/challenge-145/abigail/python/ch-1.py new file mode 100644 index 0000000000..43913ceb0e --- /dev/null +++ b/challenge-145/abigail/python/ch-1.py @@ -0,0 +1,18 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +a = [int (x) for x in input () . split (" ")] +b = [int (x) for x in input () . split (" ")] + +sum = 0 +for i in range (len (a)): + sum = sum + a [i] * b [i] + +print (sum) diff --git a/challenge-145/abigail/python/ch-2.py b/challenge-145/abigail/python/ch-2.py new file mode 100644 index 0000000000..e3bd9f7986 --- /dev/null +++ b/challenge-145/abigail/python/ch-2.py @@ -0,0 +1,23 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + |
