Skip to main content
added 5 characters in body
Source Link
Maarten Fabré
  • 9.1k
  • 1
  • 15
  • 27

Apart from the remarks already given about returning instead of printing, and an argument for the number of doors, this code looks good.

Instead of looping over the list, you can also use slicing:

def check_doors_round_slicecheck_doors_round_splice(n, num_doors=100):
    """Check which door is open after n rounds"""
    doors = [False] * num_doors
    for step in range(min(n, num_doors)):
        doors[stepmy_slice ::= slice(step, +None, 1]step =+ [1)
          doors[my_slice] = not[not door for door in doors[step :: step + 1]
        ]doors[my_slice]]
    return doors

Timing

This is a lot faster:

%timeit check_doors_round(100)
1.01 ms ± 40.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit check_doors_round_splice(100)
66 µs ± 4.65 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Apart from the remarks already given about returning instead of printing, and an argument for the number of doors, this code looks good.

Instead of looping over the list, you can also use slicing:

def check_doors_round_slice(n, num_doors=100):
    """Check which door is open after n rounds"""
    doors = [False] * num_doors
    for step in range(min(n, num_doors)):
        doors[step :: step + 1] = [
            not door for door in doors[step :: step + 1]
        ]
    return doors

Timing

This is a lot faster:

%timeit check_doors_round(100)
1.01 ms ± 40.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit check_doors_round_splice(100)
66 µs ± 4.65 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Apart from the remarks already given about returning instead of printing, and an argument for the number of doors, this code looks good.

Instead of looping over the list, you can also use slicing:

def check_doors_round_splice(n, num_doors=100):
    """Check which door is open after n rounds"""
    doors = [False] * num_doors
    for step in range(min(n, num_doors)):
        my_slice = slice(step, None, step + 1)
        doors[my_slice] = [not door for door in doors[my_slice]]
    return doors

Timing

This is a lot faster:

%timeit check_doors_round(100)
1.01 ms ± 40.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit check_doors_round_splice(100)
66 µs ± 4.65 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
fix for n==0; added timing
Source Link
Maarten Fabré
  • 9.1k
  • 1
  • 15
  • 27

Apart from the remarks already given about returning instead of printing, and an argument for the number of doors, this code looks good.

Instead of looping over the list, you can also use splicingslicing:

def check_doors_round_splicecheck_doors_round_slice(n, num_doors=100):
    """Check which door is open after n rounds"""
    doors = [True][False] * num_doors
    for step in range(1, min(n, num_doors)):
        doors[step :: step + 1] = [
            not door for door in doors[step :: step + 1]
        ]
    return doors

Timing

This is a lot faster:

%timeit check_doors_round(100)
1.01 ms ± 40.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit check_doors_round_splice(100)
66 µs ± 4.65 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Apart from the remarks already given about returning instead of printing, and an argument for the number of doors, this code looks good.

Instead of looping over the list, you can also use splicing:

def check_doors_round_splice(n, num_doors=100):
    """Check which door is open after n rounds"""
    doors = [True] * num_doors
    for step in range(1, min(n, num_doors)):
        doors[step :: step + 1] = [
            not door for door in doors[step :: step + 1]
        ]
    return doors

Apart from the remarks already given about returning instead of printing, and an argument for the number of doors, this code looks good.

Instead of looping over the list, you can also use slicing:

def check_doors_round_slice(n, num_doors=100):
    """Check which door is open after n rounds"""
    doors = [False] * num_doors
    for step in range(min(n, num_doors)):
        doors[step :: step + 1] = [
            not door for door in doors[step :: step + 1]
        ]
    return doors

Timing

This is a lot faster:

%timeit check_doors_round(100)
1.01 ms ± 40.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit check_doors_round_splice(100)
66 µs ± 4.65 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Source Link
Maarten Fabré
  • 9.1k
  • 1
  • 15
  • 27

Apart from the remarks already given about returning instead of printing, and an argument for the number of doors, this code looks good.

Instead of looping over the list, you can also use splicing:

def check_doors_round_splice(n, num_doors=100):
    """Check which door is open after n rounds"""
    doors = [True] * num_doors
    for step in range(1, min(n, num_doors)):
        doors[step :: step + 1] = [
            not door for door in doors[step :: step + 1]
        ]
    return doors