1. Code War: Quick Structure Check
- Posted by "Lucius L. Hilley III" <lhilley at CDC.NET> May 09, 2000
- 647 views
I will be working with multidimensional data that will be required to adhere to strict structuring. A simple example would be a 3D array. sequence s s = repeat(repeat(repeat(0,2),3),4)-- this is equal to. s = { {{0,0},{0,0},{0,0}}, {{0,0},{0,0},{0,0}}, {{0,0},{0,0},{0,0}}, {{0,0},{0,0},{0,0}} } Testing specifically for this exact structure is very easy. HOWEVER, there is a slight twist to the assignment. s = { --this should fail. { 0 ,{0,0},{0,0}}, --error 0, --error {0, 0, 0, 0}, -error {{0,0},{0,0},{0,0}} } s = { --this should pass. {{} ,{0,0,0}}, {{0,0},{0,0},{0,0},{0,0,0}}, {{},{}} } Notice that if it is declared a sequence then it MUST be a sequence BUT it doesn't have to be of the exact length requested. Any clues on how to QUICKLY handle this versatility. I will be writing a slow recursive calling routine to handle this for now. I would much prefer some sort of magic using sequences and compares or something of that nature. Lucius L. Hilley III lhilley at cdc.net +--------------+--------------+ | ICQ: 9638898 | AIM: LLHIII | +--------------+--------------+ | http://www.cdc.net/~lhilley | +-----------------------------+
2. Re: Code War: Quick Structure Check
- Posted by "Carl R. White" <cyrek at BIGFOOT.COM> May 11, 2000
- 590 views
Since most of Lucius' message is quoted in the code below, I'll not quote here :) The closest 'magic' sequence trick I can think of is: function same_structure(object a, object b) return equal({a}*0, {b}*0) end function ...but this only returns if they have *exactly* the same structure, right down to the atoms. To get to the level of abstraction outside atoms, a function has to be written (either that or I've gotten rusty at sequence magic recently :) ). Here's my attempt at a generic solution, but I haven't extensively tested it. It works with the supplied sequences though: function equal_dimension(integer dimension, object x) if atom(x) then return (dimension = 0) else if not length(x) then return (dimension = 1) end if for i = 1 to length(x) do if not equal_dimension(dimension - 1, x[i]) then return 0 end if end for return 1 end if end function type three_dimensional(object x) return equal_dimension(3, x) end type three_dimensional s s = repeat(repeat(repeat(0,2),3),4) s = { --this passes {{} ,{0,0,0}}, {{0,0},{0,0},{0,0},{0,0,0}}, {{},{}} } s = { --this fails { 0 ,{0,0},{0,0}}, --error 0, --error {0, 0, 0, 0}, --error {{0,0},{0,0},{0,0}} } HTH, Carl -- Carl R. White - Software Tester @ CTD - Carl- at -comodo.net Cyrek the Illogical - Geek - cyrek- at -bigfoot.com Remove hyphens before mailing s'il vous plait...
3. Re: Code War: Quick Structure Check
- Posted by "Lucius L. Hilley III" <lhilley at CDC.NET> May 15, 2000
- 566 views
- Last edited May 16, 2000
Very good code Carl. The examples I gave you obviously weren't extensive enough. I wasn't sure of exactly what I wanted until I started adding a few more tests. Your function appears to handle the following example incorrectly. sequence s s = {{{}}}--'s' is obviously 3 dimesions and... ? equal_dimension(1, s)--Your code thinks it is 1. To fix it just change 'return 1' after the for loop to 'return (dimension > 0)'. Your code is much tighter than the bloat I had created. Thanks for your contribution. Lucius L. Hilley III lhilley at cdc.net +----------+--------------+--------------+ | Hollow | ICQ: 9638898 | AIM: LLHIII | | Horse +--------------+--------------+ | Software | http://www.cdc.net/~lhilley | +----------+-----------------------------+ ----- Original Message ----- From: "Carl R. White" <cyrek at BIGFOOT.COM> To: "Removing Subscription" <EUPHORIA at LISTSERV.MUOHIO.EDU> Cc: "Carl R. White" <cyrek at BIGFOOT.COM> Sent: Thursday, May 11, 2000 8:45 AM Subject: Re: Code War: Quick Structure Check Since most of Lucius' message is quoted in the code below, I'll not quote here :) The closest 'magic' sequence trick I can think of is: function same_structure(object a, object b) return equal({a}*0, {b}*0) end function ...but this only returns if they have *exactly* the same structure, right down to the atoms. To get to the level of abstraction outside atoms, a function has to be written (either that or I've gotten rusty at sequence magic recently :) ). Here's my attempt at a generic solution, but I haven't extensively tested it. It works with the supplied sequences though: function equal_dimension(integer dimension, object x) if atom(x) then return (dimension = 0) else if not length(x) then return (dimension = 1) end if for i = 1 to length(x) do if not equal_dimension(dimension - 1, x[i]) then return 0 end if end for return 1 end if end function type three_dimensional(object x) return equal_dimension(3, x) end type three_dimensional s s = repeat(repeat(repeat(0,2),3),4) s = { --this passes {{} ,{0,0,0}}, {{0,0},{0,0},{0,0},{0,0,0}}, {{},{}} } s = { --this fails { 0 ,{0,0},{0,0}}, --error 0, --error {0, 0, 0, 0}, --error {{0,0},{0,0},{0,0}} } HTH, Carl -- Carl R. White - Software Tester @ CTD - Carl- at -comodo.net Cyrek the Illogical - Geek - cyrek- at -bigfoot.com Remove hyphens before mailing s'il vous plait...
4. Re: Code War: Quick Structure Check
- Posted by "Carl R. White" <cyrek at BIGFOOT.COM> May 16, 2000
- 598 views
Drat this infernal web based e-mail thingy. I composed a reply, hit the wrong key, and my reply vanished... Grrr. Never mind. I'll try again (in more concise terms): Lucius said (more or less): > equal_dimension() fails on {{{}}}. It accepts it as 1 dimensional rather > than 3. Changing the 'return 1' line to 'return (dimension > 0)' should > fix it. I tried this on my computer here at work and equal_dimension(3, {{{}}}) returns '1', and '0' when dimension is not 3. It's possible that the 'if not length(...' line is missing from your code, as this would cause the 'for' loop to fall straight through to the 'return 1' when the sequence is empty, causing the problem. > ...(dimension > 0)... The negative dimension problem is dealt with by the code inherently (I hadn't actually planned this, but it works). Run the code in trace mode (a good example of recursion, even if I do say so myself :) ) and you'll see what's going on. dimension never actually gets below -1, because a few statements later, the whole thing returns '0'. On a lighter note, a fun question: What's the dimension of this sequence: {{1,2,3},{{4,4},5,6}} ...?!?! For extra credit, use your answer to tell me why equal_dimension() is implemented in the way that it is. Carl -- I think my .sig ran away.
5. Re: Code War: Quick Structure Check
- Posted by "Lucius L. Hilley III" <lhilley at CDC.NET> May 16, 2000
- 576 views
> It's possible that the 'if not length(...' line is missing from your > code, You are correct. I intentionally removed it for my more flexible purposes. I was unaware the not length line was preventing negative dimensions. > > On a lighter note, a fun question: What's the dimension of this sequence: > > {{1,2,3},{{4,4},5,6}} ^------^ ^---^ ^--^ 2D 3D 2D It doesn't fit any one dimension setting. With your definition of equal_dimension(). equal_dimension(3,{}) is 0. When I altered your equal_dimension() I renamed it as well. REASON: your code correctly defines equal dimension. Your code answers the question. Is the dimension of {} equal to 3 The answer is no. The dimension of {} is 1. I wanted to ask the question. Is the data given suitable? suitable(3, {})-- the answer is yes. If I want 3 dimensions out of {} I can easily bloat it to. {{{}}} No data is lost in the bloating so the data is Suitable. Of course suitable(3, {1}) is false. You can't expand that data into 3 dimensions and not Corrupt the data. The data simply doesn't properly fit the dimensional design set forth. Let me elaborate even more. sequence format object exact_dim format = {5, 10} exact_dim = 0 for A = length(format) to 1 by -1 do exact_dim = repeat(exact_dim,format[A]) end for --exact_dim now looks like this. --{{0,0,0,0,0,0,0,0,0,0}, -- {0,0,0,0,0,0,0,0,0,0}, -- {0,0,0,0,0,0,0,0,0,0}, -- {0,0,0,0,0,0,0,0,0,0}, -- {0,0,0,0,0,0,0,0,0,0} --} exact_dim is a perfect 2D sequence. {"Lucius", "Lamar", "Hilley", "III", "123456789abc"} -- is suitable data to this format. However, it doesn't perfectly fit. It can be expanded to {{'L','u','c','i','u','s',0,0,0,0}, {'L','a','m','a','r',0,0,0,0,0}, {'I','I','I',0,0,0,0,0,0,0}, {'1','2','3','4','5','6','7','8','9','a'}, --some of this was truncated to fit. {0,0,0,0,0,0,0,0,0,0} } equal_dimension would have handled this without any problems. But it wouldn't take care of this. {} -- Your code realizes this is only 1 dimension. My code realizes that it is suitable. Then more of my code is able to fix it to look like the exact_dim defined above. > > For extra credit, use your answer to tell me why equal_dimension() is > implemented in the way that it is. I guess I don't get extra credit. Lucius L. Hilley III lhilley at cdc.net +----------+--------------+--------------+ | Hollow | ICQ: 9638898 | AIM: LLHIII | | Horse +--------------+--------------+ | Software | http://www.cdc.net/~lhilley | +----------+-----------------------------+ ----- Original Message ----- From: "Carl R. White" <cyrek at BIGFOOT.COM> To: "Removing Subscription" <EUPHORIA at LISTSERV.MUOHIO.EDU> <--- very interesting. Cc: "Carl R. White" <cyrek at BIGFOOT.COM> Sent: Tuesday, May 16, 2000 6:57 AM Subject: Re: Code War: Quick Structure Check <SNIP> > > Lucius said (more or less): > > equal_dimension() fails on {{{}}}. It accepts it as 1 dimensional rather > > than 3. Changing the 'return 1' line to 'return (dimension > 0)' should > > fix it. > > I tried this on my computer here at work and equal_dimension(3, {{{}}}) > returns '1', and '0' when dimension is not 3. It's possible that the 'if > not length(...' line is missing from your code, as this would cause > the 'for' loop to fall straight through to the 'return 1' when the sequence > is empty, causing the problem. > > > ...(dimension > 0)... > <SNIP> > > Carl > > -- > I think my .sig ran away. >
6. Re: Code War: Quick Structure Check
- Posted by Kat <gertie at PELL.NET> May 16, 2000
- 594 views
On 16 May 2000, at 6:57, Carl R. White wrote: Date sent: Tue, 16 May 2000 06:57:59 -0400 Send reply to: Euphoria Programming for MS-DOS <EUPHORIA at LISTSERV.MUOHIO.EDU> From: "Carl R. White" <cyrek at BIGFOOT.COM> Subject: Re: Code War: Quick Structure Check To: EUPHORIA at LISTSERV.MUOHIO.EDU > Drat this infernal web based e-mail thingy. I composed a reply, hit > the wrong key, and my reply vanished... Grrr. > > Never mind. I'll try again (in more concise terms): Easy solution, write the reply in a text editor, and paste it to the web. Kat
7. Re: Code War: Quick Structure Check
- Posted by "Carl R. White" <cyrek at BIGFOOT.COM> May 17, 2000
- 568 views
On Tue, 16 May 2000 14:16:27 -0400, Lucius L. Hilley III <lhilley at CDC.NET> wrote: >I was unaware the not length line was preventing negative dimensions. That wasn't really what I put it there for. Consider the following run- throughs of equal_dimension() (abbreviated as as eq_d()): *** 1. eq_d(1,{5,7}) -- check all elements for "1d less 1" compliance (0d) 1.1 => eq_d(0,5) -- 5 is an atom, atoms are 0d, return 1 1.2 => eq_d(0,7) -- 7 is an atom, atoms are 0d, return 1 2. No zeroes found -- {5,7} must be 1d, return 1 *** 1. eq_d(1, {9}) -- check all elements for "1d less 1" compliance (0d) 1.1 => eq_d(0,9) -- 9 is an atom, atoms are 0d, return 1 2. No zeroes found -- {9} must be 1d, return 1 *** 1. eq_d(1, {}) -- we know this is 1d by sight -- check all elements for "1d less 1" compliance (0d) -- but this time there are *no* elements, so we *must* -- check for {} and dimension=1 rather than recursing one -- more time to look for atoms at dimension=0. >> On a lighter note, a fun question: What's the dimension of this sequence: >> >> {{1,2,3},{{4,4},5,6}} > ^------^ ^---^ ^--^ > 2D 3D 2D > >It doesn't fit any one dimension setting. > >> For extra credit, use your answer to tell me why equal_dimension() is >> implemented in the way that it is. > > I guess I don't get extra credit. You are close with what you say here: >REASON: your code correctly defines equal dimension. It could quite easily have been expected to somehow run like this: function dimension_of(object x) -- Insert code here return dimension end function This would hiccup quite badly at things like a mixed dimension sequence, though. >I wanted to ask the question. Is the data given suitable? >suitable(3, {})-- the answer is yes. Ah-ha! Now I see where your... if not length(x) return (dimension > 0) end if ...idea came from. You want to *loosely* define the dimension of the empty sequence, so it can be expanded afterwards. >{"Lucius", "Lamar", "Hilley", "III", "123456789abc"} -- is suitable data to >this format. >However, it doesn't perfectly fit. It can be expanded to >{{'L','u','c','i','u','s',0,0,0,0}, > {'L','a','m','a','r',0,0,0,0,0}, > {'I','I','I',0,0,0,0,0,0,0}, > {'1','2','3','4','5','6','7','8','9','a'}, --some of this was truncated to >fit. > {0,0,0,0,0,0,0,0,0,0} >} I think what you're wanting to do is to make an n-dimensional sequence into a perfect 'block', am I right? Carl -- .sig still missing.