How to resolve the algorithm Range expansion step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Range expansion step by step in the Wren 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).
Expand the range description: Note that the second element above, is the range from minus 3 to minus 1.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Range expansion step by step in the Wren programming language
Source code in the wren programming language
var expandRange = Fn.new { |s|
var list = []
var items = s.split(",")
for (item in items) {
var count = item.count { |c| c == "-" }
if (count == 0 || (count == 1 && item[0] == "-")) {
list.add(Num.fromString(item))
} else {
var items2 = item.split("-")
var first
var last
if (count == 1) {
first = Num.fromString(items2[0])
last = Num.fromString(items2[1])
} else if (count == 2) {
first = Num.fromString(items2[1]) * -1
last = Num.fromString(items2[2])
} else {
first = Num.fromString(items2[1]) * -1
last = Num.fromString(items2[3]) * -1
}
for (i in first..last) list.add(i)
}
}
return list
}
var s = "-6,-3--1,3-5,7-11,14,15,17-20"
System.print(expandRange.call(s))
You may also check:How to resolve the algorithm Partition an integer x into n primes step by step in the VBScript programming language
You may also check:How to resolve the algorithm Function composition step by step in the FunL programming language
You may also check:How to resolve the algorithm Pascal matrix generation step by step in the PL/I programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Ruby programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the Rust programming language