Skip to main content
Added more test harness
Source Link
cory.todd
  • 201
  • 1
  • 5

Nim (537)(490)

Nim Compiler 0.10.2

I've been looking for a reason to learn nim so here we go.

For code golf, I have leveraged variable parameters and implicit returns. The variable parameters, per the documentation are less stack efficient. Personally, I find the implicit returns harder to read and would probably only use them in trivial procedures.

As for the algorithms, they are simple enough. For all operations except NOT, we compare each bit and manually compare them to our expected truth table. Set each bit as needed along the way in our output variable. In Nim, result is the implicit return value.

I wasn't sure if we were allowed to use built-in OR and AND for asserting two boolean conditions so the notZero procedure was put in their place.

proc s(x, y, b: var int)= x = x div 2 y = y div 2 b *= 2 proc n(x: var int): int = return -(x+1) proc a(x, y: var int): int = var b = 1 while x > 0 and y > 0: if (x mod 2 + y mod 2) == 2: result += b s(x,y,b) proc o(x, y: var int): int = var b = 1 while x + y > 0: if (x mod 2 + y mod 2) >= 1: result += b s(x,y,b) proc r(x, y: var int): int = var b = 1 while x + y > 0: if (x mod 2 + y mod 2) == 1: result += b s(x,y,b) 

Still looking for a better method...

Here is the non-squished version plus full test harness to run on your own machine.
If you just want to run a couple of inputs, here is test case lite.

Nim (537)(490)

Nim Compiler 0.10.2

I've been looking for a reason to learn nim so here we go.

For code golf, I have leveraged variable parameters and implicit returns. The variable parameters, per the documentation are less stack efficient. Personally, I find the implicit returns harder to read and would probably only use them in trivial procedures.

As for the algorithms, they are simple enough. For all operations except NOT, we compare each bit and manually compare them to our expected truth table. Set each bit as needed along the way in our output variable. In Nim, result is the implicit return value.

I wasn't sure if we were allowed to use built-in OR and AND for asserting two boolean conditions so the notZero procedure was put in their place.

proc s(x, y, b: var int)= x = x div 2 y = y div 2 b *= 2 proc n(x: var int): int = return -(x+1) proc a(x, y: var int): int = var b = 1 while x > 0 and y > 0: if (x mod 2 + y mod 2) == 2: result += b s(x,y,b) proc o(x, y: var int): int = var b = 1 while x + y > 0: if (x mod 2 + y mod 2) >= 1: result += b s(x,y,b) proc r(x, y: var int): int = var b = 1 while x + y > 0: if (x mod 2 + y mod 2) == 1: result += b s(x,y,b) 

Still looking for a better method...

Here is the non-squished version plus test harness.

Nim (537)(490)

Nim Compiler 0.10.2

I've been looking for a reason to learn nim so here we go.

For code golf, I have leveraged variable parameters and implicit returns. The variable parameters, per the documentation are less stack efficient. Personally, I find the implicit returns harder to read and would probably only use them in trivial procedures.

As for the algorithms, they are simple enough. For all operations except NOT, we compare each bit and manually compare them to our expected truth table. Set each bit as needed along the way in our output variable. In Nim, result is the implicit return value.

I wasn't sure if we were allowed to use built-in OR and AND for asserting two boolean conditions so the notZero procedure was put in their place.

proc s(x, y, b: var int)= x = x div 2 y = y div 2 b *= 2 proc n(x: var int): int = return -(x+1) proc a(x, y: var int): int = var b = 1 while x > 0 and y > 0: if (x mod 2 + y mod 2) == 2: result += b s(x,y,b) proc o(x, y: var int): int = var b = 1 while x + y > 0: if (x mod 2 + y mod 2) >= 1: result += b s(x,y,b) proc r(x, y: var int): int = var b = 1 while x + y > 0: if (x mod 2 + y mod 2) == 1: result += b s(x,y,b) 

Still looking for a better method...

Here is the non-squished version plus full test harness to run on your own machine.
If you just want to run a couple of inputs, here is test case lite.

code mod
Source Link
cory.todd
  • 201
  • 1
  • 5

Nim (537)(537490)

Nim Compiler 0.10.2

I've been looking for a reason to learn nim so here we go.

For code golf, I have leveraged variable parameters and implicit returns. The variable parameters, per the documentation are less stack efficient. Personally, I personally find the implicit returns harder to read and would probably only use them in trivial procedures.

As for the algorithms, they are simple enough. For all operations except NOT, we compare each bit and manually compare them to our expected truth table. Set each bit as needed along the way in our output variable. In Nim, result is the implicit return value.

I wasn't sure if we were allowed to use built-in OR and AND for asserting two boolean conditions so the notZero procedure was put in their place.

proc shiftys(x, y, b: var int)= x=xx = x div 2 y=yy = y div 2 b=b*2b *= 2 proc LogicalNotn(x: var int):int= int = return -(x+1) * -1 proc LogicalAnda(x, y: var int):int= int = var b = 1 while x > 0 and y > 0: if (x mod 2+y2  + y mod 2)==2 == 2: result += b   shiftys(x,y,b)  return  proc LogicalOro(x, y: var int):int= int = var b=1b = 1 while x>0x or+ y>0y > 0: if (x mod 2+y2 + y mod 2)>=1 >= 1: result+=bresult += b  shiftys(x,y,b) return  proc LogicalXorr(x, y: var int):int= int = var b=1b = 1 while x>0x or+ y>0y > 0: if (x mod 2+y2 + y mod 2)==1 == 1: result+=bresult += b  shiftys(x,y,b) return  

Still looking for a better method...

Here is the non-squished version plus test harness.

Nim (537)

Nim Compiler 0.10.2

I've been looking for a reason to learn nim so here we go.

For code golf, I have leveraged variable parameters and implicit returns. The variable parameters, per the documentation are less stack efficient. I personally find the implicit returns harder to read and would probably only use them in trivial procedures.

As for the algorithms, they are simple enough. For all operations except NOT, we compare each bit and manually compare them to our expected truth table. Set each bit as needed along the way in our output variable. In Nim, result is the implicit return value.

I wasn't sure if we were allowed to use built-in OR and AND for asserting two boolean conditions so the notZero procedure was put in their place.

proc shifty(x,y,b:var int)= x=x div 2 y=y div 2 b=b*2 proc LogicalNot(x: var int):int= return (x+1) * -1 proc LogicalAnd(x, y: var int):int= var b = 1 while x > 0 and y > 0: if(x mod 2+y mod 2)==2: result += b shifty(x,y,b)  return  proc LogicalOr(x,y:var int):int= var b=1 while x>0 or y>0: if(x mod 2+y mod 2)>=1: result+=b shifty(x,y,b) return  proc LogicalXor(x,y:var int):int= var b=1 while x>0 or y>0: if(x mod 2+y mod 2)==1: result+=b shifty(x,y,b) return  

Still looking for a better method...

Here is the non-squished version plus test harness.

Nim (537)(490)

Nim Compiler 0.10.2

I've been looking for a reason to learn nim so here we go.

For code golf, I have leveraged variable parameters and implicit returns. The variable parameters, per the documentation are less stack efficient. Personally, I find the implicit returns harder to read and would probably only use them in trivial procedures.

As for the algorithms, they are simple enough. For all operations except NOT, we compare each bit and manually compare them to our expected truth table. Set each bit as needed along the way in our output variable. In Nim, result is the implicit return value.

I wasn't sure if we were allowed to use built-in OR and AND for asserting two boolean conditions so the notZero procedure was put in their place.

proc s(x, y, b: var int)= x = x div 2 y = y div 2 b *= 2 proc n(x: var int): int = return -(x+1) proc a(x, y: var int): int = var b = 1 while x > 0 and y > 0: if (x mod 2  + y mod 2) == 2: result += b   s(x,y,b) proc o(x, y: var int): int = var b = 1 while x + y > 0: if (x mod 2 + y mod 2) >= 1: result += b  s(x,y,b) proc r(x, y: var int): int = var b = 1 while x + y > 0: if (x mod 2 + y mod 2) == 1: result += b  s(x,y,b) 

Still looking for a better method...

Here is the non-squished version plus test harness.

improved code, added link to gist
Source Link
cory.todd
  • 201
  • 1
  • 5

Nim (681537)

I wasn't sure if we were allowed to use built-in OR and AND for asserting two boolean conditions so the notZero procedure was put in their place.I wasn't sure if we were allowed to use built-in OR and AND for asserting two boolean conditions so the notZero procedure was put in their place.

proc notZeroshifty(x,y: int; both: bool),b: bool = var c int)= 0 if x > 0: c += 1   if y > 0: c += 1 x=x ifdiv both:2   return cy=y ==div 2 else:  return c >= 1 b=b*2 proc LogicalNot(x: var int): int =int= return (x+1) * -1   proc LogicalAnd(x, y: var int): int =int= var b = 1 while notZero(x, > 0 and y, true)> 0: if (x mod 2 + y2+y mod 2) == 2==2: result += b     x = shifty(x div 2 y = ,y div 2 b = ,b * 2) return proc LogicalOr(x, y: var int): int =int= var b = 1b=1 while notZero(x,y,x>0 false)or y>0: if (x mod 2 + y2+y mod 2) >= 1>=1:   result += b result+=b   x = shifty(x div 2 y = ,y div 2 b = ,b * 2) return proc LogicalXor(x, y: var int): int =int= var b = 1b=1 while notZero(x,y,x>0 false)or y>0: if (x mod 2 + y2+y mod 2) == 1==1:   result += b result+=b   x = shifty(x div 2 y = ,y div 2 ,b)  = breturn * 2  return 

I'm sure there isStill looking for a more compactbetter method but I'd like to see if this meets the requirements before I go deeper...

Here is the non-squished version plus test harness.

Nim (681)

I wasn't sure if we were allowed to use built-in OR and AND for asserting two boolean conditions so the notZero procedure was put in their place.

proc notZero(x,y: int; both: bool): bool = var c = 0 if x > 0: c += 1   if y > 0: c += 1  if both:   return c == 2 else:  return c >= 1  proc LogicalNot(x: var int): int = return (x+1) * -1   proc LogicalAnd(x, y: var int): int = var b = 1 while notZero(x,y, true): if (x mod 2 + y mod 2) == 2: result += b     x = x div 2 y = y div 2 b = b * 2 return proc LogicalOr(x, y: var int): int = var b = 1 while notZero(x,y, false): if (x mod 2 + y mod 2) >= 1:   result += b    x = x div 2 y = y div 2 b = b * 2 return proc LogicalXor(x, y: var int): int = var b = 1 while notZero(x,y, false): if (x mod 2 + y mod 2) == 1:   result += b    x = x div 2 y = y div 2 b = b * 2  return 

I'm sure there is a more compact method but I'd like to see if this meets the requirements before I go deeper.

Nim (537)

I wasn't sure if we were allowed to use built-in OR and AND for asserting two boolean conditions so the notZero procedure was put in their place.

proc shifty(x,y,b:var int)= x=x div 2 y=y div 2 b=b*2 proc LogicalNot(x: var int):int= return (x+1) * -1 proc LogicalAnd(x, y: var int):int= var b = 1 while x > 0 and y > 0: if(x mod 2+y mod 2)==2: result += b shifty(x,y,b) return proc LogicalOr(x,y:var int):int= var b=1 while x>0 or y>0: if(x mod 2+y mod 2)>=1: result+=b shifty(x,y,b) return proc LogicalXor(x,y:var int):int= var b=1 while x>0 or y>0: if(x mod 2+y mod 2)==1: result+=b shifty(x,y,b)  return 

Still looking for a better method...

Here is the non-squished version plus test harness.

Source Link
cory.todd
  • 201
  • 1
  • 5
Loading