diff options
| author | CY Fung <fungcheokyin@gmail.com> | 2023-08-07 07:25:24 +0800 |
|---|---|---|
| committer | CY Fung <fungcheokyin@gmail.com> | 2023-08-07 07:25:24 +0800 |
| commit | c9662a25ca07b7a97dc02eab28d8dccf71a29646 (patch) | |
| tree | 931a75386973ca58bbc95c94f40575682cc73c76 | |
| parent | 97242d0a91cdee85d2e853225ee7008629e215af (diff) | |
| download | perlweeklychallenge-club-c9662a25ca07b7a97dc02eab28d8dccf71a29646.tar.gz perlweeklychallenge-club-c9662a25ca07b7a97dc02eab28d8dccf71a29646.tar.bz2 perlweeklychallenge-club-c9662a25ca07b7a97dc02eab28d8dccf71a29646.zip | |
Week 228
| -rw-r--r-- | challenge-228/cheok-yin-fung/lisp/ch-1.lsp | 6 | ||||
| -rw-r--r-- | challenge-228/cheok-yin-fung/lisp/ch-2.lsp | 19 | ||||
| -rw-r--r-- | challenge-228/cheok-yin-fung/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-228/cheok-yin-fung/perl/ch-2.pl | 26 |
4 files changed, 72 insertions, 0 deletions
diff --git a/challenge-228/cheok-yin-fung/lisp/ch-1.lsp b/challenge-228/cheok-yin-fung/lisp/ch-1.lsp new file mode 100644 index 0000000000..b4f1604c49 --- /dev/null +++ b/challenge-228/cheok-yin-fung/lisp/ch-1.lsp @@ -0,0 +1,6 @@ +(setq arr (list 2 1 1 3 4 )) + +(defun test (item) + (= (position item arr) (- (length arr) (position item (reverse arr)) 1 ))) + +(reduce '+ (remove-if-not #'test arr)) diff --git a/challenge-228/cheok-yin-fung/lisp/ch-2.lsp b/challenge-228/cheok-yin-fung/lisp/ch-2.lsp new file mode 100644 index 0000000000..47eb3646d7 --- /dev/null +++ b/challenge-228/cheok-yin-fung/lisp/ch-2.lsp @@ -0,0 +1,19 @@ +(setq arr (list 3 4 2)) + +(defun action (li) + (if (= (first li) (apply #'min li)) + (rest li) + (append (rest li) (list (first li))))) + +;(defmacro while (condition &body body) +; `(loop while ,condition do (progn ,@body))) + +;(while (> (length arr) 0) +;(progn (setq arr (action arr)) (setq c (+ 1 c)))) + +(let ((c 0)) + (loop + (setq arr (action arr)) + (incf c) + (unless (> (length arr) 0) + (print c)(return)))) diff --git a/challenge-228/cheok-yin-fung/perl/ch-1.pl b/challenge-228/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..a7f53f7357 --- /dev/null +++ b/challenge-228/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,21 @@ +# The Weekly Challenge 228 +# Task 1 Unique Sum +use v5.30.0; +use warnings; +use List::Util qw/sum first/; + +sub us { + my @arr = @_; + my @brr = (0); + for my $t (@arr) { + push @brr, $t if + (first {$arr[$_] == $t} 0..$#arr) + == (first {$arr[$_] == $t} reverse 0..$#arr); + } + return sum @brr; +} + +use Test::More tests=>3; +ok us(2,1,3,2) == 4; +ok us(1,1,1,1) == 0; +ok us(2,1,3,4) == 10; diff --git a/challenge-228/cheok-yin-fung/perl/ch-2.pl b/challenge-228/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..9915433fae --- /dev/null +++ b/challenge-228/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,26 @@ +# The Weekly Challenge 228 +# Task 2 Empty Array +use v5.30.0; +use warnings; +use List::Util qw/min/; + +sub ea { + my @int = @_; + my $c = 0; + while (@int != 0) { + if ($int[0] == min @int) { + shift @int; + } + else { + my $temp = $int[0]; + shift @int; + push @int, $temp; + } + $c++; + } + return $c; +} + +use Test::More tests=>2; +ok ea(3,4,2) == 5; +ok ea(1,2,3) == 3; |
