Einzelnen Beitrag anzeigen
Alt 22.06.08, 11:55   #37 (permalink)
Lesco
Senior Member
 
Registriert seit: 03.09.05
Lesco Leistung: Facit NTK
Likes: 0
Standard

Hier noch eine Lösung in Lisp:
lisp   

Code:
(defparameter mapping '(("I" 1)	("IV" 4) ("V" 5) ("IX" 9) ("X" 10) ("XL" 40)
			("L" 50) ("XC" 90) ("C" 100) ("CD" 400) ("D" 500)
			("CM" 900) ("M" 1000) ("MA" 4000) ("A" 5000)))

(defun get-highest-match (number)
  (find-if (lambda (x)
	     (<= x number))
	   mapping
	   :key #'second
	   :from-end t))
;decimal to roman
(defun toroem (number)
  (if (= number 0)
      ""
      (let ((match (get-highest-match number)))
	(string-concat (car match)
		       (toroem (- number (second match)))))))

(defun get-string-match (string)
  ;sort first to catch IV,IX,XL....
  (let ((sorted-mapping (sort mapping #'>
			      :key (lambda (x) (length (car x))))))
    (dolist (el sorted-mapping)
      ;check for long enough 'string' before subseq'ing
      (if (and (<= (length (car el)) (length string))
	       (string= (car el)
			(subseq string 0 (length (car el)))))
	  (return el)))))
;roman to decimal, no error checking
(defun todec (string)
  (let ((match (get-string-match string)))
    (if (string= "" string)
	0
	(+ (second match) (todec (subseq string (length (car match))))))))
Lesco ist offline   Mit Zitat antworten
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61