Chalarangelo / 30-seconds-of-python

Short Python code snippets for all your development needs
https://www.30secondsofcode.org/python/p/1
Creative Commons Attribution 4.0 International
8.83k stars 1.26k forks source link

[QUESTION]Whether to pay attention to parameter checking and exception handling #207

Closed zhangfelix closed 4 years ago

zhangfelix commented 4 years ago

Should we pay attention to parameter checking and exception handling, or should we focus more on showing a possible idea for solving the problem?

For example in lcm.md, the current implementation has the potential to cause a divide by zero exception.

Also, by definition, lcm should be a positive integer. However, the current implementation may result in the return of a negative number.

Current implementation

from functools import reduce
from math import gcd

def lcm(numbers):
  return reduce((lambda x, y: int(x * y / gcd(x, y))), numbers)
Chalarangelo commented 4 years ago

Hey there!

The decision to not check for errors and edge cases was made a long time ago when we started these projects and the reason behind it was to keep them simple and showcase interesting techniques and ways of thinking. All snippets expect the user to have a basic understanding of the problem, the language and potential errors that might occur, thus we are not adding exception handling or parameter checking to snippets.

Thank you.