Task 1 - Pythagorean means
I'm never quite sure what is wanted or considered good in these tasks. The options seem to be:
- There's a module to do this
- I can do this in one somewhat inscrutable line
- I can do this in a readable way with lots of comments
If we're aiming to save coder time and not reinventing the wheel, then option 1 is clearly correct. I would almost always use that in a production environment.
Option 2 gives people a chance to demonstrate how compact - and maybe fast - Perl can be, which is a valid aim. If the code is never going to be changed then it's fine, but I suspect that whoever has to come along later and maintain it - even if that's you - is going to scratch their heads for a while.
Option 3 is the way to go if you're trying to write easily maintainable code. It may not be the fastest solution - although often it is easily fast enough.
So for this task I went squarely for option 3: code it just as it's explained in Wikipedia, ie take the list elements one at a time and produce a running sum, product and sum of reciprocals.
And as always, I try to produce output just as Mohammad specifies.
Task 2 - Brazilian numbers
I've never been to Brazil, but maybe that's not necessary. I have to say I didn't find the statement of the task very easy to understand, but as always Wikipedia came to my aid.
My solution simply checks $n in every base up to $n/2. You can stop there, because any higher base will give representations between 1x (where x is the representation of (base - 1), eg 9 in decimal) down to 11. That means that I don't quite replicate Mohammad's example output because, for example, I don't check 6 base 4 because I know it won't be a repeating number.
And another reason for not going beyond base $n/2 is that for base $n you need $n symbols to represent a number. I used 0-9, A-Z, a-z and then 10 punctuation symbols, giving 72 symbols and - using my algorithm - the ability to check $n up to about 200.
No comments:
Post a Comment