This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def min_abs_sum(a) | |
n = a.size | |
[1, -1].repeated_permutation(n).map do |s| | |
val(a,s).abs | |
end.min | |
end | |
def val(a,s) | |
a.map.with_index{|e, i| a[i] * s[i]}.inject(:+) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cyclic_rotation(a, k) | |
size = a.size | |
return a if k == size || k == 0 | |
result = a.clone | |
a.each_with_index{|e, i| | |
result[(i + k) % size] = e | |
} | |
result | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Returns the maximum binary gap of a number. | |
# e.g.: max binary gap of 529 (1000010001 in binary) is 4. | |
# | |
def max_binary_gap(n) | |
return 0 if (n < 1) | |
result = n.to_s(2).scan(/10+1/).map{|m| | |
m.gsub('1', '').size | |
}.select{|gap| | |
gap >= 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- see http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html MAPPING | |
WITH timezone_lookup(rails_timezone, pg_timezone) | |
AS ( | |
VALUES | |
('International Date Line West', 'Pacific/Midway'), | |
('Midway Island', 'Pacific/Midway'), | |
('American Samoa', 'Pacific/Pago_Pago'), | |
('Hawaii', 'Pacific/Honolulu'), | |
('Alaska', 'America/Juneau'), | |
('Pacific Time (US & Canada)', 'America/Los_Angeles'), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.17; | |
contract Inbox{ | |
string public message; | |
function Inbox(string initialMessage) public { | |
message = initialMessage; | |
} | |
function setMessage(string newMessage) public { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SPC s c remove highlight | |
**** Files manipulations key bindings | |
Files manipulation commands (start with ~f~): | |
| Key Binding | Description | | |
|-------------+----------------------------------------------------------------| | |
| ~SPC f c~ | copy current file to a different location | | |
| ~SPC f C d~ | convert file from unix to dos encoding | | |
| ~SPC f C u~ | convert file from dos to unix encoding | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# find the shortest knight path | |
# in a game of chess on an infinite board | |
def knight_moves(a, b) | |
ratio = a.to_f / b.to_f | |
unless (((ratio >= 0.5 && ratio <= 2.0) || (ratio <= -0.5 && ratio >= -2.0)) && ((a+b)% 3 == 0)) | |
puts 'unreachable' | |
-1 | |
else | |
(a+b)/3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#unit test | |
#how to run: $ ruby tc_tweetstorm.rb | |
require_relative "tweetstorm" | |
require "minitest/autorun" | |
class TestTweetStorm < Minitest::Test | |
def setup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point | |
attr_reader :x, :y | |
def initialize(x, y) | |
@x = x | |
@y = y | |
end | |
def to_s | |
"(#{@x}, #{@y})" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* WARNING: it won`t work on your browser/development, only works on your phone! */ | |
//sources: | |
// http://stackoverflow.com/questions/27845345/ionic-cordova-camera-not-working | |
/* install ionic and cordova */ | |
$ sudo npm install -g ionic cordova | |
/* create ionic app */ | |
$ ionic start myApp |