Author Topic: List of numbers to compacted string?  (Read 12832 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
List of numbers to compacted string?
« on: October 13, 2006, 02:31:13 PM »
OK, so it's Friday, it's been a long week, and I just can't seem to get into coding this seemingly simple function. Anyone care to help out?

Given: List of numbers, sorted lowest->highest, not necessarily sequential.
Needed: String of said numbers, compacting the sequential ones.

Example:
This list: '(1 3 4 5 8 10 11 14 15 16)
would return this string: "1,3-5,8,10,11,14-16"

Thanks

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: List of numbers to compacted string?
« Reply #1 on: October 13, 2006, 03:03:49 PM »
Be gentle, very quick, very dirty --

Code: [Select]
(defun IntsToRangedString ( ints / sorted result group )

    ;;  first sort em, losing any duplicates
   
    (setq
        sorted (vl-sort ints '<)
        result (list (list (last sorted)))
    )

    ;;  now roll 'em ...
   
    (foreach x (cdr (reverse sorted))
        (setq result
            (if (eq (1+ x) (car (setq group (car result))))
                (cons
                    (cons x (list (last group)))
                    (cdr result)
                )
                (cons
                    (list x)
                    result
                )
            )
        )   
    )
   
    (setq result
        (mapcar
           '(lambda ( lst / a b )
                (cond
                    (   (eq 1 (length lst))
                        (itoa (car lst))
                    )
                    (   (eq (1+ (setq a (car lst))) (setq b (cadr lst)))
                        (strcat (itoa a) "," (itoa b))
                    )
                    (   (strcat (itoa a) "-" (itoa b))  )
                )
            )
            result
        )
    )
   
    (apply 'strcat
        (append
            (list (car result))
            (mapcar
               '(lambda (x) (strcat "," x))
                (cdr result)
            )
        )
    )
   
)

(IntsToRangedString '(1 3 4 5 8 10 11 14 15 16))

"1,3-5,8,10,11,14-16"
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: List of numbers to compacted string?
« Reply #2 on: October 13, 2006, 03:12:26 PM »
Here is mine.
Code: [Select]
(defun ForJeff (NumList / Str PrevNum Hyphen)

(foreach Num NumList
 (if (not Str)
  (setq Str (itoa Num))
  (if (equal (1- Num) PrevNum)
   (setq Hyphen T)
   (setq Str
    (strcat
     Str
     (if Hyphen
      (progn
       (setq Hyphen nil)
       (strcat "-" (itoa PrevNum) "," (itoa Num))
      )
      (strcat "," (itoa Num))
     )
    )
   )
  )
 )
 (if
  (and
   (equal Num (last NumList))
   (equal (1- Num) PrevNum)
  )
  (setq Str
   (strcat
    Str
    (if Hyphen
     (progn
      (setq Hyphen nil)
      (strcat "-" (itoa Num))
     )
     (strcat "," (itoa Num))
    )
   )
  )
 )
 (setq PrevNum Num)
; (prompt (strcat "\n Testing: " Str))
)
Str
)
Return
Quote
Command: (forjeff '(1 3 4 5 8 10 11 14 15 16))
"1,3-5,8,10-11,14-16"

Command: (forjeff '(1 3 4 5 8 10 11 14 15 16 20))
"1,3-5,8,10-11,14-16,20"
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: List of numbers to compacted string?
« Reply #3 on: October 13, 2006, 03:14:18 PM »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: List of numbers to compacted string?
« Reply #4 on: October 13, 2006, 03:18:51 PM »
Thanks, Michael! Looks almost like what I was imagining but couldn't quite code.

Works great, too! :-)

Thanks to Tim, too! Only minor deficiency I see is "10-11" should be "10,11" but looks great, too.

Now I can get back to some real work.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: List of numbers to compacted string?
« Reply #5 on: October 13, 2006, 03:21:29 PM »
Here is mine ...

Cool.

:)
Same could/is said about yours.  Going the list route.  Took me a little while to see the whole flow.  I like the example.  :-)

Thanks to Tim, too! Only minor deficiency I see is "10-11" should be "10,11" but looks great, too.

Now I can get back to some real work.
Guess I could look at that, but since Michael's works.......
Have fun today.   :-D
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: List of numbers to compacted string?
« Reply #6 on: October 13, 2006, 03:27:47 PM »
You're very welcome Jeff. I'm surprised I can code anything today -- have a cold / splitting headache -- but I am in the comfort of my own home* -- maybe that's what enabled a solution to percolate up.

:)

* Officially unemployed (of my own volition) as of yesterday but starting with a new client on the 30th, so all is cool.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: List of numbers to compacted string?
« Reply #7 on: October 13, 2006, 03:34:40 PM »
You're very welcome Jeff. I'm surprised I can code anything today -- have a cold / splitting headache -- but I am in the comfort of my own home* -- maybe that's what enabled a solution to percolate up.
Sorry to hear this.  Get better soon.

* Officially unemployed (of my own volition) as of yesterday but starting with a new client on the 30th, so all is cool.
Happy to hear this.  Hope the new client will stimulate you mind, and keep things fresh and fun.  Good luck there Michael.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: List of numbers to compacted string?
« Reply #8 on: October 13, 2006, 03:42:32 PM »
You're very welcome Jeff. I'm surprised I can code anything today -- have a cold / splitting headache -- but I am in the comfort of my own home* -- maybe that's what enabled a solution to percolate up.
Sorry to hear this.  Get better soon.

Thanks Tim!

* Officially unemployed (of my own volition) as of yesterday but starting with a new client on the 30th, so all is cool.
Happy to hear this.  Hope the new client will stimulate you mind, and keep things fresh and fun.  Good luck there Michael.

Thanks x 2! The new job has abundant promise: A spectrum of automation challenges, no development software handcuffs + good peeps, folks I've worked with before and have lots of respect for (that bring buckets of expertise and experience to the table). Bidirectional mentoring = true. What's not to like?

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: List of numbers to compacted string?
« Reply #9 on: October 13, 2006, 03:44:49 PM »
Dittos on the get well soon, MP. While it'll be good to be off the next few weeks to recharge your system, it would be far better if you felt good during that period.


ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: List of numbers to compacted string?
« Reply #10 on: October 13, 2006, 03:45:53 PM »
Code: [Select]
(defun test (lst)
  (if (and lst (listp (car lst)))
    (cond
      ((not (cadr lst))
       (strcat (itoa (caar lst)) "-" (itoa (cadar lst)))
      )
      ((= (1+ (cadar lst)) (cadr lst))
       (test (cons (list (caar lst) (cadr lst)) (cddr lst)))
      )
      (t (strcat (itoa (caar lst)) "-" (itoa (cadar lst)) "," (test (cddr lst))))
    ) ;_  cond
    (cond
      ((null lst) "")
      ((not (cadr lst)) (itoa (car lst)))
      ((= (1+ (car lst)) (cadr lst))
       (test (cons (list (car lst) (cadr lst)) (cddr lst)))
      )
      (t (strcat (itoa (car lst)) "," (test (cddr lst))))
    ) ;_  cond
  ) ;_  if
)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: List of numbers to compacted string?
« Reply #11 on: October 13, 2006, 03:47:28 PM »
Dittos on the get well soon, MP. While it'll be good to be off the next few weeks to recharge your system, it would be far better if you felt good during that period.

Thanks and true Jeff. I'm exploiting the time off / bed rest by doing as much geek reading as my eyes will permit.

:)
« Last Edit: October 13, 2006, 03:50:00 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: List of numbers to compacted string?
« Reply #12 on: October 13, 2006, 03:49:19 PM »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: List of numbers to compacted string?
« Reply #13 on: October 13, 2006, 03:55:52 PM »
Great thread guys !
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: List of numbers to compacted string?
« Reply #14 on: October 13, 2006, 03:58:02 PM »
Hi,

Another way :

Code: [Select]
(defun intlst2str (lst / str)
  (setq lst (vl-sort lst '<)
str ""
  )
  (while lst
    (setq str (strcat str (itoa (car lst))))
    (if (= (+ 2 (car lst)) (caddr lst))
      (progn
(setq str (strcat str "-"))
(while (= (1+ (car lst)) (cadr lst))
  (setq lst (cdr lst))
)
(setq str (strcat str (itoa (car lst))))
      )
    )
    (setq lst (cdr lst))
    (if lst
      (setq str (strcat str ","))
    )
    str
  )
)
Speaking English as a French Frog