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
a = [1, 2, 3, 4] | |
(0...arr.length - 1).map{|i| arr.dup.tap{ |a| a[i, 2] = a[i, 2].reverse } } | |
# => [[2, 1, 3, 4, 5], [1, 3, 2, 4, 5], [1, 2, 4, 3, 5], [1, 2, 3, 5, 4]] | |
a = [1, 2, 3] | |
# => [1, 2, 3] | |
(0...a.length - 1).map{|i| a.dup.tap{|a| a[i, 2] = a[i, 2].reverse}} | |
# => [[2, 1, 3], [1, 3, 2]] |
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
# read text from file, map to integer and sum all | |
File.new(file_name, 'r').each_line.map(&:strip).map(&:to_i).inject(:+) |
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
require 'matrix' | |
arr = [ | |
%w(J O I J O), | |
%w(I J O J O), | |
%w(I I J I J) | |
] | |
myarr1 = Array(arr) | |
# [["J", "O", "I", "J", "O"], ["I", "J", "O", "J", "O"], ["I", "I", "J", "I", "J"]] |
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
# http://stackoverflow.com/questions/23382474/is-there-any-way-to-create-2-x-2-array-matrix-from-a-larger-array-matrix | |
require 'matrix' | |
def find_in_matrix(arr,sub) | |
sub_nrows = sub.size | |
sub_ncols = sub.first.size | |
rows = Array(0..(arr.size - sub_nrows)) | |
cols = Array(0..(arr.first.size - sub_ncols)) | |
arr_m = Matrix[*arr] | |
sub_m = Matrix[*sub] |
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
[[0, 0, 3], [0, 1, 4], [1, 0, 5], [1, 1, 6]].select{ |i, j, k| p "#{ i } is #{ j } is #{ k }" } | |
# "0 is 0 is 3" | |
# "0 is 1 is 4" | |
# "1 is 0 is 5" | |
# "1 is 1 is 6" | |
# => [[0, 0, 3], [0, 1, 4], [1, 0, 5], [1, 1, 6]] |
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 | |
OlderNewer