# Enter your code here. Read input from STDIN. Print output to STDOUT
print "Hello HackerRank!!"
# Enter your code here. Read input from STDIN. Print output to STDOUT
print self
def odd_or_even(number)
number.even?
end
(0...gets.to_i).each do |i|
puts odd_or_even(gets.to_i)
end
a.range? b,c
def scoring(array)
array.each do |user|
user.update_score
end
end
def scoring(array)
array.each do |user|
unless user.is_admin?
user.update_score
end
end
end
loop do
coder.practice
break if coder.oh_one?
end
Execute method coder.practice untl coder.oh_one? becomes true.
coder.practice until coder.oh_one?
def identify_class(obj)
case obj
when Hacker
puts "It's a Hacker!"
when Submission
puts "It's a Submission!"
when TestCase
puts "It's a TestCase!"
when Contest
puts "It's a Contest!"
else
puts "Some other class"
end
end
def element_at(arr, index)
arr[index]
end
def inclusive_range(arr, start_pos, end_pos)
arr[start_pos,end_pos]
end
def non_inclusive_range(arr, start_pos, end_pos)
arr[start_pos...end_pos]
end
def start_and_length(arr, start_pos, length)
arr[start_pos,length]
end
def neg_pos(arr, index)
# return the element of the array at the position `index` from the end of the list
# Clue : arr[-index]
arr[-index]
end
def first_element(arr)
# return the first element of the array
arr.first
end
def last_element(arr)
# return the last element of the array
arr.last
end
def first_n(arr, n)
# return the first n elements of the array
arr.take n
end
def drop_n(arr, n)
# drop the first n elements of the array and return the rest
arr.drop n
end
def end_arr_add(arr, element)
# Add `element` to the end of the Array variable `arr` and return `arr`
arr.push element
end
def begin_arr_add(arr, element)
# Add `element` to the beginning of the Array variable `arr` and return `arr`
arr.unshift element
end
def index_arr_add(arr, index, element)
# Add `element` at position `index` to the Array variable `arr` and return `arr`
arr.insert(index, element)
end
def index_arr_multiple_add(arr, index)
# add any two elements to the arr at the index
arr.insert(index, 1, 2)
end
def end_arr_delete(arr)
# delete the element from the end of the array and return the deleted element
arr.pop
end
def start_arr_delete(arr)
# delete the element at the beginning of the array and return the deleted element
arr.shift
end
def delete_at_arr(arr, index)
# delete the element at the position #index
arr.delate_at(index)
end
def delete_all(arr, val)
# delete all the elements of the array where element = val
arr.delate(val)
end
def select_arr(arr)
# select and return all odd numbers from the Array variable `arr`
arr.select {|a| a.odd?}
end
def reject_arr(arr)
# reject all elements which are divisible by 3
arr.reject {|a| a % 3 == 0}
end
def delete_arr(arr)
# delete all negative elements
arr.delete_if {|a| a <= -1}
end
def keep_arr(arr)
# keep all non negative elements ( >= 0)
arr.keep_if {|a| a >= 0}
end
# Initialize an empty Hash with the variable name empty_hash
empty_hash = Hash.new
#Initialize an empty Hash with the variable name default_hash and the default value of every key set to 1.
default_hash = Hash.new(1)
#Initialize a hash with the variable name hackerrank and having the key-value pairs
hackerrank = {"simmy" => 100, "vivmbbs" => 200}
def iter_hash(hash)
hash.each do |key, value|
puts key
puts value
end
end
# Enter your code here.
#A key-value pair [543121, 100] to the hackerrank object using store
hackerrank.store(543121, 100)
#Retain all key-value pairs where keys are Integers ( clue : is_a? Integer )
hackerrank.keep_if{|a| a.is_a? Integer}
#Delete all key-value pairs where keys are even-valued.
hackerrank.delete_if{|a| a.even?}
## return all collection in array
def iterate_colors(colors)
colors.to_a
end
def rot13(secret_messages)
secret_messages.map {|m| m.tr!("a-z", "n-za-m")}
end
def sum_terms(n)
=begin
->loop 1
val = 1
sum = 0
return value on {sum += (val * val + 1)} = 2
->loop 2
val = 2
sum = 2
return value on {sum += (val * val + 1)} = 7
->loop 3
val = 3
sum = 7
return value on {sum += (val * val + 1)} = 17
...
=end
(1..n).reduce(0) {|sum, val| sum += (val * val + 1)}
end
def func_any(hash)
# Check and return true if any key object within the hash is of the type Integer
# If not found, return false.
hash.any?{|key,value| key.is_a?(Integer) }
end
def func_all(hash)
# Check and return true if all the values within the hash are Integers and are < 10
# If not all values satisfy this, return false.
hash.all? {|key, value| value.is_a?(Integer) && value < 10}
end
def func_none(hash)
# Check and return true if none of the values within the hash are nil
# If any value contains nil, return false.
hash.none?{|key,value| value.nil?}
end
def func_find(hash)
# Check and return the first object that satisfies either of the following properties:
# 1. There is a [key, value] pair where the key and value are both Integers and the value is < 20
# 2. There is a [key, value] pair where the key and value are both Strings and the value starts with `a`.
hash.find do |key,value|
(key.is_a?(Integer) && value.is_a?(Integer) && value < 20) ||
(key.is_a?(String) && value.is_a?(String) && value.start_with?("a"))
end
end