aboutsummaryrefslogtreecommitdiff
path: root/challenge-119
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2021-07-12 10:48:31 -0400
committerDave Jacoby <jacoby.david@gmail.com>2021-07-12 10:48:31 -0400
commit34a6514808c066bee4e7f3d7d8bdeb67db056392 (patch)
tree05d0e268045ef3d6f971ec0e0c3eb1a48bdb7edd /challenge-119
parentb59f8f4008bb8ec491a9e89f097f04ce54aed4c0 (diff)
parent1aa7b6eaba2a58fc1ef0612373e3aed6b61f345d (diff)
downloadperlweeklychallenge-club-34a6514808c066bee4e7f3d7d8bdeb67db056392.tar.gz
perlweeklychallenge-club-34a6514808c066bee4e7f3d7d8bdeb67db056392.tar.bz2
perlweeklychallenge-club-34a6514808c066bee4e7f3d7d8bdeb67db056392.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-119')
-rw-r--r--challenge-119/james-smith/README.md4
-rw-r--r--challenge-119/james-smith/cesil/cesil.pl2
-rwxr-xr-xchallenge-119/jaredor/perl/ch-2.pl2
-rw-r--r--challenge-119/paul-fajman/perl/ch-1.pl55
-rw-r--r--challenge-119/paul-fajman/perl/ch-2.pl58
-rw-r--r--challenge-119/paulo-custodio/basic/ch-1.bas2
-rw-r--r--challenge-119/paulo-custodio/basic/ch-2.bas2
-rw-r--r--challenge-119/paulo-custodio/fortran/ch-1.f9039
-rw-r--r--challenge-119/paulo-custodio/fortran/ch-2.f9083
-rwxr-xr-xchallenge-119/stuart-little/lua/ch-1.lua5
-rwxr-xr-xchallenge-119/stuart-little/lua/ch-2.lua18
11 files changed, 264 insertions, 6 deletions
diff --git a/challenge-119/james-smith/README.md b/challenge-119/james-smith/README.md
index 11f1a24015..ee078890c0 100644
--- a/challenge-119/james-smith/README.md
+++ b/challenge-119/james-smith/README.md
@@ -14,7 +14,7 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-119/ja
# Task 1 - Swap Nibbles
-***You are given a positive integer `$N`. Write a script to swap the two nibbles of the binary representation of the given number and print the decimal number of the new binary representation.(((
+***You are given a positive integer `$N`. Write a script to swap the two nibbles of the binary representation of the given number and print the decimal number of the new binary representation.***
## The solution
@@ -176,7 +176,7 @@ my %commands = (
## Parser loop
while(<>) {
((@in=map{/^\s+-?\d+\s*$/?0+$_:()}<>),last)if/^ {8}%/;
- ($ptrs{$1},$_)=(0+@code,$2) if m/^(\S{1,7})\s+(.*)/;
+ ($ptrs{$1},$_)=(0+@code,$2) if /^(\S{1,7})\s+(.*)/;
my($cmd,$data) = split/\s+/,s/^\s+//r=~s/\s+$//r, 2;
die "\n## Unknown command [$cmd @ ",1+@code,"]\n"
unless exists $commands{$cmd};
diff --git a/challenge-119/james-smith/cesil/cesil.pl b/challenge-119/james-smith/cesil/cesil.pl
index 64b0355de7..756f10227b 100644
--- a/challenge-119/james-smith/cesil/cesil.pl
+++ b/challenge-119/james-smith/cesil/cesil.pl
@@ -36,7 +36,7 @@ my %commands = (
## Parser loop
while(<>) {
((@in=map{/^\s+-?\d+\s*$/?0+$_:()}<>),last)if/^ {8}%/;
- ($ptrs{$1},$_)=(0+@code,$2) if m/^(\S{1,7})\s+(.*)/;
+ ($ptrs{$1},$_)=(0+@code,$2) if /^(\S{1,7})\s+(.*)/;
my($cmd,$data) = split/\s+/,s/^\s+//r=~s/\s+$//r, 2;
die "\n## Unknown command [$cmd @ ",1+@code,"]\n"
unless exists $commands{$cmd};
diff --git a/challenge-119/jaredor/perl/ch-2.pl b/challenge-119/jaredor/perl/ch-2.pl
index d4c5ca117b..3d67492cfa 100755
--- a/challenge-119/jaredor/perl/ch-2.pl
+++ b/challenge-119/jaredor/perl/ch-2.pl
@@ -11,7 +11,7 @@ use Pod::Usage;
# For this challenge
-use Data::Dump qw(pp);
+#use Data::Dump qw(pp);
# Validate Input
diff --git a/challenge-119/paul-fajman/perl/ch-1.pl b/challenge-119/paul-fajman/perl/ch-1.pl
new file mode 100644
index 0000000000..cd2b2f0165
--- /dev/null
+++ b/challenge-119/paul-fajman/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#/usr/bin/perl
+
+# Weekly Challenge 119 Task #1
+# You are given a positive integer $N.
+# Write a script to swap the two nibbles of the binary representation of the
+# given number and print the decimal number of the new binary representation.
+#
+# Input: $N = 101
+# Output: 86
+
+# Binary representation of decimal 101 is 1100101 or as 2 nibbles (0110)(0101).
+# The swapped nibbles would be (0101)(0110) same as decimal 86.
+
+# Input: $N = 18
+# Output: 33
+
+# Binary representation of decimal 18 is 10010 or as 2 nibbles (0001)(0010).
+# The swapped nibbles would be (0010)(0001) same as decimal 33.
+##############################
+
+use strict;
+use warnings;
+
+use POSIX;
+
+my $int = $ARGV[0];
+my $quo = $int;
+my ($i, $rem);
+my $total = 0;
+my @nibbles=();
+
+# Calculate binary number
+while($quo ne 0) {
+ $rem = floor($quo % 2);
+ unshift @nibbles, $rem;
+ $quo = floor($quo/2);
+}
+
+# Check that final number is 8 digits
+while ($#nibbles+1 lt 8) {
+ unshift @nibbles, 0;
+}
+
+# Swap the nibbles
+my @final = splice(@nibbles, 4);
+@final = (@final, @nibbles);
+
+# Calcuate the new decimal number
+for ($i=7; $i>-1; $i--) {
+ $total+= $final[$i]*(2**(7-$i));
+}
+
+print "Input: \$N = $int\n";
+print "Output: $total\n";
+
diff --git a/challenge-119/paul-fajman/perl/ch-2.pl b/challenge-119/paul-fajman/perl/ch-2.pl
new file mode 100644
index 0000000000..f764ea153a
--- /dev/null
+++ b/challenge-119/paul-fajman/perl/ch-2.pl
@@ -0,0 +1,58 @@
+#/usr/bin/perl
+
+# Weekly Challenge 119 Task #2
+# Write a script to generate sequence starting at 1. Consider the
+# increasing sequence of integers which contain only 1’s, 2’s and
+# 3’s, and do not have any doublets of 1’s like below. Please
+# accept a positive integer $N and print the $Nth term in the
+# generated sequence.
+
+# 1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131
+
+use strict;
+use warnings;
+
+use POSIX;
+
+my $input = $ARGV[0];
+my @numbers;
+
+my ($quo, $rem, $i);
+my $check=0;
+my ($oneone, $output);
+
+for ($i=1;$i<$input+1;$i++) {
+ $quo = $i;
+ # Calculate base 4 number
+ # Later throw out any values with 0
+ while ($quo ne 0) {
+ $rem = floor($quo % 4);
+ unshift @numbers, $rem;
+ $quo = floor($quo/4);
+ }
+ # Check for any digit with a 0
+ foreach (@numbers) {
+ if ($_ eq 0) {
+ $check=1;
+ next;
+ }
+ $oneone.=$_;
+ }
+
+ # Check if any digits are consecutive ones or if one was a 0;
+ if ($oneone =~ m/11/ || $check eq 1) {
+ undef($oneone);
+ undef(@numbers);
+ $check = 0;
+ $input++;
+ next;
+ }
+
+ $output = $oneone;
+ undef(@numbers);
+ undef($oneone);
+}
+
+print "INPUT: \$N = $ARGV[0]\n";
+print "OUTPUT: $output\n";
+
diff --git a/challenge-119/paulo-custodio/basic/ch-1.bas b/challenge-119/paulo-custodio/basic/ch-1.bas
index bacfeddcac..f93c21d499 100644
--- a/challenge-119/paulo-custodio/basic/ch-1.bas
+++ b/challenge-119/paulo-custodio/basic/ch-1.bas
@@ -27,4 +27,4 @@
dim n as integer
n = val(command(1))
n = ((int(n / 16) mod 16) + ((n mod 16) * 16))
-print trim(str(n))
+print n
diff --git a/challenge-119/paulo-custodio/basic/ch-2.bas b/challenge-119/paulo-custodio/basic/ch-2.bas
index 9b761f6b94..ae3836bbaf 100644
--- a/challenge-119/paulo-custodio/basic/ch-2.bas
+++ b/challenge-119/paulo-custodio/basic/ch-2.bas
@@ -47,4 +47,4 @@ end sub
dim num as integer, i as integer
num = val(command(1))
for i=1 to num: next_seq: next i
-print trim(str(n))
+print n
diff --git a/challenge-119/paulo-custodio/fortran/ch-1.f90 b/challenge-119/paulo-custodio/fortran/ch-1.f90
new file mode 100644
index 0000000000..ac7f8fa924
--- /dev/null
+++ b/challenge-119/paulo-custodio/fortran/ch-1.f90
@@ -0,0 +1,39 @@
+! Challenge 119
+!
+! TASK #1 - Swap Nibbles
+! Submitted by: Mohammad S Anwar
+! You are given a positive integer $N.
+!
+! Write a script to swap the two nibbles of the binary representation of the
+! given number and print the decimal number of the new binary representation.
+!
+! A nibble is a four-bit aggregation, or half an octet.
+!
+! To keep the task simple, we only allow integer less than or equal to 255.
+!
+! Example
+! Input: $N = 101
+! Output: 86
+!
+! Binary representation of decimal 101 is 1100101 or as 2 nibbles (0110)(0101).
+! The swapped nibbles would be (0101)(0110) same as decimal 86.
+!
+! Input: $N = 18
+! Output: 33
+!
+! Binary representation of decimal 18 is 10010 or as 2 nibbles (0001)(0010).
+! The swapped nibbles would be (0010)(0001) same as decimal 33.
+
+program ch1
+ implicit none
+
+ integer :: n, stat
+ character(len=50) :: arg
+
+ call get_command_argument(1, arg)
+ if (len_trim(arg) /= 0) then
+ read(arg,*,iostat=stat) n
+ n = ((mod((n / 16), 16)) + (mod(n, 16) * 16))
+ print *, n
+ end if
+end program ch1
diff --git a/challenge-119/paulo-custodio/fortran/ch-2.f90 b/challenge-119/paulo-custodio/fortran/ch-2.f90
new file mode 100644
index 0000000000..ea5555091e
--- /dev/null
+++ b/challenge-119/paulo-custodio/fortran/ch-2.f90
@@ -0,0 +1,83 @@
+! Challenge 119
+!
+! TASK #2 - Sequence without 1-on-1
+! Submitted by: Cheok-Yin Fung
+! Write a script to generate sequence starting at 1. Consider the increasing
+! sequence of integers which contain only 1's, 2's and 3's, and do not have any
+! doublets of 1's like below. Please accept a positive integer $N and print the
+! $Nth term in the generated sequence.
+!
+! 1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131, ...
+!
+! Example
+! Input: $N = 5
+! Output: 13
+!
+! Input: $N = 10
+! Output: 32
+!
+! Input: $N = 60
+! Output: 2223
+
+program ch2
+ implicit none
+
+ integer :: i, num, n, stat
+ character(len=50) :: arg
+
+ call get_command_argument(1, arg)
+ if (len_trim(arg) /= 0) then
+ read(arg,*,iostat=stat) num
+
+ n = 0
+ do i = 1, num
+ n = next_seq(n)
+ end do
+
+ print *, n
+ end if
+
+contains
+ function num_ok(n1)
+ logical :: num_ok
+ integer, intent(in) :: n1
+ integer :: n, digit, last_digit
+ logical :: failed
+
+ n = n1
+ failed = .false.
+ if (n <= 0) then
+ failed = .true.
+ end if
+
+ digit = 0
+ do while (n > 0)
+ last_digit = digit
+ digit = mod(n, 10)
+ n = n / 10
+ if (digit < 1 .or. digit > 3 .or. (digit == 1 .and. last_digit == 1)) then
+ failed = .true.
+ end if
+ end do
+
+ num_ok = .not. failed
+ end function num_ok
+
+ function next_seq(n1)
+ integer :: next_seq
+ integer, intent(in) :: n1
+ integer :: n
+
+ n = n1
+ n = n+1
+ do while (.true.)
+ if (num_ok(n)) then
+ exit
+ else
+ n = n+1
+ end if
+ end do
+
+ next_seq = n
+ end function next_seq
+end program ch2
diff --git a/challenge-119/stuart-little/lua/ch-1.lua b/challenge-119/stuart-little/lua/ch-1.lua
new file mode 100755
index 0000000000..ef9a6daa31
--- /dev/null
+++ b/challenge-119/stuart-little/lua/ch-1.lua
@@ -0,0 +1,5 @@
+#!/usr/bin/env lua
+
+-- run <script> <number>
+
+print((arg[1] & 15)*16 + (arg[1] >> 4 & 15))
diff --git a/challenge-119/stuart-little/lua/ch-2.lua b/challenge-119/stuart-little/lua/ch-2.lua
new file mode 100755
index 0000000000..c10111e775
--- /dev/null
+++ b/challenge-119/stuart-little/lua/ch-2.lua
@@ -0,0 +1,18 @@
+#!/usr/bin/env lua
+
+-- run <script> <number>
+
+function nxt(s)
+ a,b=string.match(s:reverse(),"(3*)(.*)")
+ pre,threes=b:reverse(),a
+ if pre:len() ~= 0 then
+ return math.floor(pre+1) .. string.sub(string.rep("12",threes:len()),1,threes:len())
+ end
+ return string.sub(string.rep("12",s:len()),1,s:len()+1)
+end
+
+out="0"
+for i=1,arg[1] do
+ out=nxt(out)
+end
+print(out)