Skip to content

Instantly share code, notes, and snippets.

@karuna
Last active August 26, 2016 09:57
Show Gist options
  • Save karuna/29e9cb0d4b4122ea3d78efe121d57dcb to your computer and use it in GitHub Desktop.
Save karuna/29e9cb0d4b4122ea3d78efe121d57dcb to your computer and use it in GitHub Desktop.
training
#!/bin/ruby
t = gets.strip.to_i
k = 2Math.log2(t/3+1).floor
puts 3*(2**k) - (k*3)
1 = 3 = 2 ** 0 * 3 - (k * 3)
2 = 2 = 2 ** 0 * 3 - 1
3 = 1 = 2 ** 0 * 3 - 2
4 = 6 = 2 ** 1 * 3 - 0
5 = 5 = 2 ** 1 * 3 - 1
6 = 4 = 2 ** 1 * 3 - 2
7 = 3 = 2 ** 1 * 3 - 3
8 = 2 = 2 ** 1 * 3 - 4
9 = 1 = 2 ** 1 * 3 - 5
10 = 12 = 2 ** 2 * 3 - 0
11 = 11 = 2 ** 2 * 3 - 1
12 = 10 = 2 ** 2 * 3 - 2
#!/bin/ruby
n = gets.strip.to_i
grid = Array.new(n)
for grid_i in (0..n-1)
grid[grid_i] = gets.strip.split('').map(&:to_i)
end
convenient = [1,0,-1]
puts grid[0].join('') # add first line
(1..(n-2)).each do |nth|
this_line = grid[nth]
new_line = ""
new_line << grid[nth][0].to_s # add first item on nth line
# process each item inside nth line
(1..(n-2)).each do |mnth|
item = 'X'
convenient.each do |vpos|
convenient.each do |hpos|
item = grid[nth][mnth].to_s if grid[vpos][hpos] > grid[nth][mnth]
end
end
new_line << item
end
new_line << grid[nth][n-1].to_s # add last item on nth line
puts new_line
end
puts grid[n-1].join('')
# Enter your code here. Read input from STDIN. Print output to STDOUT
def arr_to_hash(arr)
result = Hash.new(0)
arr.each do |item|
result[item] += 1
end
result
end
def get_array(num)
gets.strip.downcase.split(/\s+/)
end
num1 = gets.strip.to_i
arr1 = get_array(num1)
num2 = gets.strip.to_i
arr2 = get_array(num2)
has1 = arr_to_hash(arr1)
has2 = arr_to_hash(arr2)
result = has1.merge(has2)
result.collect {|k,v|k}.each do |key|
if has1[key] == has2[key]
result.delete(key)
else
result[key] = (has1[key] - has2[key]).abs
end
end
ar_result = []
result.each do |k,v|
v.times do
ar_result.push(k)
end
end
puts ar_result.join(' ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment