+ Reply to Thread
Results 1 to 8 of 8

Thread: List elements

  1. #1
    Full Member
    Using
    AutoCAD 2011
    Join Date
    Aug 2008
    Posts
    36

    Default List elements

    Registered forum members do not see this ad.

    Greetings to all!
    Code:
     
    (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
    Code:
    (8 1)
    (8 2)
    (8 3)
    (8 4)
    (8 5)
    (8 6)
    (8 7)
    (8 8) No duplicate
    (8 9)
    Number of combination:>>>> ?
    Three elements with two keynumber
    Code:
    (8 2 1)
    (8 2 2) No duplicate
    (8 2 3)
    (8 2 4)
    (8 2 5)
    (8 2 6)
    (8 2 7)
    (8 2 8) No duplicate
    (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.

  2. #2
    Super Member fixo's Avatar
    Computer Details
    fixo's Computer Details
    Operating System:
    Windows 7
    Motherboard:
    E7500
    CPU:
    Intel(R)Core(TM)2 DUO CPU 2.93HGz
    RAM:
    4098 Gb
    Graphics:
    1024 Gb
    Using
    AutoCAD 2009
    Join Date
    Jul 2005
    Location
    Pietari, Venäjä
    Posts
    1,587

    Default

    Seems to be working for me

    Code:
    (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'~
    The soul is healed by being with children. - Fyodor Dostoyevsky, novelist (1821-1881)

  3. #3
    Senior Member
    Computer Details
    jammie's Computer Details
    Operating System:
    Ubuntu / XP / W8
    Discipline
    Civil
    jammie's Discipline Details
    Occupation
    Design Technician
    Discipline
    Civil
    Using
    AutoCAD 2012
    Join Date
    May 2006
    Location
    Ireland
    Posts
    299

    Default

    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


    Code:
    ;>>>>>>>>>>>>>>>>>>>>>>>	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

  4. #4
    Super Member fixo's Avatar
    Computer Details
    fixo's Computer Details
    Operating System:
    Windows 7
    Motherboard:
    E7500
    CPU:
    Intel(R)Core(TM)2 DUO CPU 2.93HGz
    RAM:
    4098 Gb
    Graphics:
    1024 Gb
    Using
    AutoCAD 2009
    Join Date
    Jul 2005
    Location
    Pietari, Venäjä
    Posts
    1,587

    Default

    Thanks, Jammie
    Regards,
    Oleg

    ~'J'~
    The soul is healed by being with children. - Fyodor Dostoyevsky, novelist (1821-1881)

  5. #5
    Full Member
    Using
    AutoCAD 2011
    Join Date
    Aug 2008
    Posts
    36

    Default

    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.

  6. #6
    Super Member CAB's Avatar
    Using
    AutoCAD 2000
    Join Date
    May 2004
    Location
    Tampa, Florida
    Posts
    801

    Default

    Not sure I understand the rules.
    Code:
    (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)
    )

  7. #7
    Full Member
    Using
    AutoCAD 2011
    Join Date
    Aug 2008
    Posts
    36

    Default

    The end result of the code should print out like this:
    Code:
     
    (8 1)
    (8 2)
    (8 3)
    (8 4)
    (8 5)
    (8 6)
    (8 7)
    (8 9)
    Number of combination:>>>> 8
    or like this:
    Code:
     
    ((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.

  8. #8
    Super Member CAB's Avatar
    Using
    AutoCAD 2000
    Join Date
    May 2004
    Location
    Tampa, Florida
    Posts
    801

    Default

    Registered forum members do not see this ad.

    Trying again.
    Code:
    (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)
    )

Similar Threads

  1. drawing contains authoring elements...
    By Little Bandit in forum AutoCAD General
    Replies: 6
    Last Post: 17th Sep 2010, 12:25 pm
  2. change an elements layer
    By studiorat in forum AutoCAD General
    Replies: 8
    Last Post: 3rd Jan 2008, 05:57 pm
  3. adding new elements to blocks
    By Bogbadbob658 in forum AutoCAD General
    Replies: 7
    Last Post: 3rd Nov 2007, 01:09 pm
  4. Finite elements analysis
    By calcador in forum AutoCAD Drawing Management & Output
    Replies: 1
    Last Post: 24th Mar 2007, 01:56 pm
  5. Moving 3D elements
    By panfield in forum AutoCAD Drawing Management & Output
    Replies: 3
    Last Post: 4th Feb 2007, 05:31 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts