Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite4b1196

Browse files
committed
Fix median function
1 parent8386b7d commite4b1196

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

‎ch06-functions-and-loops/5-challenge-track-your-investments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Solution to challenge
33

44

5-
# Calculatecompoundinterest to track the growth of an investment
5+
# Calculate interest to track the growth of an investment
66

77

88
definvest(amount,rate,years):

‎ch09-lists-tuples-and-dictionaries/4-challenge-list-of-lists.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,27 @@ def enrollment_stats(list_of_universities):
1818

1919

2020
defmean(values):
21+
"""Return the mean value in the list `values`"""
2122
returnsum(values)/len(values)
2223

2324

2425
defmedian(values):
26+
"""Return the median value of the list `values`"""
2527
values.sort()
26-
length=len(values)
27-
ifnotlength%2:
28-
returnvalues[(length-1)/2]
29-
returnvalues[int(length/2)]
28+
# If the number of valus is odd,
29+
# return the middle value of the list
30+
iflen(values)%2==1:
31+
# The value at the center of the list is the value
32+
# at whose index is half of the length of the list,
33+
# rounded down
34+
center_index=int(len(values)/2)
35+
returnvalues[center_index]
36+
# Otherwise, if the length of the list is even, return
37+
# the mean of the two center values
38+
else:
39+
left_center_index= (len(values)-1)/2
40+
right_center_index= (len(values)+1)/2
41+
returnmean([left_center_index,right_center_index])
3042

3143

3244
universities= [

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp