diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-08 19:44:57 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-08 19:44:57 +0000 |
| commit | 3a6ff4e60e9c97c2644ce9463bb522077c36a2bc (patch) | |
| tree | 8d82aacbf31da03452fb9dab015481e2177e4ba2 | |
| parent | eec30c1f4243a121d0dc433ce1590023ba9541c9 (diff) | |
| parent | a61f57c4f8345cb73ea908f8f867d97966f78df4 (diff) | |
| download | perlweeklychallenge-club-3a6ff4e60e9c97c2644ce9463bb522077c36a2bc.tar.gz perlweeklychallenge-club-3a6ff4e60e9c97c2644ce9463bb522077c36a2bc.tar.bz2 perlweeklychallenge-club-3a6ff4e60e9c97c2644ce9463bb522077c36a2bc.zip | |
Merge pull request #7682 from E7-87-83/newt
Week 207
| -rw-r--r-- | challenge-203/cheok-yin-fung/node/ch-2.js | 27 | ||||
| -rw-r--r-- | challenge-207/cheok-yin-fung/perl/ch-1.pl | 50 | ||||
| -rw-r--r-- | challenge-207/cheok-yin-fung/perl/ch-2.pl | 20 |
3 files changed, 97 insertions, 0 deletions
diff --git a/challenge-203/cheok-yin-fung/node/ch-2.js b/challenge-203/cheok-yin-fung/node/ch-2.js new file mode 100644 index 0000000000..deef8ef5cc --- /dev/null +++ b/challenge-203/cheok-yin-fung/node/ch-2.js @@ -0,0 +1,27 @@ +// The Weekly Challenge 203 +// Task 2 Copy Directory +// Usage: node ch-2.js source target +// 2023 March 7th + +const fs = require('fs'); +const path = require('path'); +const { exec } = require('child_process'); + +const source = process.argv[2]; +const target = process.argv[3]; + +const dir_walk = (head) => { + if (fs.lstatSync(head).isDirectory()) { + const dirname = head.split("/")[head.split("/").length-1]; + const relpath = path.relative(source, head); + if (source != head) { + exec(`cd ${path.join(target, relpath, '..')} && mkdir ${dirname}`); + } + const filenames = fs.readdirSync(head); + filenames.forEach( f => { + dir_walk(path.join(head, f)); + }); + } +} + +dir_walk(source); diff --git a/challenge-207/cheok-yin-fung/perl/ch-1.pl b/challenge-207/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..a30764252b --- /dev/null +++ b/challenge-207/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,50 @@ +# The Weekly Challenge 207 +# Task 1 Keyboard Word +use v5.30.0; +use warnings; +use List::Util qw/uniq/; + +sub kw { + my @row = ("qwertyuiop", "asdfghjkl", "zxcvbnm"); + my @_row = map {join "", sort {$a cmp $b} split "", $_} @row; + + my @ans; + my @words = @_; + foreach my $word (@words) { + my $_word = join "", uniq sort {$a cmp $b} split "", lc $word; + push @ans, $word if subseq($_row[0], $_word); + push @ans, $word if subseq($_row[1], $_word); + push @ans, $word if subseq($_row[2], $_word); + } + return @ans; +} + +sub subseq { + # @mseq and @sseq are with distinct elements, + # and sorted in the same way + my $valid; + my @mseq = split "", $_[0]; + my @sseq = split "", $_[1]; + my $i = 0; + my $j = 0; + while ($j < scalar @sseq) { + if ($sseq[$j] lt $mseq[$i]) { + $valid = 0; + last; + } + if ($sseq[$j] eq $mseq[$i]) { + $j++; + $i++; + $valid = 1; + } + last if $j > $#sseq || $i > $#mseq; + $i++ if $sseq[$j] gt $mseq[$i]; + last if $i > $#mseq; + } + return 1 if $valid && $j == 1+$#sseq && $i <= 1+$#mseq; + return 0; +} + + + +say join " ", kw("Hello","Alaska","Dad","Peace", "OMG", "Bye"); diff --git a/challenge-207/cheok-yin-fung/perl/ch-2.pl b/challenge-207/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..16c9c8ede0 --- /dev/null +++ b/challenge-207/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,20 @@ +# The Weekly Challenge 207 +# Task 2 H-Index +use v5.30.0; +use warnings; + +sub hi { + my @citations = @_; + @citations = sort {$b<=>$a} @citations; + $i = 0; + while ($i <= $#citations && $citations[$i] >= $i+1) { + $i++ + } + return $i; +} + +use Test::More tests=>4; +ok hi(10,8,5,4,3) == 4; +ok hi(25,8,5,3,3) == 3; +ok hi(25) == 1; +ok hi(2) == 1; |
