You ever run across a problem that you just say, "why?" And then you had to write a quick snippet to solve it?
This was mine as I was doing my near midnight DIC browsing. [Hey ooooo!]
Behold, a solution!
This was mine as I was doing my near midnight DIC browsing. [Hey ooooo!]
Behold, a solution!
str = "aaabbbcbbaaa"
pairs = []
counter = 1
(0..(str.length-1)).each do |i|
if not str[i] == str[i+1]
pairs << [ str[i], counter ]
counter = 1
else
counter = counter + 1
end
end
print pairs
Quote
$ ruby encode.rb
[["a", 3], ["b", 3], ["c", 1], ["b", 2], ["a", 3]]
[["a", 3], ["b", 3], ["c", 1], ["b", 2], ["a", 3]]
2 Comments On This Entry
Page 1 of 1
Lemur
04 May 2013 - 12:49 AM
http://www.ruby-doc....Enumerable.html
Enumerable has a number of methods that work quite well to simplify problems of this and similar natures. Inject is especially amazing.
Enumerable has a number of methods that work quite well to simplify problems of this and similar natures. Inject is especially amazing.
Page 1 of 1
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Tags
My Blog Links
Recent Entries
Recent Comments
Search My Blog
15 user(s) viewing
15 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



2 Comments









|