From 973c66cd12ed324880767de16c4788e376ff778f Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Fri, 9 Oct 2020 10:55:06 +0200 Subject: Challenge 081 Task 1 LK Perl --- challenge-081/lubos-kolouch/perl/ch-1.pl | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 challenge-081/lubos-kolouch/perl/ch-1.pl diff --git a/challenge-081/lubos-kolouch/perl/ch-1.pl b/challenge-081/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..6aed6bb23b --- /dev/null +++ b/challenge-081/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: Perl Weekly Challenge 081 +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-081/ +# Task 1 - Common base string +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: YOUR NAME (), +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 10/09/2020 10:43:10 AM +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; + + +sub get_common_strings { + my $arg = shift; + + my $str1 = $arg->{str1}; + my $str2 = $arg->{str2}; + + my @result; + + my ($res1) = $str1 =~ /(.*)\1+/; + push @result, $res1; + + my ($res2) = $str2 =~ /(.*)\1+/; + push @result, $res2 unless $res1 eq $res2; + + return \@result; + +} + +use Test::More; + +is_deeply(get_common_strings({str1 => 'abcdabcd', str2 => 'abcdabcdabcdabcd'} ),['abcd','abcdabcd']); +is_deeply(get_common_strings({str1 => 'aaa', str2 => 'aa'} ),['a']); +is_deeply(get_common_strings({str1 => 'abc', str2 => 'aa'} ),['','a']); + -- cgit From 0b0246ec4c2367a968b49aeb3f876e34e448344c Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Fri, 9 Oct 2020 11:59:28 +0200 Subject: Challenge 2 Perl --- challenge-081/lubos-kolouch/perl/ch-1.pl | 1 + challenge-081/lubos-kolouch/perl/ch-2.pl | 56 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 challenge-081/lubos-kolouch/perl/ch-2.pl diff --git a/challenge-081/lubos-kolouch/perl/ch-1.pl b/challenge-081/lubos-kolouch/perl/ch-1.pl index 6aed6bb23b..9890053c01 100644 --- a/challenge-081/lubos-kolouch/perl/ch-1.pl +++ b/challenge-081/lubos-kolouch/perl/ch-1.pl @@ -48,3 +48,4 @@ is_deeply(get_common_strings({str1 => 'abcdabcd', str2 => 'abcdabcdabcdabcd'} ), is_deeply(get_common_strings({str1 => 'aaa', str2 => 'aa'} ),['a']); is_deeply(get_common_strings({str1 => 'abc', str2 => 'aa'} ),['','a']); +done_testing; diff --git a/challenge-081/lubos-kolouch/perl/ch-2.pl b/challenge-081/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..c8d93d17f8 --- /dev/null +++ b/challenge-081/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: Perl Weekly Challenge 081 +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-081/ +# Task 2 - Frequency Sort +# +# AUTHOR: Lubos Kolouch +# CREATED: 10/09/2020 10:43:10 AM +#=============================================================================== + +use strict; +use warnings; +use Data::Dumper; + +sub frequency_sort { + my $arg = shift; + + # sanitize the input as per the challenge + $arg =~ s/[."\(\)--]/ /g; + $arg =~ s/(?:'s)/ /g; + + my %count; + # I'm sure it can be done in one-liner with map, but I took the longer approach + + # First split the array and create hash of counts of each word + for (split ' ', $arg) { + $count{$_}++; + } + + my %rev_count; + + # create reverse hash with counts as key and the words as array + my @result; + for my $key (keys %count) { + push @{$rev_count{$count{$key}}}, $key; + } + + # sort the arrays and put them to the result output + for my $key (keys %rev_count) { + push @result, $key.' '.join(' ', sort @{$rev_count{$key}}); + } + + return [sort @result]; + +} + +use Test::More; + +is_deeply(frequency_sort("West Side Story The award-winning adaptation of the classic romantic tragedy \"Romeo and Juliet\". The feuding families become two warring New York City gangs, the white Jets led by Riff and the Latino Sharks, led by Bernardo. Their hatred escalates to a point where neither can coexist with any form of understanding. But when Riff\'s best friend (and former Jet) Tony and Bernardo\'s younger sister Maria meet at a dance, no one can do anything to stop their love. Maria and Tony begin meeting in secret, planning to run away. Then the Sharks and Jets plan a rumble under the highway--whoever wins gains control of the streets. Maria sends Tony to stop it, hoping it can end the violence. It goes terribly wrong, and before the lovers know what\'s happened, tragedy strikes and doesn\'t stop until the climactic and heartbreaking ending." ),['1 But City It Jet Juliet Latino New Romeo Side Story Their Then West York adaptation any anything at award away become before begin best classic climactic coexist control dance do doesn\'t end ending escalates families feuding form former friend gains gangs goes happened hatred heartbreaking highway hoping in know love lovers meet meeting neither no one plan planning point romantic rumble run secret sends sister streets strikes terribly their two under understanding until violence warring what when where white whoever winning wins with wrong younger', '2 Bernardo Jets Riff Sharks The by it led tragedy','3 Maria Tony a can of stop','4 to','9 and the']); + +done_testing; -- cgit