Lisp和clojure代码在Google KickStart竞争对手重新键入问题上失败

2024-09-27 09:32:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我曾尝试用不同的编程语言进行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

Sample.in:

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

Tags: samplepath代码inforreadgooglemin
2条回答

我不知道你的代码的目的是什么,但是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,测试运行程序希望运行脚本而不是-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)))))

相关问题 更多 >

    热门问题