Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 2 characters in body
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56
l-> // Consumer<List<String>>
 l.sort( // Use the incorporated sort method which uses a...
 (a,b)->{ // Comparator of Strings
 int r=0, // define r as the result, initiated to 0
 i=58; // i as the codepoint to test for.
 for(;r==0&i-->48;) // for each digit codepoint from '9' to '0',
 // and while no difference was found.
 r= // set r as the difference between
 (b.indexOf(i)>>9)- // was the digit found in b? then 0 else -1 using the bit-shift operator
 (a.indexOf(i)>>9); // and was the digit found in a? then 0 else -1.
 return r; // return the comparison result.
 }
 )
l-> // Consumer<List<String>>
 l.sort( // Use the incorporated sort method which uses a...
 (a,b)->{ // Comparator of Strings
 int r=0, // define r as the result, initiated to 0
 i=58; // i as the codepoint to test for.
 for(;r==0&i-->48;) // for each digit codepoint from '9' to '0',
 // and while no difference was found.
 r= // set r as the difference between
 (b.indexOf(i)>>9)- // was the digit found in b? then 0 else 1 using the bit-shift operator
 (a.indexOf(i)>>9); // and was the digit found in a? then 0 else 1.
 return r; // return the comparison result.
 }
 )
l-> // Consumer<List<String>>
 l.sort( // Use the incorporated sort method which uses a...
 (a,b)->{ // Comparator of Strings
 int r=0, // define r as the result, initiated to 0
 i=58; // i as the codepoint to test for.
 for(;r==0&i-->48;) // for each digit codepoint from '9' to '0',
 // and while no difference was found.
 r= // set r as the difference between
 (b.indexOf(i)>>9)- // was the digit found in b? then 0 else -1 using the bit-shift operator
 (a.indexOf(i)>>9); // and was the digit found in a? then 0 else -1.
 return r; // return the comparison result.
 }
 )
Changed 31 by 9 because a number will never contain more than 7 digits (see requirements).
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56
  • "999".indexOf('9') will return 0. Right-shifted by 31, this is still 0.
  • "000".indexOf('9') will return -1. Right-shifted by 31, this is still -1

I needed a way to map numbers to either 0/1 or 0/-1.

HoweverindexOf has the nice property that it's consistently returning -1 for characters not found.-1 right-shifted by any number is always -1. Any positive number right-shifted by a big enough number will always produce 0.

  • "000999".indexOf('9') will return 3. I needed a way to change it to 0 while still keeping -1 as is. Right-shifting does just that.

So here we are:

input input.indexOf('9') input.indexOf('9')>>9
"999" 0 0
"111119" 5 0
"123456" -1 -1
  • "999".indexOf('9') will return 0. Right-shifted by 31, this is still 0.
  • "000".indexOf('9') will return -1. Right-shifted by 31, this is still -1

However...

  • "000999".indexOf('9') will return 3. I needed a way to change it to 0 while still keeping -1 as is. Right-shifting does just that.

I needed a way to map numbers to either 0/1 or 0/-1.

indexOf has the nice property that it's consistently returning -1 for characters not found.-1 right-shifted by any number is always -1. Any positive number right-shifted by a big enough number will always produce 0.

So here we are:

input input.indexOf('9') input.indexOf('9')>>9
"999" 0 0
"111119" 5 0
"123456" -1 -1
Changed 31 by 9 because a number will never contain more than 7 digits (see requirements).
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56

Java (JDK), 10098 bytes

l->l.sort((a,b)->{int r=0,i=58;for(;r==0&i-->48;)r=(b.indexOf(i)>>31>>9)-(a.indexOf(i)>>31>>9);return r;})

Try it online! Try it online!

Explanation

l-> // Consumer<List<String>>
 l.sort( // Use the incorporated sort method which uses a...
 (a,b)->{ // Comparator of Strings
 int r=0, // define r as the result, initiated to 0
 i=58; // i as the codepoint to test for.
 for(;r==0&i-->48;) // for each digit codepoint from '9' to '0',
 // and while no difference was found.
 r= // set r as the difference between
 (b.indexOf(i)>>31>>9)- // was the digit found in b? then 0 else 1 using the bit-shift operator
 (a.indexOf(i)>>31>>9); // and was the digit found in a? then 0 else 1.
 return r; // return the comparison result.
 }
 )

Note:

  • "999".indexOf('9') will return 0. Right-shifted by 31, this is still 0.
  • "000".indexOf('9') will return -1. Right-shifted by 31, this is still -1

However...

  • "000999".indexOf('9') will return 3. I needed a way to change it to 0 while still keeping -1 as is. Right-shifting does just that.

Java (JDK), 100 bytes

l->l.sort((a,b)->{int r=0,i=58;for(;r==0&i-->48;)r=(b.indexOf(i)>>31)-(a.indexOf(i)>>31);return r;})

Try it online!

Explanation

l-> // Consumer<List<String>>
 l.sort( // Use the incorporated sort method which uses a...
 (a,b)->{ // Comparator of Strings
 int r=0, // define r as the result, initiated to 0
 i=58; // i as the codepoint to test for.
 for(;r==0&i-->48;) // for each digit codepoint from '9' to '0',
 // and while no difference was found.
 r= // set r as the difference between
 (b.indexOf(i)>>31)- // was the digit found in b? then 0 else 1 using the bit-shift operator
 (a.indexOf(i)>>31); // and was the digit found in a? then 0 else 1.
 return r; // return the comparison result.
 }
 )

Java (JDK), 98 bytes

l->l.sort((a,b)->{int r=0,i=58;for(;r==0&i-->48;)r=(b.indexOf(i)>>9)-(a.indexOf(i)>>9);return r;})

Try it online!

Explanation

l-> // Consumer<List<String>>
 l.sort( // Use the incorporated sort method which uses a...
 (a,b)->{ // Comparator of Strings
 int r=0, // define r as the result, initiated to 0
 i=58; // i as the codepoint to test for.
 for(;r==0&i-->48;) // for each digit codepoint from '9' to '0',
 // and while no difference was found.
 r= // set r as the difference between
 (b.indexOf(i)>>9)- // was the digit found in b? then 0 else 1 using the bit-shift operator
 (a.indexOf(i)>>9); // and was the digit found in a? then 0 else 1.
 return r; // return the comparison result.
 }
 )

Note:

  • "999".indexOf('9') will return 0. Right-shifted by 31, this is still 0.
  • "000".indexOf('9') will return -1. Right-shifted by 31, this is still -1

However...

  • "000999".indexOf('9') will return 3. I needed a way to change it to 0 while still keeping -1 as is. Right-shifting does just that.
deleted 6 characters in body
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56
Loading
deleted 6 characters in body
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56
Loading
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56
Loading

AltStyle によって変換されたページ (->オリジナル) /