Skip to content

Instantly share code, notes, and snippets.

@SahbiOuali13
Last active November 1, 2022 08:46
Show Gist options
  • Save SahbiOuali13/8ec57fdf68224132ae96a8f2a65aeb83 to your computer and use it in GitHub Desktop.
Save SahbiOuali13/8ec57fdf68224132ae96a8f2a65aeb83 to your computer and use it in GitHub Desktop.
# Sensitifity to types and letter_casing / whitespaces / characters ("" / '' / ...) - In practice, doctest is very strict when matching expected output with actual results. - For example, using integers instead of floating-point numbers will break t
# queue.py
from collections import deque
class Queue:
def __init__(self):
self._elements = deque()
def enqueue(self, element):
"""Add items to the right end of the queue.
>>> numbers = Queue()
>>> numbers
Queue([])
>>> for number in range(1, 4):
... numbers.enqueue(number)
>>> numbers
Queue([1, 2, 3])
"""
self._elements.append(element)
def dequeue(self):
"""Remove and return an item from the left end of the queue.
>>> numbers = Queue()
>>> for number in range(1, 4):
... numbers.enqueue(number)
>>> numbers
Queue([1, 2, 3])
>>> numbers.dequeue()
1
>>> numbers.dequeue()
2
>>> numbers.dequeue()
3
>>> numbers
Queue([])
"""
return self._elements.popleft()
def __repr__(self):
return f"{type(self).__name__}({list(self._elements)})"

Functions to Perform Arithmetic Calculations

The calculations.py Python module provides basic arithmetic operations, including addition, subtraction, multiplication, and division.

Here are a few examples of how to use the functions in calculations.py:

>>> import calculations

>>> calculations.add(2, 2)
4.0

>>> calculations.subtract(2, 2)
0.0

>>> calculations.multiply(2, 2)
4.0

>>> calculations.divide(2, 2)
1.0

These examples show how to use the calculations.py module in your code.

calculations.py

# calculations.py

"""Provide several sample math calculations.

This module allows the user to make mathematical calculations.

Module-level tests:
>>> add(2, 4)
6.0
>>> subtract(5, 3)
2.0
>>> multiply(2.0, 4.0)
8.0
>>> divide(4.0, 2)
2.0
"""

def add(a, b):
    """Compute and return the sum of two numbers.

    Tests for add():
    >>> add(4.0, 2.0)
    6.0
    >>> add(4, 2)
    6.0
    """
    return float(a + b)

def subtract(a, b):
    """Calculate the difference of two numbers.

    Tests for subtract():
    >>> subtract(4.0, 2.0)
    2.0
    >>> subtract(4, 2)
    2.0
    """
    return float(a - b)

def multiply(a, b):
    """Compute and return the product of two numbers.

    Tests for multiply():
    >>> multiply(4.0, 2.0)
    8.0
    >>> multiply(4, 2)
    8.0
    """
    return float(a * b)

def divide(a, b):
    """Compute and return the quotient of two numbers.

    Tests for divide():
    >>> divide(4.0, 2.0)
    2.0
    >>> divide(4, 2)
    2.0
    >>> divide(4, 0)
    Traceback (most recent call last):
    ZeroDivisionError: division by zero
    """
    return float(a / b)

python -m doctest -v README.md

Trying:
    import calculations
Expecting nothing
ok
Trying:
    calculations.add(2, 2)
Expecting:
    4.0
ok
Trying:
    calculations.subtract(2, 2)
Expecting:
    0.0
ok
Trying:
    calculations.multiply(2, 2)
Expecting:
    4.0
ok
Trying:
    calculations.divide(2, 2)
Expecting:
    1.0
ok
1 items passed all tests:
   5 tests in README.md
5 tests in 1 items.
5 passed and 0 failed.
Test passed.
# calculations.py
# ...
def divide(a, b):
"""Compute and return the quotient of two numbers.
Usage examples:
>>> divide(84, 2)
42.0
>>> divide(15, 3)
5.0
>>> divide(42, -2)
-21.0
>>> divide(42, 0)
Traceback (most recent call last):
ZeroDivisionError: division by zero
"""
return float(a / b)
def greet(name="World"):
"""Print a greeting.
Usage examples:
>>> greet("Pythonista")
Hello, Pythonista!
<BLANKLINE>
How have you been?
"""
print(f"Hello, {name}!")
print()
print("How have you been?")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment