diff options
| -rw-r--r-- | challenge-272/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-272/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-272/jeanluc2020/perl/ch-1.pl | 47 | ||||
| -rwxr-xr-x | challenge-272/jeanluc2020/perl/ch-2.pl | 90 |
4 files changed, 139 insertions, 0 deletions
diff --git a/challenge-272/jeanluc2020/blog-1.txt b/challenge-272/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..11e85f2259 --- /dev/null +++ b/challenge-272/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-272-1.html diff --git a/challenge-272/jeanluc2020/blog-2.txt b/challenge-272/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..f4bddaff24 --- /dev/null +++ b/challenge-272/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-272-2.html diff --git a/challenge-272/jeanluc2020/perl/ch-1.pl b/challenge-272/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..38ad0509cb --- /dev/null +++ b/challenge-272/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-272/#TASK1 +# +# Task 1: Defang IP Address +# ========================= +# +# You are given a valid IPv4 address. +# +# Write a script to return the defanged version of the given IP address. +# +### A defanged IP address replaces every period “.” with “[.]". +# +## Example 1 +## +## Input: $ip = "1.1.1.1" +## Output: "1[.]1[.]1[.]1" +# +## Example 2 +## +## Input: $ip = "255.101.1.0" +## Output: "255[.]101[.]1[.]0" +# +############################################################ +## +## discussion +## +############################################################ +# +# There are two pretty simple approaches here. Either substitute +# all "." with "[.]" directly, or split the string at "." and +# join it using "[.]" as the glue. Both work fine. + +use strict; +use warnings; + +defang_ip_address("1.1.1.1"); +defang_ip_address("255.101.1.0"); + +sub defang_ip_address { + my $ip = shift; + print "Input: $ip\n"; + $ip =~ s/\./\[.\]/g; + # or: + # $ip = join("[.]", split/\./, $ip); + print "Output: $ip\n"; +} + diff --git a/challenge-272/jeanluc2020/perl/ch-2.pl b/challenge-272/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..ee9a8dd196 --- /dev/null +++ b/challenge-272/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,90 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-272/#TASK2 +# +# Task 2: String Score +# ==================== +# +# You are given a string, $str. +# +# Write a script to return the score of the given string. +# +### The score of a string is defined as the sum of the absolute difference +### between the ASCII values of adjacent characters. +# +## Example 1 +## +## Input: $str = "hello" +## Output: 13 +## +## ASCII values of characters: +## h = 104 +## e = 101 +## l = 108 +## l = 108 +## o = 111 +## +## Score => |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| +## => 3 + 7 + 0 + 3 +## => 13 +# +## Example 2 +## +## Input: "perl" +## Output: 30 +## +## ASCII values of characters: +## p = 112 +## e = 101 +## r = 114 +## l = 108 +## +## Score => |112 - 101| + |101 - 114| + |114 - 108| +## => 11 + 13 + 6 +## => 30 +# +## Example 3 +## +## Input: "raku" +## Output: 37 +## +## ASCII values of characters: +## r = 114 +## a = 97 +## k = 107 +## u = 117 +## +## Score => |114 - 97| + |97 - 107| + |107 - 117| +## => 17 + 10 + 10 +## => 37 +# +############################################################ +## +## discussion +## +############################################################ +# +# Split the string into an array of characters. Then start at +# index 1 and calculate the diff of the ASCII values of the +# character at the given index-1 and the given index, until the +# index reaches the index of the last element in the array. The +# ASCII value can be calculated by the ord() function, the absolute +# value of the diff will be revealed by the abs() function. + +use strict; +use warnings; + +string_score("hello"); +string_score("perl"); +string_score("raku"); + +sub string_score { + my $str = shift; + print "Input: '$str'\n"; + my @chars = split //, $str; + my $score = 0; + foreach my $i (1..$#chars) { + $score += abs(ord($chars[$i-1]) - ord($chars[$i])); + } + print "Output: $score\n"; +} + |
