Skip to content

Instantly share code, notes, and snippets.

def full_name(first, *rest)
array = []
rest.map{|x| array << x }
array2 = []
return error if first == nil
array2 << first
array2.concat(array).join(" ")
end
### array "names" includes all arguments
def full_name(*names)
def evaluate_num(val)
return "RED" if val <= 2
return "BLUE" if val > 3
end
(1..5).group_by {|x|
evaluate_num(x)
}
#=> {"RED"=>[1, 2], "BLUE"=>[3, 4, 5]}
#marks = {"Ramesh":23, "Vivek":40, "Harsh":88, "Mohammad":60}
@daiando
daiando / enumerable_other_sample001.rb
Created December 8, 2015 01:03
ruby>enumerator>any, all, none, find
def func_any(hash)
# Check and return if any key object within the hash is of the type Integer
hash.any? {|key, value| key.is_a? Integer}
end
def func_all(hash)
# Check and return if all the values within the hash are Integers and are < 10
hash.all? {|key, value| value < 10}
end
@daiando
daiando / enumerable_reduce_sample01.rb
Created December 7, 2015 01:13
Consider and arithmetico-geometric sequence where the nth term of the sequence is denoted by t(n)=n**2+1,n>0. In this challenge, your task is to complete the sum method which takes an integer n and returns the sum to the n terms of the series.
def sum_terms(n)
(1..n).reduce(0){|sum, i| sum + i**2 + 1}
end
@daiando
daiando / enumerable_map_rot13.rb
Last active December 7, 2015 01:13
write a method which takes an array of strings (containing secret enemy message bits!) and decodes its elements using ROT13 cipher system; returning an array containing the final messages.
def rot13(secret_messages)
secret_messages.map { |x|
x.tr!("A-Za-z", "N-ZA-Mn-za-m")
}
end
array = []
sample = %w(a b c d e)
sample.each_with_index { |item, index| array[index] = "#{index}:#{item}" }
p array
=> ["0:a","1:b","2:c","3:d","4:e"]
@daiando
daiando / use_enumerable_map.rb
Last active December 1, 2015 00:07
Ruby>enumerable>map>sample001
#You have been provided with a custom object called colors that defines it's own each method.
#You need to iterate over the items and return an Array containing the values.
def iterate_colors(colors)
colors.map{|value| value}
end
@daiando
daiando / recursive_tree.sh
Last active November 23, 2015 01:06
Recursive tree memo
#!/bin/bash
read N
if [ ${N} -gt 5 -o ${N} -lt 1 ]; then
echo "input number must be =< 5 & => 1"
fi
###design
## vertical
for((countY=0;countY<${N};countY++)); do
USE_VERTICAL=$((${USE_VERTICAL} + 2 * $((16 / 2 ** ${countY}))))
done