diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-25 22:15:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-25 22:15:24 +0100 |
| commit | e0cc523f4bed884e476c02b3d1042dbbc7aa95ba (patch) | |
| tree | c7a58bac1f9470d18eceb12f8485160d6418b0e4 | |
| parent | da2be9db3f8b3b6ad32892ca777344798254741a (diff) | |
| parent | d77720c58cbe4f106e76631c8d30d38f9a89bb9c (diff) | |
| download | perlweeklychallenge-club-e0cc523f4bed884e476c02b3d1042dbbc7aa95ba.tar.gz perlweeklychallenge-club-e0cc523f4bed884e476c02b3d1042dbbc7aa95ba.tar.bz2 perlweeklychallenge-club-e0cc523f4bed884e476c02b3d1042dbbc7aa95ba.zip | |
Merge pull request #4596 from Abigail/abigail/week-122
Abigail/week 122
37 files changed, 6516 insertions, 41 deletions
diff --git a/challenge-122/abigail/README.md b/challenge-122/abigail/README.md index 6179e4c324..fff6c14deb 100644 --- a/challenge-122/abigail/README.md +++ b/challenge-122/abigail/README.md @@ -1,37 +1,25 @@ # Solutions by Abigail -## [Invert Bit][task1] +## [Average of Stream][task1] -[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-121/#TASK1 - -> You are given integers `0 <= $m <= 255` and `1 <= $n <= 8`. +> You are given a stream of numbers, `@N`. > -> Write a script to invert `$n` bit from the end of the binary -> representation of `$m` and print the decimal representation of -> the new binary number. - -### Examples -~~~~ -Input: $m = 12, $n = 3 -Output: 8 -~~~~ - -* Binary representation of `$m = 00001100` -* Invert 3rd bit from the end `= 00001000` -* Decimal equivalent of `00001000 = 8` +> Write a script to print the average of the stream at every point. +### Example ~~~~ -Input: $m = 18, $n = 4 -Output: 26 +Input: @N = (10, 20, 30, 40, 50, 60, 70, 80, 90, ...) +Output: 10, 15, 20, 25, 30, 35, 40, 45, 50, ... ~~~~ -* Binary representation of `$m = 00010010` -* Invert 3rd bit from the end `= 00011010` -* Decimal equivalent of `00011010 = 26` - +* Average of first number is `10`. +* Average of first 2 numbers `(10+20)/2 = 15`. +* Average of first 3 numbers `(10+20+30)/3 = 20`. +* Average of first 4 numbers `(10+20+30+40)/4 = 25` and so on. ### Solutions * [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.bf) * [C](c/ch-1.c) @@ -48,25 +36,43 @@ Output: 26 * [Tcl](tcl/ch-1.tcl) ### Blog -[Invert Bit][blog1] +[Average of Stream][blog1] -## [The Travelling Salesman][task2] +## [Basketball Points][task2] -> You are given a `NxN` matrix containing the distances between `N` cities. +> You are given a score `$S`. +> +> You can win basketball points e.g. `1` point, `2` points and `3` points. > -> Write a script to find a round trip of minimum length visiting all `N` -> cities exactly once and returning to the start. +> Write a script to find out the different ways you can score `$S`. -### Example +### Examples +~~~~ +Input: $S = 4 +Output: 1 1 1 1 + 1 1 2 + 1 2 1 + 1 3 + 2 1 1 + 2 2 + 3 1 ~~~~ -Matrix: [0, 5, 2, 7] - [5, 0, 5, 3] - [3, 1, 0, 6] - [4, 5, 4, 0] -Output: - length = 10 - tour = (0 2 1 3 0) +~~~~ +Input: $S = 5 +Output: 1 1 1 1 1 + 1 1 1 2 + 1 1 2 1 + 1 1 3 + 1 2 1 1 + 1 2 2 + 1 3 1 + 2 1 1 1 + 2 1 2 + 2 2 1 + 2 3 + 3 1 1 + 3 2 ~~~~ ### Solutions @@ -80,11 +86,11 @@ Output: * [Ruby](ruby/ch-2.rb) ### Blog -[The Travelling Salesman][blog2] +[Basketball Points][blog2] -[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-121/#TASK1 -[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-121/#TASK2 -[blog1]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-121-1.html -[blog2]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-121-2.html +[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-122/#TASK1 +[task1]: https://perlweeklychallenge.org/blog/perl-weekly-challenge-122/#TASK2 +[blog1]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-122-1.html +[blog2]: https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-122-2.html diff --git a/challenge-122/abigail/awk/ch-1.awk b/challenge-122/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..56429b32de --- /dev/null +++ b/challenge-122/abigail/awk/ch-1.awk @@ -0,0 +1,11 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +{print ((s += $1) / NR)} diff --git a/challenge-122/abigail/awk/ch-2.awk b/challenge-122/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..32ed981320 --- /dev/null +++ b/challenge-122/abigail/awk/ch-2.awk @@ -0,0 +1,30 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +{ + c [0] = 0 + c [1] = 0 + c [2] = 1 + s [2, 0] = "" + + for (i = 3; i < $1 + 3; i ++) { + c [i] = 0 + for (j = 1; j <= 3; j ++) { + for (k = 0; k < c [i - j]; k ++) { + s [i, c [i]] = j " " s [i - j, k] + c [i] ++ + } + } + } + + for (k = 0; k < c [$1 + 2]; k ++) { + print s [$1 + 2, k] + } +} diff --git a/challenge-122/abigail/bash/ch-1.sh b/challenge-122/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..29d3a79f9d --- /dev/null +++ b/challenge-122/abigail/bash/ch-1.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +while read n; do echo $(((s += n) / ++ c)); done diff --git a/challenge-122/abigail/bash/ch-2.sh b/challenge-122/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..d523f918bf --- /dev/null +++ b/challenge-122/abigail/bash/ch-2.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +declare scores +l=$'\n' +scores[2]=$l + +read n + +for ((i = 3; i < n + 3; i ++)) +do for ((j = 1; j <= 3; j ++)) + do scores[$i]=${scores[$i]}${scores[$((i - j))]//$l/$l$j } + done +done + +echo "${scores[$((n + 2))]/$l/}" diff --git a/challenge-122/abigail/basic/ch-1.bas b/challenge-122/abigail/basic/ch-1.bas new file mode 100644 index 0000000000..d13b6f7b41 --- /dev/null +++ b/challenge-122/abigail/basic/ch-1.bas @@ -0,0 +1,15 @@ +010 REM +020 REM See ../README.md +030 REM + +040 REM +050 REM Run as: basic.pl ch-1.bas < input-file +051 REM Input file should be terminated with a 0. +060 REM + +100 INPUT n +110 IF n = 0 THEN END +150 s = s + n +160 c = c + 1 +170 PRINT s / c +200 GOTO 100 diff --git a/challenge-122/abigail/bc/ch-1.bc b/challenge-122/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..a1ec963533 --- /dev/null +++ b/challenge-122/abigail/bc/ch-1.bc @@ -0,0 +1,15 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc < input-file +# +# Terminate the input with 0 +# + +while (1) { + n = read () + if (n == 0) {break} + (s += n) / ++ c +} diff --git a/challenge-122/abigail/befunge-93/ch-1.bf93 b/challenge-122/abigail/befunge-93/ch-1.bf93 new file mode 100644 index 0000000000..9d4d852ed5 --- /dev/null +++ b/challenge-122/abigail/befunge-93/ch-1.bf93 @@ -0,0 +1,2 @@ +088p 0 >>>>>>>>>>>>>>>>>>>>>>>>>>>v +& :1+!#@_ + : 88g1+ :88p / . 55+, > diff --git a/challenge-122/abigail/blog.txt b/challenge-122/abigail/blog.txt new file mode 100644 index 0000000000..6e3e030814 --- /dev/null +++ b/challenge-122/abigail/blog.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-122-1.html diff --git a/challenge-122/abigail/blog1.txt b/challenge-122/abigail/blog1.txt new file mode 100644 index 0000000000..4100cddcd6 --- /dev/null +++ b/challenge-122/abigail/blog1.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-122-2.html diff --git a/challenge-122/abigail/c/ch-1.c b/challenge-122/abigail/c/ch-1.c new file mode 100644 index 0000000000..4d81d738fd --- /dev/null +++ b/challenge-122/abigail/c/ch-1.c @@ -0,0 +1,23 @@ +# 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) { + int n; + int s = 0; + int c = 0; + + while (scanf ("%d", &n) == 1) { + printf ("%d\n", (s += n) / ++ c); + } + + return (0); +} diff --git a/challenge-122/abigail/c/ch-2.c b/challenge-122/abigail/c/ch-2.c new file mode 100644 index 0000000000..4d3282c024 --- /dev/null +++ b/challenge-122/abigail/c/ch-2.c @@ -0,0 +1,138 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +typedef long long number; + +int main (void) { + int n; + char *** scores; + number * count; /* Counts the number of possibilities for each + entry in scores */ + size_t ** lengths; /* Length of each entry in scores */ + if (scanf ("%d", &n) != 1) { + perror ("Unexpected input"); + exit (1); + } + + if ((scores = (char ***) malloc ((n + 3) * sizeof (char **))) == NULL) { + perror ("Malloc scores failed"); + exit (1); + } + if ((count = (number *) malloc ((n + 3) * sizeof (number))) == NULL) { + perror ("Malloc count failed"); + exit (1); + } + if ((lengths = (size_t **) malloc ((n + 3) * sizeof (size_t *))) == NULL) { + perror ("Malloc lengths failed"); + exit (1); + } + + /* + * Initialize + */ + count [0] = 0; + count [1] = 0; + count [2] = 1; + + if ((scores [2] = (char **) malloc (sizeof (char *))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + if ((scores [2] [0] = (char *) malloc (sizeof (char))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + if ((lengths [2] = (size_t *) malloc (sizeof (size_t))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + scores [2] [0] [0] = '\0'; + lengths [2] [0] = 0; + + /* + * Main loop + */ + for (int i = 3; i < n + 3; i ++) { + /* + * First, allocate the right amount of for scores [i] and + * lengths [i] which is going to be + * count [i] = count [i - 1] + count [i - 2] + count [i - 3] + */ + count [i] = count [i - 1] + count [i - 2] + count [i - 3]; + if ((scores [i] = (char **) malloc (count [i] * sizeof (char *))) + == NULL) { + perror ("Malloc failed"); + exit (1); + } + if ((lengths [i] = (size_t *) malloc (count [i] * sizeof (size_t))) + == NULL) { + perror ("Malloc failed"); + exit (1); + } + /* + * Copy strings, with single scores prepended. + */ + number l = 0; + for (int j = 1; j <= 3; j ++) { + for (int k = 0; k < count [i - j]; k ++) { + lengths [i] [l] = 2 + lengths [i - j] [k]; + if ((scores [i] [l] = (char *) malloc + ((lengths [i] [l] + 1) * sizeof (char))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + scores [i] [l] [0] = j + '0'; + scores [i] [l] [1] = ' '; + strncpy (scores [i] [l] + 2, scores [i - j] [k], + lengths [i - j] [k]); + scores [i] [l] [lengths [i] [l]] = '\0'; + + l ++; + } + } + /* + * We now don't need lengths [i - 3] or scores [i - 3] anymore, + * so we can free() up the memory it uses. + */ + if (i - 3 > 1) { + for (int k = 0; k < count [i - 3]; k ++) { + free (scores [i - 3] [k]); + } + free (scores [i - 3]); + free (lengths [i - 3]); + } + } + + /* + * Print the wanted scores + */ + for (int i = 0; i < count [n + 2]; i ++) { + printf ("%s\n", scores [n + 2] [i]); + } + + /* + * free() memory we have not released yet. + */ + for (int i = n; i <= n + 2; i ++) { + for (int k = 0; k < count [i]; k ++) { + free (scores [i] [k]); + } + free (scores [i]); + free (lengths [i]); + } + + free (scores); + free (lengths); + free (count); + + return (0); +} diff --git a/challenge-122/abigail/go/ch-1.go b/challenge-122/abigail/go/ch-1.go new file mode 100644 index 0000000000..f7059b6091 --- /dev/null +++ b/challenge-122/abigail/go/ch-1.go @@ -0,0 +1,28 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" +) + +func main () { + var n, s, c int; + s = 0 + c = 0 + for { + var _, err = fmt . Scanf ("%d", &n) + if (err != nil) { + break + } + s += n + c ++ + fmt . Printf ("%d\n", s / c) + } +} diff --git a/challenge-122/abigail/java/ch-1.java b/challenge-122/abigail/java/ch-1.java new file mode 100644 index 0000000000..d11ff387c4 --- /dev/null +++ b/challenge-122/abigail/java/ch-1.java @@ -0,0 +1,19 @@ +// +// 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); + int s = 0, c = 0; + while (scanner . hasNext ()) { + System . out . println ((s += scanner . nextInt ()) / ++ c); + } + } +} diff --git a/challenge-122/abigail/lua/ch-1.lua b/challenge-122/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..b20e835f46 --- /dev/null +++ b/challenge-122/abigail/lua/ch-1.lua @@ -0,0 +1,18 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +local s = 0 +local c = 0 + +for n in io . lines () do + s = s + tonumber (n) + c = c + 1 + print (s / c) +end diff --git a/challenge-122/abigail/lua/ch-2.lua b/challenge-122/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..949ff29456 --- /dev/null +++ b/challenge-122/abigail/lua/ch-2.lua @@ -0,0 +1,30 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +local n = io . read ("*number") +local scores = {} + +scores [1] = {} +scores [2] = {} +scores [3] = {} +scores [3] [1] = "" + +for i = 4, n + 3 do + scores [i] = {} + for j = 1, 3 do + for _, v in ipairs (scores [i - j]) do + scores [i] [1 + #scores [i]] = j .. " " .. v + end + end +end + +for i, v in ipairs (scores [n + 3]) do + print (v) +end diff --git a/challenge-122/abigail/node/ch-1.js b/challenge-122/abigail/node/ch-1.js new file mode 100644 index 0000000000..4626ee3f41 --- /dev/null +++ b/challenge-122/abigail/node/ch-1.js @@ -0,0 +1,16 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + +let s = 0 +let c = 0 + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', n => {console . log ((s +=+ n) / ++ c)}) diff --git a/challenge-122/abigail/node/ch-2.js b/challenge-122/abigail/node/ch-2.js new file mode 100644 index 0000000000..d1e0cd8b0b --- /dev/null +++ b/challenge-122/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', n => { + let scores = [] + scores [0] = [] + scores [1] = [] + scores [2] = [""] + + n =+ n + for (i = 3; i < n + 3; i ++) { + scores [i] = [] + for (j = 1; j <= 3; j ++) { + scores [i] . push (... scores [i - j] . map (s => j + " " + s)) + } + } + + scores [n + 2] . forEach (s => console . log (s)) +}) + diff --git a/challenge-122/abigail/pascal/ch-1.p b/challenge-122/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..5979459606 --- /dev/null +++ b/challenge-122/abigail/pascal/ch-1.p @@ -0,0 +1,24 @@ +Program Average; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +var + n: integer; + s: integer = 0; + c: integer = 0; + + +begin + while not eof () do begin + readln (n); + s := s + n; + c := c + 1; + writeln (s div c); + end +end. diff --git a/challenge-122/abigail/perl/ch-1.pl b/challenge-122/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..98659c4a39 --- /dev/null +++ b/challenge-122/abigail/perl/ch-1.pl @@ -0,0 +1,11 @@ +#!/opt/perl/bin/perl + +# +# See ../README.md +# + +# +# Run as: perl -pl ch-1.pl < input-file +# + +$_=($;+=$_)/$. diff --git a/challenge-122/abigail/perl/ch-2.pl b/challenge-122/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..0fc39351e8 --- /dev/null +++ b/challenge-122/abigail/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/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 +# + +# +# Reads a single value from standard input +# + +# +# $s [n] will contain the possible ways to get to a score of n - 2: +# +# $s [0] is empty, as there is no way to get to a score of -2 +# $s [1] is empty, as there is no way to get to a score of -1 +# $s [2] contains the empty string, as there is only one way to get +# to a score of 0 (not scoring). +# +# To calculate $s [k], k > 2, we take the union of: +# +# The scores from $s [k - 1], prepended by "1"; +# the scores from $s [k - 2], prepended by "2"; +# the scores from $s [k - 3], prepended by "3"; +# +# A triple nested map will do the trick. +# + +my @s = ([], [], [""]); +map {push @s => [map {my $s = $_; map {"$s $_"} @{$s [-$s]}} 1 .. 3]} 1 .. <>; +say for @{$s [-1]} diff --git a/challenge-122/abigail/python/ch-1.py b/challenge-122/abigail/python/ch-1.py new file mode 100644 index 0000000000..b2fdeea20c --- /dev/null +++ b/challenge-122/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 + +s = 0 +c = 0 + +for n in fileinput . input (): + s = s + int (n) + c = c + 1 + print (s // c) diff --git a/challenge-122/abigail/python/ch-2.py b/challenge-122/abigail/python/ch-2.py new file mode 100644 index 0000000000..2c10b39f25 --- /dev/null +++ b/challenge-122/abigail/python/ch-2.py @@ -0,0 +1,25 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import sys + +n = int (sys . stdin . readline ()) + +scores = [[], [], [""]] + +for i in range (n): + new = [] + for j in range (3): + for k in scores [len (scores) - 1 - j]: + new . append (str (j + 1) + " " + k) + scores . append (new) + +for score in scores [-1]: + print (score) diff --git a/challenge-122/abigail/r/ch-1.r b/challenge-122/abigail/r/ch-1.r new file mode 100644 index 0000000000..f6b4ad74b0 --- /dev/null +++ b/challenge-122/abigail/r/ch-1.r @@ -0,0 +1,18 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r < input-file +# + +stdin <- file ('stdin', 'r') +s <- 0 +c <- 0 +repeat { + n <- readLines (stdin, n = 1) + if (length (n) == 0) { + break + } + cat ((s <- s + as.integer (n)) / (c <- c + 1), "\n") +} diff --git a/challenge-122/abigail/ruby/ch-1.rb b/challenge-122/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..5bf102ac2a --- /dev/null +++ b/challenge-122/abigail/ruby/ch-1.rb @@ -0,0 +1,19 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +s = 0 +c = 0 + +ARGF . each_line do + |n| + s += n . to_i + c += 1 + puts (s / c) +end diff --git a/challenge-122/abigail/ruby/ch-2.rb b/challenge-122/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..8627a9ae8a --- /dev/null +++ b/challenge-122/abigail/ruby/ch-2.rb @@ -0,0 +1,27 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +n = gets . to_i + +scores = [[], [], [""]] + +for i in 1 .. n do + new = [] + for j in 1 .. 3 do + for k in scores [-j] do + new . push (j . to_s + " " + k) + end + end + scores . push (new) +end + +for score in scores [-1] do + puts (score) +end diff --git a/challenge-122/abigail/scheme/ch-1.scm b/challenge-122/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..62d55033a5 --- /dev/null +++ b/challenge-122/abigail/scheme/ch-1.scm @@ -0,0 +1,27 @@ +;;; |
