How to write a program to output multiple results
10-27-2020, 04:55 AM
Post: #10
 cloudxff Junior Member Posts: 6 Joined: Oct 2020
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 !

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
 « Next Oldest | Next Newest »

 Messages In This Thread How to write a program to output multiple results - cloudxff - 10-25-2020, 03:52 AM RE: How to write a program to output multiple results - pinkman - 10-25-2020, 06:47 PM RE: How to write a program to output multiple results - cloudxff - 10-26-2020, 03:35 AM RE: How to write a program to output multiple results - pinkman - 10-26-2020, 10:27 AM RE: How to write a program to output multiple results - cloudxff - 10-26-2020, 11:32 AM RE: How to write a program to output multiple results - Albert Chan - 10-26-2020, 12:49 PM RE: How to write a program to output multiple results - pinkman - 10-26-2020, 01:23 PM RE: How to write a program to output multiple results - Albert Chan - 10-26-2020, 06:54 PM RE: How to write a program to output multiple results - cloudxff - 10-27-2020 04:55 AM RE: How to write a program to output multiple results - StephenG1CMZ - 10-26-2020, 02:51 PM

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