If you receive correct results, then the question is off topic for Stack Overflow. If you confirm the results are correct, and are only asking for a review of your code in order to make it better in terms of efficiency, elegance, best practice, ... then you could consider visiting
Code Review, but make sure to read their guidelines, which are different from those of Stack Overflow. —
trincot 34 secs ago