Author Topic: (Challenge) Transformation of the list.  (Read 8370 times)

0 Members and 1 Guest are viewing this topic.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
(Challenge) Transformation of the list.
« on: February 02, 2007, 03:32:23 AM »
Yesterday, I have received the letter:

Quote
14:17 Evgeniy hello :-)
If there is time, help my, to make something beautiful

I had perfectly a good time!

I suggest you to write program TEST
Code: [Select]
Example 1:
(setq l '(((11))
          ((21) (22))
          ((31))
          )
)
(test l)
; ==>>
'(((11) (21) (31))
  ((11) (22) (31)))

Example 2:
(setq l '(((11) (12))
          ((21))
          ((31) (32))
          ((41) (42))
          )
)
(test l)
; ==>>
'(((11) (21) (31) (41))
  ((11) (21) (31) (42))
  ((11) (21) (32) (41))
  ((11) (21) (32) (42))
  ((12) (21) (31) (41))
  ((12) (21) (31) (42))
  ((12) (21) (32) (41))
  ((12) (21) (32) (42)))


Example 3:
(setq l '((11)
          (21 22)
          (31 32 33)
          )
)
(test l)
; ==>>
'((11 21 31)
  (11 21 32)
  (11 21 33)
  (11 22 31)
  (11 22 32)
  (11 22 33))

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #1 on: February 02, 2007, 12:08:22 PM »

On Monday, at this time, I shall show my variant...
Have a good time!

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #2 on: February 03, 2007, 09:37:29 AM »
I am surprised!
Nobody wishes to show the code, for the decision of this problem...
Probably I have badly explained the task?
Tell to me, in what complexity...

PS. This task is not interesting?

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: (Challenge) Transformation of the list.
« Reply #3 on: February 03, 2007, 10:01:19 AM »
Sorry Evgeniy,
I am still trying to understand the problem. (complex.)

Can you explain again?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #4 on: February 03, 2007, 10:05:11 AM »
(setq lst '((11 12) (21) (31 32)))
It is necessary to find all variants of the transposed list without repetitions
(test lst)
==
((11 21 31) (11 21 32) (12 21 31) (12 21 32))

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #5 on: February 03, 2007, 10:10:27 AM »

(setq lst '((11 12) (21) (31 32)))
11,12,21,31,32 = atom or list...
(length lst) = any
(length (car lst)) = any

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: (Challenge) Transformation of the list.
« Reply #6 on: February 03, 2007, 10:13:54 AM »
...
It is necessary to find all variants of the transposed list without repetitions
...

Ahh!! Very good!! Thank you.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #7 on: February 03, 2007, 10:15:44 AM »
Sorry Evgeniy,
I am still trying to understand the problem. (complex.)

Can you explain again?

No problem!
The problem for a long time is solved, I wished to enable to have a good time to you...
It is a little to knead brains.  :-)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #8 on: February 03, 2007, 10:48:30 AM »
I wish to explain the task once again...  :?

Code: [Select]
(test '((11 12) (21 22)))
Returns:
(([11 or 12] [21 or 22]) ([11 or 12] [21 or 22])....)



(test '((11 12 13) (21 22) (31 32 33)))
Returns:
(([11 or 12 or 13] [21 or 22] [31 or 32 or 33])....)


(test '((11) (21 22) (31 32 33)))
Returns:
(([only 11] [21 or 22] [31 or 32 or 33])....)

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: (Challenge) Transformation of the list.
« Reply #9 on: February 03, 2007, 10:51:14 AM »
I believe Evgeniy has give us a challenge involving ``Permutations and Combinations''

This will be a lot of fun!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: (Challenge) Transformation of the list.
« Reply #10 on: February 03, 2007, 12:39:06 PM »
Hi,

Here's my contribution.

EDIT: I'm sorry there was a little mistake (sub1 instead of sub).

Code: [Select]
(defun test (l / sub)

  (defun sub (e l)
    (if l
      (cons (append e (list (car l)))
    (sub e (cdr l))
      )
    )
  )

  (setq l (cons (mapcar 'list (car l)) (cdr l)))
  (while (cadr l)
    (setq l (cons
      (apply 'append
     (mapcar '(lambda (x)
(sub x (cadr l))
      )
     (car l)
     )
      )
      (cddr l)
    )
    )
  )
  (car l)
)
« Last Edit: February 03, 2007, 01:00:57 PM by gile »
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #11 on: February 03, 2007, 12:50:48 PM »
Greetings gile

At you it has very well turned out!

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: (Challenge) Transformation of the list.
« Reply #12 on: February 04, 2007, 03:00:05 AM »
hi,evgeniy, I use recursive method to do it

Code: [Select]
(defun ab (lst1 lst2 / res)
  (foreach x lst1
    (foreach y lst2
      (if (atom y)
(setq y (list y))
      )
      (setq res (append
  res
  (list (cons x y))
)
      )
    )
  )
  res
)

(defun test (lst / res)
  (if (= (cdr lst) nil)
    (setq res (car lst))
    (setq res (ab (car lst) (test (cdr lst))))
  )
)

(setq l (list (list 1 2 3) (list 4 5 ) (list 6 7 8 9) (list 10 11)))
(test l)=>

((1 4 6 10) (1 4 6 11) (1 4 7 10) (1 4 7 11) (1 4 8 10) (1 4 8 11) (1 4 9 10) (1 4 9 11) (1 5 6 10) (1 5 6 11) (1 5 7 10) (1 5 7 11) (1 5 8 10) (1 5 8 11) (1 5 9 10) (1 5 9 11) (2 4 6 10) (2 4 6 11) (2 4 7 10) (2 4 7 11) (2 4 8 10) (2 4 8 11) (2 4 9 10) (2 4 9 11) (2 5 6 10) (2 5 6 11) (2 5 7 10) (2 5 7 11) (2 5 8 10) (2 5 8 11) (2 5 9 10) (2 5 9 11) (3 4 6 10) (3 4 6 11) (3 4 7 10) (3 4 7 11) (3 4 8 10) (3 4 8 11) (3 4 9 10) (3 4 9 11) (3 5 6 10) (3 5 6 11) (3 5 7 10) (3 5 7 11) (3 5 8 10) (3 5 8 11) (3 5 9 10) (3 5 9 11))

:)

and if the l structure is different
like
(setq l '(((11))((21) (22))((31))))
the code maybe like as follow
Code: [Select]
(defun ab (lst1 lst2 / res)
  (foreach x lst1
    (foreach y lst2
      (if (atom (car y))
(setq y (list y))
      )
      (setq res (append
  res
  (list (cons x y))
)
      )
    )
  )
  res
)
(defun test (lst / res)
  (if (= (cdr lst) nil)
    (setq res (car lst))
    (setq res (ab (car lst) (test (cdr lst))))
  )
)

(setq l '(((11))((21) (22))((31))))
(test1 l)===>
(((11) ((21) (31))) ((11) ((22) (31))))

(setq l '(((11)) ((21) (22))
       ((31)) ((41)(42)(43))
      )
)
(test l)
===>
(((11) (21) (31) (41)) ((11) (21) (31) (42)) ((11) (21) (31) (43)) ((11) (22) (31) (41)) ((11) (22) (31) (42)) ((11) (22) (31) (43)))
« Last Edit: February 04, 2007, 10:11:07 AM by yuanqiu »
http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #13 on: February 04, 2007, 03:47:12 AM »

and if the l structure is different
like
(setq l '(((11))((21) (22))((31))))
the code maybe like as follow
Code: [Select]
(defun cd (lst1 lst2 / res)
  (foreach x lst1
    (foreach y lst2
      (setq res (append
  res
  (list (list x y))
)
      )
    )
  )
  res
)
(defun test1 (lst / res)
  (if (= (cdr lst) nil)
    (setq res (car lst))
    (setq res (cd (car lst) (test1 (cdr lst))))
  )
)

(setq l '(((11))((21) (22))((31))))
(test1 l)===>
(((11) ((21) (31))) ((11) ((22) (31))))

It is very a pity, but for the list
(setq l '(((11))((21) (22))((31))))
It is necessary to receive
(((11) (21) (31)) ((11) (22) (31)))

 :?

For other lists.
(caar lst) == atom
Your program works! :)
« Last Edit: February 04, 2007, 03:50:38 AM by ElpanovEvgeniy »

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: (Challenge) Transformation of the list.
« Reply #14 on: February 04, 2007, 10:12:17 AM »
:)
Evgeniy, I modify my code, now it works, sorry, it can do Example 1 and 2 ,but not 3, it seems that I still not finished.
« Last Edit: February 04, 2007, 10:17:43 AM by yuanqiu »
http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #15 on: February 04, 2007, 10:18:29 AM »
:)
Evgeniy, I modify my code, now it works

Greetings Chen! :-)

At you it has perfectly turned out to perform work.
But you have decided to divide the task for two parts and have written two different programs...

I hoped to see one program, for all variants of the list...

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #16 on: February 04, 2007, 10:22:05 AM »
You needed to unite the programs in one - then will work for all examples!  :-)

Code: [Select]
(if (atom (caar lst))
 (test-1 lst)
 (test-2 lst)
)

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: (Challenge) Transformation of the list.
« Reply #17 on: February 05, 2007, 03:17:00 AM »
In order to fit the two case in one, I have to use a global variable-> *A
Now it can sove the three sample.

but it make me very sad :-(, I think it must have a good solve method.
I will try to solve it in a more simple way.

Code: [Select]
(defun ab (lst1 lst2 / res)
  (foreach x lst1
    (foreach y lst2
      (if (atom y)
(setq y (list y)
      *A T
)
(if (and
      (atom (car y))
      (not *A)
    )
  (setq y (list y)
*A nil
  )
)
      )
      (setq res (append
  res
  (list (cons x y))
)
      )
    )
  )
  res
)
(defun test (lst / res)
  (if (= (cdr lst) nil)
    (setq res (car lst))
    (setq res (ab (car lst) (test (cdr lst))))
  )
)
http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #18 on: February 05, 2007, 03:45:12 AM »
My congratulations Chen!
Last program works correctly! :)

edit >>
 :-(

Code: [Select]
(test '(((11)) ((21) (22)) ((31))))
return
(((11) (21) 31) ((11) (22) 31)) 
but necessary
(((11) (21) (31)) ((11) (22) (31)))
« Last Edit: February 05, 2007, 03:53:17 AM by ElpanovEvgeniy »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: (Challenge) Transformation of the list.
« Reply #19 on: February 05, 2007, 04:54:40 AM »
Hi,

Quite the same as the one I posted, but recursive form.

Code: [Select]
(defun test (l / sub1 sub2)

  (defun sub1 (e l)
    (if l
      (cons (append e (list (car l)))
    (sub1 e (cdr l))
      )
    )
  )

  (defun sub2 (l)
    (if (cadr l)
      (sub2
(cons (apply 'append
     (mapcar '(lambda (x)
(sub1 x (cadr l))
      )
     (car l)
     )
      )
      (cddr l)
)
      )
      (car l)
    )
  )
 
  (sub2 (cons (mapcar 'list (car l)) (cdr l)))
)
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #20 on: February 05, 2007, 05:04:34 AM »
Hi,

Quite the same as the one I posted, but recursive form.

Excellently!
You have made the program which is very similar to mine...
All differences - at me all in one function (test).

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Transformation of the list.
« Reply #21 on: February 05, 2007, 11:27:46 AM »
My version of the program... 

Code: [Select]
(defun te (l)
 ;; (te l)
 ;; By ElpanovEvgeniy
 (if (cdr l)
  (apply
   (function append)
   (mapcar (function (lambda (a) (mapcar (function (lambda (b) (cons a b))) (te (cdr l)))))
           (car l)
   ) ;_  mapcar
  ) ;_  apply
  (mapcar (function list) (car l))
 ) ;_  if
) ;_  defun

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: (Challenge) Transformation of the list.
« Reply #22 on: February 05, 2007, 12:30:55 PM »
 :-o One more time, a wonder of concision !!!
« Last Edit: February 06, 2007, 01:04:04 AM by gile »
Speaking English as a French Frog

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: (Challenge) Transformation of the list.
« Reply #23 on: February 05, 2007, 04:42:24 PM »
WOW!!!

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: (Challenge) Transformation of the list.
« Reply #24 on: February 07, 2007, 08:05:22 PM »
so short code.

Evgeniy, could you tell me how to learn mapcar and recursive, is the only way that practice, or there are some quick way.:)
http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (Challenge) Transformation of the list.
« Reply #25 on: February 07, 2007, 09:10:37 PM »
so short code.

Evgeniy, could you tell me how to learn mapcar and recursive, is the only way that practice, or there are some quick way.:)

 :wink:  ... about 15 years of practice   :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.