aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFung Cheok Yin <61836418+E7-87-83@users.noreply.github.com>2020-08-02 19:48:13 +0800
committerGitHub <noreply@github.com>2020-08-02 19:48:13 +0800
commitf556bfc2abc73f8f2631be144ca1a2dfe36c7e00 (patch)
tree0a1070cf4bacff8c4185af99e05bb7345432e6ad
parentf6795e0b8479e43c601aeccd0d8091f7e7a67517 (diff)
downloadperlweeklychallenge-club-f556bfc2abc73f8f2631be144ca1a2dfe36c7e00.tar.gz
perlweeklychallenge-club-f556bfc2abc73f8f2631be144ca1a2dfe36c7e00.tar.bz2
perlweeklychallenge-club-f556bfc2abc73f8f2631be144ca1a2dfe36c7e00.zip
Add files via upload
-rw-r--r--ch-1.lsp29
-rw-r--r--ch-1.pl34
-rw-r--r--ch-2.pl95
3 files changed, 158 insertions, 0 deletions
diff --git a/ch-1.lsp b/ch-1.lsp
new file mode 100644
index 0000000000..538cf3169e
--- /dev/null
+++ b/ch-1.lsp
@@ -0,0 +1,29 @@
+(setf n 20)
+(setf seq () )
+(setf peak_eles ())
+(setf range 50)
+(make-random-state)
+
+(loop for i from 1 upto n do
+ (setf seq (cons (random range) seq))
+)
+
+(setf seq2 seq)
+
+
+(setf x (cadr seq2))
+(setf nm2 (- n 2))
+
+(loop for i from 1 upto nm2 do
+ (setf a (car seq2))
+ (setf b (caddr seq2))
+ (if (AND (< a x) (< b x)) (setf peak_eles (cons x peak_eles)) () )
+ (setf seq2 (cdr seq2) )
+ (setf x (cadr seq2) )
+ (setf l (length seq2) )
+)
+
+(setf peak_eles (reverse peak_eles))
+
+seq ; the sequence given
+peak_eles ; peak elements
diff --git a/ch-1.pl b/ch-1.pl
new file mode 100644
index 0000000000..a395d02ddc
--- /dev/null
+++ b/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+#
+# Perl Weekly Challenge #071 Task 1 Peak Elements
+#
+# Usage: ch-1.pl $N
+
+use strict;
+use warnings;
+
+my $N;
+my @seq = ();
+my @peak_eles = ();
+
+if ($ARGV[0]) {$N = $ARGV[0];} else {$N = 10;}
+
+for (1..$N) {
+ push @seq, 1+int(rand(50))
+}
+
+sub checkpeak {
+ my $p = $_[0];
+ if ($seq[$p-1] < $seq[$p] and $seq[$p+1] < $seq[$p]) {
+ push @peak_eles, $seq[$p];
+ }
+}
+
+for (1..$N-2) {
+ checkpeak($_);
+}
+
+print "Array: [ ", join ", ", @seq;
+print "]\n";
+print "Peak: [ ", join ", ", @peak_eles;
+print "]\n";
diff --git a/ch-2.pl b/ch-2.pl
new file mode 100644
index 0000000000..d0be12efd0
--- /dev/null
+++ b/ch-2.pl
@@ -0,0 +1,95 @@
+#!/usr/bin/perl
+# Perl Weekly Challenge 071 Task 2
+#
+# ref: http://rosettacode.org/wiki/Linked_list#Singly-Linked_List
+#
+# Usage: ch-2.pl [ITEMS in the list] [$N]
+
+use Data::Dumper;
+use strict;
+use warnings;
+
+my @input;
+my $N;
+my @last_np1th_ref = ();
+
+@input = (5,6,3,2);
+$N = 3;
+
+if ($ARGV[0] and $ARGV[1]) {
+ $N = pop @ARGV;
+ @input = @ARGV;
+}
+
+sub insert_after {
+ my $node = $_[0];
+ my $next = $node->{_next};
+ shift;
+ while (defined $_[0]) {
+ $node->{_next} = $_[0];
+ $node = $node->{_next};
+ shift;
+ }
+ $node->{_next} = $next;
+}
+
+sub stored_np1th_ref_from_tail {
+ push @last_np1th_ref, $_[0];
+ if ( $#last_np1th_ref > $N) {
+ shift @last_np1th_ref;
+ }
+}
+
+my $list = {value => 'HEAD'};
+
+sub insert_input {
+ for my $v (reverse @input) { #why need reverse here?
+ insert_after $list, { value=> $v};
+ }
+}
+
+sub print_linked_list {
+# print "HEAD->";
+ my $node = $list->{_next};
+ print $node->{value}, "->";
+ while ($node = $node->{_next}) {
+ print $node->{value};
+ print "->"
+ }
+ print "nil";
+ print "\n";
+}
+
+insert_input;
+print_linked_list;
+
+print "The '$N'-th node counted from the tail will be deleted. \n";
+
+my $size = 0;
+my $f = $list;
+while (defined(my $x = $f->{_next})) {
+ $size++;
+ stored_np1th_ref_from_tail( \$x );
+ $f = $x;
+}
+
+sub remove_ref_as_req {
+ if ( $N > 1 ) {
+ if ($N < $size) {
+ undef ${$last_np1th_ref[1]};
+ ${$last_np1th_ref[0]}->{_next} = ${$last_np1th_ref[2]};
+ }
+ else {
+ undef $list->{_next};
+ $list->{_next} = ${$last_np1th_ref[1]};
+ }
+ }
+ else {
+ undef ${$last_np1th_ref[1]};
+ ${$last_np1th_ref[0]}->{_next} = undef;
+ }
+}
+
+
+remove_ref_as_req;
+print_linked_list;