This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def IQ(score): | |
| ranges = (70, 85, 115, 130, 145, 160, 180) | |
| rating = ("intellectually disabled", | |
| "below average", | |
| "average", | |
| "bright", | |
| "gifted", | |
| "highly gifted", | |
| "exceptionally gifted", | |
| "profoundly gifted/genius") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> IQ(65) | |
| 'intellectually disabled' | |
| >>> IQ(100) | |
| 'average' | |
| >>> IQ(125) | |
| 'bright' | |
| >>> IQ(200) | |
| 'profoundly gifted/genius' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def IQ(score): | |
| """ IQ score -> rating """ | |
| if score < 70: | |
| return "intellectually disabled" | |
| elif score in range(70, 85): | |
| return "below average" | |
| elif score in range(85, 115): | |
| return "average" | |
| elif score in range(115, 130): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> l=[10, 12, 15, 17, 18, 20, 20, 21, 21, 21, 24, 24] | |
| >>> occurs(l, 18) | |
| range(4, 5) | |
| >>> occurs(l, 20) | |
| range(5, 7) | |
| >>> list(occurs(l, 21)) | |
| [7, 8, 9] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def occurs(a, x): | |
| """ Return all occurrences of x in a """ | |
| start = search(a, x) | |
| end = search_right(a, x) | |
| if start != -1 and end != -1: | |
| return range(start, end, 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> l=[10, 12, 15, 17, 18, 20, 20, 21, 21, 21, 24, 24] | |
| >>> search(l, 18) | |
| 4 | |
| >>> search(l, 20) | |
| 5 | |
| >>> search(l, 21) | |
| 7 | |
| >>> search_right(l, 21) | |
| 10 | |
| >>> search_right(l, 24) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def search(a, x): | |
| """ Locate the index of the leftmost value exactly equal to x """ | |
| i = bisect.bisect_left(a, x) | |
| if i != len(a) and a[i] == x: | |
| return i | |
| return -1 | |
| def search_right(a, x): | |
| """ Locate the index of the right most value exactly equal to x """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> bisect.insort_left(l, 12) | |
| >>> l | |
| [10, 12, 15, 18, 20, 21, 21, 21, 24, 24] | |
| >>> bisect.insort_right(l, 20) | |
| >>> l | |
| [10, 12, 15, 18, 20, 20, 21, 21, 21, 24, 24] | |
| >>> bisect.insort_left(l, 17) | |
| >>> l | |
| [10, 12, 15, 17, 18, 20, 20, 21, 21, 21, 24, 24] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> l = [10, 15, 18, 20, 21, 21, 21, 24, 24] | |
| >>> binary_search(l, 18) | |
| 2 | |
| >>> binary_search(l, 21) | |
| 4 | |
| >>> binary_search(l, 24) | |
| 7 | |
| >>> binary_search(l, 100) | |
| -1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import bisect | |
| def binary_search(a, x, lo=0, hi=None): | |
| """ Search for item x in a using bisect binary search """ | |
| if hi is None: | |
| hi = len(a) | |
| pos = bisect.bisect_left(a, x, lo, hi) | |
| if pos != hi and a[pos] == x: | |
| return pos |