File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed
Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change 674674 (if (>= (+ _i _size) (len _L))
675675 (set _i (len _L)))
676676 (set _i (+ 1 _i)) }) }))
677+
678+ # @brief Transpose a list of lists or list of strings
679+ # @details The original list is not modified. Each element should have the same length
680+ # @param _L list of lists/strings to transpose
681+ # =begin
682+ # (let data [[1 2 3] [4 5 6] [7 8 9])
683+ # (print (list:transpose data)) # [[1 4 7] [2 5 8] [3 6 9]]
684+ # =end
685+ # @author https://github.com/SuperFola
686+ (let transpose (fun (_L) {
687+ (mut _output [])
688+ (mut _i 0)
689+ (let _width (len (head _L)))
690+ (let _height (len _L))
691+
692+ (while (< _i _width) {
693+ (mut _column [])
694+ (mut _k 0)
695+ (while (< _k _height) {
696+ (append! _column (@@ _L _k _i))
697+ (set _k (+ _k 1)) })
698+
699+ (append! _output _column)
700+ (set _i (+ 1 _i)) })
701+ _output }))
702+
Original file line number Diff line number Diff line change 188188 (test:eq (@ l 1) (+ 1 i))
189189 (set i (+ 1 i))
190190 (test:eq 2 (len l)) }))
191- (test:eq i 4) }) })
191+ (test:eq i 4) })
192+
193+ (test:case "transpose" {
194+ (test:eq (list:transpose [[1 2 3] [4 5 6] [7 8 9]]) [[1 4 7] [2 5 8] [3 6 9]])
195+ (let data ["123 328 51" " 45 64 387"])
196+ (test:eq (list:transpose data) [["1" " "] ["2" "4"] ["3" "5"] [" " " "] ["3" "6"] ["2" "4"] ["8" " "] [" " " "] [" " "3"] ["5" "8"] ["1" "7"]])
197+ (test:eq (list:transpose (list:transpose zipped)) zipped)
198+ (test:eq (list:transpose [[]]) [])
199+ (test:eq (list:transpose [[1]]) [[1]]) }) })
200+
You can’t perform that action at this time.
0 commit comments