Skip to main content
More cases.
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676

Choosing one of two numbers based on a condition

You already know to use the list selection [x,y][b] with a Boolean b for the ternary expression y if b else x. The variables x, y, and b can also be expressions, though note that both x and y are always evaluated unlike in the ternaryeven when not selected.

Here's some potential optimizations when x and y are numbers.

  • x=0 Use [0,y][b] -> y*b
  • x=1: Use [1,y][b] -> y**b
  • y=1: Use [x,1][b] -> b or x
  • y=x+1 (or y=x-1): Use [x,x+1][b] -> x+b (or  
  • [x,x-1][b] -> x-b)
  • General explicit numbers x,y: Use [1,-1][b] -> 1|-b
  • [x,~x][b] -> x^-b
  • [x,y][b] -> x+z*b or(or y-z*b), where z=y-x.

You can also switch x and y if you can rewrite b to be its negation instead.

Choosing one of two numbers based on a condition

You already know to use the list selection [x,y][b] with a Boolean b for the ternary expression y if b else x. The variables x, y, and b can also be expressions, though note that both x and y are always evaluated unlike in the ternary.

Here's some potential optimizations when x and y are numbers.

  • x=0 Use y*b
  • x=1: Use y**b
  • y=1: Use b or x
  • y=x+1 (or y=x-1): Use x+b (or x-b)
  • General explicit numbers x,y: Use x+z*b or y-z*b where z=y-x.

You can also switch x and y if you can rewrite b to be its negation instead.

Choosing one of two numbers based on a condition

You already know to use the list selection [x,y][b] with a Boolean b for the ternary expression y if b else x. The variables x, y, and b can also be expressions, though note that both x and y are evaluated even when not selected.

Here's some potential optimizations when x and y are numbers.

  • [0,y][b] -> y*b
  • [1,y][b] -> y**b
  • [x,1][b] -> b or x
  • [x,x+1][b] -> x+b 
  • [x,x-1][b] -> x-b
  • [1,-1][b] -> 1|-b
  • [x,~x][b] -> x^-b
  • [x,y][b] -> x+z*b (or y-z*b), where z=y-x.

You can also switch x and y if you can rewrite b to be its negation instead.

replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

Choosing one of two numbers based on a condition

You already knowalready know to use the list selection [x,y][b] with a Boolean b for the ternary expression y if b else x. The variables x, y, and b can also be expressions, though note that both x and y are always evaluated unlike in the ternary.

Here's some potential optimizations when x and y are numbers.

  • x=0 Use y*b
  • x=1: Use y**b
  • y=1: Use b or x
  • y=x+1 (or y=x-1): Use x+b (or x-b)
  • General explicit numbers x,y: Use x+z*b or y-z*b where z=y-x.

You can also switch x and y if you can rewrite b to be its negation instead.

Choosing one of two numbers based on a condition

You already know to use the list selection [x,y][b] with a Boolean b for the ternary expression y if b else x. The variables x, y, and b can also be expressions, though note that both x and y are always evaluated unlike in the ternary.

Here's some potential optimizations when x and y are numbers.

  • x=0 Use y*b
  • x=1: Use y**b
  • y=1: Use b or x
  • y=x+1 (or y=x-1): Use x+b (or x-b)
  • General explicit numbers x,y: Use x+z*b or y-z*b where z=y-x.

You can also switch x and y if you can rewrite b to be its negation instead.

Choosing one of two numbers based on a condition

You already know to use the list selection [x,y][b] with a Boolean b for the ternary expression y if b else x. The variables x, y, and b can also be expressions, though note that both x and y are always evaluated unlike in the ternary.

Here's some potential optimizations when x and y are numbers.

  • x=0 Use y*b
  • x=1: Use y**b
  • y=1: Use b or x
  • y=x+1 (or y=x-1): Use x+b (or x-b)
  • General explicit numbers x,y: Use x+z*b or y-z*b where z=y-x.

You can also switch x and y if you can rewrite b to be its negation instead.

Formatting.
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676

Choosing one of two numbers based on a condition

You already know to use the list selection [x,y][b] with a Boolean b for the ternary expression y if b else x. The variables x, y, and b can also be expressions, though note that both x and y are always evaluated unlike in the ternary.

Here's some potential optimizations when x and y are numbers.

  • x=0 Use y*b
  • x=1: Use y**b
  • y=1: Use b or x
  • y=x+1 (or y=x-1y=x-1): Use x+b (or x-b)
  • General explicit numbers x,y: Use x+z*b or y-z*b where z=y-x.

You can also switch x and y if you can rewrite b to be its negation instead.

Choosing one of two numbers based on a condition

You already know to use the list selection [x,y][b] with a Boolean b for the ternary expression y if b else x. The variables x, y, and b can also be expressions, though note that both x and y are always evaluated unlike in the ternary.

Here's some potential optimizations when x and y are numbers.

  • x=0 Use y*b
  • x=1: Use y**b
  • y=1: Use b or x
  • y=x+1 (or y=x-1): Use x+b (or x-b)
  • General explicit numbers x,y: Use x+z*b or y-z*b where z=y-x.

You can also switch x and y if you can rewrite b to be its negation instead.

Choosing one of two numbers based on a condition

You already know to use the list selection [x,y][b] with a Boolean b for the ternary expression y if b else x. The variables x, y, and b can also be expressions, though note that both x and y are always evaluated unlike in the ternary.

Here's some potential optimizations when x and y are numbers.

  • x=0 Use y*b
  • x=1: Use y**b
  • y=1: Use b or x
  • y=x+1 (or y=x-1): Use x+b (or x-b)
  • General explicit numbers x,y: Use x+z*b or y-z*b where z=y-x.

You can also switch x and y if you can rewrite b to be its negation instead.

Mod Removes Wiki by Doorknob
deleted 5 characters in body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676
Loading
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676
Loading