Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

Replacing consecutive numbers with dash between first and last in a range

# Replacing consecutive numbers with dash between first and last in a range
def range_extraction(list)
  list.chunk_while {|i, j| i+1 == j }.map do |a|
    if a.size > 2
      a.first.to_s + "-" + a.last.to_s
    else
      a  
    end
  end.join(',')
end

print range_extraction([-3,-2,-1,2,10,15,16,18,19,20])  # "-3--1,2,10,15,16,18-20"
Comment

PREVIOUS NEXT
Code Example
Ruby :: how do i fix FATAL: password authentication failed for user "root" 
Ruby :: start times from 1 in ruby 
Ruby :: ruby zip rows and columns 
Ruby :: devise trackable not working for authentication 
Ruby :: common functions in rails 
Ruby :: rails generate controller without view ,test 
Ruby :: expect method call inside rescue rspec 
Ruby :: how to overwrite last line of console in ubuntu rails 
Ruby :: rails update column without callbacks 
Ruby :: ruby on rails db column contains string 
Ruby :: ruby string interpolation lazy evaluation 
Ruby :: how to run one line pry 
Ruby :: ruby array join 
R :: r sort character number 
R :: r type of all columns 
R :: i have library(dplyr) but i still get Error in select(., 
R :: multiple intersect r 
R :: delete rows by rowname in R 
R :: r language comment 
R :: how to convert matrix to numeric in r 
R :: write text r 
R :: remove first and last character from string R 
R :: check R package 
R :: r dot product 
R :: r for loop 
R :: rstudio github upload 
R :: not displaying prints and on.exit in r 
R :: cut function R 
R :: créer un dataframe dans r 
R :: digits in format and formatc in r 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =