From f05f12de78c1993126d47fc93088b6490b057bb1 Mon Sep 17 00:00:00 2001 From: JunaidIRF <61500818+JunaidIRF@users.noreply.github.com> Date: Sun, 6 Sep 2026 16:46:44 +0500 Subject: [PATCH] Improve typing annotations, doctests, and docstrings in gray_code_sequence --- bit_manipulation/gray_code_sequence.py | 27 +++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/bit_manipulation/gray_code_sequence.py b/bit_manipulation/gray_code_sequence.py index 636578d89754..ab66eb7b1e7c 100644 --- a/bit_manipulation/gray_code_sequence.py +++ b/bit_manipulation/gray_code_sequence.py @@ -1,4 +1,4 @@ -def gray_code(bit_count: int) -> list: +def gray_code(bit_count: int) -> list[int]: """ Takes in an integer n and returns a n-bit gray code sequence @@ -7,12 +7,15 @@ def gray_code(bit_count: int) -> list: a) Every integer is between [0,2^n -1] inclusive b) The sequence begins with 0 - c) An integer appears at most one times in the sequence - d)The binary representation of every pair of integers differ + c) An integer appears at most one time in the sequence + d) The binary representation of every pair of integers differ by exactly one bit e) The binary representation of first and last bit also differ by exactly one bit + >>> gray_code(0) + [0] + >>> gray_code(2) [0, 1, 3, 2] @@ -37,21 +40,19 @@ def gray_code(bit_count: int) -> list: if bit_count < 0: raise ValueError("The given input must be positive") - # get the generated string sequence + # get the generated string sequence and convert them to integers sequence = gray_code_sequence_string(bit_count) - # - # convert them to integers - for i in range(len(sequence)): - sequence[i] = int(sequence[i], 2) - - return sequence + return [int(code, 2) for code in sequence] -def gray_code_sequence_string(bit_count: int) -> list: +def gray_code_sequence_string(bit_count: int) -> list[str]: """ - Will output the n-bit grey sequence as a + Will output the n-bit Gray code sequence as a string of bits + >>> gray_code_sequence_string(0) + ['0'] + >>> gray_code_sequence_string(2) ['00', '01', '11', '10'] @@ -73,7 +74,7 @@ def gray_code_sequence_string(bit_count: int) -> list: # recursive answer will generate answer for n-1 bits smaller_sequence = gray_code_sequence_string(bit_count - 1) - sequence = [] + sequence: list[str] = [] # append 0 to first half of the smaller sequence generated for i in range(seq_len // 2):