Skip to content

Instantly share code, notes, and snippets.

@fawkeswei
Created March 24, 2012 10:38
Show Gist options
  • Save fawkeswei/2180961 to your computer and use it in GitHub Desktop.
Save fawkeswei/2180961 to your computer and use it in GitHub Desktop.
Programming Problem from Cardinal Blue
#############################
# Problem 1: Ruby
#
"i love to code"­.split(" ").ma­p{|x| x.rev­erse}.join­(" ")
#############################
# Problem 2: Same Digits
#
# Ans: 142857
# start the search from 1
$num = 1;
# indicates if this is the number we want
$flag = false;
# the search will stop when a number pass all tests
until $flag == true do
for i in (2..6)
# test if both numbers contain exactly the same digits
$flag = (($num.to_s.split('').sort <=> (i*$num).to_s.split('').sort) == 0);
if $flag == false
# this is not the number we are seaching for, move on to the next one
$num += 1;
break;
end
end
end
# output the magic number we found => 142857
puts $num;
#############################
# Problem 3: Pinterest Screen Scraping
#
# Note: This script depends on Nokogiri(http://nokogiri.org/) to perform screen scraping
require "open-uri"
require "nokogiri"
target_url = "http://pinterest.com/piccollage/star-struck/";
result_arr = [];
# get a nokogiri HTML document for the target_url
doc = Nokogiri::HTML(open(target_url));
# get the urls of all target images
result_arr = doc.xpath('//img[@class=\'PinImageImg\']/@src')
# output the results
puts result_arr;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment