Programming Challenge

Programming Challenge

john.uhden
Mentor Mentor
8,302 Views
91 Replies
Message 1 of 92

Programming Challenge

john.uhden
Mentor
Mentor

It is claimed that the sentence "The quick brown fox jumped over the lazy dog." contains at least one of all the letters in the English alphabet.  Case doesn't matter.

Your challenge is to post a function that proves or disproves that claim.  It should take the sentence as a string and return T or nil, e.g.

(defun true? (str)

  (and

    (= (type str) 'STR)

    (vl-yes-they-are-all-there str)

  )

)

Have fun, OR ELSE!

John F. Uhden

Replies (91)
Message 21 of 92

hak_vz
Advisor
Advisor
Accepted solution

@john.uhden wrote:
@hak_vz
It hurts me to say this to someone of your imminent eminent stature, but
take another look at Doug's.

@john.uhden

First of all your idea with the challenge is great and we can make it regularly, let say once a week or whatever suitable. I know that my code is not the best from perspective of lisp alike programming languages and their brutally simple one line solutions, but I have made it this way intentionally. It is always a challenge among us who will create shortest and more stunning solution. This way here we have different solutions that can be of help to beginners and I think that has to be primary purpose of this forum. In reality we have tons of code hidden in depths of this forum that are of little educational use (those who search will find what needed).

For next challenge I would like to see codding tryouts from new forum members and we can choose best solution among them.

I agree, @dbroad  has provided excellent solution. Also I'm happy to see new post from @doaiena  whose solutions were always top notch and I remember how we used to challenge ourselves who will provide better solution and grasp solution point. Hope to see him at this forum more often! 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 22 of 92

john.uhden
Mentor
Mentor
Well, leave it to me to be the forum wise-@$$.
I think the primary purpose is to have fun.
I miss the old Compuserve days of "Take Five."

John F. Uhden

Message 23 of 92

hak_vz
Advisor
Advisor
Accepted solution

Here is my digression about what @dbroad stated in post #21, about avoiding or minimizing  of variable usage. 

Peter Norvig, a highly respected name in Lisp communities, has stated many times that directly passing result from one function to another is one of the best ways to shorten execution times of Lisp programs. He also stated that in its essence Lisp code is supposed to be written that way. Less time we use variables, memory usage is more effective and also garbage collector is lot less polluted. Historically, when amounts of available memory were scarce, it has had huge impact on execution time. Drawback of this code style is harder code readability for less experienced programmers. 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 24 of 92

john.uhden
Mentor
Mentor
@hak_vz
That's good to know.
I just like making separate functions to perform specific tasks mainly
because I find it easier to grasp the entire structure. Plus, you can snag
functions from one file and put them in another, or better yet have a base
file that loads them all. It's a far cry from my early Basic days when my
code was filled with line numbers and all I knew was "GOTO."

John F. Uhden

0 Likes
Message 25 of 92

hak_vz
Advisor
Advisor

@john.uhden

GOTO statements are typical to Basic, Fortran, Cobol, and Pascal. Since I have being studying Informatics for three years, I have had opportunity to work with all of them  on PC's and large system like Digital VAX 700. I remember that one of criteria for better grade was having a code with less  GOTO statements. It affects speed of code execution, but provides better code readability.      

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 26 of 92

Paul_Gander
Advocate
Advocate
Accepted solution
(defun vl-yes-they-are-all-there (str)
  (numberp
    (vl-string-search
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      (vl-list->string (vl-sort (vl-string->list (strcase str)) '<))
    )
  )
)
Message 27 of 92

john.uhden
Mentor
Mentor
That is VERY clever as well!
1. No locals.
2. Taking full advantage of vl- functions.
3. Short and sweet.
4. A uniquely named function. 😁
5. A novel approach.

Where have you been hiding?

The only problem we have now is that I won't be getting together with the
other judges (3/4 of my granddaughters 4-14) until Thanksgiving. 😕
The 12-year-old is the smartest, but the 14-year-old has a best friend who
thinks I'm funny. Maybe I can substitute her for my absent 4th (the oldest
at almost 16). That's okay 'cause next year there will be 5
granddaughters. The one on the way will have about 7 months to catch up,
except she might be a he. We don't want to know until it happens.

John F. Uhden

0 Likes
Message 28 of 92

pbejse
Mentor
Mentor
Accepted solution

@john.uhden wrote:
;; but I've never figured out how to use vl-every

(Defun _MomsSpeedTypingTest (Str)  
 (setq str (strcase str))
   	(vl-every
	  (function (lambda (l)
		(vl-string-search l str) 
		      )
	    )
	    '("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N"
	      "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z")
	  )
	)

A variation of Dbroads contribution, removing conversion of string to a list

(defun _Allin (str / n)
  (setq	n 64 str (strcase str)
  )
  (while (and (< (setq n (1+ n)) 91)
	      (vl-string-position n str)
	 )
    )
    (= n 91)
  )

 

Message 29 of 92

Sea-Haven
Mentor
Mentor

Hmm "GOTO" does not exist in Autocad does in Word. Does for me.

 

; GOTO layout just like other goto a page
; By Alan H 2013
; menu command [GOTO]^c^C^p(load "goto")
; enter a big number like 99 to jump to last if 100+ layouts then 123 etc
; Model space is 0 zero GOTO 0

(defun C:goto ( / x alllayouts laynum num)
 (if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq num (atoi (nth 0  (AH:getvalsm (list "Go To A Layout" "Enter layout number" 5 4 "1")))))
(setq alllayouts (vla-get-Layouts (vla-get-activedocument (vlax-get-acad-object))))
(SETQ LAYNUM 0)
(vlax-for x alllayouts
(Setq laynum (+ 1 laynum))
) ;total number of layouts
(if (> num laynum)
(setq num (- laynum 1))
)
(vlax-for lay alllayouts
(if (= num (vla-get-taborder lay))
(setvar "ctab" (vla-get-name lay))
)
)
)

; I often type it wrong so added this one also
(defun C:goot ()
(c:goto)
) ; defun
Message 30 of 92

john.uhden
Mentor
Mentor

@pbejse 

Ah, another country heard from.  I'm sure that @dbroad is flattered.

I think that's the first vl-string-position in this thread.

Could your Mom's version be just...

(Defun _MomsSpeedTypingTest (Str)  
  (vl-every
    (function (lambda (l)(vl-string-search l (strcase str))))
     '("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N"
       "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z")
    )
  )
)

 without losing too much speed?

AND thanks for the vl-every lesson!

Of course it's very much akin to something like

(defun maybe (str)
  (= 26 (length (vl-sort (vl-remove-if-not '(lambda (x)(< 64 x 91)) (vl-string->list (strcase str))) '<)))
)

John F. Uhden

Message 31 of 92

-didier-
Advisor
Advisor
Accepted solution

Bonjour @john.uhden 

 

Sorry for the delay of response but I had not seen this message, it is a friend in France, who was surprised not to see the French answer that alerted me.

Here is my proposed response for the audit of pangrams.

I may not have the efficiency of certain champions, but it does the job, I think I’m in the right.

At the beginning of the code I put examples in French, I deleted the accents because it complicates still.

Amicalement

;(setq str "The quick brown fox jumps over the lazy dog")
;(setq str "Portez ce vieux whisky au juge blond qui fume")
;setq str "Monsieur Jack vous dactylographiez bien mieux que Wolf")
(setq str "Zut je crois que le chien Sambuca prefere le whisky revigorant au doux porto")

(defun pangram (str / lm n str0 test)
(setq str0 (vl-list->string (vl-remove 32 (vl-string->list (strcase str T)))))
(setq n 97)
(repeat 26
  (setq test (vl-position n (vl-string->list str0)))
  (if (not test)
    (setq lm (cons (chr n) lm))
    )
  (setq n (1+ n))
  )
  (setq aa lm)
  
  (if (/= lm nil)
    (progn
      (prompt (strcat "The string <" str "> is not a pangram\n"))
      (foreach letter aa
        (prompt (strcat "missing letter : " letter "\n"))
        )
      )
    (prompt (strcat "The string <" str "> is a correct pangram"))
    )
  (princ)
  )

 

 

Éternel débutant.. my site for learning : Programmer dans AutoCAD

DA

EESignature

Message 32 of 92

john.uhden
Mentor
Mentor
@Anonymous
I had not known of the word "pangram."
>From Google:
"A pangram or holoalphabetic sentence is *a sentence using every letter of
a given alphabet at least once*."

In fact I had never heard of the word "holoalphabetic" before.
I'll have to try to translate your French pangrams into English.
Ahah! "A hot blonde whiskey drinking judge." Just my type. 😏

You and Wikipedia claim there are 26 letters (based on the Latin alphabet),
but how do you handle the accent aigu and accent grave?

John F. Uhden

Message 33 of 92

-didier-
Advisor
Advisor

Bonjour @john.uhden 

I took the accents out of my pangram because it complicates the choices, but it’s doable.

The definition of pangrams does not specify all the characters but all the letters of the alphabet.
Amicalement

 

Éternel débutant.. my site for learning : Programmer dans AutoCAD

DA

EESignature

0 Likes
Message 34 of 92

john.uhden
Mentor
Mentor
@Anonymous
Ah, characters vs.letters. That definition makes our work a little easier.
Merci!

John F. Uhden

0 Likes
Message 35 of 92

-didier-
Advisor
Advisor

Bonjour @john.uhden 

 

I didn’t try to make it easy, I made it a little easier.
With accents, pangrams in French would be much more complicated.
Thank you for launching this challenge, it was a good exercise.

I did not answer perfectly because I do not return T or nil.
I chose to highlight the missing letter or letters in case of error in the pangram to be tested.

 

Amicalement

Éternel débutant.. my site for learning : Programmer dans AutoCAD

DA

EESignature

0 Likes
Message 36 of 92

braudpat
Mentor
Mentor
Accepted solution

Hello @john.uhden 

 

https://cadxp.com/topic/53474-challenge-lisp-lance-par-john-uhden/

 

A French Developper "VDH-Bruno" who has not an Autodesk Account, has written 2 Lisp routines ...

 

The Health, Bye, Patrice

 

(defun test2 (str)
  (= 26 (length (member 90 (reverse (member 65 (vl-sort (vl-string->list (strcase str)) '<))))))
)

 

(defun vl-yes-they-are-all-there (str / f)
  (defun f (l m)
    (if	l
      (f (vl-remove (car l) (cdr l)) (vl-remove (car l) m))
      m
    )
  )
  (not(f (vl-string->list (strcase str)) '(65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90)))
)

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


Message 37 of 92

john.uhden
Mentor
Mentor
@Anonymous
Your French fellow should join our group if he can write in English as well
as you.
You can never have too much talent at hand.

John F. Uhden

0 Likes
Message 38 of 92

john.uhden
Mentor
Mentor

@hak_vz 

That's a great idea about having new (or somewhat silent) participants/lurkers initiate a challenge.

I could pose hundreds of challenges, but that would get boring really fast... "Oh no, not Uhden again."

I just would prefer not to see the words "I need."

We have had some challenging requests, like the one to find the shortest route from A to B through random points (or circles or something).  Those are fun.

I actually have two in mind right now, but I am hesitant.  One of them is probably very complex.

John F. Uhden

0 Likes
Message 39 of 92

Sea-Haven
Mentor
Mentor

Go on post a new one never know if it exists remember doing a bubblesort many years ago in lisp, before Vl-sort, I stll have problems with strings with mix of alpha and numbers sorting correctly.

 

0 Likes
Message 40 of 92

hak_vz
Advisor
Advisor
Accepted solution

@john.uhden  I hope you will like this.

(defun hvz_str (str / bucket)
	(foreach x (vl-string->list (strcase str))
		(cond
			((< 64 x 91)
				(cond 
					((not bucket) (setq bucket (cons x bucket)))
					((not (member x bucket))(setq bucket (cons x bucket)))
				)
			)
		)
	)
	(= 26 (length bucket))
)
Command: (setq str_a "The quick brown fox jumps over the lazy dog")
Command: (hvz_str str_a)
T
Command: (setq str_b "The quick brown fox jumped over the lazy dog")
Command: (hvz_str str_b)
nil

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.