From range1 to range4

how do I add range2+range 3+range4 more conveniently, I add the series together now one by one.

import Foundation

print("Xcode Swift")

// create some ranges let range1 = 1 ... 10 let range2 = 2 ... 10 let range3 = 3 ... 10 let range4 = 4 ... 10 let range5 = 5 ... 10

// Count the ranges together from range2 to range4 let range6 = range2.count + range3.count + range4.count

// print the number of values

print(range6)

Answered by HulaBalooFI in 743223022

import Foundation

print("Xcode Swift")

// create some ranges let range1 = 1 ... 10 let range2 = 2 ... 10 let range3 = 3 ... 10 let range4 = 4 ... 10 let range5 = 5 ... 10

// Count the ranges together from range2 to range4 let range6 = range2.count + range3.count + range4.count

// print the number of values

print(range6)

Accepted Answer

import Foundation

print("Xcode Swift")

// create some ranges let range1 = 1 ... 10 let range2 = 2 ... 10 let range3 = 3 ... 10 let range4 = 4 ... 10 let range5 = 5 ... 10

// Count the ranges together from range2 to range4 let range6 = range2.count + range3.count + range4.count

// print the number of values

print(range6)

Try this:

let range1 = 1 ... 10
let range2 = 2 ... 10
let range3 = 3 ... 10
let range4 = 4 ... 10
let range5 = 5 ... 10
let ranges = [range1, range2, range3, range4, range5]
let total = ranges
    .map { $0.count }
    .reduce(0, +)
print(total)    // 40

Generally, if you have lots of similar values with numbers in the name, like rangeX, you want to but those values in some sort of collection so you can operate on them as a whole.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I would like calculate range2 - range4 and I would like only specifyi start and end. (range2+range3+range4)

import Foundation

print("Xcode Swift")

// create some ranges let range1 = 1 ... 10 let range2 = 2 ... 10 let range3 = 3 ... 10 let range4 = 4 ... 10 let range5 = 5 ... 10

// Count the ranges together from range2 to range4 let range6 = range2.count + range3.count + range4.count

// print the number of values

print(range6)

import Foundation

print("Xcode Swift")

// create some ranges
let range1 = 1 ... 10
let range2 = 2 ... 10
let range3 = 3 ... 10
let range4 = 4 ... 10
let range5 = 5 ... 10

// Count the ranges together from range2 to range4
let range6 =
      range2.count +
      range3.count +
      range4.count

// print the number of values

print(range6)

Note. It is not a good idea to give names as range1, range2 to ranges and then name an Int as range6.

You should call the last as rangeCount, or rangeCount2to4…


// create some ranges
let Range1 = 1 ... 10
let Range2 = 2 ... 10
let Range3 = 15 ... 20
let Range4 = 40 ... 44
let Range5 = 50 ... 60

// Count the ranges together from range2 to range4
let RangeBetween =
      Range2.count +
      Range3.count +
      Range4.count

// print the number of values

print(RangeBetween)

Thanks, good point, but still I have not been able to solve this. I was also wondering how to get the missing numbers between the sets listed, example 11, 12, 13, 14..

@HulaBalooFI if that's not solved, why did you mark the answer as correct ? No one will bother anymore trying to answer…

.

Count the ranges together from range2 to range4

what result do you expect ?

If you want to join the ranges, use joined():

let range1 = 1 ... 10
let range2 = 2 ... 10
let range3 = 15 ... 20
let range4 = 40 ... 44
let range5 = 50 ... 60

let range6 = [range2, range3, range4].joined()
print(range6.count)

You get 20.

Note: change the var or constants names to start with lowercase.

.

 I was also wondering how to get the missing numbers between the sets listed, example 11, 12, 13, 14..

Please explain, that's really unclear. And do it in a new thread.

From range1 to range4
 
 
Q