Skip to content

Commit cc46c7a

Browse files
bites 118 - list and set
1 parent f901e52 commit cc46c7a

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

118/duplicates.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def get_duplicate_indices(words):
2+
"""Given a list of words, loop through the words and check for each
3+
word if it occurs more than once.
4+
If so return the index of its first ocurrence.
5+
For example in the following list 'is' and 'it'
6+
occurr more than once, and they are at indices 0 and 1 so you would
7+
return [0, 1]:
8+
['is', 'it', 'true', 'or', 'is', 'it', 'not?'] => [0, 1]
9+
Make sure the returning list is unique and sorted in ascending order."""
10+
11+
# list of the words that occurs more than once
12+
duplicated = [word for word in set(words) if words.count(word) > 1]
13+
# list of first occurence position of duplicated words
14+
pos = [words.index(dupword) for dupword in duplicated]
15+
return sorted(pos)

118/test_duplicates.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from duplicates import get_duplicate_indices
2+
3+
4+
def test_get_duplicate_indices_docstring():
5+
words = ['is', 'it', 'true', 'or', 'is', 'it', 'not']
6+
assert get_duplicate_indices(words) == [0, 1]
7+
8+
9+
def test_get_duplicate_indices_bite_text():
10+
words = ['this', 'is', 'a', 'new', 'bite', 'I', 'hope', 'this',
11+
'bite', 'will', 'teach', 'you', 'something', 'new']
12+
assert get_duplicate_indices(words) == [0, 3, 4]
13+
14+
15+
def test_get_duplicate_indices_another_text():
16+
# keeping it simple with split on space, so lists != lists.
17+
words = ('List comprehensions provide a concise way to create '
18+
'lists. Common applications are to make new lists where '
19+
'each element is the result of some operations applied '
20+
'to each member of another sequence or iterable, or to '
21+
'create a subsequence of those elements that satisfy a '
22+
'certain condition').split()
23+
assert get_duplicate_indices(words) == [3, 6, 7, 17, 22, 32]

140-pandas/medals.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from collections import namedtuple
2+
13
import pandas as pd
24

35
data = "https://bites-data.s3.us-east-2.amazonaws.com/summer.csv"

0 commit comments

Comments
 (0)