aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2024-07-19 06:28:47 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2024-07-19 06:28:47 +0800
commitcbb4830d700aae2157fd59d506c0807a0c14f8dc (patch)
treef119c671b6ff1f2ae1f76707dd713afa246ce3ec
parentf95090c2d9bea2620b02e67836e7b5d8cf75f243 (diff)
downloadperlweeklychallenge-club-cbb4830d700aae2157fd59d506c0807a0c14f8dc.tar.gz
perlweeklychallenge-club-cbb4830d700aae2157fd59d506c0807a0c14f8dc.tar.bz2
perlweeklychallenge-club-cbb4830d700aae2157fd59d506c0807a0c14f8dc.zip
Perl and Lisp Solutions
-rw-r--r--challenge-177/cheok-yin-fung/raku/ch-2.raku4
-rw-r--r--challenge-246/cheok-yin-fung/perl/ch-2.pl1
-rw-r--r--challenge-258/cheok-yin-fung/lisp/ch-1.lsp8
-rw-r--r--challenge-258/cheok-yin-fung/lisp/ch-2.lsp26
-rw-r--r--challenge-271/cheok-yin-fung/lisp/ch-1.lsp21
-rw-r--r--challenge-271/cheok-yin-fung/lisp/ch-2.lsp19
-rw-r--r--challenge-273/cheok-yin-fung/lisp/ch-2.lsp16
-rw-r--r--challenge-278/cheok-yin-fung/lisp/ch-1.lsp21
-rw-r--r--challenge-278/cheok-yin-fung/lisp/ch-2.lsp22
-rw-r--r--challenge-278/cheok-yin-fung/perl/ch-1.pl21
-rw-r--r--challenge-278/cheok-yin-fung/perl/ch-2.pl24
11 files changed, 181 insertions, 2 deletions
diff --git a/challenge-177/cheok-yin-fung/raku/ch-2.raku b/challenge-177/cheok-yin-fung/raku/ch-2.raku
index c30f2a6c52..ae9a36c9df 100644
--- a/challenge-177/cheok-yin-fung/raku/ch-2.raku
+++ b/challenge-177/cheok-yin-fung/raku/ch-2.raku
@@ -3,7 +3,7 @@
# Performance: real 0m0.294s user 0m0.441s sys 0m0.032s
use v6;
-sub is-prime ($num) {
+sub my-is-prime ($num) {
my $k = 1;
while ($num !%% (6*$k-1) && $num !%% (6*$k+1) && (6*$k+1 <= sqrt($num))) {
$k++;
@@ -17,7 +17,7 @@ my $pali = 1;
while (@arr.elems < 20) {
if (flip $pali) !%% 2 && $pali !%% 3 && $pali !~~ /0/ {
my $num = $pali~"0"~flip $pali;
- if is-prime($num) {
+ if my-is-prime($num) {
say $num;
push @arr, $num;
}
diff --git a/challenge-246/cheok-yin-fung/perl/ch-2.pl b/challenge-246/cheok-yin-fung/perl/ch-2.pl
index 06db7aa977..8bb1ad6efe 100644
--- a/challenge-246/cheok-yin-fung/perl/ch-2.pl
+++ b/challenge-246/cheok-yin-fung/perl/ch-2.pl
@@ -127,3 +127,4 @@ ok check(0, 3, 0, 0, 0);
ok !check(0, 0, 3, 0, 0);
ok check(2, 4, 8, 16, 32);
+ok check(1,10,-100,1000,-10000)
diff --git a/challenge-258/cheok-yin-fung/lisp/ch-1.lsp b/challenge-258/cheok-yin-fung/lisp/ch-1.lsp
new file mode 100644
index 0000000000..e3249c61a2
--- /dev/null
+++ b/challenge-258/cheok-yin-fung/lisp/ch-1.lsp
@@ -0,0 +1,8 @@
+; The Weekly Challenge 258
+; Task 1 Count Even Digits Number
+
+(defun cedn (lst) (reduce #'+ (mapcar (lambda (r) (mod (+ 1 (length (write-to-string r))) 2)) lst)))
+
+;> (cedn '(10 1 111 24 1000))
+;3
+
diff --git a/challenge-258/cheok-yin-fung/lisp/ch-2.lsp b/challenge-258/cheok-yin-fung/lisp/ch-2.lsp
new file mode 100644
index 0000000000..b29b471ff4
--- /dev/null
+++ b/challenge-258/cheok-yin-fung/lisp/ch-2.lsp
@@ -0,0 +1,26 @@
+; The Weekly Challenge 258
+; Task 2 Sum of Values
+
+(defun bits (n) (setq acc nil)
+ (loop
+ (setq acc (cons (mod n 2) acc))
+ (setq n (ash n -1))
+ (when (= n 0) (return acc))))
+
+(defun one-bits (n)
+ (reduce #'+ (bits n)))
+
+(defun sov (lst k)
+ (setq sum 0)
+ (dotimes (i (length lst))
+ (cond
+ ((equalp k (one-bits i)) (print i) (setq sum (+ (nth i lst) sum)))))
+sum)
+
+; > (sov '(2 5 9 11 3) 1)
+; 17
+; >(sov '(2 5 9 11 3) 2)
+; 11
+; > (sov '(2 5 9 11 3) 0)
+; 2
+
diff --git a/challenge-271/cheok-yin-fung/lisp/ch-1.lsp b/challenge-271/cheok-yin-fung/lisp/ch-1.lsp
new file mode 100644
index 0000000000..6b2203e643
--- /dev/null
+++ b/challenge-271/cheok-yin-fung/lisp/ch-1.lsp
@@ -0,0 +1,21 @@
+; The Weekly Challenge 271
+; Task 1 Maximum Ones
+
+(defun count-one (lst &aux result) (setq result 0)
+ (dolist (x lst)
+ (cond
+ ((equalp 1 x) (setq result (+ result 1)))
+ (t ())))
+ result)
+
+(defun find-sum (matrix)
+ (mapcar (lambda (row) (reduce #'+ row)) matrix)
+))
+
+(defun find-max (matrix &aux ans)
+ (setq ones (find-sum matrix)) (setq rowth 0) (setq ans 1) (setq maximum 0)
+ (dotimes (i (length matrix))
+ (setq rowth (+ 1 rowth))
+ (cond
+ ((> (nth i ones) maximum) (setq maximum (nth i ones)) (setq ans rowth))))
+ans)
diff --git a/challenge-271/cheok-yin-fung/lisp/ch-2.lsp b/challenge-271/cheok-yin-fung/lisp/ch-2.lsp
new file mode 100644
index 0000000000..9cfc9da8a3
--- /dev/null
+++ b/challenge-271/cheok-yin-fung/lisp/ch-2.lsp
@@ -0,0 +1,19 @@
+; The Weekly Challenge 271
+; Task 2 Sort by 1 bits
+
+(defun bits (n) (setq acc nil)
+ (loop
+ (setq acc (cons (mod n 2) acc))
+ (setq n (ash n -1))
+ (when (= n 0) (return acc))))
+
+(defun one-bits (n)
+ (reduce #'+ (bits n)))
+
+(defun sort-by-one-bits (lst)
+ (sort lst #'(lambda (x y) (cond
+ ((< (one-bits x) (one-bits y)) t) (t (< x y))))))
+
+; > (sort-by-one-bits '(0 1 2 3 4 5 6 7))
+; (0 1 2 4 3 5 6 7)
+
diff --git a/challenge-273/cheok-yin-fung/lisp/ch-2.lsp b/challenge-273/cheok-yin-fung/lisp/ch-2.lsp
new file mode 100644
index 0000000000..469f400686
--- /dev/null
+++ b/challenge-273/cheok-yin-fung/lisp/ch-2.lsp
@@ -0,0 +1,16 @@
+; The Weekly Challenge 273
+; Task 2 B After A
+
+(defun c-b-after-a (word)
+ (cond
+ ((null (exist-b word)) nil)
+ (t (searchq "a" (subseq word (search "b" word))))))
+
+(defun searchq (alpha word)
+ (cond
+ ((null (search alpha word)) -1)
+ (t (search alpha word))))
+
+(defun b-after-a (word)
+ (and (exist-b word) (cond ((equalp -1 (c-b-after-a word)) 't) (t 'nil)))))
+
diff --git a/challenge-278/cheok-yin-fung/lisp/ch-1.lsp b/challenge-278/cheok-yin-fung/lisp/ch-1.lsp
new file mode 100644
index 0000000000..a05afb9548
--- /dev/null
+++ b/challenge-278/cheok-yin-fung/lisp/ch-1.lsp
@@ -0,0 +1,21 @@
+; The Weekly Challenge 278
+; Task 1 Sort String
+
+; https://cl-cookbook.sourceforge.net/strings.html
+ (defun split-by-one-space (string)
+ (loop for i = 0 then (1+ j)
+ as j = (position #\Space string :start i)
+ collect (subseq string i j)
+ while j))
+
+(defun parse-int (str) (parse-integer str :start (search (write-to-string (some #'digit-char-p str)) str) :end (length str)))
+(defun parse-alpha (str) (subseq str 0 (search (write-to-string (some #'digit-char-p str)) str)))
+
+(defun ord-lst (lst-a &aux result)
+ (setf result (make-array (length lst-a)))
+ (dolist (x lst-a result)
+ (setf (aref result (- (parse-int x) 1)) (parse-alpha x))))
+
+(defun sort-string (original-sentence)
+ (format nil "~{~A~^ ~}" (coerce (ord-lst (split-by-one-space original-sentence)) 'list)))
+
diff --git a/challenge-278/cheok-yin-fung/lisp/ch-2.lsp b/challenge-278/cheok-yin-fung/lisp/ch-2.lsp
new file mode 100644
index 0000000000..d4f89e0edc
--- /dev/null
+++ b/challenge-278/cheok-yin-fung/lisp/ch-2.lsp
@@ -0,0 +1,22 @@
+; The Weekly Challenge 278
+; Task 2 Reverse Word
+
+(defun searchq (alpha word)
+ (cond
+ ((null (search alpha word)) -1)
+ (t (search alpha word))))
+
+(defun reord-word (word alpha)
+ (format nil "~{~A~}"
+ (list
+ (sort (copy-seq (subseq word 0 (+ 1 (searchq alpha word)))) #'char-lessp)
+ (subseq word (+ 1 (searchq alpha word)) (length word)))))
+
+
+; > (reord-word "challenge" "e")
+; "acehllnge"
+; > (reord-word "programming" "a")
+; "agoprrmming"
+; > (reord-word "champion" "b")
+; "champion"
+
diff --git a/challenge-278/cheok-yin-fung/perl/ch-1.pl b/challenge-278/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..39e3519333
--- /dev/null
+++ b/challenge-278/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,21 @@
+# The Weekly Challenge 278
+# Task 1 Sort String
+use v5.30.0;
+use warnings;
+
+sub ss {
+ my $str = $_[0];
+ my @arr = split " ", $str;
+ my @brr = (0..$#arr);
+ for my $word (@arr) {
+ if ($word =~ /^(\w+)(\d+)$/) {
+ $brr[$2-1] = $1;
+ }
+ }
+ return join " ", @brr;
+}
+
+use Test::More tests=>3;
+ok ss("and2 Raku3 cousins5 Perl1 are4") eq "Perl and Raku are cousins";
+ok ss("guest6 Python1 most4 the3 popular5 is2 language7") eq "Python is the most popular guest language";
+ok ss("Challenge3 The1 Weekly2") eq "The Weekly Challenge";
diff --git a/challenge-278/cheok-yin-fung/perl/ch-2.pl b/challenge-278/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..044165ae5c
--- /dev/null
+++ b/challenge-278/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,24 @@
+# The Weekly Challenge 278
+# Task 2 Reverse Word
+use v5.30.0;
+use warnings;
+
+sub re_word {
+ my $str = $_[0];
+ my $chr = $_[1];
+ my $ind = index($str, $chr);
+ if ($ind > -1) {
+ my $first = substr($str, 0, $ind+1);
+ my $second = substr($str, $ind+1);
+ my $nstr = (join "", sort {$a cmp $b} split "", $first). $second;
+ return $nstr;
+ }
+ else {
+ return $str;
+ }
+}
+
+use Test::More tests=>3;
+ok re_word("challenge", "e") eq "acehllnge";
+ok re_word("programming", "a") eq "agoprrmming";
+ok re_word("champion", "b") eq "champion";