gray-code

Cartesian product in Gray code order with itertools?

北城以北 提交于 2020-04-16 05:47:16
问题 Is there something like Python's itertools.product() that provides the iteration through the Cartesian product of a set of sets in Gray code order ? For example, supposing that such a hypothetical generator existed, and it was called gray_code_product() , then gray_code_product(['a','b','c'], [0,1], ['x','y']) would generate, in the order : ('a',0,'x') ('a',0,'y') ('a',1,'y') ('a',1,'x') ('b',1,'x') ('b',1,'y') ('b',0,'y') ('b',0,'x') ('c',0,'x') ('c',0,'y') ('c',1,'y') ('c',1,'x') 回答1:

Gray code for all k element subset of {1,…,n}

穿精又带淫゛_ 提交于 2020-01-03 03:49:10
问题 I am seeking for an algorithm which iterates through all k element subsets of an n element set. I do not want to generate all those subset explicitly. There is an easy algorithm to do this, namely sorting the corresponding bit vectors lexographically and then go from the current subset to the next one. Nevertheless, I seek for an algorithm which only switches 2 bits in each step. I have read that such a code is a called "gray-code" but I did not found an algorithm for my problem. Is there a

Generating gray codes.

强颜欢笑 提交于 2020-01-01 05:46:10
问题 I tried generating gray codes in Python . This code works correctly. The issue is that I am initialising the base case ( n=1,[0,1] ) in the main function and passing it to gray_code function to compute the rest. I want to generate all the gray codes inside the function itself including the base case. How do I do that? def gray_code(g,n): k=len(g) if n<=0: return else: for i in range (k-1,-1,-1): char='1'+g[i] g.append(char) for i in range (k-1,-1,-1): g[i]='0'+g[i] gray_code(g,n-1) def main()

How to find if two numbers are consecutive numbers in gray code sequence

半城伤御伤魂 提交于 2019-12-31 16:32:41
问题 I am trying to come up with a solution to the problem that given two numbers, find if they are the consecutive numbers in the gray code sequence i.e., if they are gray code neighbors assuming that the gray code sequence is not mentioned. I searched on various forums but couldn't get the right answer. It would be great if you can provide a solution for this. My attempt to the problem - Convert two integers to binary and add the digits in both the numbers separately and find the difference

How to find if two numbers are consecutive numbers in gray code sequence

泄露秘密 提交于 2019-12-31 16:31:37
问题 I am trying to come up with a solution to the problem that given two numbers, find if they are the consecutive numbers in the gray code sequence i.e., if they are gray code neighbors assuming that the gray code sequence is not mentioned. I searched on various forums but couldn't get the right answer. It would be great if you can provide a solution for this. My attempt to the problem - Convert two integers to binary and add the digits in both the numbers separately and find the difference

Fill matrix with binary numbers, regular and gray coded

半腔热情 提交于 2019-12-20 06:25:00
问题 I have a matrix that holds 1:s or 0:s, creating binary numbers. Its width is n. For n = 2 and n = 3 it would look like: 00 000 01 001 10 010 11 011 100 101 110 111 and so on. Right now I'm using the following code to produce this. int row = (int) Math.pow(2, n); int col = n; int[][] matrix = new int[row][col]; for (int r = 0; r < row; r++) { String binaryNumber = String.format("%" + n + "s", Integer.toBinaryString(r)).replace(' ', '0'); for (int c = col - 1; c >= 0; c--) { matrix[r][c] =

Gray code pattern in tournament chart?

人走茶凉 提交于 2019-12-19 10:15:51
问题 In a tournament chart from the bottom to the top where there is a winner I've been told that it is somehow connected with the gray-code. I know that the grey code is an alternative code, it's recursive and is useful to find the best solution in various games, space-filling-curves, error correction codes, harddisk positioning and is a shorthand for the piano player but how is this code is related with a tournament chart? 回答1: Parsed the following from here: A tournament is really a node in a

input 2 integers and get binary, brgc, and hamming distance

独自空忆成欢 提交于 2019-12-12 19:00:49
问题 I've got everything except hamming distance. I keep getting the error "int() can't convert non-string with explicit base" here is my code: def int2bin(n): if n: bits = [] while n: n,remainder = divmod(n, 2) bits.insert(0, remainder) return bits else: return [0] def bin2gray(bits): return bits[:1] + [i ^ ishift for i, ishift in zip(bits[:-1], bits[1:])] def hamming(a,b): assert len(a) == len(b) count,z = 0,int(a,2)^int(b,2) while z: count += 1 z &= z-1 return count def main(): a = int(input(

Non-recursive Grey code algorithm understanding

眉间皱痕 提交于 2019-12-11 10:39:49
问题 This is task from algorithms book. The thing is that I completely don't know where to start! Trace the following non-recursive algorithm to generate the binary reflexive Gray code of order 4. Start with the n-bit string of all 0’s. For i = 1, 2, ... 2^n-1, generate the i-th bit string by flipping bit b in the previous bit string, where b is the position of the least significant 1 in the binary representation of i. So I know the Gray code for 1 bit should be 0 1 , for 2 00 01 11 10 etc. Many

Choose some numbers coding in Gray Code

≡放荡痞女 提交于 2019-12-11 07:30:15
问题 I have to write a programm that shows some numbers coding in Gray Code. I already found an algorithm written in C++ in this page ( https://www.geeksforgeeks.org/given-a-number-n-generate-bit-patterns-from-0-to-2n-1-so-that-successive-patterns-differ-by-one-bit/ ). But I want to create a new method to delete the numbers that have two "1" consecutively and have "1" in their extremity (left and right). Example : for n = 3 we get this numbers : 000 001 011 010 110 111 101 100 Now I want to delete