diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-06-30 22:17:27 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-06-30 22:17:27 +0100 |
| commit | 147f004cf7df0555cd6f3070d0cab204477b26ac (patch) | |
| tree | f15f61024814e1f797869213d5ffbb2f60dce164 | |
| parent | 6382c71829e4f53e8ee21cdcc756a5bd34e1fc0f (diff) | |
| download | perlweeklychallenge-club-147f004cf7df0555cd6f3070d0cab204477b26ac.tar.gz perlweeklychallenge-club-147f004cf7df0555cd6f3070d0cab204477b26ac.tar.bz2 perlweeklychallenge-club-147f004cf7df0555cd6f3070d0cab204477b26ac.zip | |
Add D solution to challenge 119
| -rw-r--r-- | challenge-119/paulo-custodio/d/ch_1.d | 36 | ||||
| -rw-r--r-- | challenge-119/paulo-custodio/d/ch_2.d | 58 |
2 files changed, 94 insertions, 0 deletions
diff --git a/challenge-119/paulo-custodio/d/ch_1.d b/challenge-119/paulo-custodio/d/ch_1.d new file mode 100644 index 0000000000..b8fb08e22c --- /dev/null +++ b/challenge-119/paulo-custodio/d/ch_1.d @@ -0,0 +1,36 @@ +/* +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 std.stdio; +import std.conv; + +void main(string[] args) { + auto n = to!int(args[1]); + n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0); + writeln(n); +} diff --git a/challenge-119/paulo-custodio/d/ch_2.d b/challenge-119/paulo-custodio/d/ch_2.d new file mode 100644 index 0000000000..d431d1e7e1 --- /dev/null +++ b/challenge-119/paulo-custodio/d/ch_2.d @@ -0,0 +1,58 @@ +/* +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 +*/ + +import std.stdio; +import std.conv; + +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; + } +} + +void main(string[] args) { + auto num = to!int(args[1]); + + int seq = 0; + foreach (int i; 0 .. num) + seq = next_seq(seq); + + writeln(seq); +} |
