diff --git a/Ruby/Ruby-ArrayIndex_part1.rb b/Ruby/Ruby-ArrayIndex_part1.rb new file mode 100644 index 0000000..7d8dbe1 --- /dev/null +++ b/Ruby/Ruby-ArrayIndex_part1.rb @@ -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 \ No newline at end of file diff --git a/Ruby/Ruby-ArrayIndex_part2.rb b/Ruby/Ruby-ArrayIndex_part2.rb new file mode 100644 index 0000000..b0e89cc --- /dev/null +++ b/Ruby/Ruby-ArrayIndex_part2.rb @@ -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 \ No newline at end of file diff --git a/Ruby/Ruby-Array_addition.rb b/Ruby/Ruby-Array_addition.rb new file mode 100644 index 0000000..881cacc --- /dev/null +++ b/Ruby/Ruby-Array_addition.rb @@ -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 \ No newline at end of file diff --git a/Ruby/Ruby-Array_deletion.rb b/Ruby/Ruby-Array_deletion.rb new file mode 100644 index 0000000..a068860 --- /dev/null +++ b/Ruby/Ruby-Array_deletion.rb @@ -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 \ No newline at end of file diff --git a/Ruby/Ruby-Array_initialization.rb b/Ruby/Ruby-Array_initialization.rb new file mode 100644 index 0000000..50a963c --- /dev/null +++ b/Ruby/Ruby-Array_initialization.rb @@ -0,0 +1,5 @@ +# Initialize 3 variables here as explained in the problem statement + +array=Array.new +array_1=[nil] +array_2=[10,10] \ No newline at end of file diff --git a/Ruby/Ruby-Array_selection.rb b/Ruby/Ruby-Array_selection.rb new file mode 100644 index 0000000..7ab2386 --- /dev/null +++ b/Ruby/Ruby-Array_selection.rb @@ -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 \ No newline at end of file diff --git a/Ruby/Ruby-Control_structure_case.rb b/Ruby/Ruby-Control_structure_case.rb new file mode 100644 index 0000000..46b9af0 --- /dev/null +++ b/Ruby/Ruby-Control_structure_case.rb @@ -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 \ No newline at end of file diff --git a/Ruby/Ruby-Control_structures_Infiniteloop.rb b/Ruby/Ruby-Control_structures_Infiniteloop.rb new file mode 100644 index 0000000..f3dad8d --- /dev/null +++ b/Ruby/Ruby-Control_structures_Infiniteloop.rb @@ -0,0 +1,4 @@ +loop do + coder.practice + break if coder.oh_one? +end \ No newline at end of file diff --git a/Ruby/Ruby-Control_structures_Untill.rb b/Ruby/Ruby-Control_structures_Untill.rb new file mode 100644 index 0000000..d3147be --- /dev/null +++ b/Ruby/Ruby-Control_structures_Untill.rb @@ -0,0 +1,3 @@ +until coder.oh_one? + coder.practice +end \ No newline at end of file diff --git a/Ruby/Ruby-Everything_is_object.rb b/Ruby/Ruby-Everything_is_object.rb new file mode 100644 index 0000000..5877fa8 --- /dev/null +++ b/Ruby/Ruby-Everything_is_object.rb @@ -0,0 +1,2 @@ +# Enter your code here. Read input from STDIN. Print output to STDOUT +print self diff --git a/Ruby/Ruby-Introduction_HelloHackerrank.rb b/Ruby/Ruby-Introduction_HelloHackerrank.rb new file mode 100644 index 0000000..f2ed39b --- /dev/null +++ b/Ruby/Ruby-Introduction_HelloHackerrank.rb @@ -0,0 +1,2 @@ +# Enter your code here. Read input from STDIN. Print output to STDOUT +print "Hello HackerRank!!" diff --git a/Ruby/Ruby-Object_methods.rb b/Ruby/Ruby-Object_methods.rb new file mode 100644 index 0000000..6ba83b8 --- /dev/null +++ b/Ruby/Ruby-Object_methods.rb @@ -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 + \ No newline at end of file