How close my solutions of the Algorithm Scriptings should match the Wiki?

Hi all,

After completing “Validate US Telephone Numbers” I looked at basically two line solution with regexp on the Wiki and compared it to my 25 lines of code using three if statements and one for loop.

I also solved factorial with a for loop, but used recursion on array flattening etc.

So my question is - should I go back and do the assignments “the right way” or is it ok “as is” as long as the code passes the tests? Am I impeding my learning by doing the challenges “my way”?

1 Like

The simple answer is: if all the tests pass, then your code is functionally complete. Congratulations! You’ve earned yourself a refreshing beverage.

A more complete answer is: no, you’re not done yet. For good programmers, passing the tests is only the first step in finishing their work. Passing tests gives them confidence that they understand the problem, and it gives them a way to verify that any changes they make in the future will still yield valid code. Why change code that already works? Several reasons:

  1. Efficiency, either time-wise (making it run faster) or memory-wise (reducing bloat).
  2. Legibility: making your code easier to understand by others.
  3. Repetition: reducing unnecessarily repeated code (the Don’t Repeat Yourself principle).
  4. Modularity: making your code reusable by other people and/or in other contexts.
  5. Consistency: if there’s a particular way others have done something similar, you should probably do it that way too (e.g., the [Pythonic way] (http://docs.python-guide.org/en/latest/writing/style/)).
  6. Learning: if there are multiple ways to do something, why not learn them so you can see the pros and cons of each?

All of these come under the general concept of [refactoring] (https://en.wikipedia.org/wiki/Code_refactoring). For the bible on this subject, see Martin Fowler’s classic book [Refactoring: Improving the Design of Existing Code] (http://www.amazon.com/Refactoring-Improving-Design-Existing-Code/dp/0201485672/ref=asap_bc?ie=UTF8).

4 Likes