diff --git a/sympy/combinatorics/graycode.py b/sympy/combinatorics/graycode.py index 8e67f587954a..8c84868f5c5c 100644 --- a/sympy/combinatorics/graycode.py +++ b/sympy/combinatorics/graycode.py @@ -362,8 +362,8 @@ def bin_to_gray(bin_list): gray_to_bin """ b = [bin_list[0]] - for i in range(0, len(bin_list) - 1): - b += str(int(bin_list[i]) ^ int(b[i - 1])) + for i in range(1, len(bin_list)): + b += str(int(bin_list[i]) ^ int(bin_list[i - 1])) return ''.join(b) diff --git a/sympy/combinatorics/tests/test_graycode.py b/sympy/combinatorics/tests/test_graycode.py index d67e97d31610..8864a8b9ce6b 100644 --- a/sympy/combinatorics/tests/test_graycode.py +++ b/sympy/combinatorics/tests/test_graycode.py @@ -1,5 +1,6 @@ from sympy.combinatorics.graycode import (GrayCode, bin_to_gray, - random_bitstring, get_subset_from_bitstring, graycode_subsets) + random_bitstring, get_subset_from_bitstring, graycode_subsets, + gray_to_bin) def test_graycode(): @@ -55,3 +56,9 @@ def test_graycode(): assert list(graycode_subsets(['a', 'b', 'c'])) == \ [[], ['c'], ['b', 'c'], ['b'], ['a', 'b'], ['a', 'b', 'c'], ['a', 'c'], ['a']] + +def test_live_issue_117(): + assert bin_to_gray('0100') == '0110' + assert bin_to_gray('0101') == '0111' + for bits in ('0100', '0101'): + assert gray_to_bin(bin_to_gray(bits)) == bits