aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFung Cheok Yin <61836418+E7-87-83@users.noreply.github.com>2020-07-20 05:19:32 +0800
committerGitHub <noreply@github.com>2020-07-20 05:19:32 +0800
commit17553eff091a1b96c4ddd7a118787e8b4d38da5d (patch)
tree9340d1b0c03be85bd735cd151ed3a9e86305853e
parent29dfa6833d2200d2d08d9737be16fc5b286a0ec2 (diff)
downloadperlweeklychallenge-club-17553eff091a1b96c4ddd7a118787e8b4d38da5d.tar.gz
perlweeklychallenge-club-17553eff091a1b96c4ddd7a118787e8b4d38da5d.tar.bz2
perlweeklychallenge-club-17553eff091a1b96c4ddd7a118787e8b4d38da5d.zip
Add files via upload
-rw-r--r--challenge-069/cheok-yin-fung/perl/ch-2.pl107
1 files changed, 107 insertions, 0 deletions
diff --git a/challenge-069/cheok-yin-fung/perl/ch-2.pl b/challenge-069/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..f01259207d
--- /dev/null
+++ b/challenge-069/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,107 @@
+#!/usr/bin/perl
+#
+# Perl Weekly Challenge #069 Task 2
+# Usage: ch-2.pl $N
+
+# SN = SN-1 + “0” + switch(reverse(SN-1))
+# SN = SN-2 + “0” + switch(reverse(SN-2)) + "0" + SN-2 + "1" + switch(reverse(SN-2))
+# on my computer, ordinary action goes "out of memory" when $N>26.
+# Therefore I write a roundabout to tackle $N=30
+
+
+use strict;
+use warnings;
+
+my $N = 3;
+
+if ($ARGV[0]) {
+ $N = $ARGV[0];
+}
+
+sub sr {
+ my @digits = split // , $_[0];
+ my @newdigits;
+ for (@digits) {
+ unshift @newdigits, (1+$_) % 2;
+ }
+ return join "", @newdigits;
+}
+
+
+my @S = ("", "0");
+my @R;
+
+for (my $d = $N % 2 + 2; $d <= $N and $d<=26 ; $d += 2) {
+ $R[$d-2] = sr($S[$d-2]);
+ $S[$d] = $S[$d-2] . "0" . $R[$d-2]. "0" . $S[$d-2]. "1". $R[$d-2];
+}
+
+if ($N <= 26) {
+ print $S[$N];
+} elsif ($N !=30) {
+ print "too large!";
+}
+
+print "\n";
+
+
+#the following are for $N=30
+
+# $S[$d] = $S[$d-2] . "0" . $R[$d-2]. "0" . $S[$d-2]. "1". $R[$d-2];
+# $R[$d] = $S[$d-2] . "0" . $R[$d-2]. "1" . $S[$d-2]. "1". $R[$d-2];
+sub print_S28 {
+ print $S[26];
+ print "0";
+ print $R[26];
+ print "0";
+ print $S[26];
+ print "1";
+ print $R[26];
+}
+
+sub print_R28 {
+ print $S[26];
+ print "0";
+ print $R[26];
+ print "1";
+ print $S[26];
+ print "1";
+ print $R[26];
+}
+if ($N == 30) {
+ $R[26] = $S[24]."0".$R[24]."1".$S[24]."1".$R[24];
+ print_S28;
+ print "0";
+ print_R28;
+ print "0";
+ print_S28;
+ print "1";
+ print_R28;
+
+}
+print "\n";
+
+
+# $N = 25 -->
+# real 0m7.241s
+# user 0m3.701s
+# sys 0m0.681s
+#
+#
+#$ perl ch-2.pl 7
+#0010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011
+
+# $ time perl ch-2.pl 26 > S26.txt
+#
+# real 0m8.045s
+# user 0m7.172s
+# sys 0m0.863s
+
+#$ time perl ch-2.pl 30 > S30.txt
+#
+# real 0m12.099s
+# user 0m7.873s
+# sys 0m1.432s
+#
+# size of S26.txt : 67.1 MB
+# size of S30.txt : 1.1 GB