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
# 1. | |
a = [1, 2, 3] | |
b = [1, 4, 3] | |
a.zip(b).map { |x, y| x == y } # [true, false, true]A | |
# 2. | |
a.zip(b).collect {|x,y| x==y } | |
# => [true, false, true] |
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 number of item which has at least 3 numbers are the same | |
arr1 = [1, 2, 3, 4] | |
arr2 = [[2, 2, 2, 4],[1, 4, 3, 4],[1, 2, 1, 4],[1, 2, 3, 1],[2, 1, 3, 4]] | |
n = 0 | |
arr2.map{|e| e.zip(arr1).collect{| x, y| x==y}}.each do |e| | |
if e.count(true) >=3 | |
n += 1 | |
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 readfile1(file_name) | |
# read a file and returns string "17 10 16 3 7 " | |
file = File.open(file_name) | |
contents = file.read.gsub(/\r\n/, ' ') | |
end | |
def readfile2(file_nam) | |
# same as above | |
File.readlines(file_name).map(&:chomp).join(' ') | |
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
# Ruby: How to find and return a duplicate value in array? | |
ary = ["A", "B", "C", "B", "A"] | |
ary.group_by { |e| e }.select { |k, v| v.size > 1 }.map(&:first) # => ["A", "B"] | |
ary.sort.chunk { |e| e }.select { |e, count| count.size > 1 }.map(&:first) | |
ary.select { |e| ary.count(e) > 1 }.uniq # => ["A", "B"] |
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
# file content | |
# | |
# 4 | |
# bar | |
# abracadabra | |
# bear | |
# bar | |
# baraxbara | |
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
# file content | |
# | |
# 17 | |
# 10 | |
# 16 | |
# 3 | |
# 7 | |
File.readlines(file_name).map(&:chomp).map(&:to_i) | |
# read number to array [17, 10, 16, 3, 7] |
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
$: << File.join(File.dirname(__FILE__), '..', 'lib') | |
require 'pry' if ENV['APP_ENV'] == 'debug' # add `binding.pry` wherever you need to debug | |
require 'fish' | |
def read_file_to_arr(file_name) | |
# ['17, 10, 16 3, 7'] | |
# ["4", "bar", "abracadabra", "bear", "bar", "baraxbara"] | |
File.readlines(file_name).map(&:chomp) | |
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
# Assign array values to different variables | |
a = [99, 97, 89, 99, 97] | |
v1, v2, *v3 = a.join(',').split(',') | |
# => ["99", "97", "89", "99", "97"] | |
v1 | |
# => "99" | |
v2 | |
# => "97" | |
v3 | |
# => ["89", "99", "97"] |
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 insertionsort(num) | |
for j in 1..(num.length - 1) do | |
key = num[j] | |
i = j - 1 | |
while i >= 0 and num[i] > key | |
num[i+1] = num[i] | |
i = i - 1 | |
end | |
num[i+1] = key | |
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
# return max hash key-value pair | |
def largest_hash_key(hash) | |
hash.max_by{|k,v| v} | |
end | |
hash = {"ZZ" => 0, "CA"=>2, "MI"=>1, "NY"=>1} | |
p largest_hash_key(hash) # return ["CA", 2] | |
# return largest hash key pair | |
p hash.max |