translate python to autolisp

translate python to autolisp

xuantientutungXUK6E
Advocate Advocate
2,386 Views
16 Replies
Message 1 of 17

translate python to autolisp

xuantientutungXUK6E
Advocate
Advocate

hello everyone,

i try to understand those code in python and translate it to auto lisp .

those code are to find subset which sum equal to an K

here are code in python

thank you

def subset_sum(numbers, target, partial=[]):    s = sum(partial)

    # check if the partial sum is equals to target
    if s == target: 
        print "sum(%s)=%s" % (partial, target)
    if s >= target:
        return  # if we reach the number why bother to continue

    for i in range( len (numbers)):        n = numbers[i]        remaining = numbers[i+1:]        subset_sum(remaining, target, partial + [n]) 


if __name__ == "__main__":    subset_sum([3,9,8,4,5,7,10],15)

    #Outputs:
    #sum([3, 8, 4])=15
    #sum([3, 5, 7])=15
    #sum([8, 7])=15
    #sum([5, 10])=15
0 Likes
2,387 Views
16 Replies
Replies (16)
Message 2 of 17

hak_vz
Advisor
Advisor
(defun sums (l target / i ret  comb co ncr *error*)
(defun *error* ()(princ))
(defun nCr ( l r ) ;https://www.cadtutor.net/forum/topic/59422-combinations-without-repetition-3-and-4-element/ (cond ( (< r 2) (mapcar 'list l) ) ( l (append (mapcar '(lambda ( x ) (cons (car l) x)) (nCr (cdr l) (1- r))) (nCr (cdr l) r) ) ) ) ) (setq i 1 comb (list)) (while (<= i (length l)) (setq comb (append comb (nCr l i)) i (+ i 1)) ) (setq i 0 ) (while (< i (length comb)) (setq co (nth i comb)) (if (and (>= (length co) 1)(= (apply '+ co) target))(setq ret (cons co ret))) (if (and (= (length co) 1)(= (car co) target))(setq ret (cons co ret))) (setq i (+ i 1)) ) ret );defun

(sums '(1 2 3 4 5 7 8 9) 18)
((1 2 3 5 7) (1 2 3 4 8) (2 4 5 7) (2 3 5 8) (2 3 4 9) (1 4 5 8) (1 3 5 9)
(1 2 7 8) (4 5 9) (3 7 8) (2 7 9) (1 8 9))

(sums '(2 3 4 5 7 9) 11)
((2 4 5) (4 7) (2 9))

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 3 of 17

hak_vz
Advisor
Advisor

Forget my first post. After optimization finale code is:

(defun sums (l target / i ret  comb co)
(defun nCr ( l r )
;https://www.cadtutor.net/forum/topic/59422-combinations-without-repetition-3-and-4-element/
   (cond
       (   (< r 2)
           (mapcar 'list l)
       )
       (   l
           (append
               (mapcar '(lambda ( x ) (cons (car l) x)) (nCr (cdr l) (1- r)))
               (nCr (cdr l) r)
           )
       )
   )
)

(setq i 1 comb (list))
(while (<= i (length l))
(setq comb (nCr l i))
(foreach co comb (if (= (apply '+ co) target)(setq ret (cons co ret))))
(setq i (+ i 1))
)
ret
)

;(sums '(1 2 5 3 9 8 7) 21) --> ((1 2 3 8 7) (2 3 9 7) (1 3 9 8) (1 5 8 7) (5 9 7))
;(sums '(1 2 5 3 9 8 7) 9) --> ((1 5 3) (2 7) (1 8) (9))

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 4 of 17

marko_ribar
Advisor
Advisor

@hak_vz, this reminds me on my attempt to solve math problems with combinations... Can you look into this code :

http://www.theswamp.org/index.php?topic=55612.msg597546#msg597546

And see if you can find happier solution - the trick is that initial list can be very long length, and version I coded works only up to where strings increment reach "zzz" or "zzzz" or "zzzzz" - I am not sure, never really tested it, but I suppose there is happier solution for length of initial list of eigenvalues up to n and n converges to infinity...

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 5 of 17

hak_vz
Advisor
Advisor

I'll take a look to this problem at TheSwamp. If I find better solution I'll post it there.

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 6 of 17

marko_ribar
Advisor
Advisor

@hak_vz wrote:

I'll take a look to this problem at TheSwamp. If I find better solution I'll post it there.


Thanks Milenko, I think I already solved it with Lee's sub... It's posted there at TheSwamp...

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 7 of 17

hak_vz
Advisor
Advisor

If you ever decide to solve that problem with QR- decomposition,  here you have my matrix library that incorporates it. I'll have to force myself to continue work on it.

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 8 of 17

marko_ribar
Advisor
Advisor

Thanks for the link... I think I already downloaded that lisp, but I am afraid I don't quite understand that much math like you... It is that problems I run into, forces me to study it more... You know, we studied math only lightly, there were no www access at the time I went to school, and I dedicated myself to architecture - I am an architect now... Yes I am saying that I have the need by my job to know much more, but time limits us all... And I program for a hobby as I am unemployed and have only rarely something concrete... As for QR-decomposition, I only stated that codes done by user chlh_jd at theswamp don't work for me, so I decided to clean codes from there and for problem of eigenvalues and eigenvectors I found solution by inspecting f(det mat) = 0.0... I know that those eigenvalues are real numbers and complex polynomia can have both real roots and complex also; So if you will there is space for improvements always... (collect them all both real and complex both eigenvalues and their eigenvectors)... It's up to good will and time we all spend on some tasks we work on daily basis that we don't have too much time for solving all things... Science seeks for more dedications and experimentation that I am not so happy to accept for now... We all live only one life and IMHO we should try to be happy, have a joy and life full of good events and not only occupation by problems and working activities that chains us to sit in the chair in front of PC or TV... But we are all different and I admire people like you and many others that are trying to help and solve tasks using experiences from professions and life in order to make things better and more advanced for future events, struggling with today problems that can benefit, if correctly solved, to better life of generations to come yet...

 

P.S. Ovo nisam morao da pisem na engleskom, ali posto je forum internacionalni, malo vezbanja gramatike i spelovanja...

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 9 of 17

hak_vz
Advisor
Advisor

Marko, nadam se da ćeš uskoro riješiti problem zaposlenja. Ovo što si napisao sve stoji, a i vježbu engleskog jezika si dobro odradio.  Pozdrav!

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 10 of 17

Anonymous
Not applicable

Sir ,

      Kindly share an auto lisp for the following requirements

 I want to copy a text and increment in the last integers when i place that in other locations

These procedures  need to be fulfilled 

1. When i copy a string it will copy the all properties of the source string like text height ,color ,style of the font and            UCS  co-ordination and everything

2. It has to check integers are there in the string from the last characters.

3. if numbers are there it count the number of digits present in the string from right to left. If it finds the any characters other than numbers it should stop the checking and copy the remaining strings as it is.

4. It should increment the last digit by 1 ,after 9 subsequent tens , hundreds and etc. while placing anywhere in the drawing.

Hope you understand my requirements.

Thanks in advance

0 Likes
Message 11 of 17

Anonymous
Not applicable

Dear sir,

             Even codes are also ok for me.

0 Likes
Message 12 of 17

Anonymous
Not applicable
Dear Sir ,

Kindly share an auto lisp for the following requirements

I want to copy a text and increment in the last integers when i place that in other locations

These procedures need to be fulfilled

1. When i copy a string it will copy the all properties of the source string like text height ,color ,style of the font and UCS co-ordination and everything

2. It has to check integers are there in the string from the last characters.

3. if numbers are there it count the number of digits present in the string from right to left. If it finds the any characters other than numbers it should stop the checking and copy the remaining strings as it is.

4. It should increment the last digit by 1 ,after 9 subsequent tens , hundreds and etc. while placing anywhere in the drawing.

Hope you understand my requirements.

Thanks in advance
0 Likes
Message 13 of 17

hak_vz
Advisor
Advisor

@Anonymous 

Start new post to this forum with details about what you want to achieve. Attached sample drawing or at least some image with states before and after for easier understanding.

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 14 of 17

Anonymous
Not applicable

Dear sir,

           ok . but how could i find you in the open forum. There are number of Auto lisp for Numeric  Increment. But when i use to insert, the text height and layer, color , Font style will differ. So if i will type the text Like L01/D001 or L05/D032 and i want to copy the text and when i insert it should come  like  L01/D002,L01/D003 for every click UNTIL ESCAPE BUTTON PRESS .For the L05/D032 it should come like L05/D033,L05/D034 and etc for every click . The inserted incremented text should be same of the source text properties

0 Likes
Message 15 of 17

Anonymous
Not applicable

Dear sir,

           ok . but how could i find you in the open forum. There are number of Auto lisp for Numeric  Increment. But when i use to insert, the text height and layer, color , Font style will differ. So if i will type the text Like L01/D001 or L05/D032 and i want to copy the text and when I insert it should come  like  L01/D002,L01/D003 for every click UNTIL ESCAPE BUTTON PRESS .For the L05/D032 it should come like L05/D033,L05/D034 and etc. for every click . The inserted incremented text should be same of the source text properties

0 Likes
Message 16 of 17

Anonymous
Not applicable

Kindly assign a command for the lisp like TIC or NIC for the lisp .

Thanks in advance

0 Likes
Message 17 of 17

hak_vz
Advisor
Advisor

@Anonymous wrote:

Dear sir,

ok . but how could i find you in the open forum.


@Anonymous  This is open forum ans you can freely post your questions. 

Go to this link  and  post your request by clicking on button POST TO FORUMS

 

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