How to print number, which missing between ranges?

import Foundation

print("Xcode Swift")

// create some ranges
let Range1 = 1 ... 10
let Range2 = 14 ... 22
let Range3 = 26 ... 30
let Range4 = 40 ... 44
let Range5 = 50 ... 60

print("Range1 SUM:",Range1.count)
print("Range2 SUM:",Range2.count)
print("Range3 SUM:",Range3.count)
print("Range4 SUM:",Range4.count)
print("Range5 SUM:",Range5.count)

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

// print the number of values

print("SUM: From Range2 to Range4:",RangeBetween)

//How to print number, which missing between ranges?

I should find the missing numbers between the series, how is that possible?


Answered by Claude31 in 743562022

@OShv

That did not work. It gives:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 15, 16, 17, 18, 19, 20, 21, 22]

I changed to:

let rangeA = 1...10
let rangeB = 14...22
let mini = min(rangeA.min()!, rangeB.min()!)
let maxi = max(rangeA.max()!, rangeB.max()!)
let allRange = mini...maxi
var missing : [Int] = Array(allRange).sorted().filter() {num in !rangeA.contains(num) && !rangeB.contains(num)}
print(missing)

And get the expected result.

Example, how to find number 11-13 and 23-25?

for numbers in Range1.upperBound...Range2.lowerBound{

    print(numbers)

}

Am I understanding your question correctly?

You can try something like this:

let rangeA = 1...10 let rangeB = 14...22

let numsBetweenRanges = Set(rangeA).symmetricDifference(Set(rangeB)) let missingNums = Array(numsBetweenRanges).sorted().filter { num in !range1.contains(num) || !range2.contains(num) }

print(missingNums) // Output: [11, 12, 13]

Example how to find following numbers: 11-13, 23-25 and 31-39?

Accepted Answer

@OShv

That did not work. It gives:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 15, 16, 17, 18, 19, 20, 21, 22]

I changed to:

let rangeA = 1...10
let rangeB = 14...22
let mini = min(rangeA.min()!, rangeB.min()!)
let maxi = max(rangeA.max()!, rangeB.max()!)
let allRange = mini...maxi
var missing : [Int] = Array(allRange).sorted().filter() {num in !rangeA.contains(num) && !rangeB.contains(num)}
print(missing)

And get the expected result.


print("Xcode Swift: Numbers")

// create some ranges
print (" ")
let Range1 = 1 ... 10
let Range2 = 14 ... 22
let Range3 = 26 ... 30
let Range4 = 40 ... 44
let Range5 = 50 ... 60

print("Range1 SUM:",Range1.count)
print("Range2 SUM:",Range2.count)
print("Range3 SUM:",Range3.count)
print("Range4 SUM:",Range4.count)
print("Range5 SUM:",Range5.count)

let Maxi = Range1.max()!
let Mini = Range2.min()!
let Maxi1 = Maxi+2

if (Maxi1 > Mini){
      print ("No free numbers between Range1 to Range2")
      }
      else {
            print("Free numbers between Range1 to Range2: ",Maxi+1 ... Mini-1)      
      }

//Give answer:
Xcode Swift: Numbers
 
Range1 SUM: 10
Range2 SUM: 9
Range3 SUM: 5
Range4 SUM: 5
Range5 SUM: 11
Free numbers between Range1 to Range2:  11...13

[Done] exited with code=0 in 0.163 seconds

This is how I solved it, maybe not the most efficient way but....

import Foundation

print("Xcode Swift: Numbers")

// create some ranges
print (" ")
let Range1 = 1 ... 10
let Range2 = 14 ... 22
let Range3 = 26 ... 30
let Range4 = 40 ... 44
let Range5 = 50 ... 60

print("Range1 SUM:",Range1.count)
print("Range2 SUM:",Range2.count)
print("Range3 SUM:",Range3.count)
print("Range4 SUM:",Range4.count)
print("Range5 SUM:",Range5.count)

let Maxi = Range1.max()!
let Mini = Range2.min()!
let Maxi1 = Maxi+2

print(" ")
if (Maxi1 > Mini){
      print ("No free numbers between Range1 to Range2")
      }
      else {
            print("Free numbers between Range1 to Range2: ",Maxi+1 ... Mini-1)      
      }

print(" ")
print("All missing numbers between Range1 to Range2:")

let mini = min(Range1.min()!, Range2.min()!)
let maxi = max(Range1.max()!, Range2.max()!)
let allRange = mini...maxi
var missing : [Int] = Array(allRange).sorted().filter() {num in !Range1.contains(num) && !Range2.contains(num)}
print(missing)

Give answer

 
Range1 SUM: 10
Range2 SUM: 9
Range3 SUM: 5
Range4 SUM: 5
Range5 SUM: 11
 
Free numbers between Range1 to Range2:  11...13
 
All missing numbers between Range1 to Range2:
[11, 12, 13]

[Done] exited with code=0 in 0.178 seconds

print("Xcode Swift: Numbers")

// create some ranges
print (" ")
let Range1 = 1 ... 10
let Range2 = 14 ... 22
let Range3 = 26 ... 30
let Range4 = 40 ... 44
let Range5 = 50 ... 60

print("Range1 SUM:",Range1.count)
print("Range2 SUM:",Range2.count)
print("Range3 SUM:",Range3.count)
print("Range4 SUM:",Range4.count)
print("Range5 SUM:",Range5.count)

let Max1 = Range1.max()!
let Max2 = Range2.max()!
let Max3 = Range3.max()!
let Max4 = Range4.max()!

let Min1 = Range2.min()!
let Min2 = Range3.min()!
let Min3 = Range4.min()!
let Min4 = Range5.min()!

let MaxPlus1 = Max1+2
let MaxPlus2 = Max2+2
let MaxPlus3 = Max3+2
let MaxPlus4 = Max4+2

print(" ")
//Should find the way how put this in the loop?
if (MaxPlus1 > Min1){} else {print("Free numbers between: ",Max1+1 ... Min1-1)}
if (MaxPlus2 > Min2){} else {print("Free numbers between: ",Max2+1 ... Min2-1)}
if (MaxPlus3 > Min3){} else {print("Free numbers between: ",Max3+1 ... Min3-1)}
if (MaxPlus4 > Min4){} else {print("Free numbers between: ",Max4+1 ... Min4-1)}

let min1 = min(Range1.min()!, Range2.min()!)
let max1 = max(Range1.max()!, Range2.max()!)

let allRange = min1...max1
var missing : [Int] = Array(allRange).sorted().filter() {num in !Range1.contains(num) && !Range2.contains(num)}
print(missing)

Should find the way how to use the loop?

How to print number, which missing between ranges?
 
 
Q