Mini (Code Golf) Challenge: How Many Days Into Year?

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 20905 Views - Last Post: 19 November 2012 - 11:56 PM

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Mini (Code Golf) Challenge: How Many Days Into Year?

Post icon  Posted 04 October 2012 - 10:14 AM

Mini Challenge: How Many Days Into Year

Challenge:

Create a function the returns how many days into year a user specified date.
Throw exception for an invalid date.

Function DaysInToYear(D As Integer,
                      M As Integer,
                      Y As Integer) As Integer
  Return  ' To be filled in '
End Function



Example Test Cases
01/01/2012 => 1
31/12/2012 => 366
30/12/2011 => 364



Restrictions
Use of the programming language's built-in date methods and functions, is prohibited.

Score
Least number of non whitespace characters, excluding the following allowances.

Allowances
The function definition signature and structure and the required return keyword.
What is provided above excluding the comments.

This post has been edited by AdamSpeight2008: 04 October 2012 - 10:17 AM


Is This A Good Question/Topic? 3
  • +

Replies To: Mini (Code Golf) Challenge: How Many Days Into Year?

#2 Curtis Rutland  Icon User is offline

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


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 10:55 AM

Quick question: do we have to determine if the year is a leap year without using System.DateTime.IsLeapYear?
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 11:04 AM

Is that one of the built-in date functions? :genius:
Was This Post Helpful? 2
  • +
  • -

#4 Curtis Rutland  Icon User is offline

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


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 11:17 AM

Just double checking.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,493
  • Joined: 05-May 12

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 11:24 AM

Here's my entry at a hefty 127 characters.

It also doesn't throw an exception for D being out of range, M being out of range, or negative years. It also doesn't check for special years when the number of days in a month were changed because some Roman emperor or another was feeling feisty, or when the shift from Julian to Gregorian calendar happened for a particular country or the world for that matter.

Spoiler


(Forgive me if I've murdered the VB.NET language since I usually just read VB.NET, not write in it.)
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 11:35 AM

My effort which I'll count later.
Spoiler

This post has been edited by AdamSpeight2008: 04 October 2012 - 12:09 PM
Reason for edit:: Missing New Keyword. So now 186

Was This Post Helpful? 1
  • +
  • -

#7 Curtis Rutland  Icon User is offline

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


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 11:49 AM

My attempt in C#, like Skydiver's it doesn't check for invalids. I'll add that later. (118 chars):

Spoiler


Edit 1: By my count Adam, yours is 183. And damn, we had similar solutions again.

Edit 2: I realized mine is actually incorrect. Stand by.

Fixed, in at 109 chars:

Spoiler


Last edit, changed my methodology slightly to get 102. The code looks more like yours, but I had always intended to try both ways to see which used more characters, a delta or a month list.

Spoiler

This post has been edited by Curtis Rutland: 04 October 2012 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#8 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 12:41 PM

Well, I didn't write this in VB.NET (as I haven't used it in a really long while), but I liked the challenge and I wanted to see what it would look like so I wrote it up in Java.

Spoiler

I have no idea what the spoiler tag is, but since it's in Java I figured it wouldn't be a problem.

This post has been edited by AdamSpeight2008: 04 October 2012 - 01:37 PM
Reason for edit:: Adding in the spoiler tags

Was This Post Helpful? 0
  • +
  • -

#9 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 01:01 PM

My entry. Didn't count number of characters, and check for input date validity.
Spoiler


Edit: Hmmmm... I understood it should have just Return.....
Edit2: Missed the date validity check requirement. Will fix that. Fixed >=28 bug.

This post has been edited by lucky3: 04 October 2012 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 01:50 PM

I counted my original at 256 - (78 of allowances) => 178
Spoiler

Was This Post Helpful? 0
  • +
  • -

#11 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 03:20 PM

Added date validation. It checks for leap year first, so it doesn't throw exception for 29th of February on leap year.
Spoiler

This post has been edited by lucky3: 04 October 2012 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,493
  • Joined: 05-May 12

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 04:18 PM

I've got it down to 46 characters including throwing exceptions for invalid dates:
Spoiler

This post has been edited by Skydiver: 04 October 2012 - 04:18 PM

Was This Post Helpful? 1
  • +
  • -

#13 Curtis Rutland  Icon User is offline

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


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 04:19 PM

Clever, but obviously violating the spirit of the challenge, if not the letter.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,493
  • Joined: 05-May 12

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 06:15 PM

80 Characters with no parameter checks and sticking to the spirit and letter of the restrictions:
Spoiler

Was This Post Helpful? 0
  • +
  • -

#15 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 05 October 2012 - 02:34 AM

If I may point out: the code should check if 29/2/2010 (non leap year) is valid first, and then do the magic.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2