Skip to content

gnome_sort.py main block missing doctest.testmod() call #14881

Description

@dhruv-gautam-dev

What would you like to share?

In sorts/gnome_sort.py, the main block does not call testmod()
to run the file's doctests, unlike other sorting algorithm files in
the same directory (e.g. bubble_sort.py, insertion_sort.py).

Why this matters:
This repository is widely used as an educational reference for
learning algorithms. When learners run a file directly with
python3 gnome_sort.py to study and verify an algorithm, they expect
the doctests to run automatically and confirm correctness, just like
they do in bubble_sort.py and insertion_sort.py. Without testmod()
being called, learners have no automatic confirmation that the
algorithm works correctly when running the file the "normal" way,
and may miss verifying the doctest examples entirely.

It also creates inconsistency across the codebase, making the sorts/
directory harder to maintain since each file behaves differently when
executed directly.

Current code:
if name == "main":
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
print(gnome_sort(unsorted))

Proposed fix:
if name == "main":
from doctest import testmod

testmod()

user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
print(gnome_sort(unsorted))

This makes gnome_sort.py consistent with the pattern used across
other files in the sorts/ directory, and ensures learners get
automatic doctest verification when running the file directly.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting triageAwaiting triage from a maintainer

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions