Author Topic: How grouping and Conversion  (Read 6517 times)

0 Members and 1 Guest are viewing this topic.

myloveflyer

  • Newt
  • Posts: 152
How grouping and Conversion
« on: May 30, 2011, 04:54:39 AM »
For example:
"165478952"
Results: "A" "/" "Y" "4"
Never give up !

Coder

  • Swamp Rat
  • Posts: 827
Re: How grouping and Conversion
« Reply #1 on: May 30, 2011, 05:12:13 AM »
Check this .  :-)

Code: [Select]
(vl-list->string '(65 47 89 52))

myloveflyer

  • Newt
  • Posts: 152
Re: How grouping and Conversion
« Reply #2 on: May 30, 2011, 05:45:36 AM »
(defun c:test()
(setq a (getstring "\nEnter numbers:"))
(setq b (vl-list->string ' a))
(princ)
)

Finally, the error
How to make a transition to form groups and then conversion
Never give up !

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How grouping and Conversion
« Reply #3 on: May 30, 2011, 06:23:11 AM »
Not certain to understand the task, but this works with the example.

(foo "165478952") returns ("A" "/" "Y" "4")

Code: [Select]
(defun foo (str / num char lst)
  (if (numberp (setq num (read str)))
    (while (< 9 (setq char (rem num 100)))
      (setq num (/ num 100)
    lst (cons (chr char) lst)
      )
    )
  )
)
« Last Edit: May 30, 2011, 07:23:27 AM by gile »
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: How grouping and Conversion
« Reply #4 on: May 30, 2011, 07:20:25 AM »
Like this ....  :-)

Code: [Select]
(defun c:test (/ ss)
  (if (setq ss (getint "\n Enter Number (ascii) :"))
    (print (chr ss))
    )
  (princ)
  )

Lee Mac

  • Seagull
  • Posts: 12910
  • London, England
Re: How grouping and Conversion
« Reply #5 on: May 30, 2011, 07:52:12 AM »
I think this task needs more clarification, but perhaps:

Code: [Select]
(defun num->str ( num )
  (cond
    ( (< 31 num 100) (list (chr num)))
    ( (< 31 num) (append (num->str (/ num 100)) (num->str (rem num 100))))
  )
)

Code: [Select]
_$ (num->str 165478952)
("A" "/" "Y" "4")

myloveflyer

  • Newt
  • Posts: 152
Re: How grouping and Conversion
« Reply #6 on: May 31, 2011, 09:10:10 PM »
Like this ....  :-)

Code: [Select]
(defun c:test (/ ss)
  (if (setq ss (getint "\n Enter Number (ascii) :"))
    (print (chr ss))
    )
  (princ)
  )
Thanks coder,but "enter number"requires an integer between -32768 and 32767.My version is 04 CAD

Not certain to understand the task, but this works with the example.

(foo "165478952") returns ("A" "/" "Y" "4")

Code: [Select]
(defun foo (str / num char lst)
  (if (numberp (setq num (read str)))
    (while (< 9 (setq char (rem num 100)))
      (setq num (/ num 100)
   lst (cons (chr char) lst)
      )
    )
  )
)
thanks,gile,When the input number is 11, when an error。
thanks,LEE,As your programs and gile error
I may be the expression of this program is not clear, I mean that when I enter the number as 11, when, for example: 16738883211, rounding first need to form a list ("67" "38" "88" "32" "11"), the group then identified the corresponding code in general is to enter the figures are rounded down first, and then behind the two figures as a whole, the basic input of the number of digits is 9 or 11, Thank you for your help, hope that help me look,

Never give up !

Lee Mac

  • Seagull
  • Posts: 12910
  • London, England
Re: How grouping and Conversion
« Reply #7 on: May 31, 2011, 09:23:42 PM »
Your figure of 16738883211 exceeds the signed integer limit for 32-bit systems (2147483647 = ((2^32)/2)-1) and is hence converted to a real in LISP.
« Last Edit: May 31, 2011, 09:54:23 PM by Lee Mac »

myloveflyer

  • Newt
  • Posts: 152
Re: How grouping and Conversion
« Reply #8 on: May 31, 2011, 09:41:40 PM »
LEE, you mean 11 out of 32-bit systems has been limited, indeed, as you said the program will error, whether to enter the number from 11 into 9 digits, so I would like to no error Right?
Never give up !

Lee Mac

  • Seagull
  • Posts: 12910
  • London, England
Re: How grouping and Conversion
« Reply #9 on: May 31, 2011, 09:53:52 PM »
LEE, you mean 11 out of 32-bit systems has been limited, indeed, as you said the program will error, whether to enter the number from 11 into 9 digits, so I would like to no error Right?

Use this instead perhaps:

Code: [Select]
(defun num->str ( num )
  (cond
    ( (< 31 num 100) (list (chr (fix num))))
    ( (< 31 num) (append (num->str (/ num 100)) (num->str (rem num 100))))
  )
)

What are you trying to achieve?

myloveflyer

  • Newt
  • Posts: 152
Re: How grouping and Conversion
« Reply #10 on: May 31, 2011, 10:12:33 PM »
(foo "165478952") returns ("A" "/" "Y" "4")

(num->str 165478952) returns ("A" "/" "Y" "4")

I need 11-bit digital input when you are as follows:
(foo "16547895233") returns ("A" "/" "Y" "4" "!")
(num->str 16547895233) returns ("A" "/" "Y" "4" "!")

Never give up !

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How grouping and Conversion
« Reply #11 on: June 01, 2011, 08:41:52 PM »
Would keeping up with a count variable work by taking the substring of count + 2?

I not fimilar enough with lisp but get sslen and while count < slen
do your magic here
« Last Edit: June 01, 2011, 08:55:02 PM by Jeff H »

Lee Mac

  • Seagull
  • Posts: 12910
  • London, England
Re: How grouping and Conversion
« Reply #12 on: June 02, 2011, 09:38:12 AM »
Nice idea Jeff, how about this approach:

Code: [Select]
(defun decode ( x / l )
  (setq x (reverse (vl-string->list x)))
  (while (cadr x)
    (setq l (cons (chr (- (+ (car x) (* 10 (cadr x))) 528)) l) x (cddr x))
  )
  l
)

Code: [Select]
_$ (decode "165478952")
("A" "/" "Y" "4")

Or, using substr (likely to be slower):

Code: [Select]
(defun decode2 ( s / l x )
  (repeat (/ (setq x (strlen s)) 2)
    (setq l (cons (chr (atoi (substr s (setq x (1- x))))) l)
          s (substr s 1 (setq x (1- x)))
    )
  )
  l
)
« Last Edit: June 02, 2011, 10:19:19 AM by Lee Mac »

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How grouping and Conversion
« Reply #13 on: June 02, 2011, 09:55:51 AM »
Nice Lee,

I played with it for a little bit and could'nt get it to work.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How grouping and Conversion
« Reply #14 on: June 02, 2011, 09:56:58 AM »
I seem to be missing a couple things here ...

  • Is the original data correct? i.e. why the superfluous 1 prefix ("165478952")?
  • What assurances does the coder have that ASCII values less than 10 or greater than 99 are not in the string? Subtitle: it would screw up the pairing.
  • What is the context of the challenge?

Mommy I need a coffee. Back to you Jim.
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst