Adrian
Adrian

Reputation: 16715

Difficulty getting readLine() to work as desired on HackerRank

I'm attempting to submit the HackerRank Day 6 Challenge for 30 Days of Code.

I'm able to complete the task without issue in an Xcode Playground, however HackerRank's site says there is no output from my method. I encountered an issue yesterday due to browser flakiness, but cleaning caches, switching from Safari to Chrome, etc. don't seem to resolve the issue I'm encountering here. I think my problem lies in inputString.

Task Given a string, S, of length N that is indexed from 0 to N-1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line (see the Sample below for more detail).

Input Format

The first line contains an integer, (the number of test cases). Each line of the subsequent lines contain a String, .

Constraints

  • 1 <= T <= 10
  • 2 <= length of S < 10,000

Output Format

For each String (where 0 <= j <= T-1), print S's even-indexed characters, followed by a space, followed by S's odd-indexed characters.

This is the code I'm submitting:

import Foundation

let inputString = readLine()!

func tweakString(string: String) {
    // split string into an array of lines based on char set
    var lineArray = string.components(separatedBy: .newlines)

    // extract the number of test cases
    let testCases = Int(lineArray[0])

    // remove the first line containing the number of unit tests
    lineArray.remove(at: 0)

    /*
    Satisfy constraints specified in the task
     */
    guard lineArray.count >= 1 && lineArray.count <= 10 && testCases == lineArray.count else { return }

    for line in lineArray {
        switch line.characters.count {
        // to match constraint specified in the task
        case 2...10000:
            let characterArray = Array(line.characters)
            let evenCharacters = characterArray.enumerated().filter({$0.0 % 2 == 0}).map({$0.1})
            let oddCharacters = characterArray.enumerated().filter({$0.0 % 2 == 1}).map({$0.1})

            print(String(evenCharacters) + " " + String(oddCharacters))
        default:
            break
        }
    }
}

tweakString(string: inputString)

I think my issue lies the inputString. I'm taking it "as-is" and formatting it within my method. I've found solutions for Day 6, but I can't seem to find any current ones in Swift.

Thank you for reading. I welcome thoughts on how to get this thing to pass.

Upvotes: 2

Views: 1625

Answers (2)

Alperk
Alperk

Reputation: 102

Hi Adrian you should call readLine()! every row . Here an example answer for that challenge;

import Foundation

func letsReview(str:String){
    var evenCharacters = ""
    var oddCharacters = ""
    var index = 0
    for char in str.characters{
        if index % 2 == 0 {
            evenCharacters += String(char)
        }
        else{
            oddCharacters += String(char)
        }
        index += 1
    }
    print (evenCharacters + " " + oddCharacters)
}

let rowCount = Int(readLine()!)!

for _ in 0..<rowCount {
    letsReview(str:String(readLine()!)!)
}

Upvotes: 1

Martin R
Martin R

Reputation: 539685

readLine() reads a single line from standard input, which means that your inputString contains only the first line from the input data. You have to call readLine() in a loop to get the remaining input data.

So your program could look like this:

func tweakString(string: String) -> String {
    // For a single input string, compute the output string according to the challenge rules ...
    return result
}

let N = Int(readLine()!)! // Number of test cases

// For each test case:
for _ in 1...N {
    let input = readLine()!
    let output = tweakString(string: input)
    print(output)
}

(The forced unwraps are acceptable here because the format of the input data is documented in the challenge description.)

Upvotes: 1

Related Questions