|
| 1 | +# Concepts of `hamming` |
| 2 | + |
| 3 | +## Example implementation |
| 4 | + |
| 5 | +From the current [example.py](https://github.com/exercism/python/blob/master/exercises/hamming/example.py): |
| 6 | + |
| 7 | +``` python |
| 8 | +def distance(s1, s2): |
| 9 | + if len(s1) != len(s2): |
| 10 | + raise ValueError("Sequences not of equal length.") |
| 11 | + |
| 12 | + return sum(a != b for a, b in zip(s1, s2)) |
| 13 | +``` |
| 14 | + |
| 15 | +## Concepts |
| 16 | + |
| 17 | +- [Function definition][function-definition]: functions are defined and named using the `def` keyword |
| 18 | +- [Function signature][function-signature]: functions take named arguments which are accessible within the body of the function; this one requires the student to make a function that accepts 2 |
| 19 | +- [Return value][return-value]: the function must return a number (int) |
| 20 | +- [Strings][strings]: strings are used generally |
| 21 | +- [Builtin functions][builtin-functions]: strings have a length, accessible by calling `len()`, a builtin python function |
| 22 | +- [Iterable][iterable]: strings are iterable, which provides a lot of opportunity to leverage Python functions against them |
| 23 | +- [Immutable][immutable]: strings are immutable (*immutability*) |
| 24 | +- [Booleans][booleans]: this solution uses Boolean values (`True` / `False`) |
| 25 | +- [Inequality][inequality]: this solution checks if `a` is not equal to `b`. |
| 26 | +- [Booleans are integers][booleans-are-integers]: Booleans values are just named aliases for the integers 1 (`True`) and 0 (`False`) |
| 27 | +- [Zip][zip]: builtin that joins multiple iterables into a single one |
| 28 | +- [Enumeration][enumeration]: `zip()` in this solution creates an iterable, which is iterated over by using the `for ... in ` syntax |
| 29 | +- [Sum][sum]: another builtin that operates on iterables |
| 30 | +- [Tuple unpacking][tuple-unpacking]: the values in an iterable can be unpacked into variables and used, i.e. `for a, b in zip(s1, s2)` |
| 31 | +- [Exception handling][exception-handling]: the exercise requires Exception handling |
| 32 | +- [Raise][raise]: the student is required to raise an `Exception` for incorrect input |
| 33 | +- [Exception hierarchy][exception-hierarchy]: the idiomatic `Exception` type is a `ValueError`, meaning the input is incorrect |
| 34 | +- [Exception catching][exception-catching]: `Exceptions` can be caught from outside the scope where they are raised, using the `try/except` syntax. All `Exceptions` types inherit from the base class, `Exception` and thus can be caught by either checking specifically for the type of Exception, or for any Exception |
| 35 | +- [Exception message][exception-message]: Custom error messages can (and should) be supplied to an Exception when raised |
| 36 | +- [Operators][operators]: `!=` is "not equal", which is not the same thing as `is`, or an identity check, but is the inverse of `==`, which is equality |
| 37 | +- [Loops][loops]: the `for ... in` syntax is useful for looping through a list or other iterable object |
| 38 | +- [Generators][generators]: generators calculate then `yield` a value one at a time, as opposed to lists which calculate and return all values in memory at once. A generator will pick up where it leaves off, and generate one item at a time, on demand |
| 39 | +- [Generator comprehension][generator-comprehension]: a generator comprehension is passed to `sum()` to drive summation without storing all the values in a list first |
| 40 | +- [Tuple unpacking][tuple-unpacking]: iterating through a list of tuples, i.e. [(1, 2), (2,3)], each piece of each tuple can be unpacked into a separate variable (syntax: `a, b = (1, 2)`); this works for any sort of iterable (lists, for example, and even strings!) but is commonly used with tuples because they are typically of a known size/length, and so can be safely unpacked into N variables, with names. |
| 41 | +- [Dunder Methods][dunder-methods]: "dunder" -> "double under", referring to the names of these methods being prefixed with two underscores, e.g. `__init__`. There is no formal privacy in Python, but conventionally a single underscore indicates a private method, or one that the programmer should assume may change at any time; methods without an underscore are considered part of an object's public API. Double underscores are even more special - they are used by Python's builtin functions like `len()`, for example, to allow objects to implement various interfaces and functionality. They can also be used for operator overloading. If you have a custom class that you would like to be able to compare to other instances of the same class, implementing `__lt__`, `__gt__`, `__eq__` etc. allow programmers to use the `>`, `<`, `=` operators. Dunder methods allow programmers to build useful objects with simple interfaces, i.e. you can add two instances together using `+` instead of writing something like `instance1.add(instance2)`. |
| 42 | +- [Builtin Function][builtin-functions]: Python has several handy builtin functions in the stdlib that can operate on many types of data, e.g. `len()`, `max()`, `min()`. Under the hood these are implemented via dunder methods - if an object (and everything in Python is an object) implements the correct dunder methods (see that topic for more information), it can support use in these functions. (For example, if an object implements `__len__`, the len(<object>) will return that value.) Because these functions are not strictly tied to any data type, they can be used almost anywhere, and will crop up again and again as we learn Python. Docs: https://docs.python.org/3/library/functions.html |
| 43 | +- [Polymorphism][polymorphism]: Python is "dynamically typed," meaning that variable names are bound to objects only, not to a particular type. You can assign `foo` to a string, and then reassign it to an `int` with no issues. "Polymorphism" formally means that different types respond to the same function - so the ability to add custom class instances together using `+`, for example, shows how Python can define the same function against different types. |
| 44 | +- [Duck Typing][duck-typing]: Python is also a good example of "Duck typing," to wit, "if it walks like a duck, talks like a duck, it's a duck.". This is accomplished partly with "magic" or "dunder" methods (double-under) that provide various interfaces to an object. If an object implements `__iter__` and `__next__`, it can be iterated through; it doesn't matter what type the object actually is. |
| 45 | + |
0 commit comments