A tutorial is not a long talk
Mathspp Blog
by
2w ago
In this opinion piece the author expresses his views on what makes a good (Python) tutorial and how that differs from a long talk. A tutorial is not a long talk Bart Simpson internalising the message of this opinion piece. The title of the article should say it all but I'll elaborate a bit on what I mean by “a tutorial is not a long talk” and I'll share what I think makes a good tutorial. I've been to talks and tutorials at different conferences (mostly Python conferences) and I've attended great talks, great tutorials, terrible talks, and terrible tutorials. I've seen it all. Why do tal ..read more
Visit website
Longest unique substring
Mathspp Blog
by
1M ago
How can you find the longest substring that contains only unique characters? Longest unique substring In this article I'll share a coding challenge with you. Then, I'll analyse different approaches to solving this problem, talking about the advantages and disadvantages of each one in terms of efficiency and code quality. Problem statement Given a string, find the longest substring that contains only unique characters. In case of a length tie, return the first one. Here are some examples to test your code with: def longest_unique_substring(string): ... lus = longest_unique_substring a ..read more
Visit website
TIL #093 – operator.methodcaller
Mathspp Blog
by
1M ago
Today I learned how to use the function operator.methodcaller. operator.methodcaller The function methodcaller from the module operator is similar to the functions itemgetter and attrgetter. Its only required argument is a string with the name of a method and its return value is a function that calls the specified method on the given object. You can see a basic example below: >>> from operator import methodcaller >>> l = [4, 2, 3, 1] >>> sorter = methodcaller("sort") >>> sorter(l) # vs. l.sort() >>> l [1, 2, 3, 4] Naturally, this works with arb ..read more
Visit website
TIL #092 – dunder method __init_subclass__
Mathspp Blog
by
1M ago
Today I learned how to use the dunder method __init_subclass__ to be notified when a class is subclassed. __init_subclass__ The dunder method __init_subclass__ is a class method that Python runs when a subclass of that class is instantiated. The snippet below sums it all: class Parent: def __init_subclass__(cls): print(f"Subclass {cls} was created.") class A(Parent): pass class B(A): pass """ Output: Subclass <class '__main__.A'> was created. Subclass <class '__main__.B'> was created. """ The code above shows that when subclasses are created (even if they ..read more
Visit website
TIL #091 – issue user warnings
Mathspp Blog
by
1M ago
Today I learned how to issue user warnings like DeprecationWarnings or SyntaxWarnings. Issue user warnings Every once in a while I see a DeprecationWarning when I'm doing some coding. Last time I saw it, I think it was in some pandas code in my pandas and matplotlib tutorial with Pokémon. Today I needed to issue a similar warning and I set out to find out how that works. Turns out there is a module in the standard library (obviously) that does this: warnings. Issuing a warning with the module warnings can be as simple as passing a string to the function warnings.warn: >>> import war ..read more
Visit website
Deducing physics formulas with genetic algorithms
Mathspp Blog
by
1M ago
This tutorial shows how to use a simple genetic algorithm to deduce physics formulas. Deducing physics formulas with genetic algorithms What's a genetic algorithm? A genetic algorithm is an algorithm that borrows ideas from Darwin's theory of evolution to find solutions to optimisation problems. So, if you have a problem and if you have a way of determining how good or bad a solution is to that problem, then a genetic algorithm can be used to run a simulation that tries to find better solutions to that problem. In order to be able to run a genetic algorithm, you need to be able to do a couple ..read more
Visit website
Biggest square
Mathspp Blog
by
2M ago
How can you find the biggest free square in a 2D map with obstacles? Biggest square Problem statement I just got a call from the President of Portugal. Portugal recently mapped out the Atlantic Ocean to the West of our coast. They want to build a sea platform to study the ecosystems of the Ocean. They need my help to figure out where to build that platform. To show you what I'm working with, here is a portion of the map they created: ..o.. .o..o ..... Each character represents a small square section of the sea. The dots . represent sections where the sea bed is uniform whereas the circles o ..read more
Visit website
N queens problem
Mathspp Blog
by
2M ago
This article shows how to solve the N queens problem in 20 lines of code. N queens problem The problem for N = 10 (You can skip to the solution or just see the code.) “Do you understand what you have to do?”, Queen #6 asks. “Uh, I– I think so.” The challenge sounded simple enough but I was a bit intimidated by the Ten Queens all looking at me. They stood tall in front of me. An impeccable pose, worthy of a Queen. Times ten. They looked rigid and cold, wearing their white and black dresses. But if you looked carefully enough, they also looked… They looked… hopeful! They believed I would be abl ..read more
Visit website
TIL #090 – patching module globals with pytest
Mathspp Blog
by
3M ago
Yesterday I spent the whole day tryint to patch a module global. This is what I ended up with. Patching module globals with pytest Suppose you have a small project that has an April Fool's joke set up: # constants.py APRIL_FOOLS = False # useful_code.py from constants import APRIL_FOOLS def add(a, b): if APRIL_FOOLS: result = a - b else: result = a + b return result Now, suppose you want to test your April Fool's joke with pytest. One thing I found online was that you could use the fixture monkeypatch and its method setattr, something like this: # test_usef ..read more
Visit website
Python deque tutorial
Mathspp Blog
by
3M ago
This tutorial shows how to work with the Python data structure collections.deque, with interactive examples. Python deque tutorial What is a deque? A deque in Python is a data structure from the module collections. A deque is often compared to a Python list because they are both ordered containers that let you append and pop elements from the right efficiently. The code below shows the similarities: from collections import deque my_list = [] my_deque = deque() # Create a `deque`. my_list.append(1) my_deque.append(1) # Append an element to its end. my_list.extend(range(2, 5)) my_deque.ex ..read more
Visit website

Follow Mathspp Blog on FeedSpot

Continue with Google
Continue with Apple
OR