TransWikia.com

JFor? More like XNor

Code Golf Asked on October 27, 2021

As it turns out, Python allows for 1j for to be compressed to 1jfor. However, jfor sounds like xnor. Since all similar-phonic phrases have something in common, there must be some property shared between jfor and xnor.

If we look at the ASCII representation of the first two characters of jfor in binary, we see:

j:   1101010
f:   1100110
j&f: 1100010

Notice that the bitwise AND of j and f has a streak of 1s at the beginning, then some 0s, then a single 1.

Definition: A pair of numbers meets the JFor property iff their bitwise AND in binary meets the following regex (excluding leading 0s): /1+0+1+0*/ (1 or more 1s, followed by 1 or more 0s, followed by 1 or more 1s, followed by 0 or more 0s)

Do the ASCII codes for x and n meet the JFor property?

x:   1111000
n:   1101110
x&n: 1101000

Yes! So my hunch was correct; jfor and xnor sound similar, and they share a property (This means, of course, that odor must have that property too).

Task

Given a pair of numbers, determine if they meet the JFor property.

The two numbers may not be distinct, but they will both be integers from 0 to 255 respectively.

Output may follow your language’s conventions for Truthy and Falsey, or you may choose any two consistent, distinct values to represent truthy and falsey respectively.

Your program/function may take input in any reasonable format to represent an ordered pair of integers/bytes.

Test cases

# Truthy:
106 102
110 120
42 26
17 29
228 159
255 253

# Falsey:
85 170
228 67
17 38
255 255
38 120
21 21

(Bounty: 50 rep to the shortest answer on July 24 if it somehow uses a XOR or XNOR operation; please mention if your submission qualifies)

18 Answers

Scala, 46 45 bytes

_.&(_).toBinaryString matches "1+0+1+0*"

Try it online

Answered by user on October 27, 2021

05AB1E, 8 7 bytes (without XOR/XNOR bonus)

&b0«ÔCн

Outputs 1 for truthy and 0/2/4 for falsey (only 1 is truthy in 05AB1E, so this is allowed according to rule "Output may follow your language's conventions for Truthy and Falsey").

Try it online or verify all test cases.

8 bytes with XOR bonus:

&x^2вO4Q

Port of @JonathanAllan's Python 2 answer.

Try it online or verify all test cases.

Explanation:

&        # Bitwise-AND the two (implicit) input-integer together
 b       # Convert it to a binary-string
  0«     # Append a trailing 0 at the end
    Ô    # Connected uniquify it
     C   # Convert it from binary back to an integer
         # (which will result in 0/2/10/42; of which only 10 is a truthy test result)
      н  # Pop and leave just the first digit (0/2/1/4, of which only 1 is 05AB1E truthy)
         # (after which the result is output implicitly)

&        # Bitwise-AND the two (implicit) input-integers together
 x       # Double it (without popping)
  ^      # Bitwise-XOR (a&b) with 2*(a&b)
   2в    # Convert this to a binary-list
     O   # Sum that list to get the amount of set bits
      4Q # And check if it's equal to 4
         # (after which the result is output implicitly)

The last four bytes has a few alternatives, like 5%3@ or ₆ÍÃĀ.

Answered by Kevin Cruijssen on October 27, 2021

APL (Dyalog Extended), 9 bytes

2=≢⊆⍨∧/⊤⎕

Try it online!

How it works

2=≢⊆⍨∧/⊤⎕  ⍝ Full program; input = a vector of two numbers
       ⊤⎕  ⍝ Binary representation of two numbers
     ∧/    ⍝ Bitwise AND
   ⊆⍨      ⍝ Extract chunks of ones
2=≢        ⍝ Test if there are exactly two chunks

Answered by Bubbler on October 27, 2021

JavaScript (V8), 44 bytes

(a,b)=>(a&b).toString(2).match(/^1+0+1+0*$/)

Try it online!

Takes input as two numbers, returns a binary string if they meet the JFor property, otherwise null

(a,b)=>(~(a^b)&(a|b)).toString(2).match(/^1+0+1+0*$/)

Try it online!

The same function, but instead uses an XOR to get the binary string.

Answered by Matthew Jensen on October 27, 2021

R, 37 34 33 bytes

-3 bytes thanks to Dominic van Essen

function(x,y)sum(rle(x&y)$v>0)==2

Try it online!

Takes input as raw bytes, as given by intToBits. In R, this gives a length 32 vector with the least significant bit first, therefore padded with many zeros. Then compute the run lengths, i.e. sequences of consecutive identical elements. The JFor property is verified if there are exactly two runs of 1s.


A (dumb) solution with XOR is:

R, 39 bytes

function(x,y)xor(sum(rle(x&y)$v>0)-2,1)

Try it online!

Answered by Robin Ryder on October 27, 2021

APL+WIN, 24 bytes and uses xnor

Prompts for input as a vector of 2 integers:

4=+/b≠9↑1↓b←∊×/(⊂9⍴2)⊤¨⎕

Try it online! Coutesy of Dyalog Classic

Answered by Graham on October 27, 2021

C# (Visual C# Interactive Compiler), 58 bytes

a=>b=>Regex.Matches(Convert.ToString(a&b,2),"01").Count==1

Try it online!

Answered by Netråm on October 27, 2021

Pip, 11 bytes

1=01NTBaBAb

Try it online! (Verify all test cases)

The main trick is borrowed from xnor's Python answer: the property is satisfied if the binary representation of the bitwise AND contains the sequence 01 exactly once.

1=01NTBaBAb
             a and b are command-line args (implicit)
  01         01 (an integer literal, but treated as a string unless used in a numeric operation)
    N        Count occurrences in:
       aBAb   Bitwise AND of a and b
     TB       Converted to binary
1=           Test whether the number of occurrences equals 1 (0 if not, 1 if so)
             Autoprint (implicit)

Answered by DLosc on October 27, 2021

Io, 60 bytes

-1 byte thanks to @DLosc.

method(x,y,(x&y)asBinary strip("0")occurancesOfSeq("01")==1)

Try it online!

Answered by user92069 on October 27, 2021

Perl 5 +-pl, 35 bytes

$_=unpack(B8,$_&<>)=~/^0*1+0+1+0*$/

Try it online!

Explanation

This accepts characters as input to allow using unpack to get the first 8 chars of the binary representation of the stringwise AND of $_ (which implicitly contains the input line) and <> (which is the following line of input) and checks for the pattern as specified. Prints 1 for JFor pair or the empty string otherwise.


Perl 5 + -pl, 34 bytes

$_=(@a=unpack(B8,$_&<>)=~/1+/g)==2

This uses @Abigail's counting approach, thanks @Dominic van Essen!

Try it online!

Answered by Dom Hastings on October 27, 2021

C (gcc), 53 bytes

f(a,b){for(a&=b,b=0;a;a=~a)for(b++;~a&1;a/=2);a=b^4;}

Try it online!

Returns 0 if the two numbers do have the JFor property, and truthy otherwise.

Answered by att on October 27, 2021

Python, 34 bytes

lambda a,b:bin(a&b).count('01')==1

Try it online!

Although this doesn't use xor, I'm xnor.

Answered by xnor on October 27, 2021

Charcoal, 23 19 bytes

≔&NNθ⁼⁴Σ⍘⁻|⊗θθ&⊗θθ²

Try it online! Link is to verbose version of code. Outputs a Charcoal boolean i.e. - for JFor, nothing if not. Edit: Switched to my version of @JonathanAllan's answer to save 4 bytes. Explanation:

≔&NNθ

Input the two numbers and take their bitwise AND.

⁼⁴Σ⍘⁻|⊗θθ&⊗θθ²

Take the bitwise XOR of twice the number with itself (Charcoal has no XOR operator, so I have to do this longhand) and check that the result (in base 2) has exactly four 1 bits.

Answered by Neil on October 27, 2021

Japt -!, 11 9 bytes

r& ¤ÔèA É

Try it

r& ¤ÔèA É     :Implicit input of integer array
r             :Reduce by
 &            :  Bitwise AND
   ¤          :Convert to binary string
    Ô         :Reverse
     è        :Count the occurrences of
      A       :  10, which gets coerced to a string
        É     :Subtract 1
              :Implicit output of Boolean negation

Answered by Shaggy on October 27, 2021

Python 2,  42  41 bytes

Uses XOR, ^

-1 thanks to Neil (double rather than halve).

lambda a,b:bin(a&b^(a&b)*2).count('1')==4

Try it online!

Answered by Jonathan Allan on October 27, 2021

Python 3, 137 bytes

def f(x,y):
	b=format;z='08b';x=b(x,z);y=b(y,z);a=""
	for i in range(8):a+=str(int(x[i])and int(y[i]))
	return int(a[7])+a.count("10")==2

Try it online!

Answered by Daniel H. on October 27, 2021

Jelly,  9  8 bytes

&BḄƝċ1=1

A dyadic Link accepting which yields 1 if jfor or 0 if not.

Try it online! Or see the test-suite.


8 bytes using XOR

-2 thanks to Neil (double rather than halve).

&Ḥ^$BS⁼4

Try it online!.

Answered by Jonathan Allan on October 27, 2021

perl -Mfeature=bitwise -alp, 41 40 bytes

$_=2==(()=sprintf("%b",$_&$F[1])=~/1+/g)

Try it online!

How does it work?

The program reads lines from STDIN, expecting two numbers on each line. 1 is printed for pairs of numbers with the jfor property, an empty line for pairs without the jfor property.

The -p switch makes that the program loops over each line of the input, making the line available in $_. And the end, it will print whatever is in $_. The -l switch removes the trailing newline. The -a switch makes that the input is split on white space, with the components placed in @F. In particular, the second number will be in $F[1].

$_ & $F [1]

Due to the -Mfeature=bitwise switch, this makes & treat its operands as numbers, and performs a bitwise and on them. This makes that while $_ contains both numbers, only the first number is considered, as that is what Perl does with a string which is used as a number: if the beginning looks like a number, this is taken. (atoi, atof, yada, yada, yada). So, we're doing a bitwise and of the two numbers.

sprintf ("%b", ...)

This returns the result in a binary representation.

() = ... =~ /1+/g

Find all the sequences of consecutive 1s. This is assigned to a list (of 0 variables). We're throwing away the results, but assignment itself does have a return value; for a list assignment, the result is the number of elements on the RHS.

$_ = 2 == (...)

Compares the result (of the list assignment above) to 2. If equal, set $_ to 1, else to the empty string.

Edit: Saved a byte by looking at sequences of 1, instead of a full pattern.

Answered by Abigail on October 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP