Skip to content

Instantly share code, notes, and snippets.

View dLobatog's full-sized avatar
🤗

Daniel Lobato García dLobatog

🤗
View GitHub Profile
@dLobatog
dLobatog / gist:2866484
Created June 4, 2012 05:12
Project euler 1
(reduce +
(filter (fn[x] (or (zero? (rem x 5))
(zero? (rem x 3))))
(range 0 1000)
)
)
@dLobatog
dLobatog / gist:2873096
Created June 5, 2012 06:25
PRoject euler 2
(defn fib-step [[a b]]
[b (+' a b)])
(defn fib-seq []
(map first (iterate fib-step [0 1]))
)
(defn -main [& args]
(reduce +' (filter even? (take 4000000 (fib-seq)))))
@dLobatog
dLobatog / gist:2876739
Created June 5, 2012 18:28
PRoject euler 2
(defn fib-step [[a b]]
[b (+' a b)])
(defn fib-seq []
(map first (iterate fib-step [0 1]))
)
(defn -main [& args]
(reduce +' (filter even? (take-while (partial > 4e6) (fib-seq)))))
@dLobatog
dLobatog / pixels.rb
Created June 6, 2012 18:48
Print all pixels of an image
require 'RMagick'
if !ARGV[0] || !ARGV[1]
puts "Usage: ruby pixels.rb path-to-image"
exit
end
image = Magick::Image::read(ARGV[0])[0]
image.each_pixel do |pixel, col, row|
@dLobatog
dLobatog / gist:2921455
Created June 13, 2012 02:30
Put sticker
def put_sticker(sticker, x, y)
photo_path = photo.file_path
loot_image = Magick::Image::read(photo_path)[0]
sticker_image = Magick::Image::read(sticker)[0]
loot_image = loot_image.composite(sticker_image, x, y, Magick::AtopCompositeOp)
out = photo_path.sub(/\./, "-sticker.")
loot_image.format = "PNG"
loot_image.write(out)
end
@dLobatog
dLobatog / gist:2921569
Created June 13, 2012 03:04
Test stickers
@image_generator.put_campaign_sticker "public/images/stickers/campaign.png"
new_photo = @image_generator.photo.file_path.sub(/\./, "-sticker.")
expected_photo = File.join(Rails.root, 'spec', 'assets', 'test-result-campaign.png')
new_image = Magick::Image.read(new_photo)[0]
expected_image = Magick::Image.read(expected_photo)[0]
new_image.signature.should eql expected_image.signature
@dLobatog
dLobatog / gist:3050034
Created July 4, 2012 23:18
Recursive functions
recursive_function(argument1) {
return recursive_function(argument1)
}
@dLobatog
dLobatog / gist:3050035
Created July 4, 2012 23:19
Recursive functions with basecase
recursive_function(argument1) {
if (argument1 == 10) { return argument1 }
return recursive_function(argument1 + 1)
}
O
S O
S (S O)
S (S (S O))
S (S (S (S O)))
...
cO
cS cO
cS (cS cO)
cS (cS (cS cO))
...
cS (cS (cS (...)))...