Last active
March 15, 2020 00:22
-
-
Save kingspp/13444df1c55c2477cf12a6ab0d65fd14 to your computer and use it in GitHub Desktop.
Collection of algorithms in pure python
This file contains 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
# Argsort | |
l = [1,8,3,10,13, 23,4] | |
# Ascending | |
args = sorted(range(len(l)), key=l.__getitem__) | |
#[0, 2, 6, 1, 3, 4, 5] | |
# Descending | |
args[::-1] | |
#[5, 4, 3, 1, 6, 2, 0] | |
# Frequency Counter | |
x = [1,2,2,3,3,4,4,4,5,5,5] | |
frequency = {} | |
for i in set(x): | |
frequency[i] = x.count(i) | |
#{1: 1, 2: 2, 3: 2, 4: 3, 5: 3} | |
# Median | |
n_num = [1, 2, 3, 4, 5] | |
n = len(n_num) | |
n_num.sort() | |
if n % 2 == 0: | |
median1 = n_num[n//2] | |
median2 = n_num[n//2 - 1] | |
median = (median1 + median2)/2 | |
else: | |
median = n_num[n//2] | |
# Mode | |
n_num = [1, 2, 3, 4, 5, 5] | |
c = {i: n_num.count(i) for i in set(n_num)} | |
list(c.keys())[list(c.values()).index(max(c.values()))] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment