From 75e69f0ee7e74f2121c38521cf4041d00f6e4353 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 4 Jan 2021 19:37:01 +0100 Subject: Readme --- challenge-094/abigail/README.md | 77 +++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 46 deletions(-) diff --git a/challenge-094/abigail/README.md b/challenge-094/abigail/README.md index ce16bd000c..b9a451dcbd 100644 --- a/challenge-094/abigail/README.md +++ b/challenge-094/abigail/README.md @@ -1,70 +1,55 @@ # Solution by Abigail -## Task 1: Max Points +## Task 1: Group Anagrams -You are given set of co-ordinates `@N`. +You are given an array of strings `@S`. -Write a script to count maximum points on a straight line when given -co-ordinates plotted on 2-d plane. +Write a script to group Anagrams together in any random order. ### Examples -~~~~ -| -| x -| x -| x -+ _ _ _ _ - -Input: (1,1), (2,2), (3,3) -Output: 3 - -| -| -| x x -| x -| x x -+ _ _ _ _ _ +#### Example 1 +~~~~ +Input: ("opt", "bat", "saw", "tab", "pot", "top", "was") +Output: [ ("bat", "tab"), + ("saw", "was"), + ("top", "pot", "opt") ] +~~~~ -Input: (1,1), (2,2), (3,1), (1,3), (5,3) -Output: 3 +#### Example 2 +~~~~ +Input: ("x") +Output: [ ("x") ] ~~~~ + ### Solutions -* [Perl](perl/ch-1.pl) -## Task 2: Sum Path +## Task 2: Binary Tree to Linked List -You are given binary tree containing numbers `0-9` only. +You are given a binary tree. -Write a script to sum all possible paths from root to leaf. +Write a script to represent the given binary tree as an object and +flatten it to a linked list object. Finally print the linked list +object. ### Examples -~~~~ -Input: - 1 - / - 2 - / \ - 3 4 - -Output: 13 -~~~~ -as sum two paths (1->2->3) and (1->2->4) +#### Example 1 ~~~~ Input: - 1 - / \ - 2 3 - / / \ - 4 5 6 - -Output: 26 + 1 + / \ + 2 3 + / \ +4 5 + / \ + 6 7 + +Output: + 1 -> 2 -> 4 -> 5 -> 6 -> 7 -> 3 ~~~~ -as sum three paths (1->2->4), (1->3->5) and (1->3->6) ### Solutions -* [Perl](perl/ch-2.pl) -- cgit From 33e8a5ad2843da4b0006851a42380c4b90b30106 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 4 Jan 2021 19:49:13 +0100 Subject: Perl solution for week 94/part 1 --- challenge-094/abigail/README.md | 1 + challenge-094/abigail/perl/ch-1.pl | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 challenge-094/abigail/perl/ch-1.pl diff --git a/challenge-094/abigail/README.md b/challenge-094/abigail/README.md index b9a451dcbd..0fc0441af3 100644 --- a/challenge-094/abigail/README.md +++ b/challenge-094/abigail/README.md @@ -24,6 +24,7 @@ Output: [ ("x") ] ### Solutions +* [Perl](perl/ch-1.pl) diff --git a/challenge-094/abigail/perl/ch-1.pl b/challenge-094/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..bccd80afaa --- /dev/null +++ b/challenge-094/abigail/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +while (<>) { + my %anagrams; + # + # Iterate over the words. We're assuming all the words are on + # a single line, and the words don't contain a double quote. + # (If we can't assume this, we could use a CSV parser instead). + # + foreach my $word (/"([^"]+)"/g) { + # + # Normalize each word: split into characters, sort them, join them. + # + my $normalized = join "" => sort split // => $word; + push @{$anagrams {$normalized}} => $word; + } + # + # Print them. We make this deterministic, so we can easily write tests + # + foreach my $key (sort keys %anagrams) { + say join ", " => map {qq {"$_"}} @{$anagrams {$key}}; + } +} + +__END__ -- cgit From 3b42ecfb0c941e90a1ee7e4ecf02437252afbaf3 Mon Sep 17 00:00:00 2001 From: Flavio Poletti Date: Tue, 5 Jan 2021 11:11:44 +0100 Subject: Add polettix's solution to PWC094 --- challenge-094/polettix/blog.txt | 1 + challenge-094/polettix/blog1.txt | 1 + challenge-094/polettix/perl/ch-1.pl | 20 +++++++++++++++ challenge-094/polettix/perl/ch-2.pl | 51 +++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 challenge-094/polettix/blog.txt create mode 100644 challenge-094/polettix/blog1.txt create mode 100644 challenge-094/polettix/perl/ch-1.pl create mode 100644 challenge-094/polettix/perl/ch-2.pl diff --git a/challenge-094/polettix/blog.txt b/challenge-094/polettix/blog.txt new file mode 100644 index 0000000000..6078f54656 --- /dev/null +++ b/challenge-094/polettix/blog.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2021/01/06/pwc094-group-anagrams/ diff --git a/challenge-094/polettix/blog1.txt b/challenge-094/polettix/blog1.txt new file mode 100644 index 0000000000..b7b66a157b --- /dev/null +++ b/challenge-094/polettix/blog1.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2021/01/07/pwc094-binary-tree-to-linked-list/ diff --git a/challenge-094/polettix/perl/ch-1.pl b/challenge-094/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..8e38c17263 --- /dev/null +++ b/challenge-094/polettix/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/env perl +use 5.024; +use warnings; +use experimental qw< postderef signatures >; +no warnings qw< experimental::postderef experimental::signatures >; +use Data::Dumper; $Data::Dumper::Indent = 1; + +sub group_anagrams (@S) { + my %group_for; + for my $item (@S) { + my $key = join '', + sort { $a cmp $b } + map { lc } + split m{}mxs, $item; + push $group_for{$key}->@*, $item; + } + return [values %group_for]; +} + +say Dumper group_anagrams(@ARGV); diff --git a/challenge-094/polettix/perl/ch-2.pl b/challenge-094/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..bb84f0d757 --- /dev/null +++ b/challenge-094/polettix/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +use 5.024; +use warnings; +use experimental qw< postderef signatures >; +no warnings qw< experimental::postderef experimental::signatures >; +$|++; + +sub build_linked_list ($input) { + my @rows = map { [ split m{}mxs ] } split m{\n}mxs, $input; + my $root = 0; + $root++ while $rows[0][$root] eq ' '; + my $pre_start = {}; + _build_linked_list_r(\@rows, 0, $root, $pre_start); + return $pre_start->{next}; +} + +sub _build_linked_list_r($rows, $rid, $cid, $previous) { + my $so_far = $previous->{next} = {value => $rows->[$rid][$cid]}; + if ($rid < $#$rows) { # there can be something more + $rid++; + if ($cid < $#{$rows->[$rid]}) { + $so_far = _build_linked_list_r($rows, $rid + 1, $cid - 2, $so_far) + if 0 < $cid && $rows->[$rid][$cid - 1] ne ' '; + $so_far = _build_linked_list_r($rows, $rid + 1, $cid + 2, $so_far) + if $rows->[$rid][$cid + 1] ne ' '; + } + } + return $so_far; +} + +sub print_linked_list ($head) { + my $separator = ''; + while ($head) { + print $separator, $head->{value}; + $separator = ' -> '; + $head = $head->{next}; + } + print "\n"; +} + +my $tree = <<'END'; + 1 + / \ + 2 3 + / \ + 4 5 + / \ + 6 7 +END + +print_linked_list(build_linked_list($tree)); -- cgit From e7fcf47a21b9995818f44053f352a016c13458e9 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 5 Jan 2021 10:15:54 +0000 Subject: Blog post for challenge #094 --- challenge-094/roger-bell-west/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-094/roger-bell-west/blog.txt diff --git a/challenge-094/roger-bell-west/blog.txt b/challenge-094/roger-bell-west/blog.txt new file mode 100644 index 0000000000..931312bdc2 --- /dev/null +++ b/challenge-094/roger-bell-west/blog.txt @@ -0,0 +1 @@ +https://blog.firedrake.org/archive/2021/01/Perl_Weekly_Challenge_94__Binary_Anagrams.html -- cgit From 366682dce7eeb7b47d27b93cb10333ecebf5810b Mon Sep 17 00:00:00 2001 From: Simon Green Date: Tue, 5 Jan 2021 22:08:33 +1000 Subject: sgreen solution to challenge 094 --- challenge-094/sgreen/README.md | 4 +-- challenge-094/sgreen/blog.txt | 1 + challenge-094/sgreen/perl/ch-1.pl | 32 ++++++++++++++++++++++ challenge-094/sgreen/perl/ch-2.pl | 56 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 challenge-094/sgreen/blog.txt create mode 100755 challenge-094/sgreen/perl/ch-1.pl create mode 100755 challenge-094/sgreen/perl/ch-2.pl diff --git a/challenge-094/sgreen/README.md b/challenge-094/sgreen/README.md index ba63ecec21..9fb2f453a3 100644 --- a/challenge-094/sgreen/README.md +++ b/challenge-094/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 093 +# The Weekly Challenge 094 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-093-1dd9) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-094-2d2h) diff --git a/challenge-094/sgreen/blog.txt b/challenge-094/sgreen/blog.txt new file mode 100644 index 0000000000..8bcaf90595 --- /dev/null +++ b/challenge-094/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-094-2d2h diff --git a/challenge-094/sgreen/perl/ch-1.pl b/challenge-094/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..419adc2eaa --- /dev/null +++ b/challenge-094/sgreen/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub _sort_word { + my $word = shift; + my @letters = split //, lc $word; + return join '', sort @letters; +} + +sub main { + # Retrieve a list of words from the command line + my @words = ( join( ' ', @_ ) =~ /([a-z]+)/ig ); + + # Group anagrammed words together + my %group = (); + foreach my $word (@words) { + push @{ $group{ _sort_word($word) } }, $word; + } + + # Display the result + my @results = ( values %group ); + foreach my $count ( 0 .. $#results ) { + my $start = $count == 0 ? '[ ("' : ' ("'; + my $end = $count == $#results ? '") ]' : '"),'; + say $start, join( '", "', @{ $results[$count] } ), $end; + } +} + +main(@ARGV); diff --git a/challenge-094/sgreen/perl/ch-2.pl b/challenge-094/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..ea7e4e8b8f --- /dev/null +++ b/challenge-094/sgreen/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +use List::Util 'sum'; + +sub _char_at { + my ( $lines, $x, $y ) = @_; + return '' if $y > $#$lines or $x > length( $lines->[$y] ); + return substr( $lines->[$y], $x, 1 ); +} + +sub _next_line { + my ( $lines, $x, $y, $direction ) = @_; + + # See if there is a / or \ 1 line below ... + my $delta = $direction eq 'left' ? -1 : 1; + my $char = $direction eq 'left' ? '/' : '\\'; + if ( $y < $#$lines and _char_at( $lines, $x + $delta, $y + 1 ) eq $char ) { + # ... and return the corresponding number below that + return ( _char_at( $lines, $x + $delta * 2, $y + 2 ), $x + $delta * 2, $y + 2 ); + } + return; +} + +sub _walk_path { + my ( $lines, $x, $y, $digits ) = @_; + push @$digits, _char_at( $lines, $x, $y ); + + foreach my $direction (qw(left right)) { + # See if there are any child elemnts of this value + if ( my ( $char, $new_x, $new_y ) = _next_line( $lines, $x, $y, $direction ) ) { + # Go to the child node + _walk_path( $lines, $new_x, $new_y, $digits ); + } + } +} + +sub main { + # Read the input from STDIN (e.g. by piping a file) + my @lines = <>; + + # Find the number on the first line. + my ( $spaces, $number ) = ( $lines[0] =~ /^(\D*)(\d)/ ); + + # Walk the paths + my @digits = (); + my @this_path = ($number); + _walk_path( \@lines, length($spaces), 0, \@digits ); + + say join ' -> ', @digits; +} + +main(); -- cgit From ed70525ee3ab4b56ce475e81528d3f63234f586c Mon Sep 17 00:00:00 2001 From: chirvasitua Date: Tue, 5 Jan 2021 09:08:23 -0500 Subject: 1st commit on 050_haskell --- challenge-050/stuart-little/haskell/ch-1.hs | 23 +++++++++++++++++++++++ challenge-050/stuart-little/haskell/ch-2.hs | 11 +++++++++++ 2 files changed, 34 insertions(+) create mode 100755 challenge-050/stuart-little/haskell/ch-1.hs create mode 100755 challenge-050/stuart-little/haskell/ch-2.hs diff --git a/challenge-050/stuart-little/haskell/ch-1.hs b/challenge-050/stuart-little/haskell/ch-1.hs new file mode 100755 index 0000000000..7938a90952 --- /dev/null +++ b/challenge-050/stuart-little/haskell/ch-1.hs @@ -0,0 +1,23 @@ +#!/usr/bin/env runghc + +-- run