Skip to content

Instantly share code, notes, and snippets.

def self.parse(string)
commands = string.split("\n")
rover = new(*commands.delete_at(0).split(' '))
output = ''
commands.each_slice(2) do |deploy_coords, instructions|
rover.deploy(*deploy_coords.split(' '))
output << rover.process(instructions) + "\n"
end
output
 end
def process(commands)
commands.each_char do |character|
case character
when 'L' then turn_left
when 'R' then turn_right
when 'M' then move_forward
end
end
[@x, @y, @face].join(' ')
case @face
when 'N'
@y += 1
when 'S'
@y -= 1
when 'E'
@x += 1
when 'W'
@x -= 1
end
def face_to(operator, step)
idx = DIRECTIONS.index(@face).method(operator).call(step) % 4
@face = DIRECTIONS[idx]
end
class RoverProblem
DIRECTIONS = %w(N E S W).freeze
attr_reader :face, :x, :y
def initialize(max_x, max_y)
@x = 0
@y = 0
@max_x = max_x.to_i
@max_y = max_y.to_i
end
end
def how_hot(topics, takes = 300)
results = topics.map do |topic|
tweets = @client.search(topic, result_type: 'recent').take(takes)
next [topic, 0, 0] if tweets.empty?
[topic, (tweets.length / (tweets.first.created_at - tweets.last.created_at) * 60), tweets.length]
end
results.sort_by {|result| result[1] }
end
def popularity(string)
tweets = @client.search(string, result_type: 'recent').take(1_000)
tweets.length / (tweets.first.created_at - tweets.last.created_at) * 60
end
client.search("to:justinbieber marry me", result_type: "recent").take(3).each do |tweet|
puts tweet.text
end
SCHEDULER.every '5s' do
stats = Requests::Wordpress.new.stats
send_event(
'page_views',
current: stats['visitors_today'],
last: stats['visitors_yesterday']
)
stars = Requests::Github.new('https://github.com/sparklemotion/nokogiri').stars_count
send_event('stars_count', current: stars)
require_relative 'request'
require 'nokogiri'
module Requests
class Github < Request
def initialize(url)
super(url)
chrome!
end