Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Ruby/Ruby-ArrayIndex_part1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def element_at(arr, index)
return arr[index]
# return the element of the Array variable `arr` at the position `index`
# arr.at(index) # or
# arr[index]
end

def inclusive_range(arr, start_pos, end_pos)
return arr[start_pos..end_pos]
# return the elements of the Array variable `arr` between the start_pos and end_pos (both inclusive)
end

def non_inclusive_range(arr, start_pos, end_pos)
return arr[start_pos...end_pos]
# return the elements of the Array variable `arr`, start_pos inclusive and end_pos exclusive
end

def start_and_length(arr, start_pos, length)
return arr[start_pos,length]
# return `length` elements of the Array variable `arr` starting from `start_pos`
end
27 changes: 27 additions & 0 deletions Ruby/Ruby-ArrayIndex_part2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def neg_pos(arr, index)
return arr[-index]
# return the element of the array at the position `index` from the end of the list
# Clue : arr[-index]
end

def first_element(arr)
return arr.first
# return the first element of the array
# arr.first
end

def last_element(arr)
return arr.last
# return the last element of the array
# arr.last
end

def first_n(arr, n)
return arr.take(n)
# return the first n elements of the array
end

def drop_n(arr, n)
return arr.drop(n)
# drop the first n elements of the array and return the rest
end
19 changes: 19 additions & 0 deletions Ruby/Ruby-Array_addition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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
19 changes: 19 additions & 0 deletions Ruby/Ruby-Array_deletion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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.delete_at(index)
end

def delete_all(arr, val)
# delete all the elements of the array where element = val
arr.delete(val)
end
5 changes: 5 additions & 0 deletions Ruby/Ruby-Array_initialization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Initialize 3 variables here as explained in the problem statement

array=Array.new
array_1=[nil]
array_2=[10,10]
19 changes: 19 additions & 0 deletions Ruby/Ruby-Array_selection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def select_arr(arr)
# select and return all odd numbers from the Array variable `arr`
arr.select{|a| a%2==1}
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<0}
end

def keep_arr(arr)
# keep all non negative elements ( >= 0)
arr.keep_if{|a| a>=0}
end
9 changes: 9 additions & 0 deletions Ruby/Ruby-Control_structure_case.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

def identify_class(obj)
case obj
when Hacker, Submission, TestCase, Contest
puts "It's a #{obj.class}!"
else
puts "It's an unknown model"
end
end
4 changes: 4 additions & 0 deletions Ruby/Ruby-Control_structures_Infiniteloop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
loop do
coder.practice
break if coder.oh_one?
end
3 changes: 3 additions & 0 deletions Ruby/Ruby-Control_structures_Untill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
until coder.oh_one?
coder.practice
end
2 changes: 2 additions & 0 deletions Ruby/Ruby-Everything_is_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Enter your code here. Read input from STDIN. Print output to STDOUT
print self
2 changes: 2 additions & 0 deletions Ruby/Ruby-Introduction_HelloHackerrank.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Enter your code here. Read input from STDIN. Print output to STDOUT
print "Hello HackerRank!!"
10 changes: 10 additions & 0 deletions Ruby/Ruby-Object_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def odd_or_even(number)

number.even?

end

(0...gets.to_i).each do |i|
puts odd_or_even(gets.to_i)
end