Skip to content

Instantly share code, notes, and snippets.

View khpatel4991's full-sized avatar
😄

Kashyap Patel khpatel4991

😄
View GitHub Profile
@khpatel4991
khpatel4991 / max_profit.rb
Created February 17, 2017 15:34
Max Profit Indices from array of stock prices
class Profit
attr_accessor :bp, :sp, :bp_index, :sp_index
def initialize(bp_index=-1, bp=0, sp_index=-1, sp=0)
@bp_index = bp_index || -1
@bp = bp || 0
@sp_index = sp_index || -1
@sp = sp || 0
end
@khpatel4991
khpatel4991 / nested_validation.rb
Created January 4, 2017 23:45
Nested Dry Validations
class AppSchema < Dry::Validation::Schema::Form
configure do |config|
config.type_specs = true
def self.messages
super.merge(
en: { errors: {
discount_pricing: 'are not valid.',
base_package_price: 'You need to have a package_price with quantity 1.',
package_names: 'should be unique.'
} }
@khpatel4991
khpatel4991 / gist:df8d4a8840a6f8eda58534c2c121aa49
Created August 29, 2016 03:29
Add Whitespace based on validity o the words
def repair_whitespace(str)
buff = []
final_words = []
str.each_char do |char|
buff << char
if valid_word?(buff.join)
final_words << buff.join
@khpatel4991
khpatel4991 / island_count.rb
Created August 29, 2016 03:28
Find Number of Islands
require 'set'
# @param {Character[][]} grid
# @return {Integer}
def num_islands(grid)
puts grid.inspect
final_count = 0
grid.length.times do |i|
grid.length.times do |j|