Skip to main content
Tweeted twitter.com/StackCompSci/status/766115755222073344
graphs!
Source Link

for n = 2

   n = 2       00            n = 3

                             000
    00                      / | \
   /  \                    /  |  \
  01  10                100  010  001
   \  /                  | \/   \/ |
    11        11           | /\   /\ |
                        110  101  011
                           \  |  /
                            \ | /
                             111

for n = 2

            00
           /  \
          01  10
           \  /
            11
   n = 2                   n = 3

                             000
    00                      / | \
   /  \                    /  |  \
  01  10                100  010  001
   \  /                  | \/   \/ |
    11                   | /\   /\ |
                        110  101  011
                           \  |  /
                            \ | /
                             111
added 280 characters in body
Source Link

edit:

I think it can be viewed as a graph:

for n = 2

            00
           /  \
          01  10
           \  /
            11

The sequence is required to traverse each edge in both directions, but in neither direction more than once.

edit:

I think it can be viewed as a graph:

for n = 2

            00
           /  \
          01  10
           \  /
            11

The sequence is required to traverse each edge in both directions, but in neither direction more than once.

Replaced C with pseudocode
Source Link

I have written a bit of code to brute force generate these codes. However, it starts to struggle at n = 5 (I'm assuming this is because of the complexity, but it may well be because there isactual code in a bugprev. revision of this question).

#include <stdio.h>
#include <stdlib.h>

struct mjc_ll {
  const unsigned long int val;
  const struct mjc_ll* next;
};

// prints integer val in binary

void mjc_print_bin(const unsigned long int val, const unsigned char sf)
{
  if (sf != 0) {
    unsigned long int s = (1 << (sf - 1));
    while (s != 0) {
      printf("%c", ((val & s) != 0) ? '1' : '0');
      s >>= 1;
    }
  }
  printf("\n");
}

unsigned long int mjc_power(const unsigned long intFUNCTION base,gen
                const unsigned long int exponent)
{
IF height returnof (exponentstack == 0) ? 1 : (base * mjc_power(base, exponent - 1));
}

// returns 1 if transition found in ll, 0 otherwise

int mjc_search_ll(const struct mjc_ll* const ll, const unsigned long int val,
          const unsigned long int prev2^bit_width)
{
  if (ll->next ==* 0bit_width) {
    // reached end of list
    returnPRINT 0;ZERO_PATTERN
  } else if ((val == ll->val) && (prev == ll->next->val))RETURN {TRUE
   IF //the change found
    return 1;
  }
  return mjc_search_ll(ll->next, val, prev);
}

// printsbetween the solution and returns 1 if a solution found, otherwise returns 0

int mjc_next(const struct mjc_ll* const ll, const unsigned long int val,
         const unsigned char bit_len, const unsigned long int count,
         const unsigned long int limit)
{
  if (count == limit) {
    //value reachedat the end
    mjc_print_bin(0, bit_len);
    return 1;
  } else if ((ll != 0) &&top (mjc_search_ll(ll,of val,stack ll->val)and !=value 0))does {
occur between other values //on foundthe duplicatestack
    returnRETURN 0;FALSE
  }
  const struct mjc_ll newPUSH =value {val,to ll};stack
  unsigned charWHILE ishift =< 0;bit_width
  while(i < bit_len) {
IF RECURSE with value ifas (mjc_next(&new, valvalue ^XOR (1 << ishift), bit_len, count + 1, limit) == 1) {
      mjc_print_bin(val,returns bit_len);TRUE
      returnPRINT 1;value
    }
   RETURN ++i;TRUE
  }
  return 0;
}

voidshift mjc(const= unsignedshift char+ bit_len)
{1
  (void)mjc_next(0, 0, bit_len, 0, mjc_power(2, bit_len) *RETURN bit_len);
}
FALSE
int main(int argc, char* argv[])
{
 SET ifstack (argcto !=an 2)empty {list
    printf("mjc takes 1 arg, the bit widthSET ofbit_width theto pattern\n");n
  } else {
 CALL gen with mjc(strtol(argv[1],value 0,as 0));
  }
  return 0;
}

I have written a bit of code to brute force generate these codes. However, it starts to struggle at n = 5 (I'm assuming this is because of the complexity, but it may well be because there is a bug).

#include <stdio.h>
#include <stdlib.h>

struct mjc_ll {
  const unsigned long int val;
  const struct mjc_ll* next;
};

// prints integer val in binary

void mjc_print_bin(const unsigned long int val, const unsigned char sf)
{
  if (sf != 0) {
    unsigned long int s = (1 << (sf - 1));
    while (s != 0) {
      printf("%c", ((val & s) != 0) ? '1' : '0');
      s >>= 1;
    }
  }
  printf("\n");
}

unsigned long int mjc_power(const unsigned long int base,
                const unsigned long int exponent)
{
  return (exponent == 0) ? 1 : (base * mjc_power(base, exponent - 1));
}

// returns 1 if transition found in ll, 0 otherwise

int mjc_search_ll(const struct mjc_ll* const ll, const unsigned long int val,
          const unsigned long int prev)
{
  if (ll->next == 0) {
    // reached end of list
    return 0;
  } else if ((val == ll->val) && (prev == ll->next->val)) {
    // change found
    return 1;
  }
  return mjc_search_ll(ll->next, val, prev);
}

// prints the solution and returns 1 if a solution found, otherwise returns 0

int mjc_next(const struct mjc_ll* const ll, const unsigned long int val,
         const unsigned char bit_len, const unsigned long int count,
         const unsigned long int limit)
{
  if (count == limit) {
    // reached the end
    mjc_print_bin(0, bit_len);
    return 1;
  } else if ((ll != 0) && (mjc_search_ll(ll, val, ll->val) != 0)) {
    // found duplicate
    return 0;
  }
  const struct mjc_ll new = {val, ll};
  unsigned char i = 0;
  while(i < bit_len) {
    if (mjc_next(&new, val ^ (1 << i), bit_len, count + 1, limit) == 1) {
      mjc_print_bin(val, bit_len);
      return 1;
    }
    ++i;
  }
  return 0;
}

void mjc(const unsigned char bit_len)
{
  (void)mjc_next(0, 0, bit_len, 0, mjc_power(2, bit_len) * bit_len);
}

int main(int argc, char* argv[])
{
  if (argc != 2) {
    printf("mjc takes 1 arg, the bit width of the pattern\n");
  } else {
    mjc(strtol(argv[1], 0, 0));
  }
  return 0;
}

I have written a bit of code to brute force generate these codes. However, it starts to struggle at n = 5 (actual code in a prev. revision of this question).

FUNCTION gen
  IF height of stack == ((2^bit_width) * bit_width)
    PRINT ZERO_PATTERN
    RETURN TRUE
  IF the change between the value at the top of stack and value does occur between other values on the stack
    RETURN FALSE

  PUSH value to stack
  WHILE shift < bit_width
    IF RECURSE with value as (value XOR (1 << shift)) returns TRUE
      PRINT value
      RETURN TRUE
    shift = shift + 1
  RETURN FALSE


SET stack to an empty list
SET bit_width to n

CALL gen with value as 0
edited tags
Link
Raphael
  • 72.9k
  • 30
  • 181
  • 393
Loading
Source Link
Loading