Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Coding puzzle 2
Message
From
04/06/2002 21:57:30
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
 
General information
Forum:
Visual FoxPro
Category:
Other
Title:
Miscellaneous
Thread ID:
00664570
Message ID:
00664800
Views:
14
>Ok, that 'puzzle' from Hilmar was far too easy (took me 10 seconds, ok .. 30).
>
>Here's a nice contest. Who can write the shortest function (using native vfp functions/commands) that returns a person's age. Input is the birthday and the current date. The winner will get 3 points from me!

Meneer Peter,

I came up with three different versions of the age function.

Version 1 is the fastest.

Version 2 is slower to execute, but execution speed wasn't specified as a criterion.

Version 3 is the shortest of them all, but won't work for people more than ca. 100 years old. It is only good for "showing off". However, I think it is correct for younger people.
* Version 1
function age(d)
y=year(date())-year(d)
retu y-iif(gomo(d,y*12)<date(),0,1)

* Version 2
function age(d)
y=0
do whil d<date()
	d=gomo(d,12)
	y=y+1
endd
retu y-1

* Version 3
function age(d)
retu iif(gomo(d,12)>=date(),0,age(gomo(d,12))+1)
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform