# Open Language Code Golf: RATS!

• (3 Pages)
• 1
• 2
• 3

## 39 Replies - 12208 Views - Last Post: 04 October 2012 - 07:15 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=292760&amp;s=dc2dfb3fa1856c5ed00b3bfb668f2ee0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• Joined: 23-December 08

# Open Language Code Golf: RATS!

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!

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• 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

```

### #3 Simown

• Blue Sprat

Reputation: 322
• 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

### #4 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• 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

### #5 Simown

• Blue Sprat

Reputation: 322
• 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

It's a great Pythony thing default parameters, you can't deny that.

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• 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

```

### #7 Simown

• Blue Sprat

Reputation: 322
• 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

### #8 xclite

• I wrote you an code

Reputation: 1264
• Posts: 4,062
• 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}

```

### #9 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• 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

### #10 atraub

• Pythoneer

Reputation: 828
• Posts: 2,235
• 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

### #11 baavgai

• Dreaming Coder

Reputation: 7119
• Posts: 14,843
• 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

```

### #12 baavgai

• Dreaming Coder

Reputation: 7119
• Posts: 14,843
• 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

### #13 Simown

• Blue Sprat

Reputation: 322
• 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 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

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• 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

### #15 Simown

• Blue Sprat

Reputation: 322
• 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