1. My first program: sublime (reverse of "extract" function)
- Posted by SocIoDim May 17, 2013
- 2566 views
I needed to sort the list to perform certain operations on the elements, and then restore them in the order in which they were before sorting.
function sublime(sequence Unsorted, sequence Sorted) sequence Sublime = {} integer Index for i = 1 to length(Sorted) do Index = 0 loop do Index = find(Sorted[i], Unsorted, Index+1) until find(Index, Sublime) = 0 end loop Sublime &= Index end for return Sublime end function Unsorted = {0.5, 0.2, 0.4, 0.3, 0.1, 0.5} Sorted = sort(Unsorted) if equal(extract(Sorted, sublime(Unsorted, Sorted)), Unsorted) then puts(1, "sublime test passed!\n") end if
It is not optimal and potentially get hung up, but enough for me.
2. Re: My first program: sublime (reverse of "extract" function)
- Posted by DerekParnell (admin) May 18, 2013
- 2484 views
I needed to sort the list to perform certain operations on the elements, and then restore them in the order in which they were before sorting.
Why go to this trouble? Just keep a copy of the original list, but I'm not sure why one even needs to do that. The sort() function does not change, in anyway, the input list.
sequence Unsorted = {0.5, 0.2, 0.4, 0.3, 0.1, 0.5} sequence CopyList = Unsorted sequence Sorted = sort(Unsorted) if equal(Unsorted, CopyList) then puts(1, "test passed!\n") end if
3. Re: My first program: sublime (reverse of "extract" function)
- Posted by ChrisB (moderator) May 18, 2013
- 2468 views
Because its his first program (first ever?)
I remember my first program on a Commodore Pet
10 print 1
20 i = i + 1
30 goto 10
and I was so impressed and so pleased with myself.
Well done SocloDim
Chris
4. Re: My first program: sublime (reverse of "extract" function)
- Posted by DerekParnell (admin) May 18, 2013
- 2456 views
Because its his first program (first ever?)
Yeah .. sorry. I did reply around 2:00AM after a glass of wine or two, so I was a little bit 'tired'.
My main point was to explain that in the Euphoria Programming Language, a routine can never affect the contents of any of its parameters beyond the scope of the routine.
For example:
function XY( atom PA, sequence PB) atom LC -- This routine modifies the parameter values supplied, -- but note that the modifications only last for the duration -- of this routine. That is to say, any modifications -- done inside a routine, never affect the supplied data -- outside of the routine. LC = PA -- Save the number that was given to me PA = length(PB) -- Get the length of the sequence that was given PB = append(PB, LC) -- Add the original number to the end of the list PB = append(PB, PA) -- Add the original list length to the end of the list return PB -- return the resulting list. end function atom GA = 42 sequence GB = {1,2,3,4,0} ? XY(GA, GB) --> {1,2,3,4,0,42,5} ? GA --> 42 ? GB --> {1,2,3,4,0} -- The data in GA and GB have not been 'permanently' changed.
5. Re: My first program: sublime (reverse of "extract" function)
- Posted by SocIoDim May 19, 2013
- 2441 views
Why go to this trouble? Just keep a copy of the original list
You did not read my words or I poorly explained in English. First I need to sort the list, then process it in a certain way, then restore the elements of the new list in the same order. This function is very important part of my future program. The example code is trivial and useless, because it is just test.
Because its his first program (first ever?)
It is my first program in Euphoria, of course. More precisely, the first useful program, made not only for the exercise. My first ever program was written in ZX BASIC about quarter of a century ago.
6. Re: My first program: sublime (reverse of "extract" function)
- Posted by jimcbrown (admin) May 19, 2013
- 2410 views
Why go to this trouble? Just keep a copy of the original list
You did not read my words or I poorly explained in English. First I need to sort the list, then process it in a certain way, then restore the elements of the new list in the same order.
Why can't you keep a copy of the original list? What type of processing are you doing that makes this solution unsuitable?
7. Re: My first program: sublime (reverse of "extract" function)
- Posted by SocIoDim May 19, 2013
- 2432 views
Why can't you keep a copy of the original list? What type of processing are you doing that makes this solution unsuitable?
That's slightly less trivial code:
sequence InputData = {0.01, 0.05, 0.08, 0.03, 0.02} sequence OutputData = sort(InputData) sequence OriginalOrder = sublime(InputData, OutputData) for i = 2 to length(OutputData) do OutputData[i] *= 1 - OutputData[i-1] end for OutputData = extract(OutputData, OriginalOrder)
P.S. I've been thinking how best to implement this feature: simple or optimal. This is the easiest and most non-optimal option. There have been other attempts.
8. Re: My first program: sublime (reverse of "extract" function)
- Posted by jimcbrown (admin) May 19, 2013
- 2406 views
Why can't you keep a copy of the original list? What type of processing are you doing that makes this solution unsuitable?
That's slightly less trivial code:
sequence InputData = {0.01, 0.05, 0.08, 0.03, 0.02} sequence OutputData = sort(InputData) sequence OriginalOrder = sublime(InputData, OutputData) for i = 2 to length(OutputData) do OutputData[i] *= 1 - OutputData[i-1] end for OutputData = extract(OutputData, OriginalOrder)
I think I get it. Your example can still be implemented as:
sequence InputData = {0.01, 0.05, 0.08, 0.03, 0.02} sequence OutputData = 1 - InputData[2..$]
But I think your point is clear - you don't want to merely use your sorted input data for a task and then reuse the unsorted version later. You don't want to just sort the output data either. You need to sort the input data for a task, then "unsort" the output data such that the each element of the output data is in the same position as the corresponding element in the original unsorted input data.
If every element is unique in the input data, and every element is unique in the output data, then you could just use mapping(). But that probably isn't true.
P.S. I've been thinking how best to implement this feature: simple or optimal. This is the easiest and most non-optimal option. There have been other attempts.
I'd go with optimal.
9. Re: My first program: sublime (reverse of "extract" function)
- Posted by SocIoDim May 19, 2013
- 2417 views
I think I get it. Your example can still be implemented as:
sequence InputData = {0.01, 0.05, 0.08, 0.03, 0.02} sequence OutputData = 1 - InputData[2..$]
NO
But I think your point is clear - you don't want to merely use your sorted input data for a task and then reuse the unsorted version later. You don't want to just sort the output data either. You need to sort the input data for a task, then "unsort" the output data such that the each element of the output data is in the same position as the corresponding element in the original unsorted input data.
YES
If every element is unique in the input data, and every element is unique in the output data, then you could just use mapping(). But that probably isn't true.
It isn't true, otherwise it would have been much easier. The second "find" in my function is designed to avoid the associated problems.
P.S. I've been thinking how best to implement this feature: simple or optimal. This is the easiest and most non-optimal option. There have been other attempts.
I'd go with optimal.
I think, most optimal way is implement my own sorting function, that will output the sorted data and unsorted indexes. I wrote such function, but erase, because it turned ugly. This is wrong way: replacing the standard language features. This is called the invention of bicycles. Other attempts have been associated with disposal of the second "find".
There was an idea to look unsorted data in a sorted array. This would allow to use a binary search. But it turned out unnecessary complexity of the algorithm due to processing conflicts associated with identical elements. This is only one of the problems. I'm not a bad analyst really. Deceptive simpleness of this algorithm was hard-won in a few days.
10. Re: My first program: sublime (reverse of "extract" function)
- Posted by jimcbrown (admin) May 19, 2013
- 2396 views
I think I get it. Your example can still be implemented as:
sequence InputData = {0.01, 0.05, 0.08, 0.03, 0.02} sequence OutputData = 1 - InputData[2..$]
NO
I just meant that particular example. This won't help your real code though, so it's not worth delving into.
I think, most optimal way is implement my own sorting function, that will output the sorted data and unsorted indexes. I wrote such function, but erase, because it turned ugly. This is wrong way: replacing the standard language features. This is called the invention of bicycles.
You can do this within the stdlib.
function compare_saving_index(object x, object y) return compare(x[2], y[2]) end function public function sort_with_index(sequence s, integer order = NORMAL_ORDER) sequence x = repeat(0, length(s)) for i = 1 to length(x) do x[i] = {i, s[i]} end for return custom_sort(routine_id("compare_saving_index"), s, order) end function public function get_indexes_from_sorted(sequence s) return vslice(s, 1) end function public function get_values_from_sorted(sequence s) return vslice(s, 2) end function sequence unsortedinput = ... sequence sortedandindex = sort_with_index(unsortedinput) sequence sortedinput = get_values_from_sorted(sortedandindex) sequence idx = get_indexes_from_sorted(sortedandindex) sequence sortedoutput = ... sortedinput ... sequence unsortedoutput = extract(sortedoutput,idx)
11. Re: My first program: sublime (reverse of "extract" function)
- Posted by SocIoDim May 19, 2013
- 2388 views
NO
I just meant that particular example.
I realized this, but your code is still not the same as mine. Apparently, you do not notice the multiplication.
This won't help your real code though, so it's not worth delving into.
Yes, it does not matter already.
You can do this within the stdlib.
Wow! It is excellent idea! I think it is possible to do even better, because input data is one-dimensional sequence of atoms.
function SortWithIndexes(sequence X) for i = 1 to length(X) do X[i] &= i end for X = sort(X) return {vslice(X, 1), vslice(X, 2)} end function sequence Unsorted = {0.5, 0.3, 0.2, 0.4, 0.1} sequence Sorted = SortWithIndexes(Unsorted) sequence Indexes = Sorted[2] Sorted = Sorted[1]
12. Re: My first program: sublime (reverse of "extract" function)
- Posted by useless_ May 19, 2013
- 2415 views
But I think your point is clear - you don't want to merely use your sorted input data for a task and then reuse the unsorted version later. You don't want to just sort the output data either. You need to sort the input data for a task, then "unsort" the output data such that the each element of the output data is in the same position as the corresponding element in the original unsorted input data.
I don't think that's quite what was meant. If the original list was sorted by a column, sorttok would re-sort by whatever other column(s) was desired, and then sorttok again on the original column(s) to put it all back in original order.
useless
13. Re: My first program: sublime (reverse of "extract" function)
- Posted by jimcbrown (admin) May 19, 2013
- 2446 views
But I think your point is clear - you don't want to merely use your sorted input data for a task and then reuse the unsorted version later. You don't want to just sort the output data either. You need to sort the input data for a task, then "unsort" the output data such that the each element of the output data is in the same position as the corresponding element in the original unsorted input data.
I don't think that's quite what was meant.
The OP agreed with me. I consider that definitive.
If the original list was sorted by a column
The OP explained that it wasn't, and that it had no columns.
14. Re: My first program: sublime (reverse of "extract" function)
- Posted by useless_ May 19, 2013
- 2432 views
But I think your point is clear - you don't want to merely use your sorted input data for a task and then reuse the unsorted version later. You don't want to just sort the output data either. You need to sort the input data for a task, then "unsort" the output data such that the each element of the output data is in the same position as the corresponding element in the original unsorted input data.
I don't think that's quite what was meant.
The OP agreed with me. I consider that definitive.
If the original list was sorted by a column
The OP explained that it wasn't, and that it had no columns.
Did all that happen after the post i responded to?
useless
15. Re: My first program: sublime (reverse of "extract" function)
- Posted by jimcbrown (admin) May 19, 2013
- 2412 views
But I think your point is clear - you don't want to merely use your sorted input data for a task and then reuse the unsorted version later. You don't want to just sort the output data either. You need to sort the input data for a task, then "unsort" the output data such that the each element of the output data is in the same position as the corresponding element in the original unsorted input data.
I don't think that's quite what was meant.
The OP agreed with me. I consider that definitive.
If the original list was sorted by a column
The OP explained that it wasn't, and that it had no columns.
Did all that happen after the post i responded to?
It happened a little under three hours before you posted your response, inbetween the post you responded to and your response.
16. Re: My first program: sublime (reverse of "extract" function)
- Posted by useless_ May 19, 2013
- 2299 views
But I think your point is clear - you don't want to merely use your sorted input data for a task and then reuse the unsorted version later. You don't want to just sort the output data either. You need to sort the input data for a task, then "unsort" the output data such that the each element of the output data is in the same position as the corresponding element in the original unsorted input data.
I don't think that's quite what was meant.
The OP agreed with me. I consider that definitive.
If the original list was sorted by a column
The OP explained that it wasn't, and that it had no columns.
Did all that happen after the post i responded to?
It happened a little under three hours before you posted your response, inbetween the post you responded to and your response.
Well, good thing i used the conditional "if" in my statement. I guess this is turning into another one of those threads where no matter what i say, we'll go on for days like this, eh?
useless
17. Re: My first program: sublime (reverse of "extract" function)
- Posted by jimcbrown (admin) May 19, 2013
- 2304 views
But I think your point is clear - you don't want to merely use your sorted input data for a task and then reuse the unsorted version later. You don't want to just sort the output data either. You need to sort the input data for a task, then "unsort" the output data such that the each element of the output data is in the same position as the corresponding element in the original unsorted input data.
I don't think that's quite what was meant.
The OP agreed with me. I consider that definitive.
If the original list was sorted by a column
The OP explained that it wasn't, and that it had no columns.
Did all that happen after the post i responded to?
It happened a little under three hours before you posted your response, inbetween the post you responded to and your response.
I guess this is turning into another one of those threads where no matter what i say, we'll go on for days like this, eh?
Maybe because you keep asking me questions? But I'm game if you are.
Well, good thing i used the conditional "if" in my statement.
You missed it in your first statement though...
I don't think that's quite what was meant.
18. Re: My first program: sublime (reverse of "extract" function)
- Posted by useless_ May 20, 2013
- 2286 views
But I think your point is clear - you don't want to merely use your sorted input data for a task and then reuse the unsorted version later. You don't want to just sort the output data either. You need to sort the input data for a task, then "unsort" the output data such that the each element of the output data is in the same position as the corresponding element in the original unsorted input data.
I don't think that's quite what was meant.
The OP agreed with me. I consider that definitive.
If the original list was sorted by a column
The OP explained that it wasn't, and that it had no columns.
Did all that happen after the post i responded to?
It happened a little under three hours before you posted your response, inbetween the post you responded to and your response.
I guess this is turning into another one of those threads where no matter what i say, we'll go on for days like this, eh?
Maybe because you keep asking me questions? But I'm game if you are.
Well, good thing i used the conditional "if" in my statement.
You missed it in your first statement though...
I don't think that's quite what was meant.
Well,
I don't think that's quite what was meant.
And i didn't think that is what was meant, that's true. And i did say "if" in the second statement:
I don't think that's quite what was meant. If the original list was sorted by a column,
<snip>
So i don't understand what your problem with it is.
useless
19. Re: My first program: sublime (reverse of "extract" function)
- Posted by jimcbrown (admin) May 20, 2013
- 2290 views
But I think your point is clear - you don't want to merely use your sorted input data for a task and then reuse the unsorted version later. You don't want to just sort the output data either. You need to sort the input data for a task, then "unsort" the output data such that the each element of the output data is in the same position as the corresponding element in the original unsorted input data.
I don't think that's quite what was meant.
The OP agreed with me. I consider that definitive.
If the original list was sorted by a column
The OP explained that it wasn't, and that it had no columns.
Did all that happen after the post i responded to?
It happened a little under three hours before you posted your response, inbetween the post you responded to and your response.
I guess this is turning into another one of those threads where no matter what i say, we'll go on for days like this, eh?
Maybe because you keep asking me questions? But I'm game if you are.
Well, good thing i used the conditional "if" in my statement.
You missed it in your first statement though...
I don't think that's quite what was meant.
Well, And i didn't think that is what was meant, that's true.
You thought wrong.
And i did say "if" in the second statement:
I don't think that's quite what was meant. If the original list was sorted by a column, <snip>
So i don't understand what your problem with it is.
I was simply trying to be helpful by pointing out, in response to your "if X, then Y", "not X".
20. Re: My first program: sublime (reverse of "extract" function)
- Posted by useless_ May 20, 2013
- 2275 views
But I think your point is clear - you don't want to merely use your sorted input data for a task and then reuse the unsorted version later. You don't want to just sort the output data either. You need to sort the input data for a task, then "unsort" the output data such that the each element of the output data is in the same position as the corresponding element in the original unsorted input data.
I don't think that's quite what was meant.
The OP agreed with me. I consider that definitive.
If the original list was sorted by a column
The OP explained that it wasn't, and that it had no columns.
Did all that happen after the post i responded to?
It happened a little under three hours before you posted your response, inbetween the post you responded to and your response.
I guess this is turning into another one of those threads where no matter what i say, we'll go on for days like this, eh?
Maybe because you keep asking me questions? But I'm game if you are.
Well, good thing i used the conditional "if" in my statement.
You missed it in your first statement though...
I don't think that's quite what was meant.
Well, And i didn't think that is what was meant, that's true.
You thought wrong.
And i did say "if" in the second statement:
I don't think that's quite what was meant. If the original list was sorted by a column, <snip>
So i don't understand what your problem with it is.
I was simply trying to be helpful by pointing out, in response to your "if X, then Y", "not X".
As was i, trying to be helpful.
useless
21. Re: My first program: sublime (reverse of "extract" function)
- Posted by petelomax May 21, 2013
- 2282 views
Bit late joining in, but I would have started with a tag sort:
sequence InputData = {0.01, 0.05, 0.08, 0.03, 0.02} sequence tags = tag_sort(InputData) -- {1,5,4,2,3}
then you could do something like:
for i=2 to length(tags) do InputData(tags[i]) *= 1-InputData(tags[i-1]) end for
which should give the required results
Regards, Pete
22. Re: My first program: sublime (reverse of "extract" function)
- Posted by SocIoDim May 22, 2013
- 2130 views
tag_sort() is my sublime() from first message? I think I understand your point. Yes, it is possible, but not relevant to my situation. The only advantage of this approach: it saves memory. The downside: I need to rewrite the function that operates on sorted data. However, the economy does not interest me. By the way, the function is already written, but it is more complicated than multiplication in above example, so I do not want to make it less readable even at a tiny fraction. Nevertheless, thank you very much for regard.
23. Re: My first program: sublime (reverse of "extract" function)
- Posted by petelomax May 22, 2013
- 2162 views
tag_sort() is my sublime() from first message?
Same results, but it leaves the original data intact. I posted this example last August.
However, the economy does not interest me. .. already written
No worries, I realised that would likely be the case when I was posting.
Pete