Skip to main content
Shaved 2 bytes
Source Link
Dendrobium
  • 2.5k
  • 13
  • 26

Javascript ES6, 6462 Bytes

f=s=>Maths=>Math.sign([...s].reduce((p,c,i)=>p+~~c*(i-s.indexOf`^`),0)) 
  • -1 if left is heavier
  • 0 if balanced
  • 1 if right is heavier

Ungolfed:

f=s=>s=> Math.sign( // output sign of weight of lever [...s].reduce((p,c,i)=> // split and reduce input p+~~c*(i-s.indexOf`^`),0)) // add weights of all elements // (elements left of pivot are negatively weighted) 

Test runs (assigning anonymous function to f):

>> f("11 ^9") << 0 >> f("321^ 12") << -1 >> f("9^ 1") << 1 

  • -11 bytes: changed output from R B L to -1 0 1
  • -3 bytes: changed e.split`` to [...e] (thanks @vihan@Vɪʜᴀɴ)
  • -33 bytes: changed algorithm to use negative weights rather than splitting at pivot
  • -9 bytes: removed pivot check (apparently, ~~'^' evaluates to 0...)
  • -2 bytes: made function anonymous (thanks @cᴏɴᴏʀ-obʀɪᴇɴ)

Javascript ES6, 64 Bytes

f=s=>Math.sign([...s].reduce((p,c,i)=>p+~~c*(i-s.indexOf`^`),0)) 
  • -1 if left is heavier
  • 0 if balanced
  • 1 if right is heavier

Ungolfed:

f=s=> Math.sign( // output sign of weight of lever [...s].reduce((p,c,i)=> // split and reduce input p+~~c*(i-s.indexOf`^`),0)) // add weights of all elements // (elements left of pivot are negatively weighted) 

Test runs:

>> f("11 ^9") << 0 >> f("321^ 12") << -1 >> f("9^ 1") << 1 

  • -11 bytes: changed output from R B L to -1 0 1
  • -3 bytes: changed e.split`` to [...e] (thanks @vihan)
  • -33 bytes: changed algorithm to use negative weights rather than splitting at pivot
  • -9 bytes: removed pivot check (apparently, ~~'^' evaluates to 0...)

Javascript ES6, 62 Bytes

s=>Math.sign([...s].reduce((p,c,i)=>p+~~c*(i-s.indexOf`^`),0)) 
  • -1 if left is heavier
  • 0 if balanced
  • 1 if right is heavier

Ungolfed:

s=> Math.sign( // output sign of weight of lever [...s].reduce((p,c,i)=> // split and reduce input p+~~c*(i-s.indexOf`^`),0)) // add weights of all elements // (elements left of pivot are negatively weighted) 

Test runs (assigning anonymous function to f):

>> f("11 ^9") << 0 >> f("321^ 12") << -1 >> f("9^ 1") << 1 

  • -11 bytes: changed output from R B L to -1 0 1
  • -3 bytes: changed e.split`` to [...e] (thanks @Vɪʜᴀɴ)
  • -33 bytes: changed algorithm to use negative weights rather than splitting at pivot
  • -9 bytes: removed pivot check (apparently, ~~'^' evaluates to 0...)
  • -2 bytes: made function anonymous (thanks @cᴏɴᴏʀ-obʀɪᴇɴ)
Shaved 9 bytes
Source Link
Dendrobium
  • 2.5k
  • 13
  • 26

Javascript ES6, 7364 Bytes

f=s=>Math.sign([...s].reduce((p,c,i)=>i==(x=s.indexOf`^`)?p:p+c*=>p+~~c*(i-xs.indexOf`^`),0)) 
  • -1 if left is heavier
  • 0 if balanced
  • 1 if right is heavier

Ungolfed:

f=s=> Math.sign(  // output sign of weight of lever [...s].reduce((p,c,i)=>  // split and reduce input i==(x=s.indexOf`^`)?p:p+c*p+~~c*(i-xs.indexOf`^`),0)) // add weights of all elements   // (elements left of pivot are negatively weighted) 

Test runs:

>> f("11 ^9") << 0 >> f("321^ 12") << -1 >> f("9^ 1") << 1 

  • -11 bytes: changed output from R B L to -1 0 1
  • -3 bytes: changed e.split`` to [...e] (thanks @vihan)
  • -33 bytes: changed algorithm to use negative weights rather than splitting at pivot
  • -9 bytes: removed pivot check (apparently, ~~'^' evaluates to 0...)

Javascript ES6, 73 Bytes

f=s=>Math.sign([...s].reduce((p,c,i)=>i==(x=s.indexOf`^`)?p:p+c*(i-x),0)) 
  • -1 if left is heavier
  • 0 if balanced
  • 1 if right is heavier

Ungolfed:

f=s=> Math.sign(  // output sign of weight of lever [...s].reduce((p,c,i)=>  // split and reduce input i==(x=s.indexOf`^`)?p:p+c*(i-x),0)) // add weights of all elements   // (elements left of pivot are negatively weighted) 

Test runs:

>> f("11 ^9") << 0 >> f("321^ 12") << -1 >> f("9^ 1") << 1 

  • -11 bytes: changed output from R B L to -1 0 1
  • -3 bytes: changed e.split`` to [...e] (thanks @vihan)
  • -33 bytes: changed algorithm to use negative weights rather than splitting at pivot

Javascript ES6, 64 Bytes

f=s=>Math.sign([...s].reduce((p,c,i)=>p+~~c*(i-s.indexOf`^`),0)) 
  • -1 if left is heavier
  • 0 if balanced
  • 1 if right is heavier

Ungolfed:

f=s=> Math.sign( // output sign of weight of lever [...s].reduce((p,c,i)=> // split and reduce input p+~~c*(i-s.indexOf`^`),0)) // add weights of all elements // (elements left of pivot are negatively weighted) 

Test runs:

>> f("11 ^9") << 0 >> f("321^ 12") << -1 >> f("9^ 1") << 1 

  • -11 bytes: changed output from R B L to -1 0 1
  • -3 bytes: changed e.split`` to [...e] (thanks @vihan)
  • -33 bytes: changed algorithm to use negative weights rather than splitting at pivot
  • -9 bytes: removed pivot check (apparently, ~~'^' evaluates to 0...)
Fixed typo
Source Link
Dendrobium
  • 2.5k
  • 13
  • 26

Javascript ES6, 73 Bytes

f=s=>Math.sign([...s].reduce((p,c,i)=>i==(x=s.indexOf`^`)?p:p+c*(i-x),0)) 
  • -1 if rightleft is heavier
  • 0 if balanced
  • 1 if leftright is heavier

Ungolfed:

f=s=> Math.sign( // output sign of weight of lever [...s].reduce((p,c,i)=> // split and reduce input i==(x=s.indexOf`^`)?p:p+c*(i-x),0)) // add weights of all elements // (elements left of pivot are negatively weighted) 

Test runs:

>> f("11 ^9") << 0 >> f("321^ 12") << -1 >> f("9^ 1") << -1 

  • -11 bytes: changed output from R B L to -1 0 1
  • -3 bytes: changed e.split`` to [...e] (thanks @vihan)
  • -33 bytes: changed algorithm to use negative weights rather than splitting at pivot

Javascript ES6, 73 Bytes

f=s=>Math.sign([...s].reduce((p,c,i)=>i==(x=s.indexOf`^`)?p:p+c*(i-x),0)) 
  • -1 if right is heavier
  • 0 if balanced
  • 1 if left is heavier

Ungolfed:

f=s=> Math.sign( // output sign of weight of lever [...s].reduce((p,c,i)=> // split and reduce input i==(x=s.indexOf`^`)?p:p+c*(i-x),0)) // add weights of all elements // (elements left of pivot are negatively weighted) 

Test runs:

>> f("11 ^9") << 0 >> f("321^ 12") << 1 >> f("9^ 1") << -1 

  • -11 bytes: changed output from R B L to -1 0 1
  • -3 bytes: changed e.split`` to [...e] (thanks @vihan)
  • -33 bytes: changed algorithm to use negative weights rather than splitting at pivot

Javascript ES6, 73 Bytes

f=s=>Math.sign([...s].reduce((p,c,i)=>i==(x=s.indexOf`^`)?p:p+c*(i-x),0)) 
  • -1 if left is heavier
  • 0 if balanced
  • 1 if right is heavier

Ungolfed:

f=s=> Math.sign( // output sign of weight of lever [...s].reduce((p,c,i)=> // split and reduce input i==(x=s.indexOf`^`)?p:p+c*(i-x),0)) // add weights of all elements // (elements left of pivot are negatively weighted) 

Test runs:

>> f("11 ^9") << 0 >> f("321^ 12") << -1 >> f("9^ 1") << 1 

  • -11 bytes: changed output from R B L to -1 0 1
  • -3 bytes: changed e.split`` to [...e] (thanks @vihan)
  • -33 bytes: changed algorithm to use negative weights rather than splitting at pivot
Fixed formatting error
Source Link
Dendrobium
  • 2.5k
  • 13
  • 26
Loading
Algorithm change, -33 bytes
Source Link
Dendrobium
  • 2.5k
  • 13
  • 26
Loading
Algorithm change, -31 bytes
Source Link
Dendrobium
  • 2.5k
  • 13
  • 26
Loading
Shaved 11 bytes by changing output format, another 3 by changing split`` to [...]
Source Link
Dendrobium
  • 2.5k
  • 13
  • 26
Loading
Source Link
Dendrobium
  • 2.5k
  • 13
  • 26
Loading