From: ashneerson on 29 Jul 2010 01:52 hi, have a puzzle with rotating a list like below {{ht a3 pr2} {ht a2 pr1} {lt a1 pr2} {lt a3 pr4} {ht a2 pr2}} into a list like this one: {{ht ht ht lt lt} {a2 a2 a1 a3} {pr1 pr2 pr2 pr2 pr4}} so that to rotate the columns to rows and retain original correspondence between the list elements. can someone please help me figure out the trick? -a.
From: Harald Oehlmann on 29 Jul 2010 02:23 On 29 Jul., 07:52, ashneerson <ashneer...(a)gmail.com> wrote: > hi, have a puzzle with rotating a list like below > > {{ht a3 pr2} {ht a2 pr1} {lt a1 pr2} {lt a3 pr4} {ht a2 pr2}} > > into a list like this one: > > {{ht ht ht lt lt} {a2 a2 a1 a3} {pr1 pr2 pr2 pr2 pr4}} > > so that to rotate the columns to rows and retain original > correspondence between the list elements. Sorry, I did not get what you mean. Could you write it with indexes ?
From: ashneerson on 29 Jul 2010 02:37 On Jul 29, 11:23 am, Harald Oehlmann <wortka...(a)yahoo.de> wrote: > On 29 Jul., 07:52, ashneerson <ashneer...(a)gmail.com> wrote: > > > hi, have a puzzle with rotating a list like below > > > {{ht a3 pr2} {ht a2 pr1} {lt a1 pr2} {lt a3 pr4} {ht a2 pr2}} > > > into a list like this one: > > > {{ht ht ht lt lt} {a2 a2 a1 a3} {pr1 pr2 pr2 pr2 pr4}} > > > so that to rotate the columns to rows and retain original > > correspondence between the list elements. > > Sorry, I did not get what you mean. Could you write it with indexes ? ok. What I want to do is convert columns to rows as follows: { {0 1 2} {0 1 3} {0 2 1} {1 1 2} {1 2 3} } converted to : { {0 0 0 1 1} {1 1 2 1 2} {2 3 1 2 3} } # initial code proc rotate_list { list } { set lWidth [llength $list] set lHeight [llength [lindex $list 0]] set w $lHeight set h $lWidth set list {} for {set i 0} {$i < h} {incr i} { for {set j 0} {$j < w} {incr j} { list [i][j] = list[w - j -1] [i] } } return list } what I am stuck at is this line: list [i][j] = list[w - j -1] [i] thanks! -a.
From: ashneerson on 29 Jul 2010 03:06 On Jul 29, 11:37 am, ashneerson <ashneer...(a)gmail.com> wrote: > On Jul 29, 11:23 am, Harald Oehlmann <wortka...(a)yahoo.de> wrote:> On 29 Jul., 07:52, ashneerson <ashneer...(a)gmail.com> wrote: > > > > hi, have a puzzle with rotating a list like below > > > > {{ht a3 pr2} {ht a2 pr1} {lt a1 pr2} {lt a3 pr4} {ht a2 pr2}} > > > > into a list like this one: > > > > {{ht ht ht lt lt} {a2 a2 a1 a3} {pr1 pr2 pr2 pr2 pr4}} > > > > so that to rotate the columns to rows and retain original > > > correspondence between the list elements. > > > Sorry, I did not get what you mean. Could you write it with indexes ? > > ok. What I want to do is convert columns to rows as follows: > > { > {0 1 2} > {0 1 3} > {0 2 1} > {1 1 2} > {1 2 3} > > } > > converted to : > > { > {0 0 0 1 1} > {1 1 2 1 2} > {2 3 1 2 3} > > } > > # initial code > proc rotate_list { list } { > set lWidth [llength $list] > set lHeight [llength [lindex $list 0]] > > set w $lHeight > set h $lWidth > > set list {} > > for {set i 0} {$i < h} {incr i} { > for {set j 0} {$j < w} {incr j} { > list [i][j] = list[w - j -1] [i] > } > } > return list > > } > > what I am stuck at is this line: > > list [i][j] = list[w - j -1] [i] > > thanks! > -a. I modified the code a bit: # code proc rotate_list { list } { set lWidth [llength $list] set lHeight [llength [lindex $list 0]] set w $lHeight set h $lWidth set lst {} for {set i 0} {$i < $h} {incr i} { set case {} for {set j 0} {$j < $w} {incr j} { #list [i][j] = list[w - j -1] [i] #lappend lst [expr {$w - $j -1} * $i] lappend case [expr {$w - $j -1} * $i] } lappend lst $case } set lst } it does the rotation but i can't figure out why it returns indices instead of letters now. % set m {{ht a2 pr2} {ht a2 pr1} {lt a1 pr2} {ht a3 pr2} {lt a3 pr4}} {ht a2 pvt2} {ht a2 pvt1} {lt a1 pvt2} {ht a3 pvt2} {lt a3 pvt4} % rotate_list $m {3 2 1} {2 1 0} {1 0 -1} {0 -1 -2} {-1 -2 -3} can someone please help with this?
From: Uwe Klein on 29 Jul 2010 04:09
ashneerson wrote: > can someone please help with this? #!/usr/bin/tclsh proc prettyprintlist list { puts \{ foreach line $list { puts -nonewline \t\{\ foreach item $line { puts -nonewline [ format %10s $item ] } puts \t\}\ } puts \} } proc listrot list { set columns {} set col 0 foreach line $list { if {!$col} { foreach item $line { lappend columns c:$col incr col } } set col 0 foreach item $line { lappend c(c:$col) $item incr col } } foreach tok $columns { lappend ret $c($tok) } return $ret } set ilist { {0 1 2} {0 1 3} {0 2 1} {1 1 2} {1 2 3} } prettyprintlist $ilist set olist [ listrot $ilist ] prettyprintlist $olist #end |