It tooks me more one and half hours to to figure it out “how to take number (interger) form user input and make a list from it”.

The trivial solution is pretty simple if the we pass the input like this as a list (1 2 3 4 5), we just need

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

Taking more than one hours to figure it out, I find the this solution but the input must not contain any white space 1234

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

When I look for the solution reading some SO questions and CLHS I find some solutions

But none of them suit my needs, because even they works fine in my SBCL machine, they will failed in HackerRanks test cases, maybe because they handle the input differently.

This Solution with some modification works on my SBCL machine, but not in HR test cases

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

After spending so much time thinking how to read the HR input nicely, I am forget that HR provides this function at the beginning of the warm up question, and this function works nicely. thanks HR guys.

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

Because the input in HR is not like these

  • "1 2 3 4 5"
  • "12345"
  • (1 2 3 4 5)

but like this 1 2 3 4 5, no quotes and no parens

I am verry happy because this is my first time I posted about practical things in programming.

Happy hacking.

Comments

So what do you think ? Leave your comments below.


Keep Reading


Published

Category

Code

Tags