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?
Post
Replies
Boosts
Views
Activity
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 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....
Example how to find following numbers: 11-13, 23-25 and 31-39?
Example, how to find number 11-13 and 23-25?
// 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..
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)
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)