Skip to main content

Hello NQE Enthusiasts, 

Several Months ago we posted a challenge to use NQE to solve the FizzBuzz challenge . We had so much fun we thought we would make it a reoccurring exercise to demonstrate the power of NQE and and have some more fun with it. 

You might be asking  “What are koans”?

"koans" refer to a series of small coding challenges designed to teach programming concepts, often in a particular language. 

The term originates from Zen Buddhism, where a "koan" is a paradoxical question or statement used to provoke deep thought and insight.


We are looking forward to see how everyone solves these challenges so tune in and give it a try, there are no wrong answers 🙂.


Challenge

Your task is to calculate the age of a list of people not in human years but in “dog years”. Assuming that one human year is equivalent to seven dog years. Calculate for the following input.


Input
 

people = - 
{ Name: "Tom", Age: 25 },
{ Name: "Jerry", Age: 30 },
{ Name: "Spike", Age: 15 }
];


Expected Output:
 

= 
{ Name: "Tom", DogAge: 175 },
{ Name: "Jerry", DogAge: 210 },
{ Name: "Spike", DogAge: 105 }
]



Hints:

I think you can figure it out

Bonus:
 

For bonus points, use the AVMA’s method of calculating dog-to-human years:

The American Veterinary Medical Association (AVMA) suggests the following guideline: the first year of a medium-sized dog's life is equal to 15 human years, the second year is about 9 human years, and each additional year is about five human years.

Contributions: @deidremccl 

I’m assuming the full years are needed for the alternative age calculation.


dogs =
{ Name: "Tom", Age: 25 },
{ Name: "Jerry", Age: 30 },
{ Name: "Spike", Age: 15 }
];

foreach dog in dogs
let avma = if dog.Age >= 2 then dog.Age * 5 + 14 else
if dog.Age >= 1 then dog.Age * 5 + 10 else
dog.Age * 5
select { Name: dog.Name, DogAge: dog.Age * 7, avma: avma }

Full transparency, I generated this with AI Assist with modifications to include Carl and MrTeeth in the data set to check my year 1 and 2 calculation and modifying the Select statement output to include the 7 year average age calculation (borrowed from @AndyL  - thanks!). 

/**
* This query was generated by AI Assist.
* As with most things AI, it may not always be completely accurate. Carefully review and verify it.
* You can use it as a starting point and build on top.
* Alternatively, you can try to refine your prompt and regenerate.

/ \__
( @\___
/ O
/ (_____/
/_____/ U
*/
dogs =
b{ Name: "Tom", Age: 25 },
{ Name: "Jerry", Age: 30 },
{ Name: "MrTeeth", Age: 1 },
{ Name: "Carl", Age: 2 },
{ Name: "Spike", Age: 15 }
];

//
//// The first year of a dog's life is 15 human years
firstYearFactor = 15;

//// The second year of a dog's life is 9 human years
secondYearFactor = 9;

//// Each subsequent year is 5 human years
subsequentYearFactor = 5;
//

foreach dog in dogs
let age = dog.Age
let AVMAhumanYears = if age < 2
then firstYearFactor
else if age == 2
then firstYearFactor + secondYearFactor
else firstYearFactor + secondYearFactor +
(age - 2) * subsequentYearFactor
select {
Dog: dog.Name,
"Dog Age": age,
"Dog Average Human Age": age * 7,
"AVMA Age": AVMAhumanYears
}

 


Rather than a maintainable and comprehensible sequence of if-then-else clauses, we can be silly but terse:

dogs =
[{ Name: "Tom", Age: 25 },
{ Name: "Jerry", Age: 30 },
{ Name: "MrTeeth", Age: 1 },
{ Name: "Carl", Age: 2 },
{ Name: "Spike", Age: 15 }
];

foreach d in dogs
let HumanAge = 15 * min([d.Age, 1]) + 9 * min([d.Age -1, 1]) + 5 * max([0, d.Age - 2])
select {Name: d.Name, HumanAge }

 


@musa shared another solution with me - this sleek Iverson bracket inspired script. 

 

given(booleanExpr) = if booleanExpr then 1 else 0;

humanAge(x) = 15 * given(x >= 1) + 9 * given(x >=2) + 5 * (x - 2) * given(x > 2);

dogs =
/{ Name: "Tom", Age: 25 },
{ Name: "Jerry", Age: 30 },
{ Name: "MrTeeth", Age: 1 },
{ Name: "Carl", Age: 2 },
{ Name: "Spike", Age: 15 }
];

foreach d in dogs select {Name: d.Name, HumanAge: humanAge(d.Age) }

This was new to me so here’s a little background on Iverson bracket notation:

The Iverson bracket is a mathematical notation introduced by Kenneth E. Iverson, commonly used in combinatorics, linear algebra, and computer science. It is defined as follows:

"P]

where P is a logical statement or condition. The Iverson bracket evaluates to:

- 1 if the statement P  is true
- 0 if the statement P is false

For example:

  • If x=5 then =x>3] becomes 1 because 5 is greater than 3.
  • If x=2 then >x>3] becomes 0 because 2 is not greater than 3.

This notation is helpful for expressing formulas in a concise and efficient way, often eliminating the need for conditional statements.

 

I learned something - thanks @musa!


Reply