diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-29 13:26:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-29 13:26:03 +0100 |
| commit | 3585bef9924cd5178c75b4e827b9244336fb2288 (patch) | |
| tree | b18a382de9a2e7e3689f22075d400cd8f9ae3962 | |
| parent | 6d3932b1a0ab89f6e6a0543be3e459948d8af4d7 (diff) | |
| parent | d6ecaf76dc40a13452340db5c6e4ac9d13518c72 (diff) | |
| download | perlweeklychallenge-club-3585bef9924cd5178c75b4e827b9244336fb2288.tar.gz perlweeklychallenge-club-3585bef9924cd5178c75b4e827b9244336fb2288.tar.bz2 perlweeklychallenge-club-3585bef9924cd5178c75b4e827b9244336fb2288.zip | |
Merge pull request #10337 from spadacciniweb/PWC-275
Add ch-1 and ch-2 in Perl and Elixir
| -rw-r--r-- | challenge-275/spadacciniweb/elixir/ch-1.exs | 63 | ||||
| -rw-r--r-- | challenge-275/spadacciniweb/elixir/ch-2.exs | 66 | ||||
| -rw-r--r-- | challenge-275/spadacciniweb/perl/ch-1.pl | 64 | ||||
| -rw-r--r-- | challenge-275/spadacciniweb/perl/ch-2.pl | 64 |
4 files changed, 257 insertions, 0 deletions
diff --git a/challenge-275/spadacciniweb/elixir/ch-1.exs b/challenge-275/spadacciniweb/elixir/ch-1.exs new file mode 100644 index 0000000000..50b96c82fa --- /dev/null +++ b/challenge-275/spadacciniweb/elixir/ch-1.exs @@ -0,0 +1,63 @@ +# Task 1: Broken Keys +# Submitted by: Mohammad Sajid Anwar +# +# You are given a sentence, $sentence and list of broken keys @keys. +# Write a script to find out how many words can be typed fully. +# +# Example 1 +# Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a') +# Output: 0 +# +# Example 2 +# Input: $sentence = "Perl and Raku", @keys = ('a') +# Output: 1 +# +# Only Perl since the other word two words contain 'a' and can't be typed fully. +# +# Example 3 +# Input: $sentence = "Well done Team PWC", @keys = ('l', 'o') +# Output: 2 +# +# Example 4 +# Input: $sentence = "The joys of polyglottism", @keys = ('T') +# Output: 2 + +defmodule BrokenKeys do + + def sentence2word(sentence, keys) do + String.downcase(sentence) + |> String.split(" ", trim: true) + |> Enum.reduce(0, fn w, acc -> case count_missing(w, keys) do + true -> acc + false -> acc+1 + end + end) + end + + def count_missing(word, keys) do + keys + |> Enum.any?(fn x -> String.contains?(word, String.downcase(x)) end) + end + + def out(sentence, keys) do + count_missing(sentence, keys) + IO.puts( "sentence '#{sentence}' keys (#{Enum.join(keys, ",")}) -> #{sentence2word(sentence, keys)}" ) + end + +end + +sentence = "Perl Weekly Challenge"; +keys = ["l", "a"]; +BrokenKeys.out(sentence, keys); + +sentence = "Perl and Raku" +keys = ["a"] +BrokenKeys.out(sentence, keys); + +sentence = "Well done Team PWC"; +keys = ["l", "o"]; +BrokenKeys.out(sentence, keys); + +sentence = "The joys of polyglottism"; +keys = ["T"]; +BrokenKeys.out(sentence, keys); diff --git a/challenge-275/spadacciniweb/elixir/ch-2.exs b/challenge-275/spadacciniweb/elixir/ch-2.exs new file mode 100644 index 0000000000..08fc444429 --- /dev/null +++ b/challenge-275/spadacciniweb/elixir/ch-2.exs @@ -0,0 +1,66 @@ +# Task 2: Replace Digits +# Submitted by: Mohammad Sajid Anwar +# +# You are given an alphanumeric string, $str, where each character is either a letter or a digit. +# Write a script to replace each digit in the given string with the value of the previous letter plus (digit) places. +# +# Example 1 +# Input: $str = 'a1c1e1' +# Ouput: 'abcdef' +# +# shift('a', 1) => 'b' +# shift('c', 1) => 'd' +# shift('e', 1) => 'f' +# +# Example 2 +# Input: $str = 'a1b2c3d4' +# Output: 'abbdcfdh' +# +# shift('a', 1) => 'b' +# shift('b', 2) => 'd' +# shift('c', 3) => 'f' +# shift('d', 4) => 'h' +# +# Example 3 +# Input: $str = 'b2b' +# Output: 'bdb' +# +# Example 4 +# Input: $str = 'a16z' +# Output: 'abgz' + +defmodule ReplaceDigits do + + def transform(mychar, v) do + <<c::utf8>> = mychar + <<c + String.to_integer( v )::utf8>> + end + + def translate(str) do + Enum.to_list(0..String.length(str)-1) + |> Enum.map(fn i -> case Regex.match?(~r/[0-9]/, String.at(str, i)) do + true -> transform( String.at(str, i-1), String.at(str, i) ) + false -> String.at(str, i) + end + end) + |> Enum.join("") + end + + def out(str) do + translate(str) + IO.puts( "'#{str}' -> '#{translate(str)}'" ) + end + +end + +str = "a1c1e1"; +ReplaceDigits.out(str); + +str = "a1b2c3d4" +ReplaceDigits.out(str); + +str = "b2b" +ReplaceDigits.out(str); + +str = "a16z" +ReplaceDigits.out(str); diff --git a/challenge-275/spadacciniweb/perl/ch-1.pl b/challenge-275/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..672c044521 --- /dev/null +++ b/challenge-275/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl + +# Task 1: Broken Keys +# Submitted by: Mohammad Sajid Anwar +# +# You are given a sentence, $sentence and list of broken keys @keys. +# Write a script to find out how many words can be typed fully. +# +# Example 1 +# Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a') +# Output: 0 +# +# Example 2 +# Input: $sentence = "Perl and Raku", @keys = ('a') +# Output: 1 +# +# Only Perl since the other word two words contain 'a' and can't be typed fully. +# +# Example 3 +# Input: $sentence = "Well done Team PWC", @keys = ('l', 'o') +# Output: 2 +# +# Example 4 +# Input: $sentence = "The joys of polyglottism", @keys = ('T') +# Output: 2 + +use strict; +use warnings; +use List::Util qw/ sum /; + +my $sentence = "Perl Weekly Challenge"; +my @keys = ('l', 'a'); +broken_keys($sentence, \@keys); + +$sentence = "Perl and Raku"; +@keys = ('a'); +broken_keys($sentence, \@keys); + +$sentence = "Well done Team PWC"; +@keys = ('l', 'o'); +broken_keys($sentence, \@keys); + +$sentence = "The joys of polyglottism"; +@keys = ('T'); +broken_keys($sentence, \@keys); + +exit 0; + +sub broken_keys { + my $sentence = shift; + my $keys = shift; + + printf "sentence '%s' keys (%s) -> %d\n", + $sentence, + (join ',', @keys), + (scalar split / /, $sentence) - + sum map { + my $tot = 0; + my $word = lc($_); + $tot += 1 if sum map { $_ = lc($_); + $word =~ /$_/ ? 1 : 0 + } @keys; + } split / /, lc($sentence); +} diff --git a/challenge-275/spadacciniweb/perl/ch-2.pl b/challenge-275/spadacciniweb/perl/ch-2.pl new file mode 100644 index 0000000000..659cb4fa0a --- /dev/null +++ b/challenge-275/spadacciniweb/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl + +# Task 2: Replace Digits +# Submitted by: Mohammad Sajid Anwar +# +# You are given an alphanumeric string, $str, where each character is either a letter or a digit. +# Write a script to replace each digit in the given string with the value of the previous letter plus (digit) places. +# +# Example 1 +# Input: $str = 'a1c1e1' +# Ouput: 'abcdef' +# +# shift('a', 1) => 'b' +# shift('c', 1) => 'd' +# shift('e', 1) => 'f' +# +# Example 2 +# Input: $str = 'a1b2c3d4' +# Output: 'abbdcfdh' +# +# shift('a', 1) => 'b' +# shift('b', 2) => 'd' +# shift('c', 3) => 'f' +# shift('d', 4) => 'h' +# +# Example 3 +# Input: $str = 'b2b' +# Output: 'bdb' +# +# Example 4 +# Input: $str = 'a16z' +# Output: 'abgz' + +use strict; +use warnings; + +my $str = 'a1c1e1'; +replace_digits($str); + +$str = 'a1b2c3d4'; +replace_digits($str); + +$str = 'b2b'; +replace_digits($str); + +$str = 'a16z'; +replace_digits($str); + +exit 0; + +sub replace_digits { + my $str = shift; + + my $char = ''; + my $new_str = join '', map { + my $curr_char = ($_ =~ /[0-9]/) + ? chr(ord($char)+$_) + : $_; + $char = $curr_char; + } split //, $str; + printf "'%s' -> '%s'\n", + $str, + $new_str; +} |
