aboutsummaryrefslogtreecommitdiff
path: root/challenge-071/duncan-c-white/README
blob: 6f2a919fd66e7f7dac7014eedb35bc1e6c21a76e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Task 1: "Character Swapping

You are given a string $S of size $N.

You are also given swap count $C and offset $O such that $C >= 1, $O >=
1, $C <= $O and $C + $O <= $N.

Write a script to perform character swapping like below:

for i = 1 to $C
  S[ i % N ] <=> S[ (i + O) % N ]

Example 1

Input:
    $S = 'perlandraku'
    $C = 3
    $O = 4

01234567890
perlandraku

Character Swapping:
    swap 1: pos 1 and 5: e <=> n = pnrlaedraku
    swap 2: pos 2 and 6: r <=> d = pndlaerraku
    swap 3: pos 3 and 7: l <=> r = pndraerlaku

Output:
    pndraerlaku
"

My notes: ok.  none of $i % N (for i=1..$C) needs the % N
given that $C + $O <= $N and min $O is 1.  So that becomes:

for i = 1 to $C
  S[ i ] <=> S[ (i + O) % N ]

and the only one of the (i + O) % N expressions that may need
the % N part is i=C, when C+O == N.  Of course i + O increments
so pre-calculate it, and set it back to 0 hit it hits N.


Task 2: "Gray Code Sequence

You are given an integer 2 <= $N <= 5.

Write a script to generate $N-bit gray code sequence.

2-bit Gray Code Sequence: [0, 1, 3, 2]

To generate the 3-bit Gray code sequence from the 2-bit Gray code sequence,
follow the step below:

2-bit Gray Code sequence:                  [0, 1, 3, 2]

Binary form of the sequence        a) S1 = [00, 01, 11, 10]

Reverse of S1                      b) S2 = [10, 11, 01, 00]

Prefix all entries of S1 with '0'  c) S1 = [000, 001, 011, 010]

Prefix all entries of S2 with '1'  d) S2 = [110, 111, 101, 100]

Concatenate S1 and S2 gives 3-bit Gray Code sequence
				   e) [000, 001, 011, 010, 110, 111, 101, 100]

Now convert them back to decimal:

3-bit Gray Code sequence              [0, 1, 3, 2, 6, 7, 5, 4]

Example

Input: N = 4
Output: [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8]
"

My notes: ok.  However, there's no point in taking the original N-1-bit gray
code sequence, converting it to binary, prepending zeroes and converting it
back to decimal - because leading zeroes don't change the decimal numbers.
So the first half of the N-bit gray code sequence is simply the N-1-bit gray
code sequence.