Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate Swift Array

Tags:

swift

I am trying to solve this question I found on a coding challenge website using Swift 3.

I'm sure most of you have seen it before, but in case you haven't here it is...

The idea is you take a string and rotate it x number of times. So using their example "12345" rotated 2x would be "34512"

I wrote this, but it when I print it out in Playground it just prints out the exact same string I entered.

func rotateSring(originalString: String, numberOfRotations: Int) -> String {

  var tempArray: [String] = []
  tempArray.append(originalString)

  let count = numberOfRotations

  for _ in 1...count {
    for letter in tempArray {

      tempArray.remove(at: 0)
      tempArray.append(letter)

    }
  }

  let newString = tempArray.joined(separator: "")

  return newString
}

I also tried a variation

func rotateSring(originalString: String, numberOfRotations: Int) -> String {

  var tempArray: [String] = []
  tempArray.append(originalString)

  let count = numberOfRotations

  for _ in 1...count {

      let test =tempArray.remove(at: 0)

      tempArray.append(test)
  }

  let newString = tempArray.joined(separator: "")

  return newString
}

Neither produce the desired result when I say

let testRun = rotateSring(originalString: "12345", numberOfRotations: 2)

I would like the "34512" but instead I get "12345"

If I had to guess what I am doing wrong, I think that I am just rotating the entire array from start to finish so it does move but it moves full circle.

If somebody could explain what I am doing wrong, and how I can fix it that would be great. Thank you

like image 747
RubberDucky4444 Avatar asked Dec 23 '22 18:12

RubberDucky4444


2 Answers

I have gone through your solution and found few mistakes. The below implementation will work.

func rotateSring(originalString: String, numberOfRotations: Int) -> String {

    var tempArray: [Character] = Array(originalString.characters)

    let count = numberOfRotations

    for _ in 1...count {
        let letter = tempArray.removeFirst()
        tempArray.append(letter)
    }

    let newString = String(tempArray)

    return newString
}

let testRun = rotateSring(originalString: "12345", numberOfRotations: 2)

Now let me explain the changes:

  var tempArray: [String] = []
  tempArray.append(originalString)

  // to
  var tempArray: [Character] = Array(originalString.characters)

In Swift, String doesn't conform to Sequence type protocol and so you need to use Character array and so when you were trying to loop over letters, you were actually looping over the whole string i.e. 12345.

  // tempArray = ["12345"] not ["1", "2", "3", "4", "5"]
  for letter in tempArray {
     tempArray.remove(at: 0)
     tempArray.append(letter)
  }
like image 67
Rahul Avatar answered Jan 06 '23 05:01

Rahul


func rotateSring(originalString: String, numberOfRotations: UInt) -> String {

    if numberOfRotations == 0 {
        return originalString
    }

    return rotateSring(originalString: originalString[originalString.index(after: originalString.startIndex)..<originalString.endIndex] + String(originalString[originalString.startIndex]),
                       numberOfRotations: numberOfRotations - 1)
}
like image 39
Alex Shubin Avatar answered Jan 06 '23 07:01

Alex Shubin