微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Lisp 和 clojure 代码在 Google KickStart 竞赛重新输入问题中失败

如何解决Lisp 和 clojure 代码在 Google KickStart 竞赛重新输入问题中失败

我正在尝试使用不同的编程语言参加 2020 年的 google KickStart 竞赛,但我的常见 lisp(sbcl) 和 clojure 代码在 google 测试中失败,但它们都在我的本地机器上正常工作,不知道我做错了什么?
工作 python 代码如下所示:

def short_path(n,k,s):
    return min((k + n),(2*k - 2*s + n))

for i in range (1,int(input())+1):
    print("Case #{}: {}".format(i,short_path(*[int(z) for z in input().split()])))

Lisp(sbcl) 版本:

(defun short_path (n k s)
  (let (x y)
    (setf x (+ n k))
    (setf y (+ (- (* k 2) (* 2 s)) n))
    (if (< x y) x y)))
(defun main ()
  (loop for i from 1 to (read)
     do (format t "Case #~a: ~a~%" i (short_path (read) (read) (read)))))

Clojure 版本:

(defn short_path [n k s]
  (min (+ n k) (+ n (- (* 2 k) (* 2 s)))))
(defn main []
  (dotimes [i (read)] (println (str "Case #" (+ i 1) ": " (short_path (read) (read) (read))))))

已编辑: 对于测试,我们应该使用:

cat Sample.in | python retype.py
cat Sample.in | sbcl --script retype.lisp
cat Sample.in | clj retype.clj

样本输入:

6
10 5 2
10 7 6
10 5 5
10 6 5
3 2 1
100 40 30

解决方法

查看 Google Kick Start platform notes for Clojure,测试运行器期望运行脚本而不是 -main 函数。我还没有注册 Google Kick Start,但这应该可以:

(defn min-path [n k s]
  (+ n (min k (* 2 (- k s)))))

(dotimes [i (read)]
  (println (str "Case #" (inc i) ": " (min-path (read) (read) (read)))))
,

我不确定您的代码的用途是什么,但 let 的使用在我看来很奇怪。最好试试:

(defun short_path (n k s)
   (let ((x (+ n k))
         (y (+ (- (* k 2) (* 2 s)) n) ))
     (if (< x y) x y)))

CL-USER> (short_path 1 2 3)
-1
CL-USER> (short_path 3 2 1)
5

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。