diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2023-03-09 20:23:18 +0100 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2023-03-09 20:23:18 +0100 |
| commit | f2945b0daa6480adc7eec4b76434199fdc50fe89 (patch) | |
| tree | 6f5644ab0a19b44673d70839cc469c7e212897cc | |
| parent | f2e33c0038917ad43651d0c4e8b0bb310eaed541 (diff) | |
| download | perlweeklychallenge-club-f2945b0daa6480adc7eec4b76434199fdc50fe89.tar.gz perlweeklychallenge-club-f2945b0daa6480adc7eec4b76434199fdc50fe89.tar.bz2 perlweeklychallenge-club-f2945b0daa6480adc7eec4b76434199fdc50fe89.zip | |
Add solutions for challenge 207
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
| -rw-r--r-- | challenge-207/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-207/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-207/jeanluc2020/perl/ch-1.pl | 75 | ||||
| -rwxr-xr-x | challenge-207/jeanluc2020/perl/ch-2.pl | 66 |
4 files changed, 143 insertions, 0 deletions
diff --git a/challenge-207/jeanluc2020/blog-1.txt b/challenge-207/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..b1b2806218 --- /dev/null +++ b/challenge-207/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-207-1.html diff --git a/challenge-207/jeanluc2020/blog-2.txt b/challenge-207/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..1eccc0264a --- /dev/null +++ b/challenge-207/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-207-2.html diff --git a/challenge-207/jeanluc2020/perl/ch-1.pl b/challenge-207/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..3fc73a6d2b --- /dev/null +++ b/challenge-207/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,75 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-207/#TASK1 +# +# Task 1: Keyboard Word +# ===================== +# +# You are given an array of words. +# +# Write a script to print all the words in the given array that can be types using alphabet on only one row of the keyboard. +# +# Let us assume the keys are arranged as below: +# +# Row 1: qwertyuiop +# Row 2: asdfghjkl +# Row 3: zxcvbnm +# +## Example 1 +## +## Input: @words = ("Hello","Alaska","Dad","Peace") +## Output: ("Alaska","Dad") +# +## Example 2 +## +## Input: @array = ("OMG","Bye") +## Output: () +# +############################################################ +## +## discussion +## +############################################################ +# +# Basically, for all input words, we create the same word in +# lowercase and for each keyboard row, we check whether we can +# build this word with this row and add it to the output if we +# can + +use strict; +use warnings; +use feature 'say'; + +keyboard_word("Hello","Alaska","Dad","Peace"); +keyboard_word("OMG","Bye"); + +sub keyboard_word { + my @words = @_; + my @output = (); + foreach my $word (@words) { + # if the lowercase "lc()" version of $word is a keyboard + # word, we can push the word onto the output + push @output, $word if is_keyboard_word(lc($word)); + } + say "Input: (" . join(",", @words) . ")"; + say "Output: (" . join(",", @output) . ")"; +} + +sub is_keyboard_word { + my $word = shift; + my @rows = ( "qwertyuiop", "asdfghjkl", "zxcvbnm"); + my @chars = split //, $word; + foreach my $row (@rows) { + # if $found_all is still 1 after all characters have been + # found in the current row, we found a keyboard word + my $found_all = 1; + foreach my $char (@chars) { + if($row !~ m/$char/) { + # we found a character that is not in this row + $found_all = 0; + } + } + return 1 if $found_all; + } + # we haven't found all characters in the same row for all rows + return 0; +} diff --git a/challenge-207/jeanluc2020/perl/ch-2.pl b/challenge-207/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..0db086a67a --- /dev/null +++ b/challenge-207/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-207/#TASK2 +# +# Task 2: H-Index +# =============== +# +# You are given an array of integers containing citations a researcher has +# received for each paper. +# +# Write a script to compute the researcher’s H-Index. For more information +# please checkout the wikipedia page. +# +### The H-Index is the largest number h such that h articles have at least h +### citations each. For example, if an author has five publications, with 9, +### 7, 6, 2, and 1 citations (ordered from greatest to least), then the +### author’s h-index is 3, because the author has three publications with 3 +### or more citations. However, the author does not have four publications +### with 4 or more citations. +# +# +## Example 1 +## +## Input: @citations = (10,8,5,4,3) +## Output: 4 +## +## Because the 4th publication has 4 citations and the 5th has only 3. +# +## Example 2 +## +## Input: @citations = (25,8,5,3,3) +## Output: 3 +## +## The H-Index is 3 because the fourth paper has only 3 citations. +# +############################################################ +## +## discussion +## +############################################################ +# +# Basically you count from 1 to the number of publications and +# check the number of citations for each. If the number of +# citations drops below the current index, you found the H-Index + +use strict; +use warnings; +use feature 'say'; + +h_index(10,8,5,4,3); +h_index(25,8,5,3,3); +h_index(7,6,5,6,7,5,5); +h_index(7,6,5,6); + +sub h_index { + my @citations = sort {$b <=> $a} @_; + say "Input: (" . join(",", @citations) . ")"; + my $h = 0; + foreach my $c (@citations) { + $h++; + if($h > $c) { + $h--; + last; + } + } + say "Output $h"; +} |
