Esempio n. 1
0
    def test_delete_nth_naive(self):

        self.assertListEqual(
            delete_nth_naive([20, 37, 20, 21, 37, 21, 21], n=1), [20, 37, 21])
        self.assertListEqual(
            delete_nth_naive([1, 1, 3, 3, 7, 2, 2, 2, 2], n=3),
            [1, 1, 3, 3, 7, 2, 2, 2])
        self.assertListEqual(
            delete_nth_naive([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1],
                             n=3), [1, 2, 3, 1, 1, 2, 2, 3, 3, 4, 5])
        self.assertListEqual(delete_nth_naive([], n=5), [])
        self.assertListEqual(
            delete_nth_naive([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1],
                             n=0), [])
Esempio n. 2
0
    def test_delete_nth_naive(self):

        self.assertListEqual(delete_nth_naive(
                             [20, 37, 20, 21, 37, 21, 21], n=1),
                             [20, 37, 21])
        self.assertListEqual(delete_nth_naive(
                             [1, 1, 3, 3, 7, 2, 2, 2, 2], n=3),
                             [1, 1, 3, 3, 7, 2, 2, 2])
        self.assertListEqual(delete_nth_naive(
                             [1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=3),
                             [1, 2, 3, 1, 1, 2, 2, 3, 3, 4, 5])
        self.assertListEqual(delete_nth_naive([], n=5),
                             [])
        self.assertListEqual(delete_nth_naive(
                             [1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=0),
                             [])
Esempio n. 3
0
def delete_nth(array, n):
    result = []
    counts = collections.defaultdict(int)  # keep track of occurrences

    for i in array:

        if counts[i] < n:
            result.append(i)
            counts[i] += 1

    return result


a = [1, 2, 3, 1, 2, 1, 2, 3]
N = 2
print(delete_nth_naive(a, N))
#Time Complexity O(n), using hash tables.

print(delete_nth(a, N))

from algorithms.arrays import delete_nth_naive, delete_nth

a = [1, 2, 3, 1, 2, 1, 2, 3]
N = 2
## Time complexity O(n^2)

print(delete_nth_naive(a, N))
#Time Complexity O(n), using hash tables.

print(delete_nth(a, N))