Author Topic: (challenge) find numbers  (Read 7872 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
(challenge) find numbers
« on: May 21, 2004, 05:12:58 PM »
I got, what i think could be a fun little challenge if your up to it.

Come up with a cool way to locate "numbers" in a string. -i.e. If i have the string:
"My pipe is 2'' in diameter." I want a procedure to tell me where (by a zero based index --the first charater is zero) that number is. So... using my example string, our procedure would have to return "11". Get it? After we come up with some cool procedures we can maybe time some and get the fastest ones ...have a race.

Sound fun? Sound hard? Sound to wimpy? Am i nuts?

OBTW, I just posted this over at The vault too. :D :P
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) find numbers
« Reply #1 on: May 21, 2004, 05:15:04 PM »
Now who's double posting......... :D
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) find numbers
« Reply #2 on: May 21, 2004, 05:16:14 PM »
- Challenge -

do we have to find _all_ the numbers or just the first one(s)?
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
(challenge) find numbers
« Reply #3 on: May 21, 2004, 05:22:35 PM »
Oh you bastard!...

Umm just the first one will do.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
(challenge) find numbers
« Reply #4 on: May 21, 2004, 05:30:56 PM »
Damn! I just ran a test (without any pseudo code or anything --i know, i know) and i did it already!?  That was kinda wussy!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) find numbers
« Reply #5 on: May 21, 2004, 05:41:55 PM »
quick-n-dirty and needs some more work.
Code: [Select]
usage: (remove-num "this is a text 123.556 string")
Code: [Select]

(defun remove-num (str / lst)
  (setq lst (vl-string->list str))
  (vl-list->string
    (vl-remove-if
      '(lambda (z)
         (cond ((> z 58))
               ((< z 45))
               ((= z 47))
               )
         )
      lst
      )
    )
  )
TheSwamp.org  (serving the CAD community since 2003)

DParcon

  • Guest
(challenge) find numbers
« Reply #6 on: May 21, 2004, 07:29:40 PM »
Here's my lisp version:

Code: [Select]


(defun getnum (xstr)
  (if (= (type xstr) 'STR)
    (progn
      (setq str ""
            flag nil
      )
      (while (and (not (eq xstr "")) (setq item (substr xstr 1 1)))
        (cond
          ((and flag (= item "."))
            (setq str (strcat str item)
                  xstr (substr xstr 2)
            )
          )          
          ((numberp (read (substr xstr 1 1)))
             (setq str (strcat str item)
                   xstr (substr xstr 2)
             )
             (if (not flag) (setq flag T))
          )
          (T (if flag
               (setq xstr "")
               (setq xstr (substr xstr 2))
             )  
          )
        );cond
      );while
    );progn
  );if
  str
);defun


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(challenge) find numbers
« Reply #7 on: May 21, 2004, 08:08:37 PM »
Returns a  pointer or nil

Code: [Select]
(defun c:get_pointer ()
  (setq pointer
         (numindex "My pipe is 2\" in diameter.")))



((defun numindex (str / lst)
  (setq lst (vl-string->list str))
  (car (vl-remove nil
         (mapcar '(lambda (x) (vl-position x lst))
                 (vl-string->list "0123456789")))))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
(challenge) find numbers
« Reply #8 on: May 21, 2004, 11:09:57 PM »
I think an article on writing specifications is due.
What is a "number" ? your quotes

How do you want these handled. ?

36 blind mice.

4 score and 7 years ago ...

11 + 201 = 212

The answer is 212.03

The answer is 212.03. 9 details are to follow.
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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
(challenge) find numbers
« Reply #9 on: May 22, 2004, 01:38:01 AM »
This isn't terribly efficient, but it's a late Friday night after a long week and I did this merely for fun (AutoCAD 2000+) ...
Code: [Select]

(defun FindNumbers ( String / Distil NumList Result )

   (defun Distil ( lst )
      (while (eq 46 (car lst)) (setq lst (cdr lst)))
      (setq lst (reverse lst))
      (while (eq 46 (car lst)) (setq lst (cdr lst)))
      (reverse lst)
   )
   
   (foreach Code
   
      (reverse
         (mapcar
           '(lambda (Code)
               (if (or (< 47 Code 58) (eq 46 Code))
                  Code
                  32
               )
            )  
            (vl-string->list String)
         )
      )  
     
      (if (eq 32 Code)
         (if NumList
            (setq
               Result  (cons NumList Result)
               NumList nil
            )
         )
         (setq NumList (cons Code NumList))
      )  
   )
   
   (mapcar 'vl-list->string
      (vl-remove-if 'null
         (mapcar 'Distil
            (if NumList
               (cons NumList Result)
               Result
            )
         )  
      )  
   )  
)

(defun c:Test ()
   (foreach string
     '(
         "36 blind mice."
         "4 score and 7 years ago ..."
         "11 + 201 = 212"
         "The answer is 212.03"
         "The answer is 212.03. 9 details are to follow"
      )
      (princ (strcat "\n\"" string "\" -> "))  
      (princ (FindNumbers string))
   )
   (princ)
)

(c:Test)

"36 blind mice." -> (36)
"4 score and 7 years ago ..." -> (4 7)
"11 + 201 = 212" -> (11 201 212)
"The answer is 212.03" -> (212.03)
"The answer is 212.03. 9 details are to follow" -> (212.03 9)
I'll toss this challenge back to you: Find negative numbers (ascii code 45) too :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Fuccaro

  • Guest
(challenge) find numbers
« Reply #10 on: May 22, 2004, 02:10:14 AM »
Code: [Select]
(defun find_nr(a / i index b)
  (setq i 0 index nil)
  (while (and (< i (strlen a)) (not index))
    (setq b (substr a (setq i (1+ i)) 1))
    (if (and (<= "0" b) (>= "9" b)) (setq index (1- i))))
  )

usage: (find_nr "string to search")

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
(challenge) find numbers
« Reply #11 on: May 22, 2004, 08:17:23 AM »
I spose i should post my "test" that i ran and got results right away with huh? Ok its early Sat. morning and i tried to rebuild what i have at work so if its wrong, i dont care --im tired. :D

Code: [Select]
(defun test (str / cntr)
  (setq cntr 0)
  (mapcar
    '(lambda (x)
       (if (eq (and (> x 48) (< x 57)) 'T)
         (setq flag cntr)
         (setq cntr (1+ cntr))))
    (vl-string->list str))
  flag)
Command: (test "Thi is a 3 test")
9
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
(challenge) find numbers
« Reply #12 on: May 22, 2004, 08:46:54 AM »
This will return position of each digit in a string

Code: [Select]
(defun digitPos (str / i)
  (setq i -1)
  (vl-remove 'nil (mapcar '(lambda (n)(setq i (1+ i))(if (numberp n) i))
     (mapcar 'read (mapcar 'chr (vl-string->list str)))))
)


(digitPos "she had 3 pigs and 34 cows")
(8 19 20)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(challenge) find numbers
« Reply #13 on: May 22, 2004, 09:30:50 AM »
Stig, late to the party but a grand entrance.. :)

Michael  you have taken it to the next logical level,
extracting the numbers that is.

Stig there was a thread some time back that dealt with
converting strings to numbers or maybe it was converting
user input to numbers. I remember it was dealing with numbers
like 13'6   20'10"  25'-8 1/2"  I can't seem to find it.
Do you remember the thread an how to find it?

CAB


Oh, thanks Se7en for the challenge.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
(challenge) find numbers
« Reply #14 on: May 22, 2004, 09:54:44 AM »
I suppose I should have responded to the challenge, not my perception of what would be useful (that's a real bad habit of mine), so ... here's my lame attempt at a more appropriate response:
Code: [Select]

(defun FindNumbers ( string / lst i result )
   (repeat (setq i (length (setq lst (vl-string->list string))))
      (if (< 47 (nth (setq i (1- i)) lst) 58)
         (setq result (cons i result))
      )  
   )
   result
)

Command: test

"36 blind mice." -> (0 1)
"4 score and 7 years ago ..." -> (0 12)
"11 + 201 = 212" -> (0 1 5 6 7 11 12 13)
"The answer is 212.03" -> (14 15 16 18 19)
"The answer is 212.03. 9 details are to follow" -> (14 15 16 18 19 22)
Cheers :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

SMadsen

  • Guest
(challenge) find numbers
« Reply #15 on: May 22, 2004, 10:17:45 AM »
CAB, nope can't remember the thread

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
(challenge) find numbers
« Reply #16 on: May 22, 2004, 02:41:44 PM »
> CAB Oh, thanks Se7en for the challenge.

I do what i can. Besides you know what they say about People how like the number Se7en, right?! "They are smart as hell and have to beat the women off with a stick." (I heard that somewhere; not really sure where, but i like it!) :P So thinking up a challenge like this wasnt that hard, you see.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org