Skip to content

Instantly share code, notes, and snippets.

@stamaniorec
stamaniorec / solution.rb
Created June 9, 2015 15:48
VMWare Garbage Wars 2015: Team NecroFiles
require 'net/http'
#$IP = '172.16.180.128'
# $IP = '192.168.125.129'
$IP = '172.16.18.230'
$PORT = '8080'
$OBJECTS_API = 'api/sector/%d/objects'
$ROOTS_API = 'api/sector/%d/roots'
def get_request link
@stamaniorec
stamaniorec / brackets.rb
Last active October 31, 2015 11:56
#algo #ruby
# You are given a template for an expression with parantheses.
# For every template, print all possible valid parantheses expressions.
def solution template, index, balance
if balance < 0
return
end
# handles ? as last character in template
if index >= template.length
@stamaniorec
stamaniorec / bunny.rb
Last active October 31, 2015 11:56
#algo #ruby
# Gosho is a bunny. He lives in a forest full of hills.
# Every hill has a different height and has a different amount of grass on it.
# He likes jumping very much, but he only jumps in zig-zag manner.
# I.e., he only jumps on a lower hill, then on an upper hill, then on a lower one again,
# or he jumps on an upper hill, then on a lower one, then on an upper one again, and so on.
# Gosho likes eating. He likes eating even more than he likes jumping.
# You like Gosho and you want to help him.
# On the first line of input you get the number N - the number of hills in the vicinity of Gosho.
# On the next N lines, you get the pair P,Q,
@stamaniorec
stamaniorec / kseq.rb
Last active October 31, 2015 11:56
#algo #ruby
# Remove every subsequence of K elements with the same value.
# 1 1 1 2 3 3 4 5
# 2
# Output: 1 2 4 5
sequence = $stdin.readline.chomp.split.map { |i| i.to_i }
k = $stdin.gets.chomp.to_i
last_digit = sequence[0]
count = 1
# You are driving a car.
# You start moving with a velocity of V1 meters per second.
# Every second you can change your velocity with D meters per second.
# After T seconds, you want to be driving with a velocity of V2.
# What is the maximum distance you can travel during those T seconds?
# 5
# 6
# 4
# 2
@stamaniorec
stamaniorec / red_john_is_back.rb
Last active October 31, 2015 11:57
#algo #ruby
# 4xN wall
# Infinite supply of 1x4 and 4x1 bricks
# M - total number of ways in which the bricks can be arranged on the wall
# P - answer - number of prime numbers up to M
# Input
# T - integer
# T lines of one integer
# Output
@stamaniorec
stamaniorec / angle_clock_hands.py
Last active October 31, 2015 11:57
#algo #python
# Given a time, calculate the angle between the hour and minute hands on a clock.
hour = 3
minutes = 27
def solution(hour, minutes):
minute_percentage = float(minutes) / 60
minute_degrees = minute_percentage * 360
hour_degrees = float(hour)/12 * 360
next_hour = (hour + 1) % 12
# You are given a sorted array of integers.
# Every element appears twice consequently, except for one.
# Find the non-repeating element in O(log n).
def f(list, start, end):
mid = (start+end) / 2
# Two elements left
if end-start <= 1:
prev = list[start-1]
SELECT
COUNT(*)
FROM
(
SELECT
clubs.club_name, kids.name
FROM
clubs
INNER JOIN
kids_and_clubs
@stamaniorec
stamaniorec / people_in_same_room.sql
Last active October 31, 2015 11:57
#sql #mosko
-- Were Ivan and Maria in the same room of the same hotel at the same time?
SELECT g1, g2, h1, r1
FROM
(
SELECT guests.name g1, room r1, hotels.name h1
FROM visits, guests, hotels
WHERE visits.guest_id = guests.guest_id AND visits.hotel_id = hotels.hotel_id
AND guests.name = 'Ivan'
) t1,