Skip to content

Instantly share code, notes, and snippets.

@adambene
Last active July 1, 2020 19:04
Show Gist options
  • Select an option

  • Save adambene/3723992a4b43a29876accbf50908e501 to your computer and use it in GitHub Desktop.

Select an option

Save adambene/3723992a4b43a29876accbf50908e501 to your computer and use it in GitHub Desktop.

Revisions

  1. adambene revised this gist Jul 1, 2020. 2 changed files with 21 additions and 8 deletions.
    8 changes: 0 additions & 8 deletions distinct_naive.py
    Original file line number Diff line number Diff line change
    @@ -1,8 +0,0 @@
    def distinct_naive(items):
    results = list()

    for item in items:
    if not item in results:
    results.append(item)

    return results
    21 changes: 21 additions & 0 deletions distinct_naive_verbose.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    def distinct_naive_verbose(items):
    """
    This naive and verbose implementation returns distinct elements of a list.
    """
    results = list()

    # iterate through all items
    for item in items:
    member = False

    # linear search for the actual item in results
    for result in results:
    if item == result:
    member = True
    break

    # if we didn't find the item in results it is unique and we append to the results
    if not member:
    results.append(item)

    return results
  2. adambene revised this gist Jul 1, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions distinct_naive.py
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    def distinct_naive(items):
    result = list()
    results = list()

    for item in items:
    if not item in result:
    result.append(item)
    if not item in results:
    results.append(item)

    return result
    return results
  3. adambene created this gist Jul 1, 2020.
    8 changes: 8 additions & 0 deletions distinct_naive.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    def distinct_naive(items):
    result = list()

    for item in items:
    if not item in result:
    result.append(item)

    return result