Skip to content

Commit 771adf4

Browse files
committed
Final QA for the Python list tutorial
1 parent 0995381 commit 771adf4

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

python-list/median.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
def median(sample):
2-
n = len(sample)
3-
index = n // 2
4-
sorted_sample = sorted(sample)
1+
def median(samples):
2+
n = len(samples)
3+
middle_index = n // 2
4+
sorted_samples = sorted(samples)
55
# Odd number of values
6-
if n % 2 != 0:
7-
return sorted_sample[index]
6+
if n % 2:
7+
return sorted_samples[middle_index]
88
# Even number of values
9-
lower, upper = index - 1, index + 1
10-
return sum(sorted_sample[lower:upper]) / 2
9+
lower, upper = middle_index - 1, middle_index + 1
10+
return sum(sorted_samples[lower:upper]) / 2

python-list/repeated.py

+12
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,15 @@ def get_unique_items(list_object):
44
if item not in result:
55
result.append(item)
66
return result
7+
8+
9+
# Faster but requires more memory:
10+
#
11+
# def get_unique_items(list_object):
12+
# result = []
13+
# unique_items = set()
14+
# for item in list_object:
15+
# if item not in unique_items:
16+
# result.append(item)
17+
# unique_items.add(item)
18+
# return result

0 commit comments

Comments
 (0)