Lisp 小口齿不清挑战赛

Lisp 小口齿不清挑战赛,lisp,Lisp,我有一个类似2d 6:36的字符串,我想用一个常数乘以小时数 这意味着我希望计算: 或者,一般来说 有什么帮助吗 编辑:我只有字符串not,d,h和m。您需要用一些有用的东西来解析字符串,例如整数列表,您可以使用: (defun parse-day-and-time (string) "Get string like 3d 23:40 and return list (3 23 40)" (multiple-value-bind (str matches) (cl-ppcre:s

我有一个类似2d 6:36的字符串,我想用一个常数乘以小时数

这意味着我希望计算:

或者,一般来说

有什么帮助吗


编辑:我只有字符串not,d,h和m。

您需要用一些有用的东西来解析字符串,例如整数列表,您可以使用:

(defun parse-day-and-time (string)
  "Get string like 3d 23:40 and return list (3 23 40)"
  (multiple-value-bind (str matches)
    (cl-ppcre:scan-to-strings "(\\d+)d ([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])" string)
    (declare (ignore str))
    (when (< (length matches) 3)
      (error "Wrong day and time string: ~A" string))
    (map 'list #'parse-integer matches)))

当然,如果你想用你的时间做更多的计算,可能最好用秒数来表示,用秒数进行计算,然后以你想要的任何字符串返回。

所以我理解数据是人类可读的时间量。。例如,2d 1:35表示2天1小时35秒或595/12小时。我很想把绳子的规格放宽如下:

(defparameter *units* '(("w" . 168) ("d" . 24) (":" . 1)
                        ("h" . 1) ("m" . 1/60) ("s" . 1/3600)))
(defun str-to-hours (str &optional (acc 0))
  (or
   (cl-ppcre:register-groups-bind (num unit rest)
                                  ("(\\d+)\\s*(\\D{0,1})\\D*(.*)" str :sharedp t)
     (str-to-hours rest
                   (+ acc
                      (* (parse-integer num)
                         (or (cdr (assoc (string-downcase unit) 
                                         *units* 
                                         :test #'equal)) 
                              1/60)))))
   acc))

(str-to-hours  "2d 6:36")  ; ==> 273/5 ~54.6
(str-to-hours  "2D6H36M")  ; ==> 273/5 ~54.6
(str-to-hours  "2 weeks, 1 day, 3 hours, 7 minutes and 10 seconds") ; ==> 130723/360 ~363.11945

(* 10 (str-to-hours "2d6:36")) ; ==> 546

你确定你想要*100吗?你试过*constant+*d24小时/m60吗?你在找什么帮助?编写LISP表达式?解析?,尼尔·弗雷斯特,你说得对@RayToal,解析是困难的部分,但是现在你已经告诉我如何编写公式了,我也需要它。Stackoverflow实际上不是一个Lisp挑战的站点。如果你能表现出一些努力去解决一个问题,那就太好了。你的mult时间不使用字符串时间,而是使用常量2d 6:36
(defun parse-day-and-time (string)
  "Get string like 3d 23:40 and return list (3 23 40)"
  (multiple-value-bind (str matches)
    (cl-ppcre:scan-to-strings "(\\d+)d ([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])" string)
    (declare (ignore str))
    (when (< (length matches) 3)
      (error "Wrong day and time string: ~A" string))
    (map 'list #'parse-integer matches)))
(defun mult-time (string-time coeff)
  (destructuring-bind (days hours minutes)
    (parse-day-and-time string-time)
    (* coeff (+ (* 24 days) hours (/ minutes 60)))))

CL-USER> (mult-time "2d 6:36" 300)
16380
(defparameter *units* '(("w" . 168) ("d" . 24) (":" . 1)
                        ("h" . 1) ("m" . 1/60) ("s" . 1/3600)))
(defun str-to-hours (str &optional (acc 0))
  (or
   (cl-ppcre:register-groups-bind (num unit rest)
                                  ("(\\d+)\\s*(\\D{0,1})\\D*(.*)" str :sharedp t)
     (str-to-hours rest
                   (+ acc
                      (* (parse-integer num)
                         (or (cdr (assoc (string-downcase unit) 
                                         *units* 
                                         :test #'equal)) 
                              1/60)))))
   acc))

(str-to-hours  "2d 6:36")  ; ==> 273/5 ~54.6
(str-to-hours  "2D6H36M")  ; ==> 273/5 ~54.6
(str-to-hours  "2 weeks, 1 day, 3 hours, 7 minutes and 10 seconds") ; ==> 130723/360 ~363.11945

(* 10 (str-to-hours "2d6:36")) ; ==> 546