8
\$\begingroup\$

Your Challenge is to take a binary string as input and get all binary numbers between itself and the reverse of its string representation (the reverse may be less than or greater than the original input).

Output must be padded with zeroes to be the same length as the input.

The Order in which the values are displayed doesn't matter, but the Code needs to work with Values that are bigger than its reverse.

Test Cases

Input: (1 or 0)
Output: None, string and its reverse are identical

Input: (any palindrome binary)
Output: None, same as above

Input: 01
Output: None, since there is no binary between 01 and 10 (1 and 2)

Input: 001
Output: 010,011 (Numbers between 001 (1) and 100 (4): 2,3)

Input: 100
Output: 010,011 (Same as above)

Input: 0010
Output: 0011 (Numbers between 0010 (2) and 0100 (4): 3)

This is Code Golf, shortest code wins!

EDIT: After seeing some of the solutions i have removed the requirement of the number representation of the binary being in ascending order. This is my first code golf challenge and i don't want to make it to tedious, also added some clarifications from the comments.

\$\endgroup\$
0

14 Answers 14

6
\$\begingroup\$

Jelly, 23 bytes

,UV€€Ḅr/BḊṖṭ@¹Uz0ZUṖṾ€€

Try It Online!

Follows the input format very strictly (input as string, output as list of strings including padding zeroes).

Jelly, 8 bytes

,UḄr/ḊṖB

Try It Online!

Using actually reasonable I/O formatting, if we accept a list of digits and output as digit lists without doing leading zeroes, this is a lot more suitable for Jelly.

\$\endgroup\$
4
\$\begingroup\$

Vyxal, 12 bytes

ṘB$BrḢƛb?L∆Z

Try it Online! Outputs as a list of lists of bits.

ṘB           # x reversed as an integer
  $B         # x as an integer
    rḢ       # Exclusive range
      ƛ      # Map...
       b     # convert to binary
          ∆Z # Zfill to...
        ?L   # Input length
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Output needs to have leading zeroes \$\endgroup\$
    – pxeger
    Dec 15, 2021 at 20:57
3
\$\begingroup\$

x86-16 machine code, 58 57 bytes

00000000: 8be9 e82a 008b da8b cdfd 4ee8 2100 3bda  ...*......N.!.;.
00000010: 7602 87d3 433b da73 1f8b cd8b c303 f957  v...C;.s.......W
00000020: 4fd1 e850 b000 1430 aa58 e2f5 5feb e533  O..P...0.X.._..3
00000030: d2ac d0e8 13d2 e2f9 c3                   .........

Listing:

8B E9           MOV  BP, CX         ; save input "Binary Pad" length to BP 
E8 002A         CALL BINDEC         ; BX = forward input value 
8B DA           MOV  BX, DX         ; forward number in BX 
8B CD           MOV  CX, BP         ; restore input pad length 
FD              STD                 ; reverse string load/store direction 
4E              DEC  SI             ; fix offset - SI is one char off 
E8 0021         CALL BINDEC         ; DX = reversed input value 
3B DA           CMP  BX, DX         ; is reversed number smaller? 
76 02           JBE  MID_LOOP       ; if not, start looping through middle numbers 
87 D3           XCHG DX, BX         ; otherwise swap numbers 
            MID_LOOP: 
43              INC  BX             ; increment middle number 
3B DA           CMP  BX, DX         ; is less than end number? 
73 1F           JAE  DONE           ; if not, return
8B CD           MOV  CX, BP         ; restore input pad length
8B C3           MOV  AX, BX         ; AX = next number
            DECBIN:                 ; convert value in AX to string at [DI] 
03 F9           ADD  DI, CX         ; move pointer to end of padded output buffer 
57              PUSH DI             ; save position for next number 
4F              DEC  DI             ; fix offset - DI is one char off 
            LOOP_BIT: 
D1 E8           SHR  AX, 1          ; CF = LSb of number 
50              PUSH AX             ; save number
B0 00           MOV  AL, 0          ; zero AL
14 30           ADC  AL, '0'        ; convert CF to ASCII 
AA              STOSB               ; write char to [DI] 
58              POP  AX             ; restore number
E2 F5           LOOP LOOP_BIT       ; loop next bit              
5F              POP  DI             ; restore next number output offset
EB E5           JMP  MID_LOOP       ; keep looping
            BINDEC:                 ; convert ASCII binary string at [SI] to DX 
33 D2           XOR  DX, DX         ; zero output value
            LOOP_DEC: 
AC              LODSB               ; load next char into AL
D0 E8           SHR  AL, 1          ; CF = LSb of ASCII char
13 D2           ADC  DX, DX         ; shift LSb left onto result
E2 F9           LOOP LOOP_DEC       ; loop until end of string
            DONE: 
C3              RET                 ; return to caller 

Callable function, input binary string in [SI] length in CX, output to [DI].

No helpful built-ins in machine code... just brute force string to decimal and back stuff!

Test program output:

enter image description here

\$\endgroup\$
3
\$\begingroup\$

Pip -p, 27 bytes

0X#a-#_._MTB$,[1]+SNFB[Raa]

Replit! Or, here's a 28-byte equivalent in Pip Classic: Try it online!

Could be 17 bytes without the 0-padding requirement.

Explanation

0X#a-#_._MTB$,[1]+SNFB[Raa]
                      [   ]  List containing
                       Ra     Reverse of argument
                         a    and argument
                    FB       Convert each from binary
                  SN         Sort in ascending order
              [1]+           Add 1 to the first (smaller) number in the list
            $,               Create a range from the first (inclusive) to the second (exclusive)
          TB                 Convert each to binary
         M                   Map this function to each:
  #a-#_                       Length of original input minus length of argument
0X                            That many zeros
       ._                     Concatenate with argument
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 88 bytes

x=>(f=y=>y<z[1]?[y,...f(y+1)]:[])(-~(z=["0b"+x,"0b"+[...x].reverse().join``]).sort()[0])
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Um, I think output has to be in binary \$\endgroup\$
    – emanresu A
    Dec 15, 2021 at 20:37
  • \$\begingroup\$ @emanresuA Oh, oops \$\endgroup\$ Dec 15, 2021 at 20:40
2
\$\begingroup\$

Python, 87 bytes

lambda s:[f"{x:b}".zfill(len(s))for x in range(*sorted([int(s,2),int(s[::-1],2)]))][1:]

Attempt This Online!

Can probably be shorter, but I'm not interested in golfing it.

\$\endgroup\$
1
  • \$\begingroup\$ lambda x:[f"{i:0{len(x)}b}"for i in range(*sorted([int(x,2),int(x[::-1],2)]))][1:] for -5 bytes \$\endgroup\$
    – Jakque
    Dec 19, 2021 at 12:01
1
\$\begingroup\$

Charcoal, 26 bytes

⊞υθ⊞υ⮌θ←E…⊕⍘⌊υ²⍘⌈υ²⮌⍘ι²UB0

Try it online! Link is to verbose version of code. Outputs the values in descending order. Explanation:

⊞υθ⊞υ⮌θ

Push the input and its reverse to the predefined empty list.

←E…⊕⍘⌊υ²⍘⌈υ²⮌⍘ι²

Convert the minimum and maximum of the input and its reverse from base 2, loop over the intermediate values, convert them back to base 2, then print them in reverse order and right-justified.

UB0

Fill unprinted areas with zeros.

\$\endgroup\$
1
\$\begingroup\$

Python3, 74 bytes:

lambda x:[f"{i:b}".zfill(len(x))for i in range(int(x,2)+1,int(x[::-1],2))]

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ This does not work with input bigger than its reverse, fixing it makes it identical to the Answer of pxeger, still cool though \$\endgroup\$ Dec 16, 2021 at 12:10
1
\$\begingroup\$

Retina, 62 bytes

$
¶$^$`
O`.+
/(.+)¶\1$/^{-1`¶
¶$%`¶
)-2%T`d`10`01*$
,,-1A`
A`$

Try it online! Link includes test cases. Explanation:

$
¶$^$`

Append the reverse of the input.

O`.+

Sort in ascending order.

/(.+)¶\1$/^{
)`

Repeat until the last two lines are identical.

-1`¶
¶$%`¶

Duplicate the last but one line.

-2%T`d`10`01*$

Increment the newly duplicated line.

,,-1A`

Delete the original input.

A`$

Delete the line that duplicated the last line.

\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Extended), 33 29 27 bytes

(∊¨-⍤≢⍕¨⍤↑¨¯1↓1↓⊢⊤¨⍤…⍥⊥⌽)⍎¨

Try it online!

Explanation (slightly outdated):

⍎¨ Evaluate each character of the input to get a vector of 0's and 1's.
…⍥⊥ Inclusive range, after converting the arguments from base 2: of the the vector and its reverse ⌽⍵.
¯1↓1↓ Make the range exclusive by dropping one value from the start and one from the end.
⊤¨ Convert all values in the exclusive range to base 2.
(-≢⍵)↑¨ Pad the binary values to the length of the input with 0's in the front.
∊¨⍕¨¨ In each binary number, convert each digit to a string, then join into a string.

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 13 bytes

R‚CŸ¨¦bygjð0:

Try it online or verify all test cases.

Explanation:

R             # Reverse the (implicit) input
 ‚            # Pair it with the (implicit) input
  C           # Convert both from a binary-string to an integer
   Ÿ          # Convert this pair to a ranged list
    ¨¦        # Remove the first and last items to make it an exclusive list
      b       # Convert each integer to a binary-string
       Ig     # Push the input-length
         j    # Pad leading spaces to each binary-string to make it that length
          ð0: # Replace every space with a 0
              # (after which the result is output implicitly)
\$\endgroup\$
0
\$\begingroup\$

JavaScript (SpiderMonkey), 90 bytes

x=>{for([x,y]=[1+x,1+[...x].reverse().join``].sort();++x<y;+t&&print(t.slice(3)))t='0b'+x}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 84 bytes

sub{($_,$e,@r)=sort@_,"".reverse@_;push@r,$_ while s/01*$/$&=~y|01|10|r/e&&$_<$e;@r}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Ruby, 78 bytes

->s{a,b=[s.to_i(2),s.reverse.to_i(2)].sort;(a+1...b).map{|x|"%0#{s.size}b"%x}}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.