This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Optional | |
from collections import deque | |
class Node: | |
def __init__(self, value: int): | |
self.value = value | |
self.left = None | |
self.right = None | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
with open('cancel.csv') as f: | |
reader = csv.DictReader(f) | |
words = {} | |
for row in reader: | |
reason = row['Reason'] | |
if reason == 'System cancelled - non payment': | |
continue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dice_distribution(num_sides: int) -> dict[int, float]: | |
""" | |
Get the distribution of possible results of two thrown dice | |
""" | |
distribution = {} | |
for x in dice_generator(num_sides): | |
for y in dice_generator(num_sides): | |
value = x + y | |
distribution.setdefault(value, 0) | |
distribution[value] += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz(stop: int): | |
"""On multiples of 3, print 'fizz' | |
On multiples of 5, print 'buzz' | |
On multiples of both, print 'fizzbuzz' | |
For all other numbers, print the number | |
""" | |
result = [] | |
for x in range(1, stop+1): | |
if x % 15 == 0: | |
result.append('fizzbuzz') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from celery_tutorial.celery import app | |
from .models import Calculation | |
def fib(n): | |
"""Calculate the Nth fibonacci number""" | |
if n < 0: | |
raise ValueError('Negative numbers are not supported') | |
elif n == 0: | |
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render, redirect | |
from django.views import View | |
from .models import Calculation | |
from .tasks import fibonacci_task | |
class FibonacciView(View): | |
def get(self, request): | |
"""Show a form to start a calculation""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Calculation(models.Model): | |
"""Track a calculation and its results""" | |
EQUATION_FIBONACCI = 'FIB' | |
EQUATIONS = ((EQUATION_FIBONACCI, 'Fibonacci'),) | |
STATUS_PENDING = 'PENDING' | |
STATUS_ERROR = 'ERROR' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fib(n): | |
"""Calculate the Nth fibonacci number. | |
Intentionally don't use dynamic programming. Goal is to simulate a long-running task. | |
""" | |
if n < 0: | |
raise ValueError('Negative numbers are not supported') | |
elif n == 0: | |
return 0 | |
elif n <= 2: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Map, filter, and reduce are all operations that we can apply against a list, tuple, or sequence | |
# These types of operations follow the "functional" approach to programming | |
# I won't go into too much detail here, but a Google search of "functional vs OOP" will give you a synopsis | |
# Here are some examples. We can talk more about them when we chat. | |
# Let's start with a simple list... | |
>>> my_list = [0, 1, 2, 3, 4, 5] | |
# Using filter, we can filter values out of the list, returning a new list! | |
# (If you haven't seen lambdas yet, don't worry! Basically, this filter uses modulo 2 to check if the number is even/odd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from io import StringIO | |
from django.core.management import call_command | |
from django.test import TestCase | |
class MyCommandTest(TestCase): | |
def test_my_command(self): | |
out = StringIO() # <-- Use StringIO to capture the output of the command from stdout | |
args = [] # <-- Any command line arguments go here | |
kwargs = {'stdout': out} # <-- Any named command line options (e.g. --my-option) go here | |
NewerOlder