难以让 readLine() 在 HackerRank 上按预期工作

Difficulty getting readLine() to work as desired on HackerRank

我正在尝试提交 HackerRank Day 6 Challenge 30 天的代码。

我能够在 Xcode Playground 中毫无问题地完成任务,但是 HackerRank 的网站说我的方法没有输出。由于浏览器不稳定,我昨天遇到了一个问题,但是清理缓存、从 Safari 切换到 Chrome 等似乎无法解决我在这里遇到的问题。我认为我的问题在于 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.

这是我要提交的代码:

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({[=10=].0 % 2 == 0}).map({[=10=].1})
            let oddCharacters = characterArray.enumerated().filter({[=10=].0 % 2 == 1}).map({[=10=].1})

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

tweakString(string: inputString)

我认为我的问题在于 inputString。我正在使用它 "as-is" 并在我的方法中对其进行格式化。我找到了第 6 天的解决方案,但我似乎无法在 Swift.

中找到任何当前的解决方案

感谢您的阅读。我欢迎关于如何让这件事通过的想法。

readLine() 从标准输入中读取 单行 ,这 表示您的 inputString 仅包含来自 输入数据。您必须循环调用 readLine() 才能获得 剩余的输入数据。

因此您的程序可能如下所示:

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)
}

(强制展开在这里是可以接受的,因为格式 输入数据记录在挑战描述中。)

嗨,阿德里安,你应该每行都打电话给 readLine()!。这是该挑战的示例答案;

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()!)!)
}