import Foundation
/*
This program extracts all integer values from a mixed string
and sorts them using the language's built‑in sorting mechanism.
It demonstrates:
- clear separation of concerns using functions
- efficient number extraction using regular expressions
- strong typing with Swift collections
- fast numeric sorting with sorted()
*/
// ------------------------------------------------------------
// Extract all integer values from a mixed string.
// Uses Swift's modern Regex engine to find digit sequences.
// ------------------------------------------------------------
func extractNumbers(from text: String) -> [Int] {
// Regex that matches one or more digits
let pattern = #/\d+/#
// Find all matches and convert them to integers
return text.matches(of: pattern).compactMap { match in
Int(match.0)
}
}
// ------------------------------------------------------------
// Print all numbers in a space‑separated format.
// ------------------------------------------------------------
func printNumbers(_ numbers: [Int]) {
print(numbers.map(String.init).joined(separator: " "))
}
// ------------------------------------------------------------
// Main
// ------------------------------------------------------------
func main() {
let text = "1000withz7 and3 or 99 give42"
// extract numbers
var numbers = extractNumbers(from: text)
// sort numbers
numbers = numbers.sorted()
// display result
print("Sorted numbers:", terminator: " ")
printNumbers(numbers)
}
main()
/*
run:
Sorted numbers: 3 7 42 99 1000
*/