aboutsummaryrefslogtreecommitdiff
path: root/challenge-119
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-07-05 00:13:51 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2021-07-05 00:13:51 +0100
commitb8f4ad974bb37720b841cb5ab0929f82dc6aa00b (patch)
tree0ab3b53b3ba723c59c88df425b8f797c031b7a09 /challenge-119
parentf25e5a0ed34601ae0267ba5a526c3274adb24d9a (diff)
parenta41fe498f2d33fce84507f041fc57fb10e8bc0f4 (diff)
downloadperlweeklychallenge-club-b8f4ad974bb37720b841cb5ab0929f82dc6aa00b.tar.gz
perlweeklychallenge-club-b8f4ad974bb37720b841cb5ab0929f82dc6aa00b.tar.bz2
perlweeklychallenge-club-b8f4ad974bb37720b841cb5ab0929f82dc6aa00b.zip
Merge branch 'paulo-custodio'
Diffstat (limited to 'challenge-119')
-rw-r--r--challenge-119/paulo-custodio/pascal/ch-1.pas37
-rw-r--r--challenge-119/paulo-custodio/pascal/ch-2.pas60
2 files changed, 97 insertions, 0 deletions
diff --git a/challenge-119/paulo-custodio/pascal/ch-1.pas b/challenge-119/paulo-custodio/pascal/ch-1.pas
new file mode 100644
index 0000000000..9e0ccc0e2e
--- /dev/null
+++ b/challenge-119/paulo-custodio/pascal/ch-1.pas
@@ -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.
+*)
+
+program ch1(input, output);
+uses sysutils;
+var
+ n: Integer;
+begin
+ n := StrToInt(paramStr(1));
+ n := (((n div 16) mod 16) + ((n mod 16) * 16));
+ WriteLn(n);
+end.
diff --git a/challenge-119/paulo-custodio/pascal/ch-2.pas b/challenge-119/paulo-custodio/pascal/ch-2.pas
new file mode 100644
index 0000000000..5207425ad9
--- /dev/null
+++ b/challenge-119/paulo-custodio/pascal/ch-2.pas
@@ -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
+*)
+
+program ch2(input, output);
+uses sysutils;
+function nextSeq(n: Integer): Integer;
+ function numOk(n: Integer): Boolean;
+ var
+ lastDigit, digit: Integer;
+ begin
+ if n <= 0 then
+ numOk := False
+ else begin
+ numOk := True;
+ digit := 0;
+ while n > 0 do begin
+ lastDigit := digit;
+ digit := n mod 10;
+ n := n div 10;
+ if (digit < 1) or (digit > 3)
+ or ((digit = 1) and (lastDigit = 1)) then
+ numOk := False;
+ end;
+ end;
+ end;
+ begin
+ repeat
+ n := n + 1;
+ until numOk(n);
+ nextSeq := n;
+ end;
+var
+ num, i, n: Integer;
+begin
+ num := StrToInt(paramStr(1));
+ n := 0;
+ for i := 1 to num do
+ n := nextSeq(n);
+ WriteLn(n);
+end.