Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Algorithm challenge: selecting a range from multiple blocks

From: dhsunanda:gm:ail at: 20-Sep-2007 15:39

Any one up for an algorithm challenge? I coded my solution to it yesterday - and it's basic hack work. I am sure there is a more elegant way - and probably several :-) THE SET UP We have a set of data, represented by a series of objects in a container block: data: reduce [ make object! [id: "dd" items: [1 2 3 4 99] ] make object! [id: "aa" items: [1 5 89 13] ] make object! [id: "xx" items: [] ] make object! [id: "yy" items: [6 5 4 3 2] ] ] What we want is a subset range of these.... a sort of analog of skip copy/part .... But that only works if the data is in one block - ours is spread across multiple blocks EXAMPLES Assume we (you!) have written a function call get-subset that takes two args: 1. the data block 2. the range block: a block of two numbers being the start and end values of the items we want to extract. These are some sample expected results: get-subset data [1 6] ;; ie items 1 through 6 [ make object! [id: "dd" items: [1 2 3 4 99] ] make object! [id: "aa" items: [1] ] ] get-subset data [5 11] ;; items 5 through 11 [ make object! [id: "dd" items: [99] ] make object! [id: "aa" items: [1 5 89 13] ] make object! [id: "yy" items: [6 5] ] ] get-subset data [7 7] ;; item 7 only [ make object! [id: "aa" items: [5] ] ] THE SMALL PRINT ** You can assume the data block always contains at least one object ** You can also assume that there is at least one data item (ie a least one of the objects will have at least one entry in its 'items block) ** you can assume that the range block has been correctly coerced to match the data, so: [51 74] -- means there are at least 74 items in the data [74 51] -- won't happen: the second number will never be lower than the first ** the objects must emerge in the same sequence as they began ** (the above three assumptions means that there will always be at least one object with at least one item in the output) ** the items must emerge in the same sequence as they began ** do not emit any objects if they have zero selected items ** there may be several thousand entries in each 'items block, so any solution that runs a crude loop will be very slow ** you can directly edit the original block and objects, or create your own copies THE JUDGES None, really -- if you contribute a solution, feel free to comment on other people's solutions. THE PRIZE! There is no prize other than the glory of having written and published an elegant solution. I would like permission to use the best R2 solution on (its part of a search CGI) to replace klunky the code I wrote myself. If you enter .... Good luck! Sunanda