Lisp 和 clojure 代码在 Google KickStart 比赛重新输入问题时失败
Lisp and clojure code fail at Google KickStart competition retype problem
我正在尝试 google 2020 年不同编程语言的 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
Sample.in :
6
10 5 2
10 7 6
10 5 5
10 6 5
3 2 1
100 40 30
我不确定您的代码的用途是什么,但 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
查看 Google Kick Start platform notes for Clojure,测试 运行ner 期望 运行 脚本而不是 -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)))))
我正在尝试 google 2020 年不同编程语言的 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
Sample.in :
6
10 5 2
10 7 6
10 5 5
10 6 5
3 2 1
100 40 30
我不确定您的代码的用途是什么,但 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
查看 Google Kick Start platform notes for Clojure,测试 运行ner 期望 运行 脚本而不是 -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)))))