Description

You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones – everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. [‘n’, ‘s’, ‘w’, ‘e’]). You always walk only a single block for each letter (direction) and you know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don’t want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.

Solutions

Julia

using Test


"""
valid walk 的标准为:走十步并回到原点
"""
#> "valid walk 的标准为:走十步并回到原点\n"
function isvalidwalk(walk::Vector{Char})::Bool
    [length(walk) == 10,
        sum(walk .== 'n') == sum(walk .== 's'),
        sum(walk .== 'e') == sum(walk .== 'w')] |> all
end
#> isvalidwalk (generic function with 1 method)

@test isvalidwalk(['n', 's', 'n', 's', 'n', 's', 'n', 's', 'n', 's']) == true
#> Test Passed
#>   Expression: isvalidwalk(['n', 's', 'n', 's', 'n', 's', 'n', 's', 'n', 's']) == true
#>    Evaluated: true == true

R

#' @description 10轮后恰好回到起点返回 TRUE,否则返回 FALSE
isValidWalk <- function(walk) {
    all(length(walk) == 10, sum(walk == "n") == sum(walk == "s"), sum(walk == "w") ==
        sum(walk == "e"))
}

JavaScript

function isValidWalk(walk) {
  let nn = walk.filter(x => x === "n").length;
  let ns = walk.filter(x => x === "s").length;
  let nw = walk.filter(x => x === "w").length;
  let ne = walk.filter(x => x === "e").length;
  if (walk.length === 10 && nn === ns && nw === ne) return true;
  else return false;
}