Skip to content

Commit

Permalink
- Added Swift solution to the "Rotate String" task.
Browse files Browse the repository at this point in the history
  • Loading branch information
manwar committed Aug 5, 2020
1 parent 6c629ee commit 81d759c
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions challenge-063/mohammad-anwar/swift/ch-2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import Foundation

/*
Perl Weekly Challenge - 063

Task #2: Rotate String

https://perlweeklychallenge.org/blog/perl-weekly-challenge-063
*/

enum ParamError: Error {
case missingWord
case invalidWord
}

do {
let paramCount:Int = Int(CommandLine.argc)

if paramCount <= 1 {
throw ParamError.missingWord
}

let word:String = CommandLine.arguments[1]

if isValidWord(word) {
let size:Int = word.count;
var temp:String = word
var i:Int = 1
var c:Int = 1

while i <= size {
let partA:String = String(temp.substring(to: i))
let partB:String = String(temp.substring(from: i))
temp = partB + partA
print("[\(c)]: [\(temp)]")

if temp == word {
break
}

i = i + 1
c = c + 1

if i > size {
i = 1
}
}

print("Rotation: \(c)")
}
else {
throw ParamError.invalidWord
}
}
catch ParamError.missingWord {
print("Missing word.")
}
catch ParamError.invalidWord {
print("Invalid word.")
}
catch let error {
print(error)
}

//
//
// Function

func isValidWord(_ word:String) -> Bool {

let pattern = "^[xy]+$"
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let range = NSRange(location: 0, length: word.utf16.count)

if regex.firstMatch(in: word, options: [], range: range) != nil {
return true
}
else {
return false
}
}

0 comments on commit 81d759c

Please sign in to comment.