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 function using range dictionary """ | |
| scores_class_dict={0: 'intellectually disabled', | |
| 70: 'below average', | |
| 85: 'average', | |
| 115: 'bright', | |
| 130: 'gifted', | |
| 145: 'highly gifted', | |
| 160: 'exceptionally gifted', |
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
| class RangeDict(dict): | |
| """ A dictionary mapping a key to a range of integers """ | |
| def __getitem__(self, key): | |
| sorted_keys = sorted(self.keys()) | |
| idx = bisect.bisect_right(sorted_keys, key) | |
| # Mapped key is one down from index but we don't | |
| # want to cycle using negative indices | |
| if idx > 0: idx -= 1 | |
| mapped_key = sorted_keys[idx] |
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] |