mirror of
https://github.com/autistic-symposium/sec-pentesting-toolkit.git
synced 2025-05-02 14:56:10 -04:00
some math scripts
This commit is contained in:
parent
9dc0d638a4
commit
ab54dc8e70
4 changed files with 151 additions and 0 deletions
35
Cryptography/tools/quick_select.py
Executable file
35
Cryptography/tools/quick_select.py
Executable file
|
@ -0,0 +1,35 @@
|
|||
def quickSelect(seq, k):
|
||||
# this part is the same as quick sort
|
||||
len_seq = len(seq)
|
||||
if len_seq < 2: return seq
|
||||
|
||||
ipivot = len_seq // 2
|
||||
pivot = seq[ipivot]
|
||||
|
||||
smallerList = [x for i,x in enumerate(seq) if x <= pivot and i != ipivot]
|
||||
largerList = [x for i,x in enumerate(seq) if x > pivot and i != ipivot]
|
||||
|
||||
# here starts the different part
|
||||
m = len(smallerList)
|
||||
if k == m:
|
||||
return pivot
|
||||
elif k < m:
|
||||
return quickSelect(smallerList, k)
|
||||
else:
|
||||
return quickSelect(largerList, k-m)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Checking the Answer
|
||||
seq = [10, 60, 100, 50, 60, 75, 31, 50, 30, 20, 120, 170, 200]
|
||||
|
||||
# we want the middle element
|
||||
k = len(seq) // 2
|
||||
|
||||
# Note that this only work for odd arrays, since median in
|
||||
# even arrays is the mean of the two middle elements
|
||||
print(quickSelect(seq, k))
|
||||
import numpy
|
||||
print numpy.median(seq)
|
Loading…
Add table
Add a link
Reference in a new issue