From 6382c71829e4f53e8ee21cdcc756a5bd34e1fc0f Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 29 Jun 2021 22:45:46 +0100 Subject: Add solutions to challenge 119 --- challenge-119/paulo-custodio/ada/ch_1.adb | 37 ++++++++++++++++ challenge-119/paulo-custodio/ada/ch_2.adb | 68 +++++++++++++++++++++++++++++ challenge-119/paulo-custodio/awk/ch-1.awk | 34 +++++++++++++++ challenge-119/paulo-custodio/awk/ch-2.awk | 40 +++++++++++++++++ challenge-119/paulo-custodio/basic/ch-1.bas | 30 +++++++++++++ challenge-119/paulo-custodio/basic/ch-2.bas | 50 +++++++++++++++++++++ challenge-119/paulo-custodio/bc/ch-1.bc | 34 +++++++++++++++ challenge-119/paulo-custodio/bc/ch-2.bc | 55 +++++++++++++++++++++++ challenge-119/paulo-custodio/c/ch-1.c | 37 ++++++++++++++++ challenge-119/paulo-custodio/c/ch-2.c | 60 +++++++++++++++++++++++++ challenge-119/paulo-custodio/cpp/ch-1.cpp | 37 ++++++++++++++++ challenge-119/paulo-custodio/cpp/ch-2.cpp | 59 +++++++++++++++++++++++++ challenge-119/paulo-custodio/forth/ch-1.fs | 34 +++++++++++++++ challenge-119/paulo-custodio/forth/ch-2.fs | 48 ++++++++++++++++++++ challenge-119/paulo-custodio/lua/ch-1.lua | 33 ++++++++++++++ challenge-119/paulo-custodio/lua/ch-2.lua | 41 +++++++++++++++++ challenge-119/paulo-custodio/perl/ch-1.pl | 31 +++++++++++++ challenge-119/paulo-custodio/perl/ch-2.pl | 37 ++++++++++++++++ challenge-119/paulo-custodio/python/ch-1.py | 32 ++++++++++++++ challenge-119/paulo-custodio/python/ch-2.py | 35 +++++++++++++++ challenge-119/paulo-custodio/t/test-1.yaml | 20 +++++++++ challenge-119/paulo-custodio/t/test-2.yaml | 30 +++++++++++++ challenge-119/paulo-custodio/test.pl | 4 ++ 23 files changed, 886 insertions(+) create mode 100644 challenge-119/paulo-custodio/ada/ch_1.adb create mode 100644 challenge-119/paulo-custodio/ada/ch_2.adb create mode 100644 challenge-119/paulo-custodio/awk/ch-1.awk create mode 100644 challenge-119/paulo-custodio/awk/ch-2.awk create mode 100644 challenge-119/paulo-custodio/basic/ch-1.bas create mode 100644 challenge-119/paulo-custodio/basic/ch-2.bas create mode 100644 challenge-119/paulo-custodio/bc/ch-1.bc create mode 100644 challenge-119/paulo-custodio/bc/ch-2.bc create mode 100644 challenge-119/paulo-custodio/c/ch-1.c create mode 100644 challenge-119/paulo-custodio/c/ch-2.c create mode 100644 challenge-119/paulo-custodio/cpp/ch-1.cpp create mode 100644 challenge-119/paulo-custodio/cpp/ch-2.cpp create mode 100644 challenge-119/paulo-custodio/forth/ch-1.fs create mode 100644 challenge-119/paulo-custodio/forth/ch-2.fs create mode 100644 challenge-119/paulo-custodio/lua/ch-1.lua create mode 100644 challenge-119/paulo-custodio/lua/ch-2.lua create mode 100644 challenge-119/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-119/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-119/paulo-custodio/python/ch-1.py create mode 100644 challenge-119/paulo-custodio/python/ch-2.py create mode 100644 challenge-119/paulo-custodio/t/test-1.yaml create mode 100644 challenge-119/paulo-custodio/t/test-2.yaml create mode 100644 challenge-119/paulo-custodio/test.pl diff --git a/challenge-119/paulo-custodio/ada/ch_1.adb b/challenge-119/paulo-custodio/ada/ch_1.adb new file mode 100644 index 0000000000..34327a8f78 --- /dev/null +++ b/challenge-119/paulo-custodio/ada/ch_1.adb @@ -0,0 +1,37 @@ +-- 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. + +with Ada.Command_Line; +with Ada.Text_IO; use Ada.Text_IO; + +procedure ch_1 is + package CL renames Ada.Command_Line; + + n : Integer := Integer'Value(CL.Argument(1)); +begin + n := (((n / 16) mod 16) + ((n mod 16) * 16)); + Put_Line(Integer'Image(n)); +end ch_1; diff --git a/challenge-119/paulo-custodio/ada/ch_2.adb b/challenge-119/paulo-custodio/ada/ch_2.adb new file mode 100644 index 0000000000..c4b4ee3277 --- /dev/null +++ b/challenge-119/paulo-custodio/ada/ch_2.adb @@ -0,0 +1,68 @@ +-- 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 + +with Ada.Command_Line; +with Ada.Text_IO; use Ada.Text_IO; + +procedure ch_2 is + package CL renames Ada.Command_Line; + + function num_ok(num : Integer) return Boolean is + digit, last_digit : Integer := 0; + n : Integer := num; + begin + if n <= 0 then return False; end if; + while n > 0 loop + last_digit := digit; + digit := n mod 10; + n := n / 10; + if digit < 1 or digit > 3 or + (digit = 1 and last_digit = 1) then + return False; + end if; + end loop; + return True; + end num_ok; + + function next_seq(num : Integer) return Integer is + n : Integer := num; + begin + loop + n := n+1; + if num_ok(n) then return n; end if; + end loop; + end next_seq; + + function seq(num : Integer) return Integer is + n, i : Integer := 0; + begin + for i in 1 .. num loop + n := next_seq(n); + end loop; + return n; + end seq; + + num, n : Integer; +begin + num := Integer'Value(CL.Argument(1)); + n := seq(num); + Put_Line(Integer'Image(n)); +end ch_2; diff --git a/challenge-119/paulo-custodio/awk/ch-1.awk b/challenge-119/paulo-custodio/awk/ch-1.awk new file mode 100644 index 0000000000..5ed8cb6c35 --- /dev/null +++ b/challenge-119/paulo-custodio/awk/ch-1.awk @@ -0,0 +1,34 @@ +#!/usr/bin/env gawk + +# 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. + +BEGIN { + n = ARGV[1] + n = ((int(n / 16) % 16) + ((n % 16) * 16)) + print n + exit +} diff --git a/challenge-119/paulo-custodio/awk/ch-2.awk b/challenge-119/paulo-custodio/awk/ch-2.awk new file mode 100644 index 0000000000..9a690ae5d2 --- /dev/null +++ b/challenge-119/paulo-custodio/awk/ch-2.awk @@ -0,0 +1,40 @@ +#!/usr/bin/env gawk + +# 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 + +function next_seq() { + while (1) { + n++ + if (match(n, "^[123]+$") && !match(n, "11")) { + return n; + } + } +} + +BEGIN { + n = 0 + num = ARGV[1] + for (i = 1; i <= num; i++) + n = next_seq(); + print n + exit +} diff --git a/challenge-119/paulo-custodio/basic/ch-1.bas b/challenge-119/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..bacfeddcac --- /dev/null +++ b/challenge-119/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,30 @@ +' 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. + +dim n as integer +n = val(command(1)) +n = ((int(n / 16) mod 16) + ((n mod 16) * 16)) +print trim(str(n)) diff --git a/challenge-119/paulo-custodio/basic/ch-2.bas b/challenge-119/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..9b761f6b94 --- /dev/null +++ b/challenge-119/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,50 @@ +' 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 + +dim shared n as integer + +function num_ok(n as integer) as boolean + dim last_digit as integer, digit as integer + num_ok = false + if n <= 0 then exit function + do while n > 0 + last_digit = digit + digit = n mod 10 + n = n \ 10 + if digit < 1 or digit > 3 or _ + (digit = 1 and last_digit = 1) then + exit function + end if + loop + num_ok = true +end function + +sub next_seq() + do + n = n+1 + if num_ok(n) then exit sub + loop +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)) diff --git a/challenge-119/paulo-custodio/bc/ch-1.bc b/challenge-119/paulo-custodio/bc/ch-1.bc new file mode 100644 index 0000000000..d18826a2c9 --- /dev/null +++ b/challenge-119/paulo-custodio/bc/ch-1.bc @@ -0,0 +1,34 @@ +#!/usr/bin/bc -ql + +/* +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. +*/ + +scale = 0 +n = read() +(((n / 16) % 16) + ((n % 16) * 16)) +quit diff --git a/challenge-119/paulo-custodio/bc/ch-2.bc b/challenge-119/paulo-custodio/bc/ch-2.bc new file mode 100644 index 0000000000..3c948ee0a6 --- /dev/null +++ b/challenge-119/paulo-custodio/bc/ch-2.bc @@ -0,0 +1,55 @@ +#!/usr/bin/bc -ql + +/* +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 +*/ + +scale = 0 +num = read() +seq = 0 + +define num_ok(n) { + auto last_digit, digit, num + num = n + if (num <= 0) return 0; + while (num > 0) { + last_digit = digit + digit = num % 10 + num = num / 10 + if (digit < 1 || digit > 3 || (digit == 1 && last_digit == 1)) + return 0; + } + return 1; +} + +define next_seq() { + while (1) { + seq = seq+1 + if (num_ok(seq)) return seq + } +} + +for (i = 1; i <= num; i++) + seq = next_seq() + +seq +quit diff --git a/challenge-119/paulo-custodio/c/ch-1.c b/challenge-119/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..6f8f418bbd --- /dev/null +++ b/challenge-119/paulo-custodio/c/ch-1.c @@ -0,0 +1,37 @@ +/* +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. +*/ + +#include +#include + +int main(int argc, char* argv[]) { + int n = 0; + if (argc == 2) n = atoi(argv[1]); + n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0); + printf("%d\n", n); +} diff --git a/challenge-119/paulo-custodio/c/ch-2.c b/challenge-119/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..683ff1a972 --- /dev/null +++ b/challenge-119/paulo-custodio/c/ch-2.c @@ -0,0 +1,60 @@ +/* +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 +*/ + +#include +#include +#include + +bool num_ok(int n) { + int last_digit, digit; + + if (n <= 0) + return false; + while (n > 0) { + last_digit = digit; + digit = n % 10; + n /= 10; + if (digit < 1 || digit > 3 || (digit == 1 && last_digit == 1)) + return false; + } + return true; +} + +int next_seq(int seq) { + while (1) { + seq++; + if (num_ok(seq)) + return seq; + } +} + +int main(int argc, char* argv[]) { + int num = 0; + if (argc == 2) num = atoi(argv[1]); + + int seq = 0; + for (int i = 0; i < num; i++) + seq = next_seq(seq); + + printf("%d\n", seq); +} diff --git a/challenge-119/paulo-custodio/cpp/ch-1.cpp b/challenge-119/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..2554e5ab5e --- /dev/null +++ b/challenge-119/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,37 @@ +/* +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. +*/ + +#include +using namespace std; + +int main(int argc, char* argv[]) { + int n = 0; + if (argc == 2) n = atoi(argv[1]); + n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0); + cout << n << endl; +} diff --git a/challenge-119/paulo-custodio/cpp/ch-2.cpp b/challenge-119/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..be94768168 --- /dev/null +++ b/challenge-119/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,59 @@ +/* +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 +*/ + +#include +using namespace std; + +bool num_ok(int n) { + int last_digit, digit; + + if (n <= 0) + return false; + while (n > 0) { + last_digit = digit; + digit = n % 10; + n /= 10; + if (digit < 1 || digit > 3 || (digit == 1 && last_digit == 1)) + return false; + } + return true; +} + +int next_seq(int seq) { + while (1) { + seq++; + if (num_ok(seq)) + return seq; + } +} + +int main(int argc, char* argv[]) { + int num = 0; + if (argc == 2) num = atoi(argv[1]); + + int seq = 0; + for (int i = 0; i < num; i++) + seq = next_seq(seq); + + cout << seq << endl; +} diff --git a/challenge-119/paulo-custodio/forth/ch-1.fs b/challenge-119/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..fac7cb63f8 --- /dev/null +++ b/challenge-119/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,34 @@ +\ 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. + +: swap_nibbles { n -- n } + [ HEX ] + n 4 RSHIFT 00F AND + n 4 LSHIFT 0F0 AND OR + [ DECIMAL ] ; + +NEXT-ARG S>NUMBER? 0= THROW DROP +swap_nibbles . CR BYE diff --git a/challenge-119/paulo-custodio/forth/ch-2.fs b/challenge-119/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..07ebb69080 --- /dev/null +++ b/challenge-119/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,48 @@ +\ 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 + +: num_ok { n -- f } + 0 0 { digit last_digit } + n 0<= IF FALSE EXIT THEN + BEGIN n 0> WHILE + digit TO last_digit + n 10 MOD TO digit + n 10 / TO n + + digit 1 < + digit 3 > OR + digit 1 = last_digit 1 = AND OR + IF FALSE EXIT THEN + REPEAT + TRUE ; + +: next_seq ( n -- n ) + BEGIN + 1+ + DUP num_ok IF EXIT THEN + AGAIN ; + +: seq ( num - n ) + 0 SWAP + 0 DO next_seq LOOP ; + +NEXT-ARG S>NUMBER? 0= THROW DROP +seq . CR BYE diff --git a/challenge-119/paulo-custodio/lua/ch-1.lua b/challenge-119/paulo-custodio/lua/ch-1.lua new file mode 100644 index 0000000000..39498727f0 --- /dev/null +++ b/challenge-119/paulo-custodio/lua/ch-1.lua @@ -0,0 +1,33 @@ +#!/usr/bin/env lua + +--[[ +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. +--]] + +n = tonumber(arg[1]) +n = (((n // 16) % 16) + ((n % 16) * 16)) +io.write(n, "\n") diff --git a/challenge-119/paulo-custodio/lua/ch-2.lua b/challenge-119/paulo-custodio/lua/ch-2.lua new file mode 100644 index 0000000000..db37740957 --- /dev/null +++ b/challenge-119/paulo-custodio/lua/ch-2.lua @@ -0,0 +1,41 @@ +#!/usr/bin/env lua + +--[[ +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 +--]] + +function next_seq(n) + while true do + n = n+1 + if not string.match(n, "[04-9]") and + not string.match(n, "11") then + return n + end + end +end + +num = tonumber(arg[1]) +n = 0 +for i = 1, num, 1 do + n = next_seq(n) +end +io.write(n, "\n") diff --git a/challenge-119/paulo-custodio/perl/ch-1.pl b/challenge-119/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..623c67d7f7 --- /dev/null +++ b/challenge-119/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +# 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. + +use Modern::Perl; +my $N = shift // 0; +say ((($N >> 4) & 0x0f) | (($N << 4) & 0xf0)); diff --git a/challenge-119/paulo-custodio/perl/ch-2.pl b/challenge-119/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..c54c60ba5e --- /dev/null +++ b/challenge-119/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +# 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 + +use Modern::Perl; + +sub next_seq { + state $n = 0; + while (1) { + $n++; + return $n if $n =~ /^[123]+$/ && $n !~ /11/; + } +} + +my $N = shift // 1; +my $n; +$n = next_seq() for 1 .. $N; +say $n; diff --git a/challenge-119/paulo-custodio/python/ch-1.py b/challenge-119/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..5001703497 --- /dev/null +++ b/challenge-119/paulo-custodio/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +# 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. + +import sys +n = int(sys.argv[1]) +n = (((n // 16) % 16) + ((n % 16) * 16)) +print(n) diff --git a/challenge-119/paulo-custodio/python/ch-2.py b/challenge-119/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..1122dfada7 --- /dev/null +++ b/challenge-119/paulo-custodio/python/ch-2.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python + +# 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 + +import sys +import re + +def next_seq(n): + while True: + n += 1 + if re.match(r'^[1-3]+$', str(n)) and not re.search(r'11', str(n)): + return n + +num = int(sys.argv[1]) +n = 0 +for i in range(num): + n = next_seq(n) +print(n) diff --git a/challenge-119/paulo-custodio/t/test-1.yaml b/challenge-119/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..36a966902f --- /dev/null +++ b/challenge-119/paulo-custodio/t/test-1.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 101 + input: + output: 86 +- setup: + cleanup: + args: 86 + input: + output: 101 +- setup: + cleanup: + args: 18 + input: + output: 33 +- setup: + cleanup: + args: 33 + input: + output: 18 diff --git a/challenge-119/paulo-custodio/t/test-2.yaml b/challenge-119/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..8899eee8f0 --- /dev/null +++ b/challenge-119/paulo-custodio/t/test-2.yaml @@ -0,0 +1,30 @@ +- setup: + cleanup: + args: 1 + input: + output: 1 +- setup: + cleanup: + args: 2 + input: + output: 2 +- setup: + cleanup: + args: 3 + input: + output: 3 +- setup: + cleanup: + args: 4 + input: + output: 12 +- setup: + cleanup: + args: 5 + input: + output: 13 +- setup: + cleanup: + args: 60 + input: + output: 2223 diff --git a/challenge-119/paulo-custodio/test.pl b/challenge-119/paulo-custodio/test.pl new file mode 100644 index 0000000000..ba6c37260b --- /dev/null +++ b/challenge-119/paulo-custodio/test.pl @@ -0,0 +1,4 @@ +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; +require '../../challenge-001/paulo-custodio/test.pl'; -- cgit