Anonymous user
Largest int from concatenated ints: Difference between revisions
Largest int from concatenated ints (view source)
Revision as of 15:05, 25 January 2018
, 6 years ago→{{header|Common Lisp}}: Swapping implementations, as the original one is deeply nested and does not match the task's suggestions, making it harder to understand
(→{{header|Common Lisp}}: Showing a Common Lisp implementation that goes by the task's suggestions) |
m (→{{header|Common Lisp}}: Swapping implementations, as the original one is deeply nested and does not match the task's suggestions, making it harder to understand) |
||
Line 420:
=={{header|Common Lisp}}==
Implementing the second algorithm suggestion :▼
<lang lisp>▼
(defun int-concat (ints)▼
(read-from-string (apply #'concatenate (cons 'string (mapcar #'princ-to-string ints)))))▼
(defun by-biggest-result (first second)▼
(> (int-concat (list first second)) (int-concat (list second first))))▼
(defun make-largest-int (ints) ▼
(int-concat (sort ints #'by-biggest-result)))▼
</lang>▼
{{out}}▼
<pre>▼
> (make-largest-int '(1 34 3 98 9 76 45 4))▼
998764543431▼
> (make-largest-int '(54 546 548 60))▼
6054854654▼
</pre>▼
<lang lisp>
;; Sort criteria is by most significant digit with least digits used as a tie
Line 452 ⟶ 477:
6054548546
998764453341
▲</pre>
▲Implementing the second algorithm suggestion :
▲<lang lisp>
▲(defun int-concat (ints)
▲ (read-from-string (apply #'concatenate (cons 'string (mapcar #'princ-to-string ints)))))
▲(defun by-biggest-result (first second)
▲ (> (int-concat (list first second)) (int-concat (list second first))))
▲(defun make-largest-int (ints)
▲ (int-concat (sort ints #'by-biggest-result)))
▲</lang>
▲{{out}}
▲<pre>
▲> (make-largest-int '(1 34 3 98 9 76 45 4))
▲998764543431
▲> (make-largest-int '(54 546 548 60))
▲6054854654
</pre>
|