Open Language Code Golf: RATS!

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 8755 Views - Last Post: 04 October 2012 - 07:15 AM Rate Topic: -----

#1 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Open Language Code Golf: RATS!

Post icon  Posted 21 September 2012 - 03:23 PM

Hey Everybody, it's time for a challenge. This is the first time I've ever attempted to do create a code golf challenge, but I just played with this one and had a lot of fun with it. We Python guys like challenges, so I think you'll like this one: RATS! Rats is an algorithm for finding integer sequences, here's how it works:

take any number as your seed, (I'll choose 59)
reverse the digits of the number (five 9 reversed becomes 95)
add the original and the reversed number together (59+95 = 154)
sort the digits of the value (154 sorted becomes 145)
the sorted value is your output (145)

then we just continue the pattern, the next number would be 668 because 145 + 541 = 686, which then gets sorted to 668. You guys follow? So, here are the rules, create a function that takes 2 parameters: a seed as its first input and the number of values to output as the second input. Then return a list of numbers. The function declaration will not be included in the character count and neither will whitespace! Let's see how small we can make it.

Extra:
Ok guys, you know I love generators, but it pains my heart that I see so few people use them! As an extra bonus, see if you can make a python generator to do the deed! I've already built one and will be submitting it as a snippet.

This post has been edited by atraub: 21 September 2012 - 04:48 PM


Is This A Good Question/Topic? 2
  • +

Replies To: Open Language Code Golf: RATS!

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Open Language Code Golf: RATS!

Posted 21 September 2012 - 04:25 PM

vb.net 148c
Iterator Function Rats(S As Integer, N As Integer) As IEnumerable(Of Integer)
  For i = 1 To N
    Dim o = Integer.Parse(New String((S + Integer.Parse(New String(S.ToString.Reverse.ToArray))).ToString.OrderBy(Function(c) c).ToArray))
    Yield o
    S = o
  Next
End Function


Was This Post Helpful? 1
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Open Language Code Golf: RATS!

Posted 21 September 2012 - 04:37 PM

Didn't think I was going to enter today, but the power of Python overwhelmed me with Pythony things.

I make 58 characters without the function definition or whitespace. This solution requires Python 3 where print is a function. I had to spend a few more characters getting around that Python "bug" with default lists:

Spoiler


Methinks I'll try it with list comprehension next :)

This post has been edited by Simown: 21 September 2012 - 04:38 PM

Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Open Language Code Golf: RATS!

Posted 21 September 2012 - 04:40 PM

I meant to say 2 AND ONLY 2 parameters :-P
Was This Post Helpful? 0
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Open Language Code Golf: RATS!

Posted 21 September 2012 - 04:57 PM

Changing the rules because I was winning? No fair :o

It's a great Pythony thing default parameters, you can't deny that.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Open Language Code Golf: RATS!

Posted 21 September 2012 - 05:44 PM

A nasty 104c variant
  Iterator Function Rats(S As Integer, N As Integer) As IEnumerable(Of Integer)
    For i = 1 To N
      S = Val(CStr(CStr(S + Val( CStr( CStr( S ).Reverse.ToArray) ) ).OrderBy( Function( c ) c).ToArray))
      Yield S
    Next
  End Function


Was This Post Helpful? 1
  • +
  • -

#7 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Open Language Code Golf: RATS!

Posted 21 September 2012 - 07:00 PM

If I'm really not allowed default arguments: here's my revised code, 110 characters - only reassigning functions where they are used more than once:

Spoiler


Edit: Down to 100:

Spoiler

This post has been edited by Simown: 21 September 2012 - 09:19 PM

Was This Post Helpful? 1
  • +
  • -

#8 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,170
  • Joined: 12-May 09

Re: Open Language Code Golf: RATS!

Posted 21 September 2012 - 07:46 PM

It's not readable, but it is short.

First, a function that returns an array containing the sequence elements.
68 characters without function definition:
def rats(s, n)
  o = []
  n.times {
    o << s = (s.to_s.reverse.to_i + s).to_s.chars.sort.join.to_i
  }
  o
end



Second, in the spirit of generators, an enumerable method:
65 characters without function definition:
def rats(s, n)
  n.times {
    yield s = (s.to_s.reverse.to_i + s).to_s.chars.sort.join.to_i
  }
end


To call the enumerable version:
rats(59, 2){|num| puts num}


Was This Post Helpful? 1
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Open Language Code Golf: RATS!

Posted 21 September 2012 - 08:14 PM

I'm glad you guys are getting into it! Here's my generator I submitted, 64 chars: (but it's an infinite set, so only 1 parameter) i.e. it's ineligible
Spoiler


and here's my real submission, 85 chars:
Spoiler

This post has been edited by atraub: 21 September 2012 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Open Language Code Golf: RATS!

Posted 21 September 2012 - 08:24 PM

I'm working on creating a comprehension too, but this is a tough one!!

EDIT
Took it down to 80 chars!
Spoiler

This post has been edited by atraub: 21 September 2012 - 08:53 PM

Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,684
  • Joined: 16-October 07

Re: Open Language Code Golf: RATS!

Posted 22 September 2012 - 04:54 AM

I felt that I need the seed in the list. It wasn't clear if this was a requirement or not. Happily, the solution I chose made it easier to leave it there.
Spoiler


That's 81 chars.

For others playing along, I found it even more amusing to write the counter.
import inspect
def sourceNoSpace(f):
	s = inspect.getsourcelines(f)
	s = ''.join(s.strip() for s in (''.join(s[0][1:])).split())
	return len(s), s



Also, it's tempting to do something like s=str if you see it in more than one usage. Keep in mind you'll need three usages to make it worthwhile:
strstr
s=strss


Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,684
  • Joined: 16-October 07

Re: Open Language Code Golf: RATS!

Posted 22 September 2012 - 05:00 AM

Duh, 78:
Spoiler


Also, I felt the urge to offer a Simown solution: 36
Spoiler

Was This Post Helpful? 2
  • +
  • -

#13 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Open Language Code Golf: RATS!

Posted 22 September 2012 - 01:18 PM

Aha! Lambda, of course, didn't even come to mind :D I accept your solution gracefully.

I must have miscounted when doing s = str, and sure, it could be smaller now.

After using str(x)[::-1] instead of reversed I can see my solution will be very similar to others.

This post has been edited by Simown: 22 September 2012 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Open Language Code Golf: RATS!

Posted 22 September 2012 - 01:24 PM

I'm not such if this even valid syntax, but isn't this possible?
Spoiler

Was This Post Helpful? 1
  • +
  • -

#15 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Open Language Code Golf: RATS!

Posted 22 September 2012 - 01:37 PM

Not valid, no. You could probably make use of short-circuting and/or, but I don't think a solution that way would improve on baavgai's

Edit: Hang on, there's only really one case to consider, so I don't think that'd even provide a solution, hmm.

This post has been edited by Simown: 22 September 2012 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3