-
-
Save domgetter/45d72196f2fa29ace17e0b17238aa819 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Int | |
def palindrome? | |
self.to_s == self.to_s.reverse | |
end | |
end | |
def nums | |
(100..9999).each do |i| | |
(i..9999).each do |j| | |
yield i*j | |
end | |
end | |
end | |
result = 0 | |
nums do |product| | |
if product > result && product.palindrome? | |
result = product | |
end | |
end | |
puts result | |
nums.enum_for.lazy.select(&.palindrome?).max |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Int | |
def palindrome? | |
self.to_s == self.to_s.reverse | |
end | |
end | |
chan = Channel(Int32 | Iterator::Stop).new(1) | |
spawn do | |
(100..999).each do |i| | |
(i..999).each do |j| | |
chan.send i * j | |
end | |
end | |
chan.send Iterator::Stop::INSTANCE | |
end | |
class Products | |
include Iterator(Int32) | |
def initialize(chan : Channel(Int32 | Iterator::Stop)) | |
@chan = chan | |
end | |
def next | |
@chan.receive | |
end | |
end | |
products = Products.new(chan) | |
puts products.select(&.palindrome?).max |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Int | |
def palindrome? | |
self.to_s == self.to_s.reverse | |
end | |
end | |
result = 0 | |
(100..999).each do |i| | |
(i..999).each do |j| | |
if i*j > result && (i*j).palindrome? | |
result = i*j | |
end | |
end | |
end | |
puts result | |
# What I'd like to write is... | |
nums = Iterator.new do |yielder| | |
(100..999).each do |i| | |
(i..999).each do |j| | |
yielder << i * j | |
end | |
end | |
end | |
nums.select(&.palindrome?).max | |
# As this would separate the work I'm doing from the things I'm doing the work on. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment