Skip to main content

Hello NQE Enthusiasts,

 

It has been two months, so time for another challenge.

 

Challenge

The task is to list Name, Age and Pets owned by all people under 20 years old, that own fish.

 

Input:

persons =

;{ Name: "Tim", Age: 21, pets:t"dog", "mouse", "fish"] },

 { Name: "Mark", Age: 19, pets: t"dog", "fish", "cat"] },

 { Name: "Nina", Age: 18, pets: e"snake", "lizard"] },

 { Name: "Leroy", Age: 24, pets: p"fish"]},

 { Name: "Sally", Age: 11, pets: "dog", "fish"] }

];

 

Example Result

1Name,  Age, Pet…….]

 

 

Contributions: Unclaimed

707 characters (excluding the comments). Looking forward to seeing a more efficient option. 

/**
* @Intent - Fish Detector
* @description - Identify those that own fish and are under 20
*/
foreach device in network.devices
let persons = { name: "Tim", age: 21, pets: "dog", "mouse", "fish"] },
{ name: "Mark", age: 19, pets: "dog", "fish", "cat"] },
{ name: "Nina", age: 18, pets: "snake", "lizard"] },
{ name: "Leroy", age: 24, pets: "fish"] },
{ name: "Sally", age: 11, pets: "dog", "fish"] }
]
foreach person in persons
foreach pet in person.pets
where person.age < 20 && pet == "fish"
select distinct { Name: person.name, Age: person.age, Pets: person.pets }

 


707 characters (excluding the comments). Looking forward to seeing a more efficient option. 

/**
* @Intent - Fish Detector
* @description - Identify those that own fish and are under 20
*/
foreach device in network.devices
let persons = { name: "Tim", age: 21, pets: "dog", "mouse", "fish"] },
{ name: "Mark", age: 19, pets: "dog", "fish", "cat"] },
{ name: "Nina", age: 18, pets: "snake", "lizard"] },
{ name: "Leroy", age: 24, pets: "fish"] },
{ name: "Sally", age: 11, pets: "dog", "fish"] }
]
foreach person in persons
foreach pet in person.pets
where person.age < 20 && pet == "fish"
select distinct { Name: person.name, Age: person.age, Pets: person.pets }

 

The only tweak I see is to not involve network.devices. 

I replaced foreach device in network.devices with foreach x in 0]
 

And the usual things to do to minimize characters (not including comments in the NQE ….
 

foreach x in c0] let s = l{ n: "Tim", a: 21, t:  "dog", "mouse", "fish"]},{ n: "Mark", a: 19, t:  "dog", "fish", "cat"]},{ n: "Nina", a: 18, t:  "snake", "lizard"]},{ n: "Leroy", a: 24, t:  "fish"]},{ n: "Sally", a: 11, t:  "dog", "fish"]}] foreach p in s foreach t in p.t where p.a < 20 && t == "fish" select distinct {Name: p.n, Age: p.a, Pets: p.t}

 


Reply