Skip to content

Table of contents

It took me more than one and half hours to figure out how to take the number from user input and make a list from it.

If we pass the input such (1 2 3 4 5), The solution is would be trivial:

(defun create-list (input)
(list input)
(format t "~a" input))

The problem is that the HackerRank input looks like this: 1 2 3 4 5.

After some search, I find this solution. Unfortunately, the input can not contain any white space. It must be 12345.

(defun number-to-list (n)
(loop for c across (write-to-string n) collect (digit-char-p c)))

I continue to look for other solutions. None of them suits my needs. Even if they work fine in my machine, they will fail in HackerRank test cases. I suppose HackerRank website passes the input differently.

This solution with slight modification works on my machine, but not in HackerRank test cases.

(defun readarray (&rest args)
(values (read-from-string
(concatenate 'string "("
(apply #'read-line args)

After spending so much time. I forget that HackerRank provides the related function at the beginning of the warm-up question. I look back and run the function. It works well, oh my bad.

(defun read-list ()
(let ((n (read *standard-input* nil)))
(if (null n)
(cons n (read-list)))))

I am very glad because this is the first time I posted about practical things in programming.

Happy hacking!