Re: A puzzle
Re: A puzzle
- Subject: Re: A puzzle
- From: Simon Topliss <email@hidden>
- Date: Fri, 21 Nov 2008 16:49:02 +0000
On 21 Nov 2008, at 15:59, Michelle Steiner wrote: Your car odometer has 6 digits, from 000000 to 999999.
If your car made it to 1,000,000 miles, how many times would the
number 1 have shown up? My first Applescript solution was: set counter to 0 repeat with i from 0 to 999999 set x to i as string if "1" is in x then repeat with j in x if contents of j is "1" then set counter to counter + 1 end if end repeat end if end repeat counter
It took 63 seconds on my machine.
This cut it down to 58 seconds:
set counter to 0 repeat with i from 0 to 999999 set x to i as string if "1" is in x then repeat with j from 1 to length of x if character j of x is "1" then set counter to counter + 1 end if end repeat end if end repeat counter
They're both brute force, and I have a gut feeling that there's a more elegant method that I'm overlooking.
I'm sure someone will do better but I got it down to 5.4 seconds.
set counter to 0 script k property l : 999999 end script repeat with i from 0 to k's l set x to k's l if 1 is in x then repeat with j from 0 to x if j is 1 then set counter to counter + 1 end if end repeat end if end repeat counter
Simon
|
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
References: | |
| >A puzzle (From: Michelle Steiner <email@hidden>) |