'Friday Fun' - Halloween (Trick or Treat) puzzel

Public forum to share code snippets, screen shorts, experiences, etc.
Post Reply
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

'Friday Fun' - Halloween (Trick or Treat) puzzel

Post by Phil Hepburn »

Hi again guys,

Well, are you up for a bit of festive fun? It will soon be Halloween here in the UK, when people do all sorts of silly things.

Here is a puzzle over which I stumbled, while researching for session material for Cologne 2018.

'God' said to Noah (of Biblical fame - you know, before and after the flood etc.) that he had to save live on earth by building an Arc from the latest .NET Framework.

God said ... "choose a class to act as the Arc, and fill this group object with all the animals, plants, fish and birds - you are also allowed to take with you your family members and your personal possessions. The Arc object is to be strongly typed (as is good design practice) BUT you must use these objects as they have been define by me, God. You can't change the code for any of the simple classes provided. So for example you will have :-

Code: Select all

CLASS Elephant 
    PUBLIC CONSTRUCTOR() 
    return
END CLASS

CLASS MotorBike 
    PUBLIC CONSTRUCTOR() 
    return
END CLASS
Now then, since Noah obviously saved live on earth, as well as his family and skateboard etc., what was his answer to God ? There is more than one simply beautiful answer.

Any ideas or thoughts?

Good Luck & Best regards,
Phil.
Wales, UK.
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

'Friday Fun' - Halloween (Trick or Treat) puzzel

Post by wriedmann »

Code: Select all

using System.Collections.Generic

class Arc
protect oChildren as List<object>

constructor()
  oChildren := List<object>{}

  return

method AddObject( oObject as object ) as void

  oChildren:Add( oObject )

  return

end class
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

'Friday Fun' - Halloween (Trick or Treat) puzzel

Post by wriedmann »

Even less code and more power:

Code: Select all

using System.Collections.Generic

class Arc
protect oChildren as List<object>

constructor()
  oChildren := List<object>{}

  return                        
  
property Children as List<object> get oChildren

end class
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

'Friday Fun' - Halloween (Trick or Treat) puzzel

Post by Phil Hepburn »

Hi Wolfgang,

OK then so 'God' has smiled, but he wonders if anyone can come up with any other potential Arcs other than 'List<Object>'.

There is also a nice solution which unfortunately is not strong typed but will get you a small prize, if NOT a cigar !

I have been replacing bedroom floorboards today, and am going on a very short vacation tomorrow and Monday. So lets get all of the answers to 'God' fun puzzle, by the time I return.

Can anyone else make a suggestion.

Cheers for now,
Phil.
Wales, UK.
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

'Friday Fun' - Halloween (Trick or Treat) puzzel

Post by wriedmann »

Hi Phil,

"God"s message was clear to me: "The Arc object is to be strongly typed (as is good design practice) BUT you must use these objects as they have been define by me, God."

Since these classes cannot be changed, <object> is the only type that can be used.
If the classes could be changed, I had made them implement an interface and defined the list as using members implementing this interface.

And about list: of course there are many collection classes that can be used, but <List> is the most simple one, and since from every animal there were two (a couple), a dictionary cannot be used.
And yes, we could also use a VO or Vulcan array, but I prefer to use .NET classes directly.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

'Friday Fun' - Halloween (Trick or Treat) puzzel

Post by Phil Hepburn »

Hi Wolfgang,

I am now back from a short holiday break, and have followed that with a couple days replacing old floor boards.

Yes, you are totally correct in your suggested solutions. 'Object' because all things need to be included in the strongly typed 'group' type class.

So yes, List<T> would be my first choice, but Queue<T>, Stack<T> and ObservableCollection<T> are also modern .NET possibilities.

If we move from collections to older type .NET 'System.' arrays then we can have 'Object[]' as well.

On the interesting side of this, we could have use 'ArrayList' as this takes any class type without being able to strongly type it as 'Object'.

Attached is a snap-shot of something that I did before going away on my country break.
ArcSolutions_01.jpg
ArcSolutions_01.jpg (63.7 KiB) Viewed 515 times
Cheers to all, and well done Wolfgang.
Phil.
Wales, UK.
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

'Friday Fun' - Halloween (Trick or Treat) puzzel

Post by wriedmann »

Hi Phil,
So yes, List<T> would be my first choice, but Queue<T>, Stack<T> and ObservableCollection<T> are also modern .NET possibilities.
First of all, I prefer the most simple choice. And in the arc none of the special properties of the other collections are needed, or IMHO some of them limit the possibilities like Stack<T> or Queue<T>, because we cannot define the order of the exit of the objects from the arc.

I'm a big fan of the VO arrays, but for the .NET arrays I see a limited usage because of their static dimension.

The observable collection would be important when combined with a databound control.

And I have to admit that I have to look better at the ArrayList<T> class - I have never used it in my programs.

Basically, we have a lot of different collections classes in the framework, but normally always the same are used. In my case this is particularly the Dictionary class, the List<T> class, and the ObservableCollection<T> class when it comes to ViewModels.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply