Post Reply 
How to write a program to output multiple results
10-27-2020, 04:55 AM
Post: #10
RE: How to write a program to output multiple results
(10-26-2020 06:54 PM)Albert Chan Wrote:  
(10-26-2020 01:23 PM)pinkman Wrote:  Yes good approach, but there is no flatten function on the Prime.
We should request it ! Big Grin

I was looking at XCas source misc.cc, I discovered there is an undocumented flatten1

XCas> flatten1([1,2,3,[4,[5]]])       → [1,2,3,4,[5]]

The code also explained why flatten is so fast.
It only scan the list 1 time, adding 1 element at a time, with time complexity of O(n)

We could emulate the functionality of flatten, but time complexity is O(n^2)
Here is a simple implementation. (note that concat is needed, which kills performance.)
Code:
flat(lst) := {
  local h := head(lst);
  return (h == lst) ? h : concat(flat(h), flat(tail(lst)));
}

The discussion is a little difficult for me to understand, so I will continue my study
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: How to write a program to output multiple results - cloudxff - 10-27-2020 04:55 AM



User(s) browsing this thread: 1 Guest(s)