Examples Settle What Words Don't
“£6 a half hour” is clear to Mia: every started half hour counts, as in Get Code Explained, so 31 minutes cost £12. The words don't say that, though. What does a walk of 45 minutes cost: £9, for one and a half half hours, or £12, for two started ones? The AI has to guess, and it doesn't always guess the same way. In the chat of the last exercise, most answers began like this one, whose function gives 9.0 for 45 minutes:
Mia charges £6 per half hour (30 minutes), so the price scales linearly with time. Here's the function:Sent in a new chat, the same message got answers that rounded up instead, and many said it was a guess:
Mia charges £6 per half hour. I've assumed that a partial half hour is charged as a full one, so a 31-minute walk costs two half hours.Each of them also ended with the other way, such as If Mia instead charges pro rata (so 45 minutes costs £9)…, which left the choice to you.
Examples settle what words don't. A number can't be read two ways: “31 minutes cost £12” says exactly what “a started half hour” means. Pick examples where the rule changes, such as 30 and 31 minutes or 60 and 61, because that's where a function most often goes wrong. They're also how you check the function: the program in the editor prints the prices of 30, 45 and 75 minutes, and you know what each should be.
A function can fit your examples and still be wrong for other numbers, such as one that knows only the walks you named. So the exercise also runs your program with other minutes, and if the function gets them wrong, it tells you which lines it changed and what the program printed then.
Instructions
- 1
The message in the chat box is the one from the last exercise. Add examples that show what Mia means: 30 minutes cost £6, 31 minutes cost £12, 45 minutes cost £12, 60 minutes cost £12 and 61 minutes cost £18. Then send it.
- 2
Copy the function and paste it in place of the comment. Keep the three lines that print, and run the program: it prints
Price: 6,Price: 12andPrice: 18.