diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-16 03:09:29 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-16 03:09:29 +0100 |
| commit | 475a4a2510e573d5f5f1b0b5f82601882193b555 (patch) | |
| tree | 0186dd0d787599a54aae469d8e95bf7e8bca240a | |
| parent | 677ef805ed27e4a322738d68f436666ba1bc626c (diff) | |
| download | perlweeklychallenge-club-475a4a2510e573d5f5f1b0b5f82601882193b555.tar.gz perlweeklychallenge-club-475a4a2510e573d5f5f1b0b5f82601882193b555.tar.bz2 perlweeklychallenge-club-475a4a2510e573d5f5f1b0b5f82601882193b555.zip | |
- Added Swift solution to "Left Rotation" task.
| -rw-r--r--[-rwxr-xr-x] | challenge-078/mohammad-anwar/swift/ch-1.swift | 0 | ||||
| -rw-r--r-- | challenge-078/mohammad-anwar/swift/ch-2.swift | 97 |
2 files changed, 97 insertions, 0 deletions
diff --git a/challenge-078/mohammad-anwar/swift/ch-1.swift b/challenge-078/mohammad-anwar/swift/ch-1.swift index 31e817c050..31e817c050 100755..100644 --- a/challenge-078/mohammad-anwar/swift/ch-1.swift +++ b/challenge-078/mohammad-anwar/swift/ch-1.swift diff --git a/challenge-078/mohammad-anwar/swift/ch-2.swift b/challenge-078/mohammad-anwar/swift/ch-2.swift new file mode 100644 index 0000000000..29ab3ac62f --- /dev/null +++ b/challenge-078/mohammad-anwar/swift/ch-2.swift @@ -0,0 +1,97 @@ +import Foundation + +/* + +Perl Weekly Challenge - 078 + +Task #2: Left Rotation + +https://perlweeklychallenge.org/blog/perl-weekly-challenge-078 + +*/ + +enum ParamError: Error { + case missingSource + case invalidSource + case missingIndex + case invalidIndex +} + +do { + let paramCount:Int = Int(CommandLine.argc) + + if paramCount <= 1 { + throw ParamError.missingSource + } + if paramCount <= 2 { + throw ParamError.missingIndex + } + + let source:String = CommandLine.arguments[1] + let index:String = CommandLine.arguments[2] + if isValidList(source) { + if isValidList(index) { + let sourceArray = source.components(separatedBy: ", ") + let indexArray = index.components(separatedBy: ", ") + + var leftRotation = [[Int]](); + for i in 0...indexArray.count-1 { + var array = [Int](); + let _index:Int = Int(indexArray[i])! + + for j in _index...sourceArray.count-1 { + array.append(Int(sourceArray[j])!) + } + + if _index > 0 { + for k in 0..._index-1 { + array.append(Int(sourceArray[k])!) + } + } + + leftRotation.append(array) + } + + print(leftRotation) + } + else { + throw ParamError.invalidIndex + } + } + else { + throw ParamError.invalidSource + } +} +catch ParamError.missingSource { + print("Missing source.") +} +catch ParamError.missingIndex { + print("Missing index.") +} +catch ParamError.invalidSource { + print("Invalid source.") +} +catch ParamError.invalidIndex { + print("Invalid index.") +} +catch let error { + print(error) +} + +// +// +// Functions + +func isValidList(_ list:String) -> Bool { + + let pattern = "^[\\-?\\d\\,?\\s?]+$" + let regex = try! NSRegularExpression(pattern: pattern) + let range = NSRange(location: 0, length: list.utf16.count) + + if regex.firstMatch(in: list, options: [], range: range) != nil { + return true + } + else { + return false + } +} |
