4 Replies - 8944 Views - Last Post: 25 October 2013 - 07:15 AM

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Challenge: Structurally Equivalent?

Post icon  Posted 22 October 2013 - 12:08 PM

Challenge: Structurally Equivalent?


Challenge: (Moderate - Hard)

Let me first define the term structurally equivalent, something in this challenge which will be XML is equivalent if they have same elements and attributes (both which can appear in any order). It doesn't matter if the they have different values, what we are more concerned about it the basic structure.

For example these two would be considered structurally equivalent.
<Fields>
  <Field Value="A" Size="1.1" />
  <Field Value="B" Size="2.2" />
</Fields>


<Fields>
  <Field Value="Bc" Size="20.2" />
  <Field Size="3.2" Value="Ax" />
</Fields>


but the following wouldn't be equivalent to any of the above.
<Fields>
  <Field Value="A" Size="1.1" Owner="Adam" />
  <Field Value="B" Size="2.2" />
</Fields>


<Positions>
  <Position X="1.0" Y="0.0" Value="Dog" />
  <Position X="10.0" Y="-20.0" Value="Cat" />
</Positions>




So engineer the function that fulfils the requirements of the challenge.

Public Module 
  Public Function IsStructurallyEqualTo( a As Xml.Linq.XElement, b As Xml.Linq.XElement ) As Boolean

  End Function
End Module




This challenge is open to all languages but VB.net enteries are preferred.

This post has been edited by AdamSpeight2008: 25 October 2013 - 05:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Challenge: Structurally Equivalent?

#2 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 178
  • View blog
  • Posts: 1,106
  • Joined: 11-May 12

Re: Challenge: Structurally Equivalent?

Posted 23 October 2013 - 08:15 AM

Non-Recursive using the dataset you provided. Guarantee it isn't the best way to do it, but I just threw it together.
xml file
Spoiler

actual code
Spoiler

Sample Output
Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

Re: Challenge: Structurally Equivalent?

Posted 23 October 2013 - 10:47 AM

Well, everyone knows I'm not a VB guy, but Adam's challenge looked like fun.

I felt I had to use VB.NET. It doesn't seem fair not to, in a VB.NET forum. It was less painful than I recalled. Some of the Dim sugar for XElement as surprising, curlies in VB!?!

Spoiler


To be fair, I doped this out in another .NET language first. It doubtless bears the fingerprint of where my brain was at. So, the F# version I did right before that:

Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Challenge: Structurally Equivalent?

Posted 24 October 2013 - 12:00 AM

This is my submission to this challenge.

  <Extension>
  Public  Function IsStructrallyEqualTo(Of T As Xml.Linq.XElement)( AXmlElement As T, BXmlElement As T) As Boolean
    Dim l2=Function(ee As XElement) ee.Name.LocalName
    Dim l0=Function(xe As XElement) From e In {xe}.Concat( xe.Elements) Order By e.Name.LocalName
    Dim l1=Function(x As IEnumerable(of XElement)) x.SelectMany(Function(xe) From a In xe.Attributes Order By a.Name.LocalName Select a.Name.LocalName )
    Dim es={l0(AXmlElement),l0(BXmlElement)}
    Return es(0).SelectMany(l2).SequenceEqual(es(1).SelectMany(l2)) AndAlso l1(es(0)).SequenceEqual(l1(es(1)))
  End Function


This post has been edited by AdamSpeight2008: 24 October 2013 - 12:03 AM

Was This Post Helpful? 0
  • +
  • -

#5 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Challenge: Structurally Equivalent?

Posted 25 October 2013 - 07:15 AM

This is mine More similar to baavgai but to my knowledge it's complete different
Spoiler

This post has been edited by thava: 25 October 2013 - 07:23 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1