|
| 1 | +def gray_code(bit_count: int) -> list: |
| 2 | + """ |
| 3 | + Takes in an integer n and returns a n-bit |
| 4 | + gray code sequence |
| 5 | + An n-bit gray code sequence is a sequence of 2^n |
| 6 | + integers where: |
| 7 | +
|
| 8 | + a) Every integer is between [0,2^n -1] inclusive |
| 9 | + b) The sequence begins with 0 |
| 10 | + c) An integer appears at most one times in the sequence |
| 11 | + d)The binary representation of every pair of integers differ |
| 12 | + by exactly one bit |
| 13 | + e) The binary representation of first and last bit also |
| 14 | + differ by exactly one bit |
| 15 | +
|
| 16 | + >>> gray_code(2) |
| 17 | + [0, 1, 3, 2] |
| 18 | +
|
| 19 | + >>> gray_code(1) |
| 20 | + [0, 1] |
| 21 | +
|
| 22 | + >>> gray_code(3) |
| 23 | + [0, 1, 3, 2, 6, 7, 5, 4] |
| 24 | +
|
| 25 | + >>> gray_code(-1) |
| 26 | + Traceback (most recent call last): |
| 27 | + ... |
| 28 | + ValueError: The given input must be positive |
| 29 | +
|
| 30 | + >>> gray_code(10.6) |
| 31 | + Traceback (most recent call last): |
| 32 | + ... |
| 33 | + TypeError: unsupported operand type(s) for <<: 'int' and 'float' |
| 34 | + """ |
| 35 | + |
| 36 | + # bit count represents no. of bits in the gray code |
| 37 | + if bit_count < 0: |
| 38 | + raise ValueError("The given input must be positive") |
| 39 | + |
| 40 | + # get the generated string sequence |
| 41 | + sequence = gray_code_sequence_string(bit_count) |
| 42 | + # |
| 43 | + # convert them to integers |
| 44 | + for i in range(len(sequence)): |
| 45 | + sequence[i] = int(sequence[i], 2) |
| 46 | + |
| 47 | + return sequence |
| 48 | + |
| 49 | + |
| 50 | +def gray_code_sequence_string(bit_count: int) -> list: |
| 51 | + """ |
| 52 | + Will output the n-bit grey sequence as a |
| 53 | + string of bits |
| 54 | +
|
| 55 | + >>> gray_code_sequence_string(2) |
| 56 | + ['00', '01', '11', '10'] |
| 57 | +
|
| 58 | + >>> gray_code_sequence_string(1) |
| 59 | + ['0', '1'] |
| 60 | + """ |
| 61 | + |
| 62 | + # The approach is a recursive one |
| 63 | + # Base case achieved when either n = 0 or n=1 |
| 64 | + if bit_count == 0: |
| 65 | + return ["0"] |
| 66 | + |
| 67 | + if bit_count == 1: |
| 68 | + return ["0", "1"] |
| 69 | + |
| 70 | + seq_len = 1 << bit_count # defines the length of the sequence |
| 71 | + # 1<< n is equivalent to 2^n |
| 72 | + |
| 73 | + # recursive answer will generate answer for n-1 bits |
| 74 | + smaller_sequence = gray_code_sequence_string(bit_count - 1) |
| 75 | + |
| 76 | + sequence = [] |
| 77 | + |
| 78 | + # append 0 to first half of the smaller sequence generated |
| 79 | + for i in range(seq_len // 2): |
| 80 | + generated_no = "0" + smaller_sequence[i] |
| 81 | + sequence.append(generated_no) |
| 82 | + |
| 83 | + # append 1 to second half ... start from the end of the list |
| 84 | + for i in reversed(range(seq_len // 2)): |
| 85 | + generated_no = "1" + smaller_sequence[i] |
| 86 | + sequence.append(generated_no) |
| 87 | + |
| 88 | + return sequence |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + import doctest |
| 93 | + |
| 94 | + doctest.testmod() |
0 commit comments