T2L Posted August 11, 2008 Posted August 11, 2008 Greetings to all! (setq mylist '(1 2 3 4 5 6 7 8 9)) (setq key#1 (getint "\nEnter first key number >>>>>: ")) ;(setq key#2 (getint "\nEnter second key number >>>>>: ")) (setq no 0) (repeat (length mylist) (setq keyno (list key#1)) (setq combo (reverse (cons (nth no mylist) keyno))) (princ combo) (princ "\n") (setq no (1+ no)) ) (princ) Please improve the above code with the following conditions: a) re-enter number if the keynumber is not in the list. b) if the list is not in order the result should be in order. If necessary add code to re-arange the list first. c) remove duplicate in the result. d) ask another keynumber for the second elements. If no second keynumber entered just perform one keynumber. Again no duplicate result. The result of the code should be the following: Two elements with one keynumber (8 1) (8 2) (8 3) (8 4) (8 5) (8 6) (8 7) (8 [color=red]No duplicate[/color] (8 9) Number of combination:>>>> ? Three elements with two keynumber (8 2 1) (8 2 2) [color=red]No duplicate[/color] (8 2 3) (8 2 4) (8 2 5) (8 2 6) (8 2 7) (8 2 [color=red]No duplicate[/color] (8 2 9) Number of combination:>>>> ? Thank you in advance to all would contributor. If you have any thoughts about the matter, please feel free to share it, or if you have any sample code to share about LIST . Again thank you. Quote
fixo Posted August 11, 2008 Posted August 11, 2008 Seems to be working for me (setq mylist '(1 2 3 4 5 6 7 8 9)) (setq key#1 (getint "\nEnter first key number >>>>>: ") key#2 (getint "\nEnter second key number >>>>>: ") ) (setq comb_list (vl-remove-if (function (lambda (a) (or (equal (cadr a) (caddr a) 0.00001) (equal (car a) (caddr a) 0.00001)))) (mapcar (function (lambda (x) (list key#1 key#2 x))) mylist) ) ) (setq num_of_combinations (length comb_list)) (alert (strcat "Number of combinations is " (itoa num_of_combinations))) ~'J'~ Quote
jammie Posted August 12, 2008 Posted August 12, 2008 Hey T2L Here is my version, took a slightly different approach. I am sure I could be trimmed down a little but it seems to meet your requirements Fixo Its very useful seeing FUNCTION MAPCR AND LAMBDA Anyhow here you go ;>>>>>>>>>>>>>>>>>>>>>>> QL V0 <<<<<<<<<<<<<<<<<<<<<<<<; ; BY : Jammie ; ; DATE : 2008-08-11 ; ; MAIN FUNCTION ; (defun QL-V0 (/ mylist temp_list final_list test str key_nr count) ; local variables ; (setq mylist '(1 2 3 4 5 6 7 8 9)) ;> add or remove intergers as required from mylist eg (1 2 3 4 5 6 7 8 9 10 11 n....) ; ; (setq temp_list nil ;>used to store valid user inputs final_list nil test t ;Test expression ) ; local variables ; (setq key_nr nil ) ;> Change key_nr variable nil to restrict number of inputs, default nil means no restriction ;>eg to restrict it to two key numbers change from nil to 2 ;> (setq key_nr 2) (while test ;:> Function will loop while test is true ; Get Input ; (setq str (getint "\n\tType number or Enter to continue >>>>>: ")) ; First check ; (if (member str mylist) ;:> If the user input is a member of the mylist (if ;:>> & (not (member str temp_list)) ;>>> If the input it has not been stored in a temp_list (progn (setq temp_list (append temp_list (list str))) ;>>> Append it & store it in the temp_list (princ temp_list) ;>>> Print the stored values );progn (princ "\n\tNumber has already been entered >>>>>: ") ;>>> If it has been stored promt the user );if (princ "\n\tNot a valid entry >>>>>: ") ;:>> If the input cannot be found in mylist promt user );if ; Key Number ; (if key_nr ;> If the user changed the key_nr variable from nil (if (= (length temp_list) key_nr) ;:>> Check the number of valid inputs againts those allowed (setq test nil) ;>>If the maximum is reached set the test variable to nil to end loop ) );if (if (= str nil) ;>if user hits enter, getint returns nil (setq test nil) ;>>Set test variable to nil to end loop );if );while ; Start of list sort ; (if temp_list ;> If any valid inputs have been saved in temp_list (progn;>do the following (setq nr (- (length mylist) (length temp_list)));>Number of combinations ; compare mylist with temp_list ; (foreach n mylist (if (not (member n temp_list));>> if anyitem in the mylist has not been entered (setq final_list (append final_list (list n))) ;>> List );if );foreach (if final_list ;>If any diffenences have been found (progn (foreach n final_list (progn (terpri);force new line (princ (append temp_list (list n)))) );foreach (PRINC (STRCAT "\n\tNumber of combination :>>>> \t" (rtos (length final_list) 2 0))) (princ) );progn (progn (princ "\n\tNothing to print as all possible numbers have been entered... : >>>>>: ") (princ) );progn );if );progn (progn (princ "\n\tExiting programme... : >>>>>: ") (princ) ) );if ) (defun c:ql () (QL-V0) ) (progn (princ (strcat "\t\nQL V0 LOADED... \t\nType QL to begin...")) (princ) ) Regards, Jammie Quote
T2L Posted August 12, 2008 Author Posted August 12, 2008 Fixo/Jammie: Both approach are excellent!! Jammie: If you are going to trim it down, I would like to suggest to make the mylist variable to be global. The example I've shown is just arbitrary. I would like to see more different take from others. Thanks. Quote
CAB Posted August 13, 2008 Posted August 13, 2008 Not sure I understand the rules. (defun c:testCAB (/ mylist key#1 key#2 cnt) (setq mylist '(1 2 3 4 5 6 7 8 9)) (setq key#1 (getint "\nEnter first key number >>>>>: ") key#2 (getint "\nEnter second key number >>>>>: ") ) (and (= key#1 key#2) (setq key#2 nil)) (setq cnt 0) (mapcar '(lambda (key) (mapcar '(lambda (x) (if (equal key x 0.00001)(setq cnt (1+ cnt)))) mylist)) (list key#1 key#2)) (or (and (zerop cnt) (not(alert "No combonations."))) (alert (strcat "Number of combinations is " (itoa (- (length mylist) cnt)))) ) (princ) ) Quote
T2L Posted August 13, 2008 Author Posted August 13, 2008 The end result of the code should print out like this: (8 1) (8 2) (8 3) (8 4) (8 5) (8 6) (8 7) (8 9) Number of combination:>>>> 8 or like this: ((8 1) (8 2) (8 3) (8 4) (8 5) (8 6) (8 7) (8 9)) Number of combination:>>>> 8 mylist should be global variable. Thanks. Quote
CAB Posted August 13, 2008 Posted August 13, 2008 Trying again. (defun c:combos (/ key#1 key#2 result) (setq key#1 (getint "\nEnter first key number >>>>>: ") key#2 (getint "\nEnter second key number >>>>>: ") ) (setq cnt 0) (mapcar '(lambda (x) (if (not(or (equal key#1 x 0.00001) (equal key#2 x 0.00001))) (setq result (cons x result)) )) mylist) (or (and (null result) (not(alert "No combonations."))) (and (princ (strcat "\nNumber of combonations is " (itoa (length result))"\n")) (princ (reverse result))) ) (princ) ) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.