diff options
| -rw-r--r-- | challenge-348/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-348/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-348/jeanluc2020/perl/ch-1.pl | 99 | ||||
| -rwxr-xr-x | challenge-348/jeanluc2020/perl/ch-2.pl | 118 |
4 files changed, 219 insertions, 0 deletions
diff --git a/challenge-348/jeanluc2020/blog-1.txt b/challenge-348/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..c0f8879913 --- /dev/null +++ b/challenge-348/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-348-1.html diff --git a/challenge-348/jeanluc2020/blog-2.txt b/challenge-348/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..1681284233 --- /dev/null +++ b/challenge-348/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-348-2.html diff --git a/challenge-348/jeanluc2020/perl/ch-1.pl b/challenge-348/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..4d27ab1ed3 --- /dev/null +++ b/challenge-348/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,99 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-348/#TASK1 +# +# Task 1: String Alike +# ==================== +# +# You are given a string of even length. +# +# Write a script to find out whether the given string can be split into two +# halves of equal lengths, each with the same non-zero number of vowels. +# +## Example 1 +## +## Input: $str = "textbook" +## Output: false +## +## 1st half: "text" (1 vowel) +## 2nd half: "book" (2 vowels) +# +# +## Example 2 +## +## Input: $str = "book" +## Output: true +## +## 1st half: "bo" (1 vowel) +## 2nd half: "ok" (1 vowel) +# +# +## Example 3 +## +## Input: $str = "AbCdEfGh" +## Output: true +## +## 1st half: "AbCd" (1 vowel) +## 2nd half: "EfGh" (1 vowel) +# +# +## Example 4 +## +## Input: $str = "rhythmmyth" +## Output: false +## +## 1st half: "rhyth" (0 vowel) +## 2nd half: "mmyth" (0 vowel) +# +# +## Example 5 +## +## Input: $str = "UmpireeAudio" +## Output: false +## +## 1st half: "Umpire" (3 vowels) +## 2nd half: "eAudio" (5 vowels) +# +############################################################ +## +## discussion +## +############################################################ +# +# We turn the string into an array of characters. Then we walk from +# both the left and the right side at the same time, counting the +# vowels as we go along. In the end we check if both halves of the +# string had the same amount of vowels, and if so whether that number +# is bigger than 1. + +use v5.36; + +string_alike("textbook"); +string_alike("book"); +string_alike("AbCdEfGh"); +string_alike("rhythmmyth"); +string_alike("UmpireeAudio"); + +sub string_alike($str) { + say "Input: \"$str\""; + my @chars = split //, $str; + my $vl = 0; + my $vr = 0; + while(@chars) { + my $l = shift @chars; + my $r = pop @chars; + $vl++ if is_vowel($l); + $vr++ if is_vowel($r); + } + if($vl == $vr) { + if($vl > 0) { + return say "Output: true"; + } + } + say "Output: false"; +} + +sub is_vowel($c) { + $c = lc($c); + return 1 if $c eq "a" or $c eq "e" or $c eq "i" or $c eq "o" or $c eq "u"; + return 0; +} diff --git a/challenge-348/jeanluc2020/perl/ch-2.pl b/challenge-348/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..e1ab3d6855 --- /dev/null +++ b/challenge-348/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,118 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-348/#TASK2 +# +# Task 2: Covert Time +# =================== +# +# You are given two strings, $source and $target, containing time in 24-hour time form. +# +# Write a script to convert the source into target by performing one of the following operations: +# +# 1. Add 1 minute +# 2. Add 5 minutes +# 3. Add 15 minutes +# 4. Add 60 minutes +# +# Find the total operations needed to get to the target. +# +# Example 1 +# +# Input: $source = "02:30" +# $target = "02:45" +# Output: 1 +# +# Just one operation i.e. "Add 15 minutes". +# +# +# Example 2 +# +# Input: $source = "11:55" +# $target = "12:15" +# Output: 2 +# +# Two operations i.e. "Add 15 minutes" followed by "Add 5 minutes". +# +# +# Example 3 +# +# Input: $source = "09:00" +# $target = "13:00" +# Output: 4 +# +# Four operations of "Add 60 minutes". +# +# +# Example 4 +# +# Input: $source = "23:45" +# $target = "00:30" +# Output: 3 +# +# Three operations of "Add 15 minutes". +# +# +# Example 5 +# +# Input: $source = "14:20" +# $target = "15:25" +# Output: 2 +# +# Two operations, one "Add 60 minutes" and one "Add 5 minutes" +# +############################################################ +## +## discussion +## +############################################################ +# +# We turn the hours:minutes into minutes since the start of the day. +# Then we calculate the diff: If the source is bigger than the target, +# the diff is the minutes until end of day plut the minutes of target, +# in the other case it's just the diff from source to target. +# Then we check how many times we can fill 60 minutes into this diff, +# then how many additional 15 minutes, then how many additional 5 minutes, +# then how many single minutes. + +use v5.36; + +my $minutes_per_day = 24 * 60; + +convert_time("02:30", "02:45"); +convert_time("11:55", "12:15"); +convert_time("09:00", "13:00"); +convert_time("23:45", "00:30"); +convert_time("14:20", "15:25"); + +sub convert_time($source, $target) { + say "Input: \$source = '$source', \$target = '$target'"; + my ($h, $m) = split /:/, $source; + $h =~ s/^0//; + $m =~ s/^0//; + my $minutes_source = $h * 60 + $m; + ($h, $m) = split /:/, $target; + $h =~ s/^0//; + $m =~ s/^0//; + my $minutes_target = $h * 60 + $m; + if($minutes_source == $minutes_target) { + return say "Output: 0"; + } + my $diff = $minutes_target - $minutes_source; + if($minutes_source > $minutes_target) { + $diff = $minutes_target + $minutes_per_day - $minutes_source; + } + my $count = 0; + while($diff >= 60) { + $diff -= 60; + $count++; + } + while($diff >= 15) { + $diff -= 15; + $count++; + } + while($diff >= 5) { + $diff -= 5; + $count++; + } + $count += $diff; + say "Output: $count"; +} |
