Answer:
import re

def parse_ymd(s):
 m = re.fullmatch(r"(\d{4})-(\d{2})-(\d{2})", s)
 if not m:
 raise ValueError(s)
 y, mo, d = (int(m.group(i)) for i in range(1, 4))
 if not 1 <= mo <= 12 or not 1 <= d <= 31:
 raise ValueError(s)
 return y, mo, d

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

import re

def parse_ymd(s):
    m = re.fullmatch(r"(\d{4})-(\d{2})-(\d{2})", s)
    if not m:
        raise ValueError(s)
    y, mo, d = (int(m.group(i)) for i in range(1, 4))
    if not 1 <= mo <= 12 or not 1 <= d <= 31:
        raise ValueError(s)
    return y, mo, d

import unittest
from solution import parse_ymd

class Test(unittest.TestCase):
    def test_ok(self):
        self.assertEqual(parse_ymd("2026-08-18"), (2026, 8, 18))
    def test_bad(self):
        with self.assertRaises(ValueError):
            parse_ymd("2026-13-01")

Read the specification
Implement the function or class
Satisfy the tests

Prompt:
Implement `parse_ymd(s)` parsing YYYY-MM-DD into (y,m,d) ints. Validate
month 1-12 and day 1-31 (do not validate month lengths).


Answer:
from collections import deque

class ExpiredQueue:
 def __init__(self):
 self.q = deque()

 def push(self, t, item):
 self.q.append((t, item))

 def pop_ready(self, now):
 out = []
 while self.q and self.q[0][0] <= now:
 out.append(self.q.popleft()[1])
 return out

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

from collections import deque

class ExpiredQueue:
    def __init__(self):
        self.q = deque()

    def push(self, t, item):
        self.q.append((t, item))

    def pop_ready(self, now):
        out = []
        while self.q and self.q[0][0] <= now:
            out.append(self.q.popleft()[1])
        return out

import unittest
from solution import ExpiredQueue

class Test(unittest.TestCase):
    def test_pop(self):
        q = ExpiredQueue()
        q.push(2, "a")
        q.push(5, "b")
        self.assertEqual(q.pop_ready(3), ["a"])
        self.assertEqual(q.pop_ready(4), [])
        self.assertEqual(q.pop_ready(5), ["b"])

Read the specification
Implement the function or class
Satisfy the tests

Prompt:
Implement `ExpiredQueue` with `push(t, item)` and `pop_ready(now)` returning
all items with t <= now in insertion order and removing them.

OPEN_REASON_RESULT {"tests_run": 1, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
from collections import deque

class ExpiredQueue:
 def __init__(self):
 self.q = deque()

 def push(self, t, item):
 self.q.append((t, item))

 def pop_ready(self, now):
 out = []
 while self.q and self.q[0][0] <= now:
 out.append(self.q.popleft()[1])
 return out

Do not weaken or delete tests
Keep the public API

Output:
OPEN_REASON_RESULT {"tests_run": 1, "failures": 1, "errors": 0, "skipped": 0, "passed": false}

test_pop (test_solution.Test.test_pop) ... FAIL

======================================================================
FAIL: test_pop (test_solution.Test.test_pop)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-zh86l1rc\test_solution.py", line 11, in test_pop
    self.assertEqual(q.pop_ready(5), ["b"])
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Lists differ: [] != ['b']

Second list contains 1 additional elements.
First extra element 0:
'b'

- []
+ ['b']

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

Seeded mutation of the reference implementation.

Reproduce the failure
Identify the defect
Apply a minimal fix
Re-run tests

Prompt:
The following Python module fails its tests. Produce a corrected solution.py that preserves the intended behaviour.

Implement `ExpiredQueue` with `push(t, item)` and `pop_ready(now)` returning
all items with t <= now in insertion order and removing them.

--- solution.py (buggy) ---
from collections import deque

class ExpiredQueue:
 def __init__(self):
 self.q = deque()

 def push(self, t, item):
 self.q.append((t, item))

 def pop_ready(self, now):
 out = []
 while self.q and self.q[0][0] < now:
 out.append(self.q.popleft()[1])
 return out

--- test_solution.py ---
import unittest
from solution import ExpiredQueue

class Test(unittest.TestCase):
 def test_pop(self):
 q = ExpiredQueue()
 q.push(2, "a")
 q.push(5, "b")
 self.assertEqual(q.pop_ready(3), ["a"])
 self.assertEqual(q.pop_ready(4), [])
 self.assertEqual(q.pop_ready(5), ["b"])

--- failure ---
OPEN_REASON_RESULT {"tests_run": 1, "failures": 1, "errors": 0, "skipped": 0, "passed": false}

test_pop (test_solution.Test.test_pop) ... FAIL

======================================================================
FAIL: test_pop (test_solution.Test.test_pop)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-zh86l1rc\test_solution.py", line 11, in test_pop
 self.assertEqual(q.pop_ready(5), ["b"])
 ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Lists differ: [] != ['b']

Second list contains 1 additional elements.
First extra element 0:
'b'

- []
+ ['b']

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)


Answer:
def wrapping_checksum(data):
 return sum(data) % 256

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def wrapping_checksum(data):
    return sum(data) % 256

import unittest
from solution import wrapping_checksum

class Test(unittest.TestCase):
    def test_sum(self):
        self.assertEqual(wrapping_checksum(bytes([255, 2])), 1)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `wrapping_checksum(data: bytes)` as (sum of bytes) mod 256.


Answer:
def block_depth(lines):
 out = []
 for line in lines:
 if not line.strip():
 continue
 spaces = len(line) - len(line.lstrip(" "))
 if spaces % 2:
 raise ValueError("indent")
 out.append(spaces // 2)
 return out

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def block_depth(lines):
    out = []
    for line in lines:
        if not line.strip():
            continue
        spaces = len(line) - len(line.lstrip(" "))
        if spaces % 2:
            raise ValueError("indent")
        out.append(spaces // 2)
    return out

import unittest
from solution import block_depth

class Test(unittest.TestCase):
    def test_d(self):
        self.assertEqual(block_depth(["a", "  b", "    c"]), [0, 1, 2])

Read the specification
Implement the function or class
Satisfy the tests

Prompt:
Implement `block_depth(lines)` using leading 2-space indents. Return a list
of depths per non-empty line. Raise ValueError if indent is not a multiple of 2.


Answer:
def fresh_names(vars):
 seen = {}
 out = []
 for name in vars:
 n = seen.get(name, 0)
 out.append(name if n == 0 else f"{name}#{n}")
 seen[name] = n + 1
 return out

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def fresh_names(vars):
    seen = {}
    out = []
    for name in vars:
        n = seen.get(name, 0)
        out.append(name if n == 0 else f"{name}#{n}")
        seen[name] = n + 1
    return out

import unittest
from solution import fresh_names

class Test(unittest.TestCase):
    def test_ren(self):
        self.assertEqual(fresh_names(["x", "y", "x"]), ["x", "y", "x#1"])

Read the specification
Implement the function or class
Satisfy the tests

Prompt:
Implement `fresh_names(vars)` assigning x, x#1, x#2, ... in order of first
appearance counts: given a list of variable uses, return a list of renamed
occurrences where the k-th use of name `v` becomes `v` then `v#1` etc.


Answer:
def fresh_names(vars):
 seen = {}
 out = []
 for name in vars:
 n = seen.get(name, 0)
 out.append(name if n == 0 else f"{name}#{n}")
 seen[name] = n + 1
 return out

Do not weaken or delete tests
Keep the public API

Output:
OPEN_REASON_RESULT {"tests_run": 1, "failures": 1, "errors": 0, "skipped": 0, "passed": false}

test_ren (test_solution.Test.test_ren) ... FAIL

======================================================================
FAIL: test_ren (test_solution.Test.test_ren)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-wlxnxm_n\test_solution.py", line 6, in test_ren
    self.assertEqual(fresh_names(["x", "y", "x"]), ["x", "y", "x#1"])
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Lists differ: ['x#0', 'y#0', 'x'] != ['x', 'y', 'x#1']

First differing element 0:
'x#0'
'x'

- ['x#0', 'y#0', 'x']
?    --     --

+ ['x', 'y', 'x#1']
?              ++

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

Seeded mutation of the reference implementation.

Reproduce the failure
Identify the defect
Apply a minimal fix
Re-run tests

Prompt:
The following Python module fails its tests. Produce a corrected solution.py that preserves the intended behaviour.

Implement `fresh_names(vars)` assigning x, x#1, x#2, ... in order of first
appearance counts: given a list of variable uses, return a list of renamed
occurrences where the k-th use of name `v` becomes `v` then `v#1` etc.

--- solution.py (buggy) ---
def fresh_names(vars):
 seen = {}
 out = []
 for name in vars:
 n = seen.get(name, 0)
 out.append(name if n != 0 else f"{name}#{n}")
 seen[name] = n + 1
 return out

--- test_solution.py ---
import unittest
from solution import fresh_names

class Test(unittest.TestCase):
 def test_ren(self):
 self.assertEqual(fresh_names(["x", "y", "x"]), ["x", "y", "x#1"])

--- failure ---
OPEN_REASON_RESULT {"tests_run": 1, "failures": 1, "errors": 0, "skipped": 0, "passed": false}

test_ren (test_solution.Test.test_ren) ... FAIL

======================================================================
FAIL: test_ren (test_solution.Test.test_ren)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-wlxnxm_n\test_solution.py", line 6, in test_ren
 self.assertEqual(fresh_names(["x", "y", "x"]), ["x", "y", "x#1"])
 ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Lists differ: ['x#0', 'y#0', 'x'] != ['x', 'y', 'x#1']

First differing element 0:
'x#0'
'x'

- ['x#0', 'y#0', 'x']
? -- --

+ ['x', 'y', 'x#1']
? ++

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)


Answer:
import re

def format_pin(name, version):
 if not re.fullmatch(r"[a-z0-9][a-z0-9._-]*", name):
 raise ValueError("name")
 if not re.fullmatch(r"[0-9]+(\.[0-9]+)*", version):
 raise ValueError("version")
 return f"{name}=={version}"

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

import re

def format_pin(name, version):
    if not re.fullmatch(r"[a-z0-9][a-z0-9._-]*", name):
        raise ValueError("name")
    if not re.fullmatch(r"[0-9]+(\.[0-9]+)*", version):
        raise ValueError("version")
    return f"{name}=={version}"

import unittest
from solution import format_pin

class Test(unittest.TestCase):
    def test_pin(self):
        self.assertEqual(format_pin("foo.bar", "1.2.3"), "foo.bar==1.2.3")
        with self.assertRaises(ValueError):
            format_pin("Foo", "1")

Read the specification
Implement the function or class
Satisfy the tests

Prompt:
Implement `format_pin(name, version)` as `name==version` after validating
name `[a-z0-9][a-z0-9._-]*` and version `[0-9]+(\.[0-9]+)*`.


Answer:
def tabs_in_indent(text):
 for line in text.splitlines():
 i = 0
 while i < len(line) and line[i] in " \t":
 if line[i] == "\t":
 return True
 i += 1
 return False

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def tabs_in_indent(text):
    for line in text.splitlines():
        i = 0
        while i < len(line) and line[i] in " \t":
            if line[i] == "\t":
                return True
            i += 1
    return False

import unittest
from solution import tabs_in_indent

class Test(unittest.TestCase):
    def test_tab(self):
        self.assertTrue(tabs_in_indent("a\n\tb\n"))
        self.assertFalse(tabs_in_indent("a\n  b\n"))

Read the specification
Implement the function or class
Satisfy the tests

Prompt:
Implement `tabs_in_indent(text)` returning True if any line uses a tab in
its leading whitespace.


Answer:
def tabs_in_indent(text):
 for line in text.splitlines():
 i = 0
 while i < len(line) and line[i] in " \t":
 if line[i] == "\t":
 return True
 i += 1
 return False

Do not weaken or delete tests
Keep the public API

Output:
OPEN_REASON_RESULT {"tests_run": 1, "failures": 1, "errors": 0, "skipped": 0, "passed": false}

test_tab (test_solution.Test.test_tab) ... FAIL

======================================================================
FAIL: test_tab (test_solution.Test.test_tab)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-bl0l6b2x\test_solution.py", line 6, in test_tab
    self.assertTrue(tabs_in_indent("a\n\tb\n"))
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: False is not true

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

Seeded mutation of the reference implementation.

Reproduce the failure
Identify the defect
Apply a minimal fix
Re-run tests

Prompt:
The following Python module fails its tests. Produce a corrected solution.py that preserves the intended behaviour.

Implement `tabs_in_indent(text)` returning True if any line uses a tab in
its leading whitespace.

--- solution.py (buggy) ---
def tabs_in_indent(text):
 for line in text.splitlines():
 i = 0
 while i < len(line) and line[i] in " \t":
 if line[i] == "\t":
 return False
 i += 1
 return False

--- test_solution.py ---
import unittest
from solution import tabs_in_indent

class Test(unittest.TestCase):
 def test_tab(self):
 self.assertTrue(tabs_in_indent("a\n\tb\n"))
 self.assertFalse(tabs_in_indent("a\n b\n"))

--- failure ---
OPEN_REASON_RESULT {"tests_run": 1, "failures": 1, "errors": 0, "skipped": 0, "passed": false}

test_tab (test_solution.Test.test_tab) ... FAIL

======================================================================
FAIL: test_tab (test_solution.Test.test_tab)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-bl0l6b2x\test_solution.py", line 6, in test_tab
 self.assertTrue(tabs_in_indent("a\n\tb\n"))
 ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: False is not true

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)


Answer:
def overall_health(checks):
 if not checks:
 return "unknown"
 return "fail" if any(c == "fail" for c in checks) else "ok"

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def overall_health(checks):
    if not checks:
        return "unknown"
    return "fail" if any(c == "fail" for c in checks) else "ok"

import unittest
from solution import overall_health

class Test(unittest.TestCase):
    def test_h(self):
        self.assertEqual(overall_health(["ok", "fail"]), "fail")
        self.assertEqual(overall_health([]), "unknown")

Read the specification
Implement the function or class
Satisfy the tests

Prompt:
Implement `overall_health(checks)` where each check is `ok` or `fail`.
Return `fail` if any fail, `ok` if all ok, `unknown` if empty.


Answer:
def chunks(seq, n):
 if n < 1:
 raise ValueError("n")
 buf = []
 for item in seq:
 buf.append(item)
 if len(buf) == n:
 yield buf
 buf = []
 if buf:
 yield buf

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def chunks(seq, n):
    if n < 1:
        raise ValueError("n")
    buf = []
    for item in seq:
        buf.append(item)
        if len(buf) == n:
            yield buf
            buf = []
    if buf:
        yield buf

import unittest
from solution import chunks

class Test(unittest.TestCase):
    def test_c(self):
        self.assertEqual(list(chunks([1, 2, 3, 4, 5], 2)), [[1, 2], [3, 4], [5]])

Read the specification
Implement the function or class
Satisfy the tests

Prompt:
Implement `chunks(seq, n)` yielding successive lists of length n, last
possibly shorter. n must be >= 1.


Answer:
def chunks(seq, n):
 if n < 1:
 raise ValueError("n")
 buf = []
 for item in seq:
 buf.append(item)
 if len(buf) == n:
 yield buf
 buf = []
 if buf:
 yield buf

Do not weaken or delete tests
Keep the public API

Output:
OPEN_REASON_RESULT {"tests_run": 1, "failures": 1, "errors": 0, "skipped": 0, "passed": false}

test_c (test_solution.Test.test_c) ... FAIL

======================================================================
FAIL: test_c (test_solution.Test.test_c)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-aqu97_k1\test_solution.py", line 6, in test_c
    self.assertEqual(list(chunks([1, 2, 3, 4, 5], 2)), [[1, 2], [3, 4], [5]])
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Lists differ: [[1], [2], [3], [4], [5]] != [[1, 2], [3, 4], [5]]

First differing element 0:
[1]
[1, 2]

First list contains 2 additional elements.
First extra element 3:
[4]

- [[1], [2], [3], [4], [5]]
?    -  -      -  -

+ [[1, 2], [3, 4], [5]]

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

Seeded mutation of the reference implementation.

Reproduce the failure
Identify the defect
Apply a minimal fix
Re-run tests

Prompt:
The following Python module fails its tests. Produce a corrected solution.py that preserves the intended behaviour.

Implement `chunks(seq, n)` yielding successive lists of length n, last
possibly shorter. n must be >= 1.

--- solution.py (buggy) ---
def chunks(seq, n):
 if n < 1:
 raise ValueError("n")
 buf = []
 for item in seq:
 buf.append(item)
 if len(buf) != n:
 yield buf
 buf = []
 if buf:
 yield buf

--- test_solution.py ---
import unittest
from solution import chunks

class Test(unittest.TestCase):
 def test_c(self):
 self.assertEqual(list(chunks([1, 2, 3, 4, 5], 2)), [[1, 2], [3, 4], [5]])

--- failure ---
OPEN_REASON_RESULT {"tests_run": 1, "failures": 1, "errors": 0, "skipped": 0, "passed": false}

test_c (test_solution.Test.test_c) ... FAIL

======================================================================
FAIL: test_c (test_solution.Test.test_c)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-aqu97_k1\test_solution.py", line 6, in test_c
 self.assertEqual(list(chunks([1, 2, 3, 4, 5], 2)), [[1, 2], [3, 4], [5]])
 ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Lists differ: [[1], [2], [3], [4], [5]] != [[1, 2], [3, 4], [5]]

First differing element 0:
[1]
[1, 2]

First list contains 2 additional elements.
First extra element 3:
[4]

- [[1], [2], [3], [4], [5]]
? - - - -

+ [[1, 2], [3, 4], [5]]

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)


Answer:
def simplify(node):
 if node[0] == "num":
 return node
 inner = simplify(node[1])
 if inner[0] == "neg":
 return inner[1]
 return ("neg", inner)

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def simplify(node):
    if node[0] == "num":
        return node
    inner = simplify(node[1])
    if inner[0] == "neg":
        return inner[1]
    return ("neg", inner)

import unittest
from solution import simplify

class Test(unittest.TestCase):
    def test_s(self):
        self.assertEqual(simplify(("neg", ("neg", ("num", 3)))), ("num", 3))

Read the specification
Implement the function or class
Satisfy the tests

Prompt:
AST nodes are tuples: ('neg', node) or ('num', int). Implement `simplify(node)`
cancelling double negation.


Answer:
def simplify(node):
 if node[0] == "num":
 return node
 inner = simplify(node[1])
 if inner[0] == "neg":
 return inner[1]
 return ("neg", inner)

Do not weaken or delete tests
Keep the public API

Output:
OPEN_REASON_RESULT {"tests_run": 1, "failures": 1, "errors": 0, "skipped": 0, "passed": false}

test_s (test_solution.Test.test_s) ... FAIL

======================================================================
FAIL: test_s (test_solution.Test.test_s)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-bvukoenz\test_solution.py", line 6, in test_s
    self.assertEqual(simplify(("neg", ("neg", ("num", 3)))), ("num", 3))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Tuples differ: ('neg', ('neg', ('num', 3))) != ('num', 3)

First differing element 0:
'neg'
'num'

- ('neg', ('neg', ('num', 3)))
+ ('num', 3)

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

Seeded mutation of the reference implementation.

Reproduce the failure
Identify the defect
Apply a minimal fix
Re-run tests

Prompt:
The following Python module fails its tests. Produce a corrected solution.py that preserves the intended behaviour.

AST nodes are tuples: ('neg', node) or ('num', int). Implement `simplify(node)`
cancelling double negation.

--- solution.py (buggy) ---
def simplify(node):
 if node[0] != "num":
 return node
 inner = simplify(node[1])
 if inner[0] == "neg":
 return inner[1]
 return ("neg", inner)

--- test_solution.py ---
import unittest
from solution import simplify

class Test(unittest.TestCase):
 def test_s(self):
 self.assertEqual(simplify(("neg", ("neg", ("num", 3)))), ("num", 3))

--- failure ---
OPEN_REASON_RESULT {"tests_run": 1, "failures": 1, "errors": 0, "skipped": 0, "passed": false}

test_s (test_solution.Test.test_s) ... FAIL

======================================================================
FAIL: test_s (test_solution.Test.test_s)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-bvukoenz\test_solution.py", line 6, in test_s
 self.assertEqual(simplify(("neg", ("neg", ("num", 3)))), ("num", 3))
 ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Tuples differ: ('neg', ('neg', ('num', 3))) != ('num', 3)

First differing element 0:
'neg'
'num'

- ('neg', ('neg', ('num', 3)))
+ ('num', 3)

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)


Answer:
CHARS = " .:-=+*#%@"

def spark(nums):
 if not nums:
 return ""
 lo, hi = min(nums), max(nums)
 if lo == hi:
 return CHARS[-1] * len(nums)
 out = []
 for x in nums:
 idx = int((x - lo) / (hi - lo) * (len(CHARS) - 1))
 out.append(CHARS[idx])
 return "".join(out)

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

CHARS = " .:-=+*#%@"

def spark(nums):
    if not nums:
        return ""
    lo, hi = min(nums), max(nums)
    if lo == hi:
        return CHARS[-1] * len(nums)
    out = []
    for x in nums:
        idx = int((x - lo) / (hi - lo) * (len(CHARS) - 1))
        out.append(CHARS[idx])
    return "".join(out)

import unittest
from solution import spark

class Test(unittest.TestCase):
    def test_sp(self):
        self.assertEqual(len(spark([0, 5, 10])), 3)
        self.assertEqual(spark([3, 3, 3]), "@@@")

Read the specification
Implement the function or class
Satisfy the tests

Prompt:
Implement `spark(nums)` mapping each value to a bar character from
` .:-=+*#%@` by min-max scaling. Constant arrays become all last char. Return a string.


Answer:
CHARS = " .:-=+*#%@"

def spark(nums):
 if not nums:
 return ""
 lo, hi = min(nums), max(nums)
 if lo == hi:
 return CHARS[-1] * len(nums)
 out = []
 for x in nums:
 idx = int((x - lo) / (hi - lo) * (len(CHARS) - 1))
 out.append(CHARS[idx])
 return "".join(out)

Do not weaken or delete tests
Keep the public API

Output:
OPEN_REASON_RESULT {"tests_run": 1, "failures": 0, "errors": 1, "skipped": 0, "passed": false}

test_sp (test_solution.Test.test_sp) ... ERROR

======================================================================
ERROR: test_sp (test_solution.Test.test_sp)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-93wn67cd\test_solution.py", line 7, in test_sp
    self.assertEqual(spark([3, 3, 3]), "@@@")
                     ~~~~~^^^^^^^^^^^
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-93wn67cd\solution.py", line 11, in spark
    idx = int((x - lo) / (hi - lo) * (len(CHARS) - 1))
              ~~~~~~~~~^~~~~~~~~~~
ZeroDivisionError: division by zero

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

Seeded mutation of the reference implementation.

Reproduce the failure
Identify the defect
Apply a minimal fix
Re-run tests

Prompt:
The following Python module fails its tests. Produce a corrected solution.py that preserves the intended behaviour.

Implement `spark(nums)` mapping each value to a bar character from
` .:-=+*#%@` by min-max scaling. Constant arrays become all last char. Return a string.

--- solution.py (buggy) ---
CHARS = " .:-=+*#%@"

def spark(nums):
 if not nums:
 return ""
 lo, hi = min(nums), max(nums)
 if lo != hi:
 return CHARS[-1] * len(nums)
 out = []
 for x in nums:
 idx = int((x - lo) / (hi - lo) * (len(CHARS) - 1))
 out.append(CHARS[idx])
 return "".join(out)

--- test_solution.py ---
import unittest
from solution import spark

class Test(unittest.TestCase):
 def test_sp(self):
 self.assertEqual(len(spark([0, 5, 10])), 3)
 self.assertEqual(spark([3, 3, 3]), "@@@")

--- failure ---
OPEN_REASON_RESULT {"tests_run": 1, "failures": 0, "errors": 1, "skipped": 0, "passed": false}

test_sp (test_solution.Test.test_sp) ... ERROR

======================================================================
ERROR: test_sp (test_solution.Test.test_sp)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-93wn67cd\test_solution.py", line 7, in test_sp
 self.assertEqual(spark([3, 3, 3]), "@@@")
 ~~~~~^^^^^^^^^^^
 File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-93wn67cd\solution.py", line 11, in spark
 idx = int((x - lo) / (hi - lo) * (len(CHARS) - 1))
 ~~~~~~~~~^~~~~~~~~~~
ZeroDivisionError: division by zero

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)


Answer:
def tripleneg(n):
 return n*3 if n<0 else n+3

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def tripleneg(n):
    return n*3 if n<0 else n+3

import unittest
from solution import tripleneg
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(tripleneg(-4), -12)
    def test_1(self):
        self.assertEqual(tripleneg(-3), -9)
    def test_2(self):
        self.assertEqual(tripleneg(-2), -6)
    def test_3(self):
        self.assertEqual(tripleneg(-1), -3)
    def test_4(self):
        self.assertEqual(tripleneg(0), 3)
    def test_5(self):
        self.assertEqual(tripleneg(1), 4)
    def test_6(self):
        self.assertEqual(tripleneg(2), 5)
    def test_7(self):
        self.assertEqual(tripleneg(3), 6)
    def test_8(self):
        self.assertEqual(tripleneg(4), 7)
    def test_9(self):
        self.assertEqual(tripleneg(5), 8)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `tripleneg`: Return n*3 if n<0 else n+3. Use only the Python standard library.

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... ok
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

----------------------------------------------------------------------
Ran 10 tests in 0.000s

OK

OPEN_REASON_RESULT {"tests_run": 10, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def clamp8(n):
 return 0 if n<0 else 8 if n>8 else n

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def clamp8(n):
    return 0 if n<0 else 8 if n>8 else n

import unittest
from solution import clamp8
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(clamp8(-2), 0)
    def test_1(self):
        self.assertEqual(clamp8(-1), 0)
    def test_2(self):
        self.assertEqual(clamp8(0), 0)
    def test_3(self):
        self.assertEqual(clamp8(1), 1)
    def test_4(self):
        self.assertEqual(clamp8(2), 2)
    def test_5(self):
        self.assertEqual(clamp8(3), 3)
    def test_6(self):
        self.assertEqual(clamp8(4), 4)
    def test_7(self):
        self.assertEqual(clamp8(5), 5)
    def test_8(self):
        self.assertEqual(clamp8(6), 6)
    def test_9(self):
        self.assertEqual(clamp8(7), 7)
    def test_10(self):
        self.assertEqual(clamp8(8), 8)
    def test_11(self):
        self.assertEqual(clamp8(9), 8)
    def test_12(self):
        self.assertEqual(clamp8(10), 8)
    def test_13(self):
        self.assertEqual(clamp8(11), 8)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `clamp8`: Clamp n into [0, 8]. Use only the Python standard library.

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_10 (test_solution.Test.test_10) ... ok
test_11 (test_solution.Test.test_11) ... ok
test_12 (test_solution.Test.test_12) ... ok
test_13 (test_solution.Test.test_13) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... ok
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

----------------------------------------------------------------------
Ran 14 tests in 0.000s

OK

OPEN_REASON_RESULT {"tests_run": 14, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def ones_mod(n):
 return n % 9

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

import unittest
from solution import ones_mod
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(ones_mod(0), 0)
    def test_1(self):
        self.assertEqual(ones_mod(1), 1)
    def test_2(self):
        self.assertEqual(ones_mod(2), 2)
    def test_3(self):
        self.assertEqual(ones_mod(3), 3)
    def test_4(self):
        self.assertEqual(ones_mod(4), 4)
    def test_5(self):
        self.assertEqual(ones_mod(5), 5)
    def test_6(self):
        self.assertEqual(ones_mod(6), 6)
    def test_7(self):
        self.assertEqual(ones_mod(7), 7)
    def test_8(self):
        self.assertEqual(ones_mod(8), 8)
    def test_9(self):
        self.assertEqual(ones_mod(9), 0)
    def test_10(self):
        self.assertEqual(ones_mod(10), 1)
    def test_11(self):
        self.assertEqual(ones_mod(11), 2)
    def test_12(self):
        self.assertEqual(ones_mod(12), 3)
    def test_13(self):
        self.assertEqual(ones_mod(13), 4)
    def test_14(self):
        self.assertEqual(ones_mod(14), 5)
    def test_15(self):
        self.assertEqual(ones_mod(15), 6)
    def test_16(self):
        self.assertEqual(ones_mod(16), 7)
    def test_17(self):
        self.assertEqual(ones_mod(17), 8)
    def test_18(self):
        self.assertEqual(ones_mod(18), 0)
    def test_19(self):
        self.assertEqual(ones_mod(19), 1)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `ones_mod`: Return n % 9, with 0 mapping to 0 (digital-root step). Use only the Python standard library.

Solution:
def ones_mod(n):
 return n % 9

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_10 (test_solution.Test.test_10) ... ok
test_11 (test_solution.Test.test_11) ... ok
test_12 (test_solution.Test.test_12) ... ok
test_13 (test_solution.Test.test_13) ... ok
test_14 (test_solution.Test.test_14) ... ok
test_15 (test_solution.Test.test_15) ... ok
test_16 (test_solution.Test.test_16) ... ok
test_17 (test_solution.Test.test_17) ... ok
test_18 (test_solution.Test.test_18) ... ok
test_19 (test_solution.Test.test_19) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... ok
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

----------------------------------------------------------------------
Ran 20 tests in 0.000s

OK

OPEN_REASON_RESULT {"tests_run": 20, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def is_sq(n):
 if n<0:
 return False
 r=int(n**0.5)
 return r*r==n

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def is_sq(n):
    if n<0:
        return False
    r=int(n**0.5)
    return r*r==n

import unittest
from solution import is_sq
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(is_sq(-1), False)
    def test_1(self):
        self.assertEqual(is_sq(0), True)
    def test_2(self):
        self.assertEqual(is_sq(1), True)
    def test_3(self):
        self.assertEqual(is_sq(2), False)
    def test_4(self):
        self.assertEqual(is_sq(3), False)
    def test_5(self):
        self.assertEqual(is_sq(4), True)
    def test_6(self):
        self.assertEqual(is_sq(5), False)
    def test_7(self):
        self.assertEqual(is_sq(6), False)
    def test_8(self):
        self.assertEqual(is_sq(7), False)
    def test_9(self):
        self.assertEqual(is_sq(8), False)
    def test_10(self):
        self.assertEqual(is_sq(9), True)
    def test_11(self):
        self.assertEqual(is_sq(10), False)
    def test_12(self):
        self.assertEqual(is_sq(11), False)
    def test_13(self):
        self.assertEqual(is_sq(12), False)
    def test_14(self):
        self.assertEqual(is_sq(13), False)
    def test_15(self):
        self.assertEqual(is_sq(14), False)
    def test_16(self):
        self.assertEqual(is_sq(15), False)
    def test_17(self):
        self.assertEqual(is_sq(16), True)
    def test_18(self):
        self.assertEqual(is_sq(17), False)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `is_sq`: Return True iff n is a perfect square (n>=0). Use only the Python standard library.

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_10 (test_solution.Test.test_10) ... ok
test_11 (test_solution.Test.test_11) ... ok
test_12 (test_solution.Test.test_12) ... ok
test_13 (test_solution.Test.test_13) ... ok
test_14 (test_solution.Test.test_14) ... ok
test_15 (test_solution.Test.test_15) ... ok
test_16 (test_solution.Test.test_16) ... ok
test_17 (test_solution.Test.test_17) ... ok
test_18 (test_solution.Test.test_18) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... ok
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

----------------------------------------------------------------------
Ran 19 tests in 0.000s

OK

OPEN_REASON_RESULT {"tests_run": 19, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def is_sq(n):
 if n<0:
 return False
 r=int(n**0.5)
 return r*r==n

Do not weaken or delete tests
Keep the public API

Output:
OPEN_REASON_RESULT {"tests_run": 19, "failures": 1, "errors": 0, "skipped": 0, "passed": false}

test_0 (test_solution.Test.test_0) ... FAIL
test_1 (test_solution.Test.test_1) ... ok
test_10 (test_solution.Test.test_10) ... ok
test_11 (test_solution.Test.test_11) ... ok
test_12 (test_solution.Test.test_12) ... ok
test_13 (test_solution.Test.test_13) ... ok
test_14 (test_solution.Test.test_14) ... ok
test_15 (test_solution.Test.test_15) ... ok
test_16 (test_solution.Test.test_16) ... ok
test_17 (test_solution.Test.test_17) ... ok
test_18 (test_solution.Test.test_18) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... ok
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

======================================================================
FAIL: test_0 (test_solution.Test.test_0)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-9g1syvlh\test_solution.py", line 5, in test_0
    self.assertEqual(is_sq(-1), False)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
AssertionError: True != False

----------------------------------------------------------------------
Ran 19 tests in 0.001s

FAILED (failures=1)

Seeded mutation of the reference implementation.

Reproduce the failure
Identify the defect
Apply a minimal fix
Re-run tests

Prompt:
The following Python module fails its tests. Produce a corrected solution.py that preserves the intended behaviour.

Implement `is_sq`: Return True iff n is a perfect square (n>=0). Use only the Python standard library.

--- solution.py (buggy) ---
def is_sq(n):
 if n<0:
 return True
 r=int(n**0.5)
 return r*r==n

--- test_solution.py ---
import unittest
from solution import is_sq
class Test(unittest.TestCase):
 def test_0(self):
 self.assertEqual(is_sq(-1), False)
 def test_1(self):
 self.assertEqual(is_sq(0), True)
 def test_2(self):
 self.assertEqual(is_sq(1), True)
 def test_3(self):
 self.assertEqual(is_sq(2), False)
 def test_4(self):
 self.assertEqual(is_sq(3), False)
 def test_5(self):
 self.assertEqual(is_sq(4), True)
 def test_6(self):
 self.assertEqual(is_sq(5), False)
 def test_7(self):
 self.assertEqual(is_sq(6), False)
 def test_8(self):
 self.assertEqual(is_sq(7), False)
 def test_9(self):
 self.assertEqual(is_sq(8), False)
 def test_10(self):
 self.assertEqual(is_sq(9), True)
 def test_11(self):
 self.assertEqual(is_sq(10), False)
 def test_12(self):
 self.assertEqual(is_sq(11), False)
 def test_13(self):
 self.assertEqual(is_sq(12), False)
 def test_14(self):
 self.assertEqual(is_sq(13), False)
 def test_15(self):
 self.assertEqual(is_sq(14), False)
 def test_16(self):
 self.assertEqual(is_sq(15), False)
 def test_17(self):
 self.assertEqual(is_sq(16), True)
 def test_18(self):
 self.assertEqual(is_sq(17), False)

--- failure ---
OPEN_REASON_RESULT {"tests_run": 19, "failures": 1, "errors": 0, "skipped": 0, "passed": false}

test_0 (test_solution.Test.test_0) ... FAIL
test_1 (test_solution.Test.test_1) ... ok
test_10 (test_solution.Test.test_10) ... ok
test_11 (test_solution.Test.test_11) ... ok
test_12 (test_solution.Test.test_12) ... ok
test_13 (test_solution.Test.test_13) ... ok
test_14 (test_solution.Test.test_14) ... ok
test_15 (test_solution.Test.test_15) ... ok
test_16 (test_solution.Test.test_16) ... ok
test_17 (test_solution.Test.test_17) ... ok
test_18 (test_solution.Test.test_18) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... ok
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

======================================================================
FAIL: test_0 (test_solution.Test.test_0)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-9g1syvlh\test_solution.py", line 5, in test_0
 self.assertEqual(is_sq(-1), False)
 ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
AssertionError: True != False

----------------------------------------------------------------------
Ran 19 tests in 0.001s

FAILED (failures=1)


Answer:
def next_even(n):
 return n if n%2==0 else n+1

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def next_even(n):
    return n if n%2==0 else n+1

import unittest
from solution import next_even
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(next_even(-3), -2)
    def test_1(self):
        self.assertEqual(next_even(-2), -2)
    def test_2(self):
        self.assertEqual(next_even(-1), 0)
    def test_3(self):
        self.assertEqual(next_even(0), 0)
    def test_4(self):
        self.assertEqual(next_even(1), 2)
    def test_5(self):
        self.assertEqual(next_even(2), 2)
    def test_6(self):
        self.assertEqual(next_even(3), 4)
    def test_7(self):
        self.assertEqual(next_even(4), 4)
    def test_8(self):
        self.assertEqual(next_even(5), 6)
    def test_9(self):
        self.assertEqual(next_even(6), 6)
    def test_10(self):
        self.assertEqual(next_even(7), 8)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `next_even`: Smallest even integer >= n. Use only the Python standard library.

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_10 (test_solution.Test.test_10) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... ok
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

----------------------------------------------------------------------
Ran 11 tests in 0.000s

OK

OPEN_REASON_RESULT {"tests_run": 11, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def collatz_step(n):
 return n//2 if n%2==0 else 3*n+1

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def collatz_step(n):
    return n//2 if n%2==0 else 3*n+1

import unittest
from solution import collatz_step
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(collatz_step(1), 4)
    def test_1(self):
        self.assertEqual(collatz_step(2), 1)
    def test_2(self):
        self.assertEqual(collatz_step(3), 10)
    def test_3(self):
        self.assertEqual(collatz_step(4), 2)
    def test_4(self):
        self.assertEqual(collatz_step(5), 16)
    def test_5(self):
        self.assertEqual(collatz_step(6), 3)
    def test_6(self):
        self.assertEqual(collatz_step(7), 22)
    def test_7(self):
        self.assertEqual(collatz_step(8), 4)
    def test_8(self):
        self.assertEqual(collatz_step(9), 28)
    def test_9(self):
        self.assertEqual(collatz_step(10), 5)
    def test_10(self):
        self.assertEqual(collatz_step(11), 34)
    def test_11(self):
        self.assertEqual(collatz_step(12), 6)
    def test_12(self):
        self.assertEqual(collatz_step(13), 40)
    def test_13(self):
        self.assertEqual(collatz_step(14), 7)
    def test_14(self):
        self.assertEqual(collatz_step(15), 46)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `collatz_step`: One Collatz step: n/2 if even else 3n+1, for positive n. Use only the Python standard library.

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_10 (test_solution.Test.test_10) ... ok
test_11 (test_solution.Test.test_11) ... ok
test_12 (test_solution.Test.test_12) ... ok
test_13 (test_solution.Test.test_13) ... ok
test_14 (test_solution.Test.test_14) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... ok
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

----------------------------------------------------------------------
Ran 15 tests in 0.000s

OK

OPEN_REASON_RESULT {"tests_run": 15, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def popcount(n):
 return bin(abs(n)).count('1')

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def popcount(n):
    return bin(abs(n)).count('1')

import unittest
from solution import popcount
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(popcount(-8), 1)
    def test_1(self):
        self.assertEqual(popcount(-7), 3)
    def test_2(self):
        self.assertEqual(popcount(-6), 2)
    def test_3(self):
        self.assertEqual(popcount(-5), 2)
    def test_4(self):
        self.assertEqual(popcount(-4), 1)
    def test_5(self):
        self.assertEqual(popcount(-3), 2)
    def test_6(self):
        self.assertEqual(popcount(-2), 1)
    def test_7(self):
        self.assertEqual(popcount(-1), 1)
    def test_8(self):
        self.assertEqual(popcount(0), 0)
    def test_9(self):
        self.assertEqual(popcount(1), 1)
    def test_10(self):
        self.assertEqual(popcount(2), 1)
    def test_11(self):
        self.assertEqual(popcount(3), 2)
    def test_12(self):
        self.assertEqual(popcount(4), 1)
    def test_13(self):
        self.assertEqual(popcount(5), 2)
    def test_14(self):
        self.assertEqual(popcount(6), 2)
    def test_15(self):
        self.assertEqual(popcount(7), 3)
    def test_16(self):
        self.assertEqual(popcount(8), 1)
    def test_17(self):
        self.assertEqual(popcount(9), 2)
    def test_18(self):
        self.assertEqual(popcount(10), 2)
    def test_19(self):
        self.assertEqual(popcount(11), 3)
    def test_20(self):
        self.assertEqual(popcount(12), 2)
    def test_21(self):
        self.assertEqual(popcount(13), 3)
    def test_22(self):
        self.assertEqual(popcount(14), 3)
    def test_23(self):
        self.assertEqual(popcount(15), 4)
    def test_24(self):
        self.assertEqual(popcount(16), 1)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `popcount`: Number of 1-bits in the absolute value of n. Use only the Python standard library.

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_10 (test_solution.Test.test_10) ... ok
test_11 (test_solution.Test.test_11) ... ok
test_12 (test_solution.Test.test_12) ... ok
test_13 (test_solution.Test.test_13) ... ok
test_14 (test_solution.Test.test_14) ... ok
test_15 (test_solution.Test.test_15) ... ok
test_16 (test_solution.Test.test_16) ... ok
test_17 (test_solution.Test.test_17) ... ok
test_18 (test_solution.Test.test_18) ... ok
test_19 (test_solution.Test.test_19) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_20 (test_solution.Test.test_20) ... ok
test_21 (test_solution.Test.test_21) ... ok
test_22 (test_solution.Test.test_22) ... ok
test_23 (test_solution.Test.test_23) ... ok
test_24 (test_solution.Test.test_24) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... ok
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

----------------------------------------------------------------------
Ran 25 tests in 0.000s

OK

OPEN_REASON_RESULT {"tests_run": 25, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def rev_digits(n):
 s=int(str(abs(n))[::-1])
 return -s if n<0 else s

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def rev_digits(n):
    s=int(str(abs(n))[::-1])
    return -s if n<0 else s

import unittest
from solution import rev_digits
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(rev_digits(-120), -21)
    def test_1(self):
        self.assertEqual(rev_digits(-10), -1)
    def test_2(self):
        self.assertEqual(rev_digits(0), 0)
    def test_3(self):
        self.assertEqual(rev_digits(7), 7)
    def test_4(self):
        self.assertEqual(rev_digits(100), 1)
    def test_5(self):
        self.assertEqual(rev_digits(1234), 4321)
    def test_6(self):
        self.assertEqual(rev_digits(900), 9)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `rev_digits`: Integer obtained by reversing decimal digits of |n|, keeping sign. Use only the Python standard library.

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok
test_6 (test_solution.Test.test_6) ... ok

----------------------------------------------------------------------
Ran 7 tests in 0.000s

OK

OPEN_REASON_RESULT {"tests_run": 7, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def sum_digits(n):
 return sum(int(c) for c in str(abs(n)))

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def sum_digits(n):
    return sum(int(c) for c in str(abs(n)))

import unittest
from solution import sum_digits
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(sum_digits(0), 0)
    def test_1(self):
        self.assertEqual(sum_digits(10), 1)
    def test_2(self):
        self.assertEqual(sum_digits(99), 18)
    def test_3(self):
        self.assertEqual(sum_digits(123), 6)
    def test_4(self):
        self.assertEqual(sum_digits(1001), 2)
    def test_5(self):
        self.assertEqual(sum_digits(-58), 13)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `sum_digits`: Sum of decimal digits of |n|. Use only the Python standard library.

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok

----------------------------------------------------------------------
Ran 6 tests in 0.000s

OK

OPEN_REASON_RESULT {"tests_run": 6, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def mid3(a,b,c):
 return sorted([a,b,c])[1]

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def mid3(a,b,c):
    return sorted([a,b,c])[1]

import unittest
from solution import mid3
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(mid3(1, 2, 3), 2)
    def test_1(self):
        self.assertEqual(mid3(9, 1, 5), 5)
    def test_2(self):
        self.assertEqual(mid3(0, 0, 1), 0)
    def test_3(self):
        self.assertEqual(mid3(-4, -1, -3), -3)
    def test_4(self):
        self.assertEqual(mid3(8, 8, 2), 8)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `mid3`: Median of three integers a,b,c. Use only the Python standard library.

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok

----------------------------------------------------------------------
Ran 5 tests in 0.000s

OK

OPEN_REASON_RESULT {"tests_run": 5, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
import math
def gcd3(a,b,c):
 return math.gcd(math.gcd(a,b),c)

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

import math
def gcd3(a,b,c):
    return math.gcd(math.gcd(a,b),c)

import unittest
from solution import gcd3
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(gcd3(12, 18, 30), 6)
    def test_1(self):
        self.assertEqual(gcd3(7, 9, 11), 1)
    def test_2(self):
        self.assertEqual(gcd3(0, 5, 10), 5)
    def test_3(self):
        self.assertEqual(gcd3(21, 14, 7), 7)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `gcd3`: gcd of three integers. Use only the Python standard library.

OPEN_REASON_RESULT {"tests_run": 4, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def powmod(a,b,m):
 return pow(a,b,m)

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def powmod(a,b,m):
    return pow(a,b,m)

import unittest
from solution import powmod
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(powmod(2, 10, 1000), 24)
    def test_1(self):
        self.assertEqual(powmod(3, 5, 7), 5)
    def test_2(self):
        self.assertEqual(powmod(5, 0, 9), 1)
    def test_3(self):
        self.assertEqual(powmod(7, 3, 5), 3)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `powmod`: Compute pow(a,b,m) for m>0, b>=0. Use only the Python standard library.

Solution:
def powmod(a,b,m):
 return pow(a,b,m)


Answer:
def ceil_div(a,b):
 return -(-a//b)

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

import unittest
from solution import ceil_div
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(ceil_div(10, 3), 4)
    def test_1(self):
        self.assertEqual(ceil_div(9, 3), 3)
    def test_2(self):
        self.assertEqual(ceil_div(1, 5), 1)
    def test_3(self):
        self.assertEqual(ceil_div(-7, 3), -2)
    def test_4(self):
        self.assertEqual(ceil_div(0, 4), 0)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `ceil_div`: Ceiling of a/b for positive b. Use only the Python standard library.

Solution:
def ceil_div(a,b):
 return -(-a//b)


Answer:
def bit_rev8(n):
 x=n & 255
 y=0
 for _ in range(8):
 y=(y<<1)|(x&1)
 x>>=1
 return y

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def bit_rev8(n):
    x=n & 255
    y=0
    for _ in range(8):
        y=(y<<1)|(x&1)
        x>>=1
    return y

import unittest
from solution import bit_rev8
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(bit_rev8(0), 0)
    def test_1(self):
        self.assertEqual(bit_rev8(1), 128)
    def test_2(self):
        self.assertEqual(bit_rev8(2), 64)
    def test_3(self):
        self.assertEqual(bit_rev8(16), 8)
    def test_4(self):
        self.assertEqual(bit_rev8(128), 1)
    def test_5(self):
        self.assertEqual(bit_rev8(170), 85)
    def test_6(self):
        self.assertEqual(bit_rev8(255), 255)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `bit_rev8`: Reverse the low 8 bits of n (ignore higher bits). Use only the Python standard library.


Answer:
def sgn(n):
 return (n>0)-(n<0)

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

import unittest
from solution import sgn
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(sgn(-3), -1)
    def test_1(self):
        self.assertEqual(sgn(-2), -1)
    def test_2(self):
        self.assertEqual(sgn(-1), -1)
    def test_3(self):
        self.assertEqual(sgn(0), 0)
    def test_4(self):
        self.assertEqual(sgn(1), 1)
    def test_5(self):
        self.assertEqual(sgn(2), 1)
    def test_6(self):
        self.assertEqual(sgn(3), 1)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `sgn`: Return -1, 0, or 1 as the sign of n. Use only the Python standard library.

Solution:
def sgn(n):
 return (n>0)-(n<0)


Answer:
def vowels(s):
 return sum(ch.lower() in 'aeiou' for ch in s)

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def vowels(s):
    return sum(ch.lower() in 'aeiou' for ch in s)

import unittest
from solution import vowels
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(vowels(''), 0)
    def test_1(self):
        self.assertEqual(vowels('xyz'), 0)
    def test_2(self):
        self.assertEqual(vowels('AEIOU'), 5)
    def test_3(self):
        self.assertEqual(vowels('Open Reason'), 5)
    def test_4(self):
        self.assertEqual(vowels('queue'), 4)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `vowels`: Count vowels aeiou in s, case-insensitive. Use only the Python standard library.


Answer:
import re
def snake(s):
 s=re.sub(r'([a-z])([A-Z])', r'\1_\2', s)
 s=re.sub(r'[^A-Za-z0-9]+', '_', s)
 return re.sub(r'_+', '_', s).strip('_').lower()

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

import unittest
from solution import snake
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(snake('OpenReason'), 'open_reason')
    def test_1(self):
        self.assertEqual(snake('already_snake'), 'already_snake')
    def test_2(self):
        self.assertEqual(snake('Hello World'), 'hello_world')
    def test_3(self):
        self.assertEqual(snake('A'), 'a')

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `snake`: Convert CamelCase or spaces to snake_case keeping alphanumerics. Use only the Python standard library.

Solution:
import re
def snake(s):
 s=re.sub(r'([a-z])([A-Z])', r'\1_\2', s)
 s=re.sub(r'[^A-Za-z0-9]+', '_', s)
 return re.sub(r'_+', '_', s).strip('_').lower()


Answer:
def runlen(s):
 if not s:
 return []
 out=[]; last=s[0]; n=1
 for ch in s[1:]:
 if ch==last:
 n+=1
 else:
 out.append((last,n)); last=ch; n=1
 out.append((last,n))
 return out

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def runlen(s):
    if not s:
        return []
    out=[]; last=s[0]; n=1
    for ch in s[1:]:
        if ch==last:
            n+=1
        else:
            out.append((last,n)); last=ch; n=1
    out.append((last,n))
    return out

import unittest
from solution import runlen
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(runlen(''), [])
    def test_1(self):
        self.assertEqual(runlen('aaa'), [('a', 3)])
    def test_2(self):
        self.assertEqual(runlen('abba'), [('a', 1), ('b', 2), ('a', 1)])
    def test_3(self):
        self.assertEqual(runlen('aabbaa'), [('a', 2), ('b', 2), ('a', 2)])

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `runlen`: Run-length encode a string as list of (char, count) pairs. Use only the Python standard library.


Answer:
def is_anagram(a,b):
 def n(x): return sorted(ch.lower() for ch in x if not ch.isspace())
 return n(a)==n(b)

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def is_anagram(a,b):
    def n(x): return sorted(ch.lower() for ch in x if not ch.isspace())
    return n(a)==n(b)

import unittest
from solution import is_anagram
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(is_anagram('listen', 'silent'), True)
    def test_1(self):
        self.assertEqual(is_anagram('a', 'b'), False)
    def test_2(self):
        self.assertEqual(is_anagram('Dormitory', 'dirty room'), True)
    def test_3(self):
        self.assertEqual(is_anagram('', ''), True)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `is_anagram`: True iff a and b are anagrams ignoring spaces and case. Use only the Python standard library.


Answer:
def titlecase_min(s):
 parts=s.split(' ')
 out=[]
 for i,w in enumerate(parts):
 if not w:
 out.append(w); continue
 if i>0 and len(w)<=2:
 out.append(w.lower())
 else:
 out.append(w[:1].upper()+w[1:].lower())
 return ' '.join(out)

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def titlecase_min(s):
    parts=s.split(' ')
    out=[]
    for i,w in enumerate(parts):
        if not w:
            out.append(w); continue
        if i>0 and len(w)<=2:
            out.append(w.lower())
        else:
            out.append(w[:1].upper()+w[1:].lower())
    return ' '.join(out)

import unittest
from solution import titlecase_min
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(titlecase_min('open reason dataset'), 'Open Reason Dataset')
    def test_1(self):
        self.assertEqual(titlecase_min('a b cde'), 'A b Cde')
    def test_2(self):
        self.assertEqual(titlecase_min('SQL'), 'Sql')

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `titlecase_min`: Title-case words, leaving words of length 1-2 lower except the first. Use only the Python standard library.

OPEN_REASON_RESULT {"tests_run": 3, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def strip_html(s):
 out=[]; i=0
 while i<len(s):
 if s[i]=='<':
 j=s.find('>', i)
 if j==-1:
 break
 i=j+1
 else:
 out.append(s[i]); i+=1
 return ''.join(out)

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def strip_html(s):
    out=[]; i=0
    while i<len(s):
        if s[i]=='<':
            j=s.find('>', i)
            if j==-1:
                break
            i=j+1
        else:
            out.append(s[i]); i+=1
    return ''.join(out)

import unittest
from solution import strip_html
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(strip_html('x'), 'x')
    def test_1(self):
        self.assertEqual(strip_html('plain'), 'plain')
    def test_2(self):
        self.assertEqual(strip_html("zy"), 'zy')

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `strip_html`: Remove <...> tags; do not parse attributes specially. Use only the Python standard library.


Answer:
def csv_escape(s):
 if any(ch in s for ch in ',\n"'):
 return '"'+s.replace('"','""')+'"'
 return s

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def csv_escape(s):
    if any(ch in s for ch in ',\n"'):
        return '"'+s.replace('"','""')+'"'
    return s

import unittest
from solution import csv_escape
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(csv_escape('plain'), 'plain')
    def test_1(self):
        self.assertEqual(csv_escape('a,b'), '"a,b"')
    def test_2(self):
        self.assertEqual(csv_escape('say "hi"'), '"say ""hi"""')
    def test_3(self):
        self.assertEqual(csv_escape('ok'), 'ok')

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `csv_escape`: Escape a field for CSV: wrap in quotes if it contains comma, quote, or newline; double quotes. Use only the Python standard library.


Answer:
def prefix_fun(xs):
 if not xs:
 return ''
 p=xs[0]
 for s in xs[1:]:
 i=0
 while i<len(p) and i<len(s) and p[i]==s[i]:
 i+=1
 p=p[:i]
 if not p:
 return ''
 return p

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def prefix_fun(xs):
    if not xs:
        return ''
    p=xs[0]
    for s in xs[1:]:
        i=0
        while i<len(p) and i<len(s) and p[i]==s[i]:
            i+=1
        p=p[:i]
        if not p:
            return ''
    return p

import unittest
from solution import prefix_fun
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(prefix_fun(['flower', 'flow', 'flight']), 'fl')
    def test_1(self):
        self.assertEqual(prefix_fun(['a', 'b']), '')
    def test_2(self):
        self.assertEqual(prefix_fun([]), '')
    def test_3(self):
        self.assertEqual(prefix_fun(['same', 'same']), 'same')

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `prefix_fun`: Longest common prefix of a list of strings; '' if empty list. Use only the Python standard library.


Answer:
def wrap_at(s,w):
 words=s.split()
 lines=[]; cur=''
 for word in words:
 if not cur:
 cur=word
 elif len(cur)+1+len(word)<=w:
 cur+=' '+word
 else:
 lines.append(cur); cur=word
 if cur:
 lines.append(cur)
 return lines

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def wrap_at(s,w):
    words=s.split()
    lines=[]; cur=''
    for word in words:
        if not cur:
            cur=word
        elif len(cur)+1+len(word)<=w:
            cur+=' '+word
        else:
            lines.append(cur); cur=word
    if cur:
        lines.append(cur)
    return lines

import unittest
from solution import wrap_at
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(wrap_at('one two three', 5), ['one', 'two', 'three'])
    def test_1(self):
        self.assertEqual(wrap_at('abcdefgh', 3), ['abcdefgh'])
    def test_2(self):
        self.assertEqual(wrap_at('a b c', 10), ['a b c'])

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `wrap_at`: Word-wrap s at width w without breaking words; words longer than w stay on their own line. Use only the Python standard library.


Answer:
import re
def kebab(s):
 s=re.sub(r'[^A-Za-z0-9]+','-',s)
 return s.strip('-').lower()

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

import re
def kebab(s):
    s=re.sub(r'[^A-Za-z0-9]+','-',s)
    return s.strip('-').lower()

import unittest
from solution import kebab
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(kebab('Open Reason'), 'open-reason')
    def test_1(self):
        self.assertEqual(kebab('already-kebab'), 'already-kebab')
    def test_2(self):
        self.assertEqual(kebab('X'), 'x')

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `kebab`: Lowercase kebab-case from a phrase. Use only the Python standard library.


Answer:
def moving_avg(xs,k):
 if k<1:
 raise ValueError('k')
 return [sum(xs[i:i+k])/k for i in range(0, len(xs)-k+1)]

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def moving_avg(xs,k):
    if k<1:
        raise ValueError('k')
    return [sum(xs[i:i+k])/k for i in range(0, len(xs)-k+1)]

import unittest
from solution import moving_avg
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(moving_avg([1, 2, 3, 4], 2), [1.5, 2.5, 3.5])
    def test_1(self):
        self.assertEqual(moving_avg([5], 1), [5.0])
    def test_2(self):
        self.assertEqual(moving_avg([2, 2, 2, 2], 3), [2.0, 2.0])

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `moving_avg`: Simple moving average of window k; skip if k<1. Return list of floats. Use only the Python standard library.


Answer:
def dedupe_keep(xs):
 seen=set(); out=[]
 for x in xs:
 if x in seen:
 continue
 seen.add(x); out.append(x)
 return out

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def dedupe_keep(xs):
    seen=set(); out=[]
    for x in xs:
        if x in seen:
            continue
        seen.add(x); out.append(x)
    return out

import unittest
from solution import dedupe_keep
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(dedupe_keep([1, 1, 2, 1, 3]), [1, 2, 3])
    def test_1(self):
        self.assertEqual(dedupe_keep([]), [])
    def test_2(self):
        self.assertEqual(dedupe_keep(['a', 'b', 'a']), ['a', 'b'])

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `dedupe_keep`: Dedupe a list keeping first occurrence. Use only the Python standard library.


Answer:
def chunk2(xs):
 return [(xs[i],xs[i+1]) for i in range(0,len(xs)-1,2)]

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def chunk2(xs):
    return [(xs[i],xs[i+1]) for i in range(0,len(xs)-1,2)]

import unittest
from solution import chunk2
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(chunk2([1, 2, 3, 4]), [(1, 2), (3, 4)])
    def test_1(self):
        self.assertEqual(chunk2([1]), [])
    def test_2(self):
        self.assertEqual(chunk2([1, 2, 3]), [(1, 2)])

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `chunk2`: Pairs of consecutive items; drop a trailing leftover. Use only the Python standard library.


Answer:
def argmin(xs):
 if not xs:
 return -1
 m=min(xs)
 return xs.index(m)

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def argmin(xs):
    if not xs:
        return -1
    m=min(xs)
    return xs.index(m)

import unittest
from solution import argmin
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(argmin([3, 1, 1]), 1)
    def test_1(self):
        self.assertEqual(argmin([]), -1)
    def test_2(self):
        self.assertEqual(argmin([5]), 0)
    def test_3(self):
        self.assertEqual(argmin([-2, 0, -2]), 0)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `argmin`: Index of the first minimum, or -1 if empty. Use only the Python standard library.


Answer:
def prefix_sums(xs):
 out=[]; t=0
 for x in xs:
 t+=x; out.append(t)
 return out

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def prefix_sums(xs):
    out=[]; t=0
    for x in xs:
        t+=x; out.append(t)
    return out

import unittest
from solution import prefix_sums
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(prefix_sums([1, 2, 3]), [1, 3, 6])
    def test_1(self):
        self.assertEqual(prefix_sums([]), [])
    def test_2(self):
        self.assertEqual(prefix_sums([-1, 1]), [-1, 0])

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `prefix_sums`: Inclusive prefix sums. Use only the Python standard library.


Answer:
def rotate_left(xs,k):
 if not xs:
 return []
 k%=len(xs)
 return xs[k:]+xs[:k]

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def rotate_left(xs,k):
    if not xs:
        return []
    k%=len(xs)
    return xs[k:]+xs[:k]

import unittest
from solution import rotate_left
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(rotate_left([1, 2, 3, 4], 1), [2, 3, 4, 1])
    def test_1(self):
        self.assertEqual(rotate_left([1, 2], 0), [1, 2])
    def test_2(self):
        self.assertEqual(rotate_left([7], 3), [7])
    def test_3(self):
        self.assertEqual(rotate_left([1, 2, 3], 5), [3, 1, 2])

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `rotate_left`: Rotate list xs left by k (k may exceed len). Use only the Python standard library.


Answer:
def matrix_trace(m):
 n=len(m)
 if any(len(row)!=n for row in m):
 raise ValueError('shape')
 return sum(m[i][i] for i in range(n))

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def matrix_trace(m):
    n=len(m)
    if any(len(row)!=n for row in m):
        raise ValueError('shape')
    return sum(m[i][i] for i in range(n))

import unittest
from solution import matrix_trace
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(matrix_trace([[1, 2], [3, 4]]), 5)
    def test_1(self):
        self.assertEqual(matrix_trace([[5]]), 5)
    def test_2(self):
        self.assertEqual(matrix_trace([[0, 1, 2], [3, 4, 5], [6, 7, 8]]), 12)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `matrix_trace`: Trace of a square list-of-lists; raise ValueError if not square. Use only the Python standard library.


Answer:
def flatten1(xs):
 out=[]
 for x in xs:
 if isinstance(x,list):
 out.extend(x)
 else:
 out.append(x)
 return out

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def flatten1(xs):
    out=[]
    for x in xs:
        if isinstance(x,list):
            out.extend(x)
        else:
            out.append(x)
    return out

import unittest
from solution import flatten1
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(flatten1([1, [2, 3], 4]), [1, 2, 3, 4])
    def test_1(self):
        self.assertEqual(flatten1([]), [])
    def test_2(self):
        self.assertEqual(flatten1([[], 1]), [1])

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `flatten1`: Flatten one level of lists. Use only the Python standard library.


Answer:
def count_ranges(xs,lo,hi):
 return sum(lo<=x<=hi for x in xs)

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def count_ranges(xs,lo,hi):
    return sum(lo<=x<=hi for x in xs)

import unittest
from solution import count_ranges
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(count_ranges([1, 2, 3, 10], 2, 9), 2)
    def test_1(self):
        self.assertEqual(count_ranges([], 0, 1), 0)
    def test_2(self):
        self.assertEqual(count_ranges([-5, 0, 5], 0, 5), 2)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `count_ranges`: Count how many numbers in xs lie in [lo, hi] inclusive. Use only the Python standard library.


Answer:
def is_sorted(xs):
 return all(xs[i]<=xs[i+1] for i in range(len(xs)-1))

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def is_sorted(xs):
    return all(xs[i]<=xs[i+1] for i in range(len(xs)-1))

import unittest
from solution import is_sorted
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(is_sorted([1, 2, 2, 3]), True)
    def test_1(self):
        self.assertEqual(is_sorted([3, 1]), False)
    def test_2(self):
        self.assertEqual(is_sorted([]), True)
    def test_3(self):
        self.assertEqual(is_sorted([1]), True)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `is_sorted`: Non-decreasing check. Use only the Python standard library.


Answer:
def fib(n):
 a,b=0,1
 for _ in range(n):
 a,b=b,a+b
 return a

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def fib(n):
    a,b=0,1
    for _ in range(n):
        a,b=b,a+b
    return a

import unittest
from solution import fib
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(fib(0), 0)
    def test_1(self):
        self.assertEqual(fib(1), 1)
    def test_2(self):
        self.assertEqual(fib(2), 1)
    def test_3(self):
        self.assertEqual(fib(3), 2)
    def test_4(self):
        self.assertEqual(fib(4), 3)
    def test_5(self):
        self.assertEqual(fib(5), 5)
    def test_6(self):
        self.assertEqual(fib(6), 8)
    def test_7(self):
        self.assertEqual(fib(7), 13)
    def test_8(self):
        self.assertEqual(fib(8), 21)
    def test_9(self):
        self.assertEqual(fib(9), 34)
    def test_10(self):
        self.assertEqual(fib(10), 55)
    def test_11(self):
        self.assertEqual(fib(11), 89)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `fib`: nth Fibonacci with F(0)=0, F(1)=1. Use only the Python standard library.

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_10 (test_solution.Test.test_10) ... ok
test_11 (test_solution.Test.test_11) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... ok
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

----------------------------------------------------------------------
Ran 12 tests in 0.000s

OK

OPEN_REASON_RESULT {"tests_run": 12, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def fact(n):
 r=1
 for i in range(2,n+1):
 r*=i
 return r

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def fact(n):
    r=1
    for i in range(2,n+1):
        r*=i
    return r

import unittest
from solution import fact
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(fact(0), 1)
    def test_1(self):
        self.assertEqual(fact(1), 1)
    def test_2(self):
        self.assertEqual(fact(2), 2)
    def test_3(self):
        self.assertEqual(fact(3), 6)
    def test_4(self):
        self.assertEqual(fact(4), 24)
    def test_5(self):
        self.assertEqual(fact(5), 120)
    def test_6(self):
        self.assertEqual(fact(6), 720)
    def test_7(self):
        self.assertEqual(fact(7), 5040)
    def test_8(self):
        self.assertEqual(fact(8), 40320)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `fact`: n! for n>=0. Use only the Python standard library.

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_2 (test_solution.Test.test_2) ... ok
test_3 (test_solution.Test.test_3) ... ok
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... ok
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... ok
test_8 (test_solution.Test.test_8) ... ok

----------------------------------------------------------------------
Ran 9 tests in 0.000s

OK

OPEN_REASON_RESULT {"tests_run": 9, "failures": 0, "errors": 0, "skipped": 0, "passed": true}


Answer:
def is_prime(n):
 if n<2: return False
 d=2
 while d*d<=n:
 if n%d==0: return False
 d+=1
 return True

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def is_prime(n):
    if n<2: return False
    d=2
    while d*d<=n:
        if n%d==0: return False
        d+=1
    return True

import unittest
from solution import is_prime
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(is_prime(0), False)
    def test_1(self):
        self.assertEqual(is_prime(1), False)
    def test_2(self):
        self.assertEqual(is_prime(2), True)
    def test_3(self):
        self.assertEqual(is_prime(3), True)
    def test_4(self):
        self.assertEqual(is_prime(4), False)
    def test_5(self):
        self.assertEqual(is_prime(5), True)
    def test_6(self):
        self.assertEqual(is_prime(6), False)
    def test_7(self):
        self.assertEqual(is_prime(7), True)
    def test_8(self):
        self.assertEqual(is_prime(8), False)
    def test_9(self):
        self.assertEqual(is_prime(9), False)
    def test_10(self):
        self.assertEqual(is_prime(10), False)
    def test_11(self):
        self.assertEqual(is_prime(11), True)
    def test_12(self):
        self.assertEqual(is_prime(12), False)
    def test_13(self):
        self.assertEqual(is_prime(13), True)
    def test_14(self):
        self.assertEqual(is_prime(14), False)
    def test_15(self):
        self.assertEqual(is_prime(15), False)
    def test_16(self):
        self.assertEqual(is_prime(16), False)
    def test_17(self):
        self.assertEqual(is_prime(17), True)
    def test_18(self):
        self.assertEqual(is_prime(18), False)
    def test_19(self):
        self.assertEqual(is_prime(19), True)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `is_prime`: True iff n is prime. Use only the Python standard library.


Answer:
def is_prime(n):
 if n<2: return False
 d=2
 while d*d<=n:
 if n%d==0: return False
 d+=1
 return True

Do not weaken or delete tests
Keep the public API

Output:
est_19) ... FAIL
test_2 (test_solution.Test.test_2) ... FAIL
test_3 (test_solution.Test.test_3) ... FAIL
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... FAIL
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... FAIL
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

======================================================================
FAIL: test_11 (test_solution.Test.test_11)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 27, in test_11
    self.assertEqual(is_prime(11), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_13 (test_solution.Test.test_13)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 31, in test_13
    self.assertEqual(is_prime(13), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_17 (test_solution.Test.test_17)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 39, in test_17
    self.assertEqual(is_prime(17), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_19 (test_solution.Test.test_19)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 43, in test_19
    self.assertEqual(is_prime(19), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_2 (test_solution.Test.test_2)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 9, in test_2
    self.assertEqual(is_prime(2), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_3 (test_solution.Test.test_3)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 11, in test_3
    self.assertEqual(is_prime(3), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_5 (test_solution.Test.test_5)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 15, in test_5
    self.assertEqual(is_prime(5), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_7 (test_solution.Test.test_7)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 19, in test_7
    self.assertEqual(is_prime(7), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

----------------------------------------------------------------------
Ran 20 tests in 0.001s

FAILED (failures=8)

test_0 (test_solution.Test.test_0) ... ok
test_1 (test_solution.Test.test_1) ... ok
test_10 (test_solution.Test.test_10) ... ok
test_11 (test_solution.Test.test_11) ... FAIL
test_12 (test_solution.Test.test_12) ... ok
test_13 (test_solution.Test.test_13) ... FAIL
test_14 (test_solution.Test.test_14) ... ok
test_15 (test_solution.Test.test_15) ... ok
test_16 (test_solution.Test.test_16) ... ok
test_17 (test_solution.Test.test_17) ... FAIL
test_18 (test_solution.Test.test_18) ... ok
test_19 (test_solution.Test.test_19) ... FAIL
test_2 (test_solution.Test.test_2) ... FAIL
test_3 (test_solution.Test.test_3) ... FAIL
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... FAIL
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... FAIL
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

======================================================================
FAIL: test_11 (test_solution.Test.test_11)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 27, in test_11
    self.assertEqual(is_prime(11), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_13 (test_solution.Test.test_13)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 31, in test_13
    self.assertEqual(is_prime(13), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_17 (test_solution.Test.test_17)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 39, in test_17
    self.assertEqual(is_prime(17), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_19 (test_solution.Test.test_19)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 43, in test_19
    self.assertEqual(is_prime(19), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_2 (test_solution.Test.test_2)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 9, in test_2
    self.assertEqual(is_prime(2), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_3 (test_solution.Test.test_3)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 11, in test_3
    self.assertEqual(is_prime(3), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_5 (test_solution.Test.test_5)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 15, in test_5
    self.assertEqual(is_prime(5), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_7 (test_solution.Test.test_7)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 19, in test_7
    self.assertEqual(is_prime(7), True)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

----------------------------------------------------------------------
Ran 20 tests in 0.001s

FAILED (failures=8)

OPEN_REASON_RESULT {"tests_run": 20, "failures": 8, "errors": 0, "skipped": 0, "passed": false}

Seeded mutation of the reference implementation.

Reproduce the failure
Identify the defect
Apply a minimal fix
Re-run tests

Prompt:
The following Python module fails its tests. Produce a corrected solution.py that preserves the intended behaviour.

Implement `is_prime`: True iff n is prime. Use only the Python standard library.

--- solution.py (buggy) ---
def is_prime(n):
 if n<2: return False
 d=2
 while d*d<=n:
 if n%d==0: return False
 d+=1
 return False

--- test_solution.py ---
import unittest
from solution import is_prime
class Test(unittest.TestCase):
 def test_0(self):
 self.assertEqual(is_prime(0), False)
 def test_1(self):
 self.assertEqual(is_prime(1), False)
 def test_2(self):
 self.assertEqual(is_prime(2), True)
 def test_3(self):
 self.assertEqual(is_prime(3), True)
 def test_4(self):
 self.assertEqual(is_prime(4), False)
 def test_5(self):
 self.assertEqual(is_prime(5), True)
 def test_6(self):
 self.assertEqual(is_prime(6), False)
 def test_7(self):
 self.assertEqual(is_prime(7), True)
 def test_8(self):
 self.assertEqual(is_prime(8), False)
 def test_9(self):
 self.assertEqual(is_prime(9), False)
 def test_10(self):
 self.assertEqual(is_prime(10), False)
 def test_11(self):
 self.assertEqual(is_prime(11), True)
 def test_12(self):
 self.assertEqual(is_prime(12), False)
 def test_13(self):
 self.assertEqual(is_prime(13), True)
 def test_14(self):
 self.assertEqual(is_prime(14), False)
 def test_15(self):
 self.assertEqual(is_prime(15), False)
 def test_16(self):
 self.assertEqual(is_prime(16), False)
 def test_17(self):
 self.assertEqual(is_prime(17), True)
 def test_18(self):
 self.assertEqual(is_prime(18), False)
 def test_19(self):
 self.assertEqual(is_prime(19), True)

--- failure ---
est_19) ... FAIL
test_2 (test_solution.Test.test_2) ... FAIL
test_3 (test_solution.Test.test_3) ... FAIL
test_4 (test_solution.Test.test_4) ... ok
test_5 (test_solution.Test.test_5) ... FAIL
test_6 (test_solution.Test.test_6) ... ok
test_7 (test_solution.Test.test_7) ... FAIL
test_8 (test_solution.Test.test_8) ... ok
test_9 (test_solution.Test.test_9) ... ok

======================================================================
FAIL: test_11 (test_solution.Test.test_11)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 27, in test_11
 self.assertEqual(is_prime(11), True)
 ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_13 (test_solution.Test.test_13)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "C:\Users\matth\AppData\Local\Temp\open-reason-sandbox-8w81s_0y\test_solution.py", line 31, in test_13
 self.assertEqual(is_prime(13), True)
 ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
AssertionError: False != True

======================================================================
FAIL: test_17 (test_solution.Test.test_17)
----------------------------------------------------------------------
Traceback (most recent call last):
 File "C:\Users\matth\AppData\L


Answer:
def n_primes(n):
 def p(x):
 if x<2: return False
 d=2
 while d*d<=x:
 if x%d==0: return False
 d+=1
 return True
 return sum(p(i) for i in range(1,n+1))

Use only the Python standard library unless the prompt says otherwise.

The hidden tests in test_solution.py must pass.

def n_primes(n):
    def p(x):
        if x<2: return False
        d=2
        while d*d<=x:
            if x%d==0: return False
            d+=1
        return True
    return sum(p(i) for i in range(1,n+1))

import unittest
from solution import n_primes
class Test(unittest.TestCase):
    def test_0(self):
        self.assertEqual(n_primes(0), 0)
    def test_1(self):
        self.assertEqual(n_primes(1), 0)
    def test_2(self):
        self.assertEqual(n_primes(2), 1)
    def test_3(self):
        self.assertEqual(n_primes(10), 4)
    def test_4(self):
        self.assertEqual(n_primes(20), 8)
    def test_5(self):
        self.assertEqual(n_primes(30), 10)

Read the specification
Implement the function or class
Satisfy the tests

Prompt: Implement `n_primes`: Count primes in 1..n inclusive. Use only the Python standard library.