From fec8edfe431599a9917ebcbb8072f15fe47704a5 Mon Sep 17 00:00:00 2001 From: CY Fung Date: Tue, 7 Mar 2023 06:25:12 +0800 Subject: Week 207 --- challenge-207/cheok-yin-fung/perl/ch-1.pl | 50 +++++++++++++++++++++++++++++++ challenge-207/cheok-yin-fung/perl/ch-2.pl | 20 +++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 challenge-207/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-207/cheok-yin-fung/perl/ch-2.pl 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; -- cgit From a61f57c4f8345cb73ea908f8f867d97966f78df4 Mon Sep 17 00:00:00 2001 From: CY Fung Date: Tue, 7 Mar 2023 21:45:17 +0800 Subject: Add a Node.js Submission for Task 2 in Week 203 --- challenge-203/cheok-yin-fung/node/ch-2.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 challenge-203/cheok-yin-fung/node/ch-2.js 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); -- cgit