aboutsummaryrefslogtreecommitdiff
path: root/challenge-278
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-278')
-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
4 files changed, 88 insertions, 0 deletions
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";