How to Create a function that returns the total number of boomerangs in an array

aaaaaaa

A boomerang is a V-shaped sequence that is either upright or upside down. Specifically, a boomerang can be defined as: sub-array of length 3, with the first and last digits being the same and the middle digit being different. Some boomerang examples:

[3, 7, 3], [1, -1, 1], [5, 6, 5]

Create a function that returns the total number of boomerangs in an array. To illustrate:

[3, 7, 3, 2, 1, 5, 1, 2, 2, -2, 2]
# 3 boomerangs in this sequence:  [3, 7, 3], [1, 5, 1], [2, -2, 2]

Be aware that boomerangs can overlap, like so:

[1, 7, 1, 7, 1, 7, 1]
# 5 boomerangs (from left to right): [1, 7, 1], [7, 1, 7], [1, 7, 1], [7, 1, 7], and [1, 7, 1]

Examples:

count_boomerangs([9, 5, 9, 5, 1, 1, 1]) ➞ 2
count_boomerangs([5, 6, 6, 7, 6, 3, 9]) ➞ 1
count_boomerangs([4, 4, 4, 9, 9, 9, 9]) ➞ 0

Note: [5, 5, 5] (triple identical digits) is NOT considered a boomerang because the middle digit is identical to the first and last.

benjessop

Some verbose way to do this:

class Array
  def same_values?
    self.uniq.length == 1
  end
end

def find_boomerangs(arr)
  split_amount = 3 # count of a possible boomerang
  scan_amount = split_amount - 1 # scanning by index for array
  new_arr = []
  arr.each_with_index { |_, indx| # we only care for the indx
    end_of_indx = indx + scan_amount
    arry = arr[indx .. end_of_indx] # collect new possible boomerang from array
    next unless arry.count == split_amount # only check if possible boomerang is length of three

    new_arr << arry if arry.values_at(0, -1).same_values? && !arry.values_at(0, 1).same_values? # checks that the values at the start and end are the same and that the middle value is not the same as the first
  }
  new_arr # holds the boomerangs | call .count if you want the count
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to create a function that returns a value if id is in an array?

How to create a function that returns the number of nodes in a tree on a given level

How can I create a function that returns Product of Digit Of Sum of a number

How to create a function that returns an index of the first repeated value in an array?

String match function returns an array, but if converted to number, returns the matched number

How to summarize total number in an array object

How to render total number of objects in array react

How to create function that returns nothing

A function that returns the number of even numbers of an array

Create a function called square that takes in a number and returns the square of that number

Create a function that takes a number num and returns each place value in the number

Create a range or array of integers up to a total int number

JavaScript: Create a function that returns the frequency distribution of an array

Cython: create C function that returns an array

C++: Create function that returns array size

How to delete an array that a function returns

How to create function for array

JavaScript How to Create a Function that returns a string with number of times a characters shows up in a string

JavaScript: How to create a function that receives an array of numbers and returns an array containing only the positive numbers?

How to create a method that returns the nth prime number?

Total unique number in array

How to write a function that accepts a list as an argument and returns the total of the values in the list?

How to make a function that returns the positional value of a number?

Create array with for and sum total

How to create a new column with total number of elements from a column of lists

How to Create a function that takes two numbers as arguments (num, length) and returns an array of multiples of num up to length?

How can I create a function that iterates through a multidimentional array, identifies which array of numbers is even, and returns the even array?

Need to create function that returns a boolean when asked, "Is this a prime number?"

Create a function which takes in a float as input and returns a string containing a number