How to resolve the algorithm Range extraction step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Range extraction step by step in the Ruby programming language

Table of Contents

Problem Statement

A format for expressing an ordered list of integers is to use a comma separated list of either Example The list of integers: Is accurately expressed by the range expression: (And vice-versa).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Range extraction step by step in the Ruby programming language

The code receives a list of integers l. The goal of this code is to convert this list into a string where the numbers in the list are grouped into ranges. It will do so by joining the start and end of each range with a comma. For example, if the input list is [0, 1, 2, 4, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39], the output string should be '0-2,4,6-8,11-12,14-15,16-25,27-29,30-33,35-36,37-39'.

It first sorts the list in ascending order and then appends a large value (Float::MAX) to the end of the list. It then initializes two variables: range, which will store the ranges as strings, and canidate_number, which will store the current number being considered for a range.

It then iterates over the sorted list in pairs of current number and next number by index. For each pair of numbers, it checks if there is a gap between the current number and its next by index. If there is a gap, it means that the current range has ended, so it adds the current range to the range array. It then updates the canidate_number to be the next number.

If there is no gap, it means that the current range is still continuing, so it continues to the next pair of numbers.

Once it has iterated over all the numbers in the list, it returns the range array joined by commas.

The second code snippet does the same thing, but it uses the slice_when method to group the numbers into ranges. The slice_when method takes a block that returns true if the current element is the end of a range, and false if it is not. In this case, the block returns true if the current number is not equal to the next number plus one. This means that the slice_when method will group the numbers into ranges of consecutive numbers.

Once the numbers have been grouped into ranges, the code converts each range to a string using the map method. If the range has only one number, it converts it to a string without any hyphens. If the range has more than one number, it converts it to a string with a hyphen between the start and end of the range.

Finally, the code joins the strings together with commas to produce the output string.

Source code in the ruby programming language

def range_extract(l)
  # pad the list with a big value, so that the last loop iteration will
  # append something to the range
  sorted, range = l.sort.concat([Float::MAX]), []
  canidate_number = sorted.first

  # enumerate over the sorted list in pairs of current number and next by index
  sorted.each_cons(2) do |current_number, next_number|
    # if there is a gap between the current element and its next by index
    if current_number.succ < next_number
      # if current element is our first or our next by index
      if canidate_number == current_number
        # put the first element or next by index into our range as a string
        range << canidate_number.to_s
      else
        # if current element is not the same as the first or next
        # add [first or next, first or next equals current add , else -, current]
        seperator = canidate_number.succ == current_number ? "," : "-"
        range << "%d%s%d" % [canidate_number, seperator, current_number]
      end
      # make the first element the next element
      canidate_number = next_number
    end
  end
  range.join(',')
end

lst = [
    0,  1,  2,  4,  6,  7,  8, 11, 12, 14,
   15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
   25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
   37, 38, 39
]

p rng = range_extract(lst)


ary = [0,1,2,4,6,7,8,11,12,14,15,16,17,18,19,20,21,22,23,24,25,27,28,29,30,31,32,33,35,36,37,38,39]
puts ary.sort.slice_when{|i,j| i+1 != j}.map{|a| a.size<3 ? a : "#{a[0]}-#{a[-1]}"}.join(",")


  

You may also check:How to resolve the algorithm Sum and product of an array step by step in the Trith programming language
You may also check:How to resolve the algorithm Singleton step by step in the Julia programming language
You may also check:How to resolve the algorithm Farey sequence step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Ordered words step by step in the Forth programming language
You may also check:How to resolve the algorithm Collections step by step in the Ruby programming language