C# Challenge: Also Code Golf

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

45 Replies - 8080 Views - Last Post: 28 March 2011 - 07:00 AM

#1 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

C# Challenge: Also Code Golf

Post icon  Posted 27 January 2011 - 01:14 PM

Well, I got this idea from jumptrooper's perl challenge, but to my dismay, I don't know perl. At all. Even a little.

So, instead of pass this game up, I'd like to extend the same challenge to you, but do it in C# (or VB.NET if you're feeling spunky).

You may use any .NET Framework version, and any namespaces (including LINQ) that are packaged with .NET that you like.

Here's the challenge; the first problem from Project Euler:

Quote

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.


Here's the twist: Do this in as little code as possible.

You can post standalone statements (as in, you don't have to post a whole method or program), which we'll test by pasting into a contextual solution. The statement(s) must calculate the value, and store the result in a variable named "result" that is presumed to be pre-defined (as in, you don't need to declare this variable, just assign to it.) It has to be named the same so we all have the same handicap there.

Namespaces are presumed to be defined, but aliases are not. No aliasing some long identifier into something short.

I'll calculate your score by the number of non-space, non-tab, and non-newline characters your code contains. Oh, and the answer it comes back with has to be correct as well. Edit: any character that returns false for char.IsWhiteSpace will be counted.

Here's the code I use to calculate the score. I print the characters as well, to make sure I didn't screw up:

Spoiler


This may be a bit simple of a question for this challenge, but we'll continue to work our way through more difficult solutions to play this game. I believe that as the complexity increases, the variety in solutions will increase as well.

Let's see what you've got!


Scores:
eker676: 49
Curtis Rutland: 62 47, (61 with F#)
Nakor: 75 (42 with Boo)
AdamSpeight2008: 38 with Nemerle
Core: 50
Luckless: 44
Nikhil_07n: 49
scalt: 46

This post has been edited by Curtis Rutland: 24 March 2011 - 11:23 AM


Is This A Good Question/Topic? 3
  • +

Replies To: C# Challenge: Also Code Golf

#2 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 01:32 PM

Spoiler


Scientific Notation to cut one character. Tried to use !(i%3) but the compiler didn't like that.

Don't forget to use my extension method for IsWhiteSpace when you calculate my score:
public static class Extensions
{
  public static bool IsWhiteSpace(this char c)
  {
    return true;
  }
}

This post has been edited by insertAlias: 27 January 2011 - 01:37 PM

Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 01:40 PM

You got the correct answer, and you score:
49

What I do to calculate that is save your code to a text file, read it in, and for count every character that isn't whitespace. So i % 3 is still counted as i%3.

Here was my first go:

Spoiler


It scored:
62

Probably not the best effort. I can do better!

This post has been edited by insertAlias: 27 January 2011 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

#4 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 01:43 PM

I'm way under par.. oh wait par is probably like 10 characters.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1535
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 02:04 PM

Lower score is better, right?
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 02:11 PM

Yep.
Was This Post Helpful? 0
  • +
  • -

#7 Skaggles   User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 255
  • View blog
  • Posts: 641
  • Joined: 01-March 09

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 02:23 PM

If you're allowing the use of VB or C#, what about F#?
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 02:39 PM

Sure, I'd love to see what you come up with.

The best I can do in F# with my limited experience:

Spoiler


That scored:
61

This post has been edited by insertAlias: 27 January 2011 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

#9 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 03:30 PM

Spoiler

This post has been edited by Nakor: 27 January 2011 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 03:39 PM

I modified your code just a tiny bit to match the rule (use a predefined variable named "result"), which actually ends up saving you 2 chars:

Spoiler


Your score is:
75

Very very similar to my first go. I used the LINQ extension methods instead of the free format syntax though, it saved me a few characters, since I didn't have to Select at all.
Was This Post Helpful? 0
  • +
  • -

#11 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 03:51 PM

View PostinsertAlias, on 27 January 2011 - 04:39 PM, said:

I modified your code just a tiny bit to match the rule (use a predefined variable named "result"), which actually ends up saving you 2 chars:

Spoiler


Your score is:
75

Very very similar to my first go. I used the LINQ extension methods instead of the free format syntax though, it saved me a few characters, since I didn't have to Select at all.


yep, saw yours after i posted mine, did a slight facepalm for not thinking to use that method, ah well, at least it demonstrated an alternative means to accomplish the same end
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 04:04 PM

I guess I'm ready to post my second go:

Spoiler


Scores:
47
Was This Post Helpful? 1
  • +
  • -

#13 scalt   User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 344
  • Joined: 22-November 07

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 08:45 PM

@insertAlias - sorry mate, got 1 better :P

Spoiler

Was This Post Helpful? 1
  • +
  • -

#14 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 08:49 PM

I have no idea how to submit this, but here's my go. I thought about it slightly differently:

Spoiler


Why scan every number when I know the ones I need? (1000/5) =)

EDITED: Added spoiler tags

This post has been edited by Luckless: 27 January 2011 - 08:53 PM

Was This Post Helpful? 0
  • +
  • -

#15 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: C# Challenge: Also Code Golf

Posted 27 January 2011 - 08:55 PM

mine is soo wrong, lol. I need to think about it more. I'll be back ;)
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »