aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMyoungjin JEON <jeongoon@gmail.com>2020-08-28 00:36:44 +1000
committerMyoungjin JEON <jeongoon@gmail.com>2020-08-28 00:36:44 +1000
commitf270b932857463ce2a02308887394ac34a032578 (patch)
tree35b00aa800a81052f7ad73ae5da577ebf6823f2d
parent259dac5ffd3c11744f484a2493403ebc6a16249e (diff)
parentdaac3b1c349ff174e90ad24255654cbf07a4b756 (diff)
downloadperlweeklychallenge-club-f270b932857463ce2a02308887394ac34a032578.tar.gz
perlweeklychallenge-club-f270b932857463ce2a02308887394ac34a032578.tar.bz2
perlweeklychallenge-club-f270b932857463ce2a02308887394ac34a032578.zip
Merge remote-tracking branch 'upstream/master' into ch-075
-rw-r--r--.gitignore3
l---------[-rw-r--r--]challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj21
l---------[-rw-r--r--]challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj34
-rw-r--r--challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj20
-rw-r--r--challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj33
-rw-r--r--challenge-075/james-smith/README.md131
-rw-r--r--challenge-075/james-smith/perl/ch-1.pl29
-rw-r--r--challenge-075/james-smith/perl/ch-2.pl34
-rw-r--r--challenge-075/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-075/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-075/luca-ferrari/raku/ch-1.p681
-rw-r--r--challenge-075/luca-ferrari/raku/ch-2.p6104
-rw-r--r--challenge-075/mark-anderson/raku/ch-1.raku41
-rw-r--r--challenge-075/mark-anderson/raku/ch-2.raku51
-rw-r--r--challenge-075/markus-holzer/raku/ch-1.raku6
-rw-r--r--challenge-075/markus-holzer/raku/ch-2.bonus.raku5
-rw-r--r--challenge-075/markus-holzer/raku/ch-2.raku3
-rw-r--r--challenge-075/mathmauney/README1
-rw-r--r--challenge-075/mathmauney/perl/ch-1.pl65
-rw-r--r--challenge-075/mathmauney/perl/ch-2.pl36
-rw-r--r--challenge-075/roger-bell-west/blog.txt1
-rwxr-xr-xchallenge-075/roger-bell-west/perl/ch-1.pl44
-rwxr-xr-xchallenge-075/roger-bell-west/perl/ch-1v.pl53
-rwxr-xr-xchallenge-075/roger-bell-west/perl/ch-2.pl31
-rwxr-xr-xchallenge-075/roger-bell-west/perl/ch-2v.pl33
-rwxr-xr-xchallenge-075/roger-bell-west/python/ch-1.py34
-rwxr-xr-xchallenge-075/roger-bell-west/python/ch-2.py24
-rwxr-xr-xchallenge-075/roger-bell-west/raku/ch-1.p640
-rwxr-xr-xchallenge-075/roger-bell-west/raku/ch-2.p628
-rw-r--r--challenge-075/sgreen/README.md94
-rw-r--r--challenge-075/sgreen/blog.txt1
-rwxr-xr-xchallenge-075/sgreen/perl/ch-1.pl61
-rwxr-xr-xchallenge-075/sgreen/perl/ch-2.pl72
-rw-r--r--challenge-075/simon-proctor/raku/ch-1.raku45
-rw-r--r--challenge-075/simon-proctor/raku/ch-2.raku62
-rw-r--r--challenge-075/tyler-wardhaugh/clojure/LICENSE214
-rw-r--r--challenge-075/tyler-wardhaugh/clojure/README.md35
-rw-r--r--challenge-075/tyler-wardhaugh/clojure/deps.edn15
-rw-r--r--challenge-075/tyler-wardhaugh/clojure/pom.xml44
-rw-r--r--challenge-075/tyler-wardhaugh/clojure/src/tw/weekly/c75.clj12
l---------challenge-075/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj1
l---------challenge-075/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj1
-rw-r--r--challenge-075/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj32
-rw-r--r--challenge-075/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj25
-rw-r--r--challenge-075/tyler-wardhaugh/clojure/test/tw/weekly/c75_test.clj15
-rw-r--r--challenge-075/ulrich-rieke/haskell/ch-2.hs17
-rw-r--r--challenge-075/ulrich-rieke/perl/ch-1.pl66
-rw-r--r--challenge-075/ulrich-rieke/perl/ch-2.pl79
-rw-r--r--challenge-075/walt-mankowski/blog.txt1
-rw-r--r--challenge-075/walt-mankowski/perl/ch-1.pl72
-rw-r--r--challenge-075/walt-mankowski/perl/ch-2.pl106
-rw-r--r--challenge-075/walt-mankowski/python/ch-1.py36
-rw-r--r--challenge-075/walt-mankowski/python/ch-2.py49
-rw-r--r--challenge-075/xkr47/rust/Cargo.toml15
-rw-r--r--challenge-075/xkr47/rust/ch-1.rs51
-rw-r--r--challenge-075/xkr47/rust/ch-2.rs47
-rw-r--r--members.json3
-rw-r--r--stats/pwc-current.json226
-rw-r--r--stats/pwc-language-breakdown-summary.json72
-rw-r--r--stats/pwc-language-breakdown.json540
-rw-r--r--stats/pwc-leaders.json840
-rw-r--r--stats/pwc-summary-1-30.json136
-rw-r--r--stats/pwc-summary-121-150.json130
-rw-r--r--stats/pwc-summary-151-180.json64
-rw-r--r--stats/pwc-summary-181-210.json42
-rw-r--r--stats/pwc-summary-31-60.json42
-rw-r--r--stats/pwc-summary-61-90.json122
-rw-r--r--stats/pwc-summary-91-120.json56
-rw-r--r--stats/pwc-summary.json74
69 files changed, 3331 insertions, 1272 deletions
diff --git a/.gitignore b/.gitignore
index 40a0244345..b3cb07c2d2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,6 @@
.DS_Store
.vstags
.*.swp
+
+# Rust languageoutput directory
+target/
diff --git a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj
index 416d2a1d85..924a7a086e 100644..120000
--- a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj
+++ b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj
@@ -1,20 +1 @@
-(ns tw.weekly.ch-1)
-
-(defn majority
- "Calculate the majority element, which is the element in a list that appears more than floor(size_of_list/2), prefering the least significant digit that matches that criteria. Returns -1 if no such element is found."
- [coll]
- (let [minimum (Math/floor (/ (count coll) 2))
- maj (->> coll
- frequencies
- (filter (comp #(> % minimum) second))
- (sort second)
- ffirst)]
- (or maj -1)))
-
-(defn -main
- "Run Task 1 with a list of integers, defaulting to the sample given in the task description."
- [& args]
- (let [N (if (not-empty args)
- (map clojure.edn/read-string args)
- [2, 2, 2, 3, 2, 4, 2])]
- (println (majority N))))
+ch_1.clj \ No newline at end of file
diff --git a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj
index 299ab63690..5a32e17ef9 100644..120000
--- a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj
+++ b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj
@@ -1,33 +1 @@
-(ns tw.weekly.ch-2
- (:require [clojure.string :as str])
- (:require [flatland.ordered.map :as fo]))
-
-(defn ordered-frequencies
- "A modification of clojure.core/frequencies that guarantees the map returns keys in the order seen in the collection. Thus it returns an ordered map from distinct items in coll to the number of times they appear."
- [coll]
- (persistent!
- (reduce (fn [counts x]
- (assoc! counts x (inc (get counts x 0))))
- (transient (fo/ordered-map)) coll)))
-
-(defn find-fnr [string]
- "Find the first non-repeating character (FNR) for a string."
- (let [counts (ordered-frequencies string)
- non-repeaters (filter (comp #(= 1 %) second) counts)]
- (-> non-repeaters last first)))
-
-(defn find-fnr-over-string
- "Generate a string comprised of the first non-repeating character (FNR) for successive substrings of a given string."
- [string]
- (->> string
- (reductions str "")
- (drop 1)
- (map find-fnr)
- (map #(or % \#))
- str/join))
-
-(defn -main
- "Run Task 2 with a string, defaulting to the sample given in the task description."
- [& args]
- (let [S (or (-> args first) "ababc")]
- (println (find-fnr-over-string S))))
+ch_2.clj \ No newline at end of file
diff --git a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj
new file mode 100644
index 0000000000..416d2a1d85
--- /dev/null
+++ b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj
@@ -0,0 +1,20 @@
+(ns tw.weekly.ch-1)
+
+(defn majority
+ "Calculate the majority element, which is the element in a list that appears more than floor(size_of_list/2), prefering the least significant digit that matches that criteria. Returns -1 if no such element is found."
+ [coll]
+ (let [minimum (Math/floor (/ (count coll) 2))
+ maj (->> coll
+ frequencies
+ (filter (comp #(> % minimum) second))
+ (sort second)
+ ffirst)]
+ (or maj -1)))
+
+(defn -main
+ "Run Task 1 with a list of integers, defaulting to the sample given in the task description."
+ [& args]
+ (let [N (if (not-empty args)
+ (map clojure.edn/read-string args)
+ [2, 2, 2, 3, 2, 4, 2])]
+ (println (majority N))))
diff --git a/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj
new file mode 100644
index 0000000000..299ab63690
--- /dev/null
+++ b/challenge-074/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj
@@ -0,0 +1,33 @@
+(ns tw.weekly.ch-2
+ (:require [clojure.string :as str])
+ (:require [flatland.ordered.map :as fo]))
+
+(defn ordered-frequencies
+ "A modification of clojure.core/frequencies that guarantees the map returns keys in the order seen in the collection. Thus it returns an ordered map from distinct items in coll to the number of times they appear."
+ [coll]
+ (persistent!
+ (reduce (fn [counts x]
+ (assoc! counts x (inc (get counts x 0))))
+ (transient (fo/ordered-map)) coll)))
+
+(defn find-fnr [string]
+ "Find the first non-repeating character (FNR) for a string."
+ (let [counts (ordered-frequencies string)
+ non-repeaters (filter (comp #(= 1 %) second) counts)]
+ (-> non-repeaters last first)))
+
+(defn find-fnr-over-string
+ "Generate a string comprised of the first non-repeating character (FNR) for successive substrings of a given string."
+ [string]
+ (->> string
+ (reductions str "")
+ (drop 1)
+ (map find-fnr)
+ (map #(or % \#))
+ str/join))
+
+(defn -main
+ "Run Task 2 with a string, defaulting to the sample given in the task description."
+ [& args]
+ (let [S (or (-> args first) "ababc")]
+ (println (find-fnr-over-string S))))
diff --git a/challenge-075/james-smith/README.md b/challenge-075/james-smith/README.md
index 6d816375c5..54043e40b0 100644
--- a/challenge-075/james-smith/README.md
+++ b/challenge-075/james-smith/README.md
@@ -1,129 +1,34 @@
Solutions by James Smith.
-# Challenge 1 - Counting letters
+# Challenge 1 - Coins Sum
-There are two ways to solve this -> split and count or using tr... one is short code - one is longer - but much faster.
-
-## Version 1 - short code
-
-Uses lc, split and grep to count the elements and put them in a hash... looping through each file a line at a time with <>
-
-```perl
-use feature 'say';
-use strict;
-my %T = map { $_=>0 } foreach 'a'..'z';
-while(<>) {
- $T{$_}++ foreach grep { /[a-z]/ } split m{}, lc $_;
-}
-
-say "$_: $T{$_}" foreach 'a'..'z';
-```
-
-Running this over 13Mbytes of PHP takes approximately 6.5 seconds...
-
-## Version 2 - faster code
-
-Now counting letters in a string is quickest using the tr or y operator - as this requires the number of characters
-changed. Without using eval you can't unfortunately sub in a variable into the pattern unlike with m/s... So we
-either need to use string eval (which is evIl) or manually replicate the loop - $T{'a'} =~ y/aA/aA/ etc - in this
-code... Note we set $/ to undef so that we slurp the whole file in in one go (to improve performance of using tr)
-and less modifications to the %T hash...
+This is just begging for a recursive solution.
```perl
-use feature 'say';
-$/=undef;
-
-while(<>) {
- $T{'a'} += y/aA/aA/;
- $T{'b'} += y/bB/bB/;
-..
-..
- $T{'y'} += y/yY/yY/;
- $T{'z'} += y/zZ/zZ/;
+sub csm {
+ my $t = shift;
+ return @{$mem{"$t @_"}||=[map {my $a=$_; $t==$a?[$a]:
+ map {[$a,@{$_}]} csm($t-$a,grep {$a<=$_&&$_<=$t} @_)} @_] };
}
-
-say $_,': ',$T{$_}||0 foreach 'a'..'z';
-
-```
-OK - so didn't want to type 26 lines so used this one liner to do it for me!
-
-```bash
-perl -E 'say " \$t{'"'"'$_'"'"'} += y/$_".uc($_)."/$_".uc($_)."/;" foreach "a".."z";'
-```
-
-This now runs in approxy 0.25 seconds a big improvement...
-
-## Version 3 - nicer output...
-
-The version 3 code just expands the version 2 code - but creates a "histogram" to show the distribution (and at the same time formats the totals better)
-
-```
-a : 584193 : ##########################
-b : 108267 : ####
-c : 287124 : #############
-d : 272798 : ############
-e : 877936 : ########################################
-f : 209371 : #########
-g : 152944 : ######
-h : 200641 : #########
-i : 546465 : ########################
-j : 15133 :
-k : 50049 : ##
-l : 326976 : ##############
-m : 214631 : #########
-n : 438874 : ###################
-o : 436059 : ###################
-p : 282120 : ############
-q : 19825 :
-r : 551144 : #########################
-s : 552344 : #########################
-t : 724711 : #################################
-u : 260233 : ###########
-v : 68882 : ###
-w : 80759 : ###
-x : 57019 : ##
-y : 115201 : #####
-z : 11021 :
```
-# Challenge 2 - multiplication square...
+Notes:
-Again going to extend the challenge to make this generic (in case someone wants a different version)
+ * %mem is a memoisation cache as it helps to not need to re-compute higher totals more than once - speeds up searches for large coin sums... not essential but nice to have
-Hidden in the solution above was getting the number of digits for a number (so we can format the totals) - we do this again to get the size of the left hand column and the main table columns.
+How it works:
-```perl
-my $sl = int(log($N)/log(10)+1); ## Get size of integer $N - defines the width of the LH column
-my $sr = int(2*log($N)/log(10)+1); ## Get size of $N squared - defines the width of other columns
-```
-and we use this to tweak the formats and the padding/line drawing elements!
-```perl
-#!/usr/bin/perl
-
-use strict;
-use feature 'say';
+ * Loop through all coin values available
+ * if the coin is the same as the amount required return a single array containing that value;
+ * if not remove that from the amount required and call again. For all values returned prepend the coin value to each array in the list
+ * when calling again remove any coins which are less than the "current coin" and greater than the amount required
-## This solves more than the puzzle - but thought I would make it more generic!
+Caveats & assumptions:
-## This gets the size of the square that we want to display...
+ * No input checking is performed - assumes the options passed are all valid and greater than 0;
-my $N = shift =~ s{\D}{}gr || 11; ## Default to 11 - but use first parameter as size of square!
-my @R = 1..$N; ## Create a "range array" - we use this 4 times!!!
+# Challenge 2 - Histogram rectangle
-## Get width of columns for use in the renderer..
+Much simpler this time.
-my $sl = int( log($N) / log(10) + 1); ## Get size of integer $N - defines the width of the LH column
-my $sr = int( 2 * log($N) / log(10) + 1); ## Get size of $N squared - defines the width of other columns
-my $fl = sprintf ' %%%dd |', $sl; ## Create a template for the first column..
-my $fr = sprintf ' %%%dd', $sr; ## .... and for the other columns!
-
-## Finally we render - make a use of sprintf with the templates and '$' x $ to generate padding
-
-say ' ' x $sl, 'x |', ## Header (LH side)
- map { sprintf $fr, $_ } @R; ## (column headers)
-say join '-', '-' x $sl, '-+', ## Separator (LH side)
- map { '-' x $sr } @R; ## (RH side)
-say sprintf( $fl, $a=$_ ), ## Body of table (LH headers)
- map { $a>$_ ? ' ' x ($sr+1) : sprintf $fr, $a*$_ } @R ## (content of row)
- foreach @R;
-```
+First we get the size of the box (max value) and render chart then we look for the maximum rectangle size - which is computed as the maximum value of the distance between any two points (inclusive) multiplied by the lowest value in between the two values. (Maxi-min problem)
diff --git a/challenge-075/james-smith/perl/ch-1.pl b/challenge-075/james-smith/perl/ch-1.pl
new file mode 100644
index 0000000000..08d9c19862
--- /dev/null
+++ b/challenge-075/james-smith/perl/ch-1.pl
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+use feature qw(say);
+
+my %mem; ## Used to cache results of CSM to speed things up for large cases...
+
+## Some test cases - example from challenge itself
+say q();
+_dump( csm( 6, qw(1 2 4) ) );
+say q();
+
+## All the values up to £2 - using standard UK coins
+foreach (1..200) {
+ _dump( csm( $_, qw(1 2 5 10 20 50 100 200) ) );
+ say q();
+}
+
+# The hardwork - use recursion
+sub csm {
+ my $t = shift;
+ return @{$mem{"$t @_"}||=[map {my $a=$_; $t==$a?[$a]:
+ map {[$a,@{$_}]} csm($t-$a,grep {$a<=$_&&$_<=$t} @_)} @_] };
+}
+
+## Support function to dump values;
+sub _dump {
+ say " @{$_}" foreach @_;
+}
+
diff --git a/challenge-075/james-smith/perl/ch-2.pl b/challenge-075/james-smith/perl/ch-2.pl
new file mode 100644
index 0000000000..16bca58aa0
--- /dev/null
+++ b/challenge-075/james-smith/perl/ch-2.pl
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+use feature qw(say);
+
+say q();
+say lrh(qw(2 1 4 5 3 7));
+say q();
+say lrh(qw(3 2 3 5 7 5));
+say q();
+
+## Could import max/min - but these are easy to write ourselves in this case....
+
+sub lrh {
+ my @V = @_;
+ my ($mx,$y) = 0;
+ $mx = $mx < $_ ? $_ : $mx foreach @V;
+## The following chunk renders the histogram as requested...
+## Render output as table....
+ say sprintf( ' %2d', $y=$_ ), map { $_ < $y ? q( ) : q( #) } @V foreach reverse 1..$mx;
+ say q( --), map { q( --) } @V;
+ say q( ), map { sprintf ' %2d', $_ } @V;
+ say q();
+
+## Now do the calculation of mx area
+ my $mx_area = 0;
+ foreach my $s ( 0 .. @V-1 ) { ## Loop through each start of block...
+ my $mn = $mx;
+ foreach ( $s .. @V-1 ) {
+ $mn = $V[$_] if $mn > $V[$_]; ## Loop through ends, keeping track of minimum value
+ $mx_area = $mn * ($_-$s+1) if $mx_area < $mn * ($_-$s+1); ## And check to see if the area is greater than any other area!
+ }
+ }
+ return $mx_area;
+}
diff --git a/challenge-075/luca-ferrari/blog-1.txt b/challenge-075/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..37064d7a85
--- /dev/null
+++ b/challenge-075/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2020/08/24/PerlWeeklyChallenge75.html#task1
diff --git a/challenge-075/luca-ferrari/blog-2.txt b/challenge-075/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..d96d2273a8
--- /dev/null
+++ b/challenge-075/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2020/08/24/PerlWeeklyChallenge75.html#task2
diff --git a/challenge-075/luca-ferrari/raku/ch-1.p6 b/challenge-075/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..b52a91d40c
--- /dev/null
+++ b/challenge-075/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,81 @@
+#!raku
+
+sub find-solutions( Int :$sum, :@coins ) {
+ my @solutions;
+
+ # add all the 'one coin' solutions
+ @solutions.push: [ $_ xx ( $sum / $_ ) ] for @coins.grep( $sum %% * );
+
+
+ # now inspect all the other cases
+ for @coins.grep( $sum !%% * ).sort( { $^b <=> $^a } ) -> $current-coin {
+ next if $current-coin > $sum;
+ my @current-solution;
+ @current-solution.push: $current-coin;
+
+ for @coins.grep( * !~~ $current-coin ).sort( { $^b <=> $^a } ) {
+ my $current-sum = [+] @current-solution;
+ # try to add the same number over and over again
+ while ( ( $current-sum + $_ ) <= $sum ) {
+ @current-solution.push: $_ ;
+ $current-sum = [+] @current-solution;
+ }
+
+ if ( $current-sum == $sum ) {
+ # use a new array to clone it, so I can delete the last value and continue over
+ @solutions.pu