Author Topic: -={ Challenge }=- Titlecase  (Read 14879 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Titlecase
« Reply #45 on: October 10, 2010, 03:32:20 PM »
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7327
Re: -={ Challenge }=- Titlecase
« Reply #46 on: October 10, 2010, 03:40:41 PM »
 :-D

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: -={ Challenge }=- Titlecase
« Reply #47 on: October 10, 2010, 05:00:02 PM »
John I think you are over reacting.

Run you own speed test. 8-)
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.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9718
Re: -={ Challenge }=- Titlecase
« Reply #48 on: October 11, 2010, 08:19:38 AM »
CAB,
Im slightly confused by that statement;
If taken literal: of course. I already have. The test is not the issue.
If taken figuratively: there's no helping not to, but I just cant stand "stupid". ...actually, i am not sure what to make of this whole thread to be honest so `stupid' may not describe "it" well enough or be too harsh;

o  It could be that the OP honestly didnt see the all the different problems and thought a simple "list processing, or mapcar-lambda" formula could handle all the possible string processing involved (different combination's of numeral, chars, hyphens, etc.).

o  It could possibly be that the OP honestly doesnt understand that (mapcar 'func lst) is (salt.grain  -->  you) the fastest and (mapcar '(lambda)) is the second fastest list processing method and therefore creating a challenge thread in the tone of: "here is my second fastest--but only because this task cant be done with the fastest method--solution, lets see yours and race *yeah* and *w00t*" seem(s/d) a bit odd.

NOTES:
1. I said a bit odd because we beat that dead horse only a little bit ago in another thread.
2. I also know that we all stand on the shoulders of giants but I mean, come on.


o  It could possibly be that the OP didnt get or understand that i was trying to divert attention--"make light" may be a better description--of a seemingly (to me? to me only?) odd/weird/crazy/whatever thread (and for what reason? I don't have the foggiest anymore and I don't really care anymore to be honest).

o  Maybe I'm the stupid one and the OP saw all this and more and had a "better reason" that I just cant see.

Whatever. Maybe I need to take a break from the internet. Maybe i need to pull a Homer Simpson and shove a crayon up my nose to be blissful.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Titlecase
« Reply #49 on: October 11, 2010, 08:34:50 AM »
Maybe i need to pull a Homer Simpson and shove a crayon up my nose to be blissful.

let me know if you need a hand  :lol:
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9718
Re: -={ Challenge }=- Titlecase
« Reply #50 on: October 11, 2010, 08:45:29 AM »
Maybe i need to pull a Homer Simpson and shove a crayon up my nose to be blissful.
let me know if you need a hand  :lol:

Oh anytime; I got my pellet gun waitin' by the front door (Halloween is fast approaching and i don't much care for Batman).
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: -={ Challenge }=- Titlecase
« Reply #51 on: October 13, 2010, 07:27:12 AM »
my version
Code: [Select]
(defun ee:TitleCase (s)
 (vl-list->string
  (mapcar
   (function
    (lambda (a b c)
     (if (= a 32) b c)
    )
   )
   (cons 32 (vl-string->list s))
   (vl-string->list (strcase s nil))
   (vl-string->list (strcase s t))
  )
 )
)
Stay home. Stay safe. Save lives.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: -={ Challenge }=- Titlecase
« Reply #52 on: October 13, 2010, 08:24:39 AM »
recursion version, not use vla-*
Code: [Select]
(defun f (s)
 (if (= s "")
  s
  (if (= (ascii s) 32)
   (strcat " " (strcase (substr s 2 1) nil) (f (substr s 3)))
   (strcat (strcase (substr s 1 1) t) (f (substr s 2)))
  )
 )
)
Stay home. Stay safe. Save lives.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: -={ Challenge }=- Titlecase
« Reply #53 on: October 13, 2010, 12:57:10 PM »
A quick test:
Code: [Select]
((lambda (/ s)
  (setq s "test string with several spaces"
        s (repeat 7 (setq s (strcat s " " s)))
  ) ;_  setq
  (princ (strcat "String Length: " (itoa (strlen s)) "\n"))
  (benchmark '((ee:TitleCase s) (LM:TitleCase2 s) (LM:TitleCase3 s) (LM:TitleCase5 s)))
 ) ;_  lambda
)

Code: [Select]
_$
String Length: 4095
Benchmarking ............Elapsed milliseconds / relative speed for 512 iteration(s):

    (EE:TITLECASE S)......1466 / 5.60 <fastest>
    (LM:TITLECASE3 S).....2059 / 3.99
    (LM:TITLECASE2 S).....2418 / 3.39
    (LM:TITLECASE5 S).....8206 / 1.00 <slowest>

 
_$
Stay home. Stay safe. Save lives.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9718
Re: -={ Challenge }=- Titlecase
« Reply #54 on: October 13, 2010, 02:00:55 PM »
Very cool!
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Andrea

  • Water Moccasin
  • Posts: 2372
Re: -={ Challenge }=- Titlecase
« Reply #55 on: October 13, 2010, 04:18:07 PM »
This is my contribution...

this tool also run when there is TAB space..

Code: [Select]

;; By Andrea Andreetti
(defun AA:TITLECASE (str / nstr s F) 
  (setq F t nstr "")
  (repeat (strlen str)
    (setq s (substr str 1 1))
    (setq nstr (strcat nstr (if F (strcase s)(strcase s t))))       
    (if (member s '(" " "\t"))(setq F T)(setq  F nil))
    (setq str (substr str 2))
   )
  nstr
  )
(AA:TITLECASE "weLl therE wE Go witH my aTtemPt")
Keep smile...

Andrea

  • Water Moccasin
  • Posts: 2372
Re: -={ Challenge }=- Titlecase
« Reply #56 on: October 13, 2010, 04:26:49 PM »
recursion version, not use vla-*
Code: [Select]
(defun f (s)
 (if (= s "")
  s
  (if (= (ascii s) 32)
   (strcat " " (strcase (substr s 2 1) nil) (f (substr s 3)))
   (strcat (strcase (substr s 1 1) t) (f (substr s 2)))
  )
 )
)

this is cool !

but...you miss the first caracter.    :kewl:
Keep smile...

Andrea

  • Water Moccasin
  • Posts: 2372
Re: -={ Challenge }=- Titlecase
« Reply #57 on: October 13, 2010, 05:39:37 PM »
another approach..

Code: [Select]
(defun AA:Titlecase (str)
(setq str (strcase str t))
(foreach n (mapcar 'vl-princ-to-string '(a b c d e f g h i j k l m n o p q r s t w x y z))
(setq str (acet-str-replace (strcat " " n) (strcat " " (strcase n)) str T)))
(vl-string-subst (strcase (substr str 1 1)) (substr str 1 1) str)
)
Keep smile...

alanjt

  • Needs a day job
  • Posts: 5340
  • Standby for witty remark...
Re: -={ Challenge }=- Titlecase
« Reply #58 on: October 13, 2010, 05:45:47 PM »
Late to the ball, but here's my contribution:
Code: [Select]
(defun AT:TitleCase (s / l)
  (vl-list->string
    (mapcar (function (lambda (a b)
                        (if (vl-position a '(9 32))
                          (cond ((<= 65 b 90) b)
                                ((<= 97 b 122) (- b 32))
                                (b)
                          )
                          (cond ((<= 65 b 90) (+ b 32))
                                ((<= 97 b 122) b)
                                (b)
                          )
                        )
                      )
            )
            (cons 32 (setq l (vl-string->list s)))
            l
    )
  )
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

cmwade77

  • Swamp Rat
  • Posts: 1278
Re: -={ Challenge }=- Titlecase
« Reply #59 on: October 15, 2010, 12:08:16 PM »
Here is my entry:
Code: [Select]
;N = Number of characters in a word that are required before the word is captilized. Specify 0 to cpatilize all words.
(defun TitleCase (s N / Title Ct)
(setq ct 1)
(setq nxtspc (vl-string-search " " s ct))
(setq Title (strcat (strcase (substr s ct 1)) (substr s (+ ct 1) nxtspc)))
(setq ct (+ nxtspc 1))
(while (and (< CT (strlen s)) (< nxtspc (strlen s)))
(setq nxtspc (vl-string-search " " s ct))
(cond
((= nxtspc nil)
(setq nxtspc (strlen s))
)
)
(cond
((> (- nxtspc ct) N)
(setq Title (strcat Title (strcase (substr s (+ ct 1) 1)) (substr s (+ ct 2) (- nxtspc ct))))
)
(T
(setq Title (strcat Title (substr s (+ ct 1) (- nxtspc ct))))
)
)
(setq CT (+ nxtspc 1))
)
Title
)
I do not know how well this will hold up in the benchmarks, but it works and it works with numbers in the string, as well as being able to specify a word length that is needed to capitalize the word.