diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-13 19:31:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-13 19:31:22 +0100 |
| commit | 6f7a2e3cbbec44cc216c6d0e6a370f9ec60e3c84 (patch) | |
| tree | 0ac9f540650e975d68834fad4197928eda9c20f2 | |
| parent | ace3e8d314fab84624d1498c05d228e5ab6df16b (diff) | |
| parent | 8d2394c5f5968392ece3d6d73a5f98e267edf40f (diff) | |
| download | perlweeklychallenge-club-6f7a2e3cbbec44cc216c6d0e6a370f9ec60e3c84.tar.gz perlweeklychallenge-club-6f7a2e3cbbec44cc216c6d0e6a370f9ec60e3c84.tar.bz2 perlweeklychallenge-club-6f7a2e3cbbec44cc216c6d0e6a370f9ec60e3c84.zip | |
Merge pull request #5017 from Abigail/abigail/week-134
Abigail/week 134
41 files changed, 824 insertions, 0 deletions
diff --git a/challenge-134/abigail/README.md b/challenge-134/abigail/README.md index d51d3d73e2..7cb7880ca3 100644 --- a/challenge-134/abigail/README.md +++ b/challenge-134/abigail/README.md @@ -2,9 +2,41 @@ ## Part 1 +* [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) +* [C](c/ch-1.c) +* [Erlang](erlang/ch-1.erl) +* [Go](go/ch-1.go) +* [Java](java/ch-1.java) +* [Lua](lua/ch-1.lua) +* [m4](m4/ch-1.m4) +* [Node.js](node/ch-1.js) +* [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) +* [PHP](php/ch-1.php) +* [Python](python/ch-1.py) +* [R](r/ch-1.r) +* [Ruby](ruby/ch-1.rb) +* [Scheme](scheme/ch-1.scm) +* [Tcl](tcl/ch-1.tcl) ## Part 2 +* [AWK](awk/ch-2.awk) +* [Bash](bash/ch-2.sh) +* [bc](bc/ch-2.bc) * [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) +* [Scheme](scheme/ch-2.scm) +* [Tcl](tcl/ch-2.tcl) diff --git a/challenge-134/abigail/awk/ch-1.awk b/challenge-134/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..e1bb7f9695 --- /dev/null +++ b/challenge-134/abigail/awk/ch-1.awk @@ -0,0 +1,16 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk +# + +BEGIN { + split ("789 798 879 897 978", a, " ") + for (i = 1; i <= 5; i ++) { + print "1023456" a [i] + } +} diff --git a/challenge-134/abigail/awk/ch-2.awk b/challenge-134/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..fa2f6a5397 --- /dev/null +++ b/challenge-134/abigail/awk/ch-2.awk @@ -0,0 +1,23 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +{ + delete a + for (i = 1; i <= $1; i ++) { + for (j = 1; j <= $2; j ++) { + a [i * j] = 1 + } + } + c = 0 + for (i in a) { + c ++ + } + print c +} diff --git a/challenge-134/abigail/bash/ch-1.sh b/challenge-134/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..3520ce0f68 --- /dev/null +++ b/challenge-134/abigail/bash/ch-1.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh +# + +for n in 789 798 879 897 978; do echo 1023456$n; done diff --git a/challenge-134/abigail/bash/ch-2.sh b/challenge-134/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..466b8d5586 --- /dev/null +++ b/challenge-134/abigail/bash/ch-2.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +declare -A seen + +while read n m +do seen=() + for ((i = 1; i <= n; i ++)) + do for ((j = 1; j <= m; j ++)) + do seen[$((i * j))]=1 + done + done + echo ${#seen[@]} +done diff --git a/challenge-134/abigail/basic/ch-1.bas b/challenge-134/abigail/basic/ch-1.bas new file mode 100644 index 0000000000..d548a47195 --- /dev/null +++ b/challenge-134/abigail/basic/ch-1.bas @@ -0,0 +1,18 @@ +010 REM +020 REM See ../README.md +030 REM + +040 REM +050 REM Run as: basic ch-1.bas +060 REM + +100 DIM T(5) +110 T (1) = 789 +120 T (2) = 798 +130 T (3) = 879 +140 T (4) = 897 +150 T (5) = 978 + +200 FOR i = 1 TO 5 +210 PRINT 1023456000 + T (i) +220 NEXT i diff --git a/challenge-134/abigail/bc/ch-1.bc b/challenge-134/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..435c743e1c --- /dev/null +++ b/challenge-134/abigail/bc/ch-1.bc @@ -0,0 +1,12 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc +# +t[1] = 789; t[2] = 798; t[3] = 879; t[4] = 897; t[5] = 978 +for (i = 1; i <= 5; i ++) { + 1023456000 + t[i] +} +quit diff --git a/challenge-134/abigail/bc/ch-2.bc b/challenge-134/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..a9ce05c2c4 --- /dev/null +++ b/challenge-134/abigail/bc/ch-2.bc @@ -0,0 +1,25 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-2.bc < input-file +# + +while (1) { + m = read(); if (m == 0) break + n = read(); if (n == 0) break + for (i = 1; i <= m * n; i ++) { + s[i] = 0 + } + count = 0 + for (i = 1; i <= n; i ++) { + for (j = 1; j <= m; j ++) { + if (s[i * j] == 0) { + count = count + 1 + s[i * j] = 1 + } + } + } + count +} diff --git a/challenge-134/abigail/befunge-93/ch-1.bf93 b/challenge-134/abigail/befunge-93/ch-1.bf93 new file mode 100644 index 0000000000..02498a6c93 --- /dev/null +++ b/challenge-134/abigail/befunge-93/ch-1.bf93 @@ -0,0 +1,4 @@ +"879798978897987"v +>,,,,,,,,,,55+, v +^ : +^"1023456" _@ diff --git a/challenge-134/abigail/c/ch-1.c b/challenge-134/abigail/c/ch-1.c new file mode 100644 index 0000000000..d65af1ef32 --- /dev/null +++ b/challenge-134/abigail/c/ch-1.c @@ -0,0 +1,22 @@ +# 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 + */ + +# define SIZE 5 + +short tail [] = {789, 798, 879, 897, 978}; + +int main (void) { + for (size_t i = 0; i < SIZE; i ++) { + printf ("1023456%d\n", tail [i]); + } + return (0); +} diff --git a/challenge-134/abigail/c/ch-2.c b/challenge-134/abigail/c/ch-2.c new file mode 100644 index 0000000000..523dc497e6 --- /dev/null +++ b/challenge-134/abigail/c/ch-2.c @@ -0,0 +1,51 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <limits.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +int main (void) { + int n, m; + char * buffer; + + while (scanf ("%d%d", &n, &m) == 2) { + int size = 1 + m * n / CHAR_BIT; + /* + * Allocate memory for our buffer + */ + if ((buffer = (char *) malloc (size * sizeof (char))) == NULL) { + perror ("Failed to malloc buffer"); + exit (1); + } + /* + * Set all the bits to 0 + */ + for (int i = 0; i < size; i ++) { + buffer [i] = (char) 0; + } + + int count = 0; /* Count the number of different products */ + + for (int i = 1; i <= m; i ++) { + for (int j = 1; j <= n; j ++) { + int index = (i * j) / CHAR_BIT; + int bit = (i * j) % CHAR_BIT; + if ((buffer [index] & (1 << bit)) == 0) { + count ++; + buffer [index] |= (1 << bit); + } + } + } + printf ("%d\n", count); + free (buffer); + } + + return (0); +} diff --git a/challenge-134/abigail/erlang/ch-1.erl b/challenge-134/abigail/erlang/ch-1.erl new file mode 100644 index 0000000000..0ce5451fb5 --- /dev/null +++ b/challenge-134/abigail/erlang/ch-1.erl @@ -0,0 +1,22 @@ +% +% See ../README.md +% + +% +% Run as: ln ch-1.erl ch1.erl +% erl -compile ch1 +% erl -noshell -s ch1 main -s init stop +% + +-module (ch1). +-export ([main/0]). + +func ([]) -> ok; +func ([H|T]) -> + io:fwrite ("1023456~w~n", [H]), + func (T). + + +main () -> + func ([789, 798, 879, 897, 978]). + diff --git a/challenge-134/abigail/go/ch-1.go b/challenge-134/abigail/go/ch-1.go new file mode 100644 index 0000000000..e83f7089bf --- /dev/null +++ b/challenge-134/abigail/go/ch-1.go @@ -0,0 +1,19 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" +) + +func main () { + for _, t := range [] int {789, 798, 879, 897, 978} { + fmt . Printf ("1023456%d\n", t) + } +} diff --git a/challenge-134/abigail/go/ch-2.go b/challenge-134/abigail/go/ch-2.go new file mode 100644 index 0000000000..0d6dd810cc --- /dev/null +++ b/challenge-134/abigail/go/ch-2.go @@ -0,0 +1,34 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go < input-file +// + +import ( + "fmt" +) + +func main () { + for { + var n, m int + c, err := fmt . Scanf ("%d %d", &n, &m); + if (c != 2 || err != nil) { + break; + } + count := 0 + seen := map [int] bool { } + for i := 1; i <= n; i ++ { + for j := 1; j <= m; j ++ { + if _, ok := seen [i * j]; !ok { + count ++; + seen [i * j] = true; + } + } + } + fmt . Printf ("%d\n", count); + } +} diff --git a/challenge-134/abigail/java/ch-1.java b/challenge-134/abigail/java/ch-1.java new file mode 100644 index 0000000000..84baa6d18f --- /dev/null +++ b/challenge-134/abigail/java/ch-1.java @@ -0,0 +1,18 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 +// + +import java.util.*; + +public class ch1 { + public static void main (String [] args) { + int [] tails = {789, 798, 879, 897, 978}; + for (int i: tails) { + System . out . println (1023456000 + i); + } + } +} diff --git a/challenge-134/abigail/java/ch-2.java b/challenge-134/abigail/java/ch-2.java new file mode 100644 index 0000000000..91ed32973e --- /dev/null +++ b/challenge-134/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); + try { + while (true) { + int n = scanner . nextInt (); + int m = scanner . nextInt (); + + Map <Integer, Integer> seen = + new Hashtable <Integer, Integer> (); + + for (int i = 1; i <= n; i ++) { + for (int j = 1; j <= m; j ++) { + seen . put (i * j, 1); + } + } + System . out . println (seen . size ()); + } + } + catch (Exception e) { + // + // EOF + // + } + } +} diff --git a/challenge-134/abigail/lua/ch-1.lua b/challenge-134/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..1b0d219dce --- /dev/null +++ b/challenge-134/abigail/lua/ch-1.lua @@ -0,0 +1,13 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua +-- + +for _, tail in ipairs ({789, 798, 879, 897, 978}) do + print (1023456000 + tail) +end diff --git a/challenge-134/abigail/lua/ch-2.lua b/challenge-134/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..4ea5926b60 --- /dev/null +++ b/challenge-134/abigail/lua/ch-2.lua @@ -0,0 +1,24 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +for line in io . lines () do + local _, _, m, n = line : find ("([0-9]+)%s+([0-9]+)") + local seen = {} + local count = 0 + for x = 1, m do + for y = 1, n do + if seen [x * y] == nil then + seen [x * y] = 1 + count = count + 1 + end + end + end + print (count) +end diff --git a/challenge-134/abigail/m4/ch-1.m4 b/challenge-134/abigail/m4/ch-1.m4 new file mode 100644 index 0000000000..35bb86b814 --- /dev/null +++ b/challenge-134/abigail/m4/ch-1.m4 @@ -0,0 +1,25 @@ +define(`com')dnl +com()dnl +com(`Macros to simulate an array. First macros fetches a value')dnl +com(`while the second macro sets a value.')dnl +com()dnl +define(`tail',`defn(format(``tail[%d]'',`$1'))')dnl +define(`tail_set',`define(format(``tail[%d]'',`$1'),`1023456$2')')dnl +com()dnl +com(`Load the "tail" array with values')dnl +com()dnl +tail_set(`5',`789')dnl +tail_set(`4',`798')dnl +tail_set(`3',`879')dnl +tail_set(`2',`897')dnl +tail_set(`1',`978')dnl +com()dnl +com(`Recursive macro to print the results from the given value')dnl +com(`down to "1". Better call it with a positive integer value')dnl +com()dnl +define(`run',`tail($1) +ifelse($1,1,,`run(eval($1-1))')')dnl +com()dnl +com(`We want five values')dnl +com()dnl +run(5)dnl diff --git a/challenge-134/abigail/node/ch-1.js b/challenge-134/abigail/node/ch-1.js new file mode 100644 index 0000000000..b58aafa82f --- /dev/null +++ b/challenge-134/abigail/node/ch-1.js @@ -0,0 +1,11 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js +// + +[789, 798, 879, 897, 978] . forEach (t => console . log ("1023456" + t)) diff --git a/challenge-134/abigail/node/ch-2.js b/challenge-134/abigail/node/ch-2.js new file mode 100644 index 0000000000..ae1d561c97 --- /dev/null +++ b/challenge-134/abigail/node/ch-2.js @@ -0,0 +1,22 @@ +#!/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 [n, m] = line . trim () . split (" ") . map (x => +x) + let seen = {} + for (let i = 1; i <= n; i ++) { + for (let j = 1; j <= m; j ++) { + seen [i * j] = 1 + } + } + console . log (Object . keys (seen) . length) +}) diff --git a/challenge-134/abigail/pascal/ch-1.p b/challenge-134/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..0a88f11efe --- /dev/null +++ b/challenge-134/abigail/pascal/ch-1.p @@ -0,0 +1,19 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +var + tails: array [1 .. 5] of integer = (789, 798, 879, 897, 978); + i: integer; + +begin + for i := 1 to 5 do begin + writeln (1023456000 + tails [i]); + end +end. diff --git a/challenge-134/abigail/pascal/ch-2.p b/challenge-134/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..b255ae62d6 --- /dev/null +++ b/challenge-134/abigail/pascal/ch-2.p @@ -0,0 +1,34 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *) +(* *) + +var + n, m, i, j, count: integer; + seen: array of integer; + +begin + while (not eof) do begin + readln (n, m); + setLength (seen, n * m); + for i := 1 to n * m do begin + seen [i] := 0; + end; + count := 0; + for i := 1 to n do begin + for j := 1 to m do begin + if seen [i * j] = 0 + then begin + count := count + 1; + seen [i * j] := 1; + end + end + end; + writeln (count); + end +end. diff --git a/challenge-134/abigail/perl/ch-1.pl b/challenge-134/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..b04f5958bb --- /dev/null +++ b/challenge-134/abigail/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/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 +# + +# +# The first 40320 (= 8!) pandigital numbers in base 10 are 10 digits long, +# all starting with '10'. The other 8 digits are all the permutations of +# of the digits 2 .. 8. +# +# So, to generate the first 5 of them, we start with the first one, +# 1023456789, chop of the last three digits (7, 8, 9), get all the +# 6 (= 3!) permutations, sort them, throw away the last, and put them +# after the first six digits. +# + +# +# 6 permutations, we can easily do by hand. That's quicker then searching +# on CPAN for a module which does the work for us. +# + +say "1023456$_" for qw [789 798 879 897 978]; diff --git a/challenge-134/abigail/perl/ch-2.pl b/challenge-134/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..3911a034ba --- /dev/null +++ b/challenge-134/abigail/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/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 +# + +# +# It's not clear what actually needs to be outputted here. +# The challenges asks to generate a multiplication table, but to +# display a count. But the output of the examples shows a table, +# a list of distinct numbers, and a count of said list. +# +# It's often that the challenge it totally unclear where the +# boundary is between "this is output we expect", and "this is us +# explaining how to get that output". +# +# So, we will just display a count, as that is what is asked for us. +# Wnat more? Spend a few more seconds on writing the challenge. +# + +# +# We'll just multiply all the numbers 1 <= $x <= $n with all the numbers +# 1 <= $y <= $m, stick them in a hash, and display the number of different +# keys in the resulting hash. +# + +while (<>) { + ($~, $=) = split; + %~ = map {$; = $_; map {$; * $_ => 1} 1 .. $~} 1 .. $=; + say ~~%~; +} + +__END__ diff --git a/challenge-134/abigail/php/ch-1.php b/challenge-134/abigail/php/ch-1.php new file mode 100644 index 0000000000..2df5731b91 --- /dev/null +++ b/challenge-134/abigail/php/ch-1.php @@ -0,0 +1,14 @@ +<?php + // + // See ../README.md + // + + // + // Run as: php ch-1.php + // + $tails = array (789, 798, 879, 897, 978); + + foreach ($tails as &$tail) { + echo "1023456$tail\n"; + } +?> diff --git a/challenge-134/abigail/python/ch-1.py b/challenge-134/abigail/python/ch-1.py new file mode 100644 index 0000000000..74f9f35566 --- /dev/null +++ b/challenge-134/abigail/python/ch-1.py @@ -0,0 +1,11 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py +# + +for t in [789, 798, 879, 897, 978]: print (1023456000 + t) diff --git a/challenge-134/abigail/python/ch-2.py b/challenge-134/abigail/python/ch-2.py new file mode 100644 index 0000000000..4f07debc11 --- /dev/null +++ b/challenge-134/abigail/python/ch-2.py @@ -0,0 +1,19 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput + +for line in fileinput . input (): + [n, m] = line . split () + seen = {} + for i in range (int (n)): # 0 .. (n - 1) + for j in range (int (m)): # 0 .. (m - 1) + seen [(i + 1) * (j + 1)] = 1 + print (len (seen)) diff --git a/challenge-134/abigail/r/ch-1.r b/challenge-134/abigail/r/ch-1.r new file mode 100644 index 0000000000..e58f54cbfd --- /dev/null +++ b/challenge-134/abigail/r/ch-1.r @@ -0,0 +1,11 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r +# + +t <- c (789, 798, 879, 897, 978) +t <- t + 1023456000 +cat (t, sep = "\n") diff --git a/challenge-134/abigail/r/ch-2.r b/challenge-134/abigail/r/ch-2.r new file mode 100644 index 0000000000..2e7c3e93e7 --- /dev/null +++ b/challenge-134/abigail/r/ch-2.r @@ -0,0 +1,29 @@ +# +# See ../READM |
