From 228d9307e10aabfee82dbfe7568446f9f9d9fe68 Mon Sep 17 00:00:00 2001 From: Shoaib19 Date: Thu, 12 Jan 2023 02:01:59 +0500 Subject: [PATCH] 2 more ruby questions --- Ruby/count_consective_numbers.rb | 10 ++++++++++ Ruby/jump_tiles.rb | 11 +++++++++++ 2 files changed, 21 insertions(+) create mode 100644 Ruby/count_consective_numbers.rb create mode 100644 Ruby/jump_tiles.rb diff --git a/Ruby/count_consective_numbers.rb b/Ruby/count_consective_numbers.rb new file mode 100644 index 0000000..4b8dc20 --- /dev/null +++ b/Ruby/count_consective_numbers.rb @@ -0,0 +1,10 @@ +# Name: Shoaib Sabir +# Username: Shoaib019 +# Approach: make hash count as value and number as key then find max value return its key +def count_consective_numbers(arr) + hash_one = {} + arr.each do |val| + hash_one[val] = arr.count(val) + end + hash_one.max_by {|k,v| v}.first +end diff --git a/Ruby/jump_tiles.rb b/Ruby/jump_tiles.rb new file mode 100644 index 0000000..6de7308 --- /dev/null +++ b/Ruby/jump_tiles.rb @@ -0,0 +1,11 @@ +# Name: Shoaib Sabir +# Username: Shoaib019 +# Approach: in loop added only those elements on which pointer value is same +# then compare pointer value with size of array +def jump_tiles(arr) + pointer = 0 + arr.each_with_index do |_,idx| + pointer = pointer + arr[idx] if pointer == idx + end + pointer >= arr.length - 1 ? true : false +end \ No newline at end of file