T2L 发表于 2022-7-6 17:31:16

列出元素

大家好!

(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)

请在以下条件下改进上述代码:
a) 如果键号不在列表中,请重新输入数字。
b) 如果列表未按顺序排列,则结果应按顺序排列。如有必要,首先添加代码重新排列列表。
c) 删除结果中的重复项。
d) 为第二个元素询问另一个键号。如果没有输入第二个keynumber,只需执行一个keynumber。
同样没有重复结果。
 
代码的结果应如下所示:
具有一个键号的两个元素
(8 1)
(8 2)
(8 3)
(8 4)
(8 5)
(8 6)
(8 7)
(8No duplicate
(8 9)
Number of combination:>>>> ?具有两个键号的三元素
(8 2 1)
(8 2 2) No duplicate
(8 2 3)
(8 2 4)
(8 2 5)
(8 2 6)
(8 2 7)
(8 2No duplicate
(8 2 9)
Number of combination:>>>> ?
 
提前感谢所有愿意捐款的人。
如果您对这件事有任何想法,请随时分享,或者如果您有任何关于列表的示例代码可以分享。
再次感谢你。

fixo 发表于 2022-7-6 17:42:48

似乎在为我工作
 

(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'~

jammie 发表于 2022-7-6 17:55:11

嘿T2L
 
这是我的版本,采取了稍微不同的方法。我相信我可以被裁减一点,但这似乎符合你的要求
 
Fixo公司
 
它非常有用的函数MAPCR和LAMBDA
 
无论如何,给你
 
 
;>>>>>>>>>>>>>>>>>>>>>>>        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_listnil        ;>used to store valid user inputs
   final_list nil
   test       t        ;Test expression
   )
   
;                                local variables                                ;

(setqkey_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)
)

       

fixo 发表于 2022-7-6 18:05:01

代码的最终结果应打印如下:
(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)
)
或者像这样:

(8 1)
(8 2)
(8 3)
(8 4)
(8 5)
(8 6)
(8 7)
(8 9)
Number of combination:>>>> 8
mylist应该是全局变量。
 
谢谢

T2L 发表于 2022-7-6 18:06:05

再试一次。

((8 1) (8 2) (8 3) (8 4) (8 5) (8 6) (8 7) (8 9))
Number of combination:>>>> 8

CAB 发表于 2022-7-6 18:21:11

T2L 发表于 2022-7-6 18:27:43

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.

CAB 发表于 2022-7-6 18:38:45

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))
页: [1]
查看完整版本: 列出元素