9 Replies - 2253 Views - Last Post: 14 March 2012 - 04:53 AM

#1 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 919
  • View blog
  • Posts: 6,443
  • Joined: 07-September 06

[Hole 3] JS code golf

Post icon  Posted 23 February 2011 - 11:09 PM

Par 2

Okay, it is time for another hole here at Javascript code golf! We are doing another “the shorter the better” contest and here is what it must do:

Create a small program which; given a range and a number will tell us if the number given is within the given range. The range that the program should take is a mathematical range, where square brackets are inclusive and parentheses are exclusive. For instance, [9, 10) is any number larger than or equal to 9, but strictly less than 10.

Sample inputs:
[0, 100]:9
(1, 1):10
(1, 3]:2



Remember, the point here is to get the shortest code which outputs the correct answer. As with before we aren’t counting any characters that aren’t required to make the program work and you can output it in any manner you see fit.

Is This A Good Question/Topic? 2
  • +

Replies To: [Hole 3] JS code golf

#2 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 334
  • View blog
  • Posts: 1,178
  • Joined: 02-April 09

Re: [Hole 3] JS code golf

Posted 24 February 2011 - 02:26 AM

I think I missed the first couple of these and if you specified any particular test harness or style I fail. I'm assuming you want everything as a function which returns a boolean as you haven't specified a sample output.
Here's a solution, anyway, that's probably not all that efficient but it'll get the ball rolling...

Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 919
  • View blog
  • Posts: 6,443
  • Joined: 07-September 06

Re: [Hole 3] JS code golf

Posted 24 February 2011 - 11:46 AM

Yes, I leave the code golf tasks a little open ended. I want you to decide how it would be best to implement and output to the user. That way you aren't restricted to using something long winded such as document.write which take a lot of characters to accomplish a single output statement. Likewise, the datatype of the output is up to you. It can be a bool, a string, an integer, whatever. At long as a user (who has been told what the output is on success/ failure) can read it and understand what the program is outputting it is good.

Also, with the shortest answer (Par 2) holes you don't need to worry about efficiency, you just want to get the code as short as possible and have it work.
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 919
  • View blog
  • Posts: 6,443
  • Joined: 07-September 06

Re: [Hole 3] JS code golf

Posted 26 February 2011 - 10:27 AM

Here is the solution I came up with. It uses 207 characters and allows for floating point numbers:

Spoiler

Was This Post Helpful? 0
  • +
  • -

#5 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 334
  • View blog
  • Posts: 1,178
  • Joined: 02-April 09

Re: [Hole 3] JS code golf

Posted 26 February 2011 - 11:15 AM

Then I'll revise mine down to 151 characters (minified):
Spoiler


I didn't assume anything other than ints for the same reason I didn't assume I had to trim the strings first and so on, just going on the sample inputs.
Was This Post Helpful? 0
  • +
  • -

#6 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 919
  • View blog
  • Posts: 6,443
  • Joined: 07-September 06

Re: [Hole 3] JS code golf

Posted 26 February 2011 - 11:36 AM

Yeah, that's fine. I understand where you're coming from, it is just nice to have at least someone else participating in these :)

Next week is going to be a much more involved one (for better or for worse) with a lot more room for interpretation and implementation (though it isn't a mini-code hole).
Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3039
  • View blog
  • Posts: 4,548
  • Joined: 08-June 10

Re: [Hole 3] JS code golf

Posted 26 February 2011 - 01:38 PM

Here is a regexp version. 149 chars :)
Spoiler


By the way, moopet, you redefine the global f inside your f function, so it can only be used once.
Also, having fixed that and removing the two extra white spaces and ; chars (which I assume don't count, as per BetaWar's "not required to make the program work" rule) I only count your code at a 148 chars. (... so close :D)
Was This Post Helpful? 0
  • +
  • -

#8 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 334
  • View blog
  • Posts: 1,178
  • Joined: 02-April 09

Re: [Hole 3] JS code golf

Posted 26 February 2011 - 02:42 PM

View PostAtli, on 26 February 2011 - 08:38 PM, said:

By the way, moopet, you redefine the global f inside your f function, so it can only be used once.


Yeah I probably should have done that properly rather than just delete the spaces in my editor :)
Was This Post Helpful? 0
  • +
  • -

#9 gion_13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-March 12

Re: [Hole 3] JS code golf

Posted 14 March 2012 - 03:32 AM

maybe not the shortest (154 chars), but I gave it a try :
function f(s){
    return eval(s.replace(/^(\[|\()(\d+),\s?(\d+)(\]|\)):(/>\d+)$/,'$2'+'<'+eval("'$1'=='['?'=':''")+'$5'+'&&'+'$5'+'<'+eval("('$4'=='['?'=':'')")+'$3'))
}

Was This Post Helpful? 0
  • +
  • -

#10 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: [Hole 3] JS code golf

Posted 14 March 2012 - 04:53 AM

Here is my soln (147 chars) :D :

Spoiler

This post has been edited by chinchang: 14 March 2012 - 04:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1