diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-19 00:00:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-19 00:00:23 +0100 |
| commit | 4a2aa5adb13b7604ee69471ccd9ecc01daebb007 (patch) | |
| tree | 9886a481f60ec15d4e5e8853dd4a0d7d6bbbafc1 /challenge-082 | |
| parent | ef78784710105485a16d2599ff37bf2ae1f60461 (diff) | |
| parent | 129934773965bac58c8584a5a341f4ccd7d8493a (diff) | |
| download | perlweeklychallenge-club-4a2aa5adb13b7604ee69471ccd9ecc01daebb007.tar.gz perlweeklychallenge-club-4a2aa5adb13b7604ee69471ccd9ecc01daebb007.tar.bz2 perlweeklychallenge-club-4a2aa5adb13b7604ee69471ccd9ecc01daebb007.zip | |
Merge pull request #2557 from tagg/branch-for-challenge-082
Solutions for challenge 082
Diffstat (limited to 'challenge-082')
| -rw-r--r-- | challenge-082/lars-thegler/perl/ch-1.pl | 58 | ||||
| -rw-r--r-- | challenge-082/lars-thegler/perl/ch-2.pl | 57 | ||||
| -rw-r--r-- | challenge-082/lars-thegler/perl/input | 3 |
3 files changed, 115 insertions, 3 deletions
diff --git a/challenge-082/lars-thegler/perl/ch-1.pl b/challenge-082/lars-thegler/perl/ch-1.pl new file mode 100644 index 0000000000..2de2d3471a --- /dev/null +++ b/challenge-082/lars-thegler/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl + +use Modern::Perl; + +# TASK #1 › Common Factors +# Submitted by: Niels van Dijke +# You are given 2 positive numbers $M and $N. + +# Write a script to list all common factors of the given numbers. + +# Example 1: +# Input: +# $M = 12 +# $N = 18 + +# Output: +# (1, 2, 3, 6) + +# Explanation: +# Factors of 12: 1, 2, 3, 4, 6 +# Factors of 18: 1, 2, 3, 6, 9 +# Example 2: +# Input: +# $M = 18 +# $N = 23 + +# Output: +# (1) + +# Explanation: +# Factors of 18: 1, 2, 3, 6, 9 +# Factors of 23: 1 + +my ( $M, $N ) = @ARGV; + +my %factors; +for ( $M, $N ) { + for my $f ( factor($_) ) { + $factors{$_}++ for $f->@*; + } +} + +my @common_factors; +for my $f (sort keys %factors) { + push @common_factors, $f if $factors{$f} == 2; +} + +say sprintf "(%s)", join ', ', @common_factors; + +sub factor +{ + my $n = shift; + my @factors; + for my $f ( 1 .. int( $n / 2 ) ) { + push @factors, $f unless $n % $f; + } + return \@factors; +} diff --git a/challenge-082/lars-thegler/perl/ch-2.pl b/challenge-082/lars-thegler/perl/ch-2.pl new file mode 100644 index 0000000000..cd0fef4c45 --- /dev/null +++ b/challenge-082/lars-thegler/perl/ch-2.pl @@ -0,0 +1,57 @@ +#! /usr/bin/env perl + +use Modern::Perl; +use Data::Printer; + +# TASK #2 › Interleave String +# Submitted by: Mohammad S Anwar +# You are given 3 strings; $A, $B and $C. + +# Write a script to check if $C is created by interleave $A and $B. + +# Print 1 if check is success otherwise 0. + +# Example 1: +# Input: +# $A = "XY" +# $B = "X" +# $C = "XXY" + +# Output: 1 +# EXPLANATION +# "X" (from $B) + "XY" (from $A) = $C +# Example 2: +# Input: +# $A = "XXY" +# $B = "XXZ" +# $C = "XXXXZY" + +# Output: 1 +# EXPLANATION +# "XX" (from $A) + "XXZ" (from $B) + "Y" (from $A) = $C +# Example 3: +# Input: +# $A = "YX" +# $B = "X" +# $C = "XXY" + +# Output: 0 + +my ($A, $B, $C) = @ARGV; + + +my %interleaves; +$interleaves{$_}++ for interleave($A, $B), interleave($B, $A); + +say exists $interleaves{$C} ? 1 : 0; + +sub interleave { + my ($a, $b) = @_; + my @interleaves; + for my $i (0..length $a) { + my $s = $a; + $s =~ s/^(.{$i})(.*)/$1$b$2/; + push @interleaves, $s; + } + return @interleaves; +}
\ No newline at end of file diff --git a/challenge-082/lars-thegler/perl/input b/challenge-082/lars-thegler/perl/input deleted file mode 100644 index 37001629ad..0000000000 --- a/challenge-082/lars-thegler/perl/input +++ /dev/null @@ -1,3 +0,0 @@ -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. |
