简体   繁体   English

Hackerrank 苹果和橘子问题测试用例在 swift 中提交失败

[英]Hackerrank apples and oranges question test cases fail on submit in swift

The detail of question is here: https://www.hackerrank.com/challenges/apple-and-orange/problem问题的详细信息在这里: https://www.hackerrank.com/challenges/apple-and-orange/problem

My code passed sample test cases when I click on Run button but when I click on submit button all test cases fail.当我单击“运行”按钮时,我的代码通过了示例测试用例,但是当我单击“提交”按钮时,所有测试用例都失败了。 My code is given below:我的代码如下:

func countApplesAndOranges(s: Int, t: Int, a: Int, b: Int, apples: [Int], oranges: [Int]) -> Void {

  var appless = [Int]()
  var orangess = [Int]()

  appless = apples.map{a + $0}
  orangess = oranges.map{b + $0}
  appless = appless.filter{$0 >= s && $0 <= t}
  orangess = orangess.filter{$0 >= s && $0 <= t}

  print(appless.count)
  print(orangess.count)

}

countApplesAndOranges(s: 7, t: 11, a: 5, b: 15, apples: [-2,2,1], oranges: [5,-6])

If someone knows what is the issue then please let me know.如果有人知道是什么问题,请告诉我。

Test cases fail because your code does not fulfil all the requirements of the question.测试用例失败是因为您的代码没有满足问题的所有要求。 Try to read question carefully.尝试仔细阅读问题。 For me the code given below passed all the test cases on submit:对我来说,下面给出的代码在提交时通过了所有测试用例:

import Foundation;

// Enter your code here

let hitRange = readLine()!.components(separatedBy: " ").map{ Int($0)! }
let treePoints = readLine()!.components(separatedBy: " ").map{ Int($0)! }
let applesOranges = readLine()!.components(separatedBy: " ").map{ Int($0)! }
let apples = readLine()!.components(separatedBy: " ").map{ Int($0)! }
let oranges = readLine()!.components(separatedBy: " ").map{ Int($0)! }


var appleInRange = 0
var orangeInRange = 0

for apple in apples {
   let result = treePoints[0] + apple
   switch result {
      case hitRange[0]...hitRange[1]:
          appleInRange += 1
      default:
          continue
   }
}

for orange in oranges {
    let result = treePoints[1] + orange
    switch result {
           case hitRange[0]...hitRange[1]:
                orangeInRange += 1
           default:
              continue
    }
}

print(appleInRange)
print(orangeInRange)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM