diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-07-05 09:09:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-05 09:09:36 +0100 |
| commit | f7832accd28639f4ff03bd7a56df4a74f7a6b70f (patch) | |
| tree | 6b387c0a4b1acf914b93bb499de331c476c1ee52 /challenge-172/walt-mankowski/python/ch-2.py | |
| parent | 62c9865aed3c8c5781d0c18fc7e7bd6949311202 (diff) | |
| parent | 79c806168990c237d27048fce85af6116ac63127 (diff) | |
| download | perlweeklychallenge-club-f7832accd28639f4ff03bd7a56df4a74f7a6b70f.tar.gz perlweeklychallenge-club-f7832accd28639f4ff03bd7a56df4a74f7a6b70f.tar.bz2 perlweeklychallenge-club-f7832accd28639f4ff03bd7a56df4a74f7a6b70f.zip | |
Merge pull request #6394 from waltman/branch-for-challenge-172
Branch for challenge 172
Diffstat (limited to 'challenge-172/walt-mankowski/python/ch-2.py')
| -rw-r--r-- | challenge-172/walt-mankowski/python/ch-2.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-172/walt-mankowski/python/ch-2.py b/challenge-172/walt-mankowski/python/ch-2.py new file mode 100644 index 0000000000..c1b356d5f3 --- /dev/null +++ b/challenge-172/walt-mankowski/python/ch-2.py @@ -0,0 +1,26 @@ +import sys + +# returns the median of a sorted list +def median_sorted(a): + len2 = len(a) // 2 + return a[len2] if len(a) % 2 == 1 else (a[len2-1] + a[len2]) / 2 + +# returns the 5 number summary of a list: minimum, lower quartile, +# median, upper quartile, maximum +def fivenum(a): + _sorted = sorted(a) + _min = _sorted[0] + _max = _sorted[-1] + _median = median_sorted(_sorted) + + len2 = len(a) // 2 + _lower = median_sorted(_sorted[0:len2]) + if len(a) % 2 == 1: # odd number of elements + _upper = median_sorted(_sorted[len2+1:]) + else: + _upper = median_sorted(_sorted[len2:]) + + return _min, _lower, _median, _upper, _max + +a = [int(x) for x in sys.argv[1:]] +print(fivenum(a)) |
