-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
行者
committed
Feb 24, 2021
1 parent
b3bf9b5
commit 4965baf
Showing
11 changed files
with
329 additions
and
6 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1 @@ | ||
x = 127 | ||
i = x | ||
while i > 0: | ||
print(bin(i)) | ||
i = (i-1) & x | ||
# print(i) | ||
print((0.994)**90) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# MD5 实现及其验证 | ||
import math | ||
import hashlib | ||
|
||
|
||
rotate_amounts = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, | ||
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, | ||
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, | ||
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21] | ||
|
||
constants = [int(abs(math.sin(i + 1)) * 2 ** 32) & 0xFFFFFFFF for i in range(64)] | ||
|
||
# A B C D | ||
init_values = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476] | ||
# 非线性函数 | ||
functions = 16 * [lambda b, c, d: (b & c) | (~b & d)] + \ | ||
16 * [lambda b, c, d: (d & b) | (~d & c)] + \ | ||
16 * [lambda b, c, d: b ^ c ^ d] + \ | ||
16 * [lambda b, c, d: c ^ (b | ~d)] | ||
|
||
index_functions = 16 * [lambda i: i] + \ | ||
16 * [lambda i: (5 * i + 1) % 16] + \ | ||
16 * [lambda i: (3 * i + 5) % 16] + \ | ||
16 * [lambda i: (7 * i) % 16] | ||
|
||
|
||
# 对x左移amount位 | ||
def left_rotate(x, amount): | ||
x &= 0xFFFFFFFF | ||
return ((x << amount) | (x >> (32 - amount))) & 0xFFFFFFFF | ||
|
||
|
||
def md5(message): | ||
message = bytearray(message) # copy our input into a mutable buffer | ||
# print(message, type(message)) | ||
orig_len_in_bits = (8 * len(message)) & 0xffffffffffffffff # 取长度的后64位 | ||
message.append(0x80) # 0x80 的二进制刚好是1000 | ||
while len(message) % 64 != 56: | ||
message.append(0) | ||
message += orig_len_in_bits.to_bytes(8, byteorder='little') | ||
|
||
hash_pieces = init_values[:] | ||
|
||
for chunk_ofst in range(0, len(message), 64): | ||
a, b, c, d = hash_pieces | ||
chunk = message[chunk_ofst:chunk_ofst + 64] # 截取出当前的512位的数据 | ||
for i in range(64): # 共四轮,每轮16个子分组,所以计算64次 | ||
f = functions[i](b, c, d) | ||
g = index_functions[i](i) | ||
to_rotate = a + f + constants[i] + int.from_bytes(chunk[4 * g:4 * g + 4], byteorder='little') # 低端 | ||
new_b = (b + left_rotate(to_rotate, rotate_amounts[i])) & 0xFFFFFFFF | ||
a, b, c, d = d, new_b, b, c | ||
for i, val in enumerate([a, b, c, d]):# 新旧相加更新 | ||
hash_pieces[i] += val | ||
hash_pieces[i] &= 0xFFFFFFFF | ||
|
||
return sum(x << (32 * i) for i, x in enumerate(hash_pieces)) | ||
|
||
|
||
def md5_to_hex(digest): | ||
raw = digest.to_bytes(16, byteorder='little') | ||
return '{:032x}'.format(int.from_bytes(raw, byteorder='big')) | ||
|
||
|
||
def true_md5(message): | ||
m = hashlib.md5() | ||
m.update(message) | ||
return m.hexdigest() | ||
|
||
|
||
def my_md5(message): | ||
return md5_to_hex(md5(message)) | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
|
||
|
||
demo = [b"", b"a", b"abc", b"message digest", b"abcdefghijklmnopqrstuvwxyz", | ||
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", | ||
b"123456789012345678901234567890123456789012345678901234567890123456789012345989867890"] | ||
for message in demo: | ||
print(my_md5(message), ' <= "', message.decode('ascii'), '"', sep='') | ||
assert true_md5(message) == my_md5(message) # 若和标准库中不同,会抛出异常 | ||
print('\nMD5测试全部通过') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution(object): | ||
def longestOnes(self, A, K): | ||
N = len(A) | ||
res = 0 | ||
left, right = 0, 0 | ||
zeros = 0 | ||
while right < N: | ||
if A[right] == 0: | ||
zeros += 1 | ||
while zeros > K: | ||
if A[left] == 0: | ||
zeros -= 1 | ||
left += 1 | ||
res = max(res, right - left + 1) | ||
|
||
right += 1 | ||
return res |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# 4 5 * | ||
s = input() | ||
|
||
while True: | ||
if s =="" or s =='\n': | ||
break | ||
s = s.split(' ') | ||
# change data | ||
m = s[0] | ||
m = int(m) | ||
n = int(s[1]) | ||
c = s[2] | ||
|
||
print(c*n) | ||
|
||
for i in range(m-2): | ||
print(' '*(i+1), end=c) | ||
print(' '*(n-2),end=c) | ||
print(' ') | ||
if m >=2: | ||
print(' ' * (m - 1), end='') | ||
print(c*n, end='') | ||
print() | ||
s = input() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
while True: | ||
s = input() | ||
if s =="": | ||
break | ||
s = int(s) | ||
|
||
l = input().split(' ') | ||
list = [int(i) for i in l] | ||
|
||
def judge(nums): | ||
for i in nums: | ||
if i > 0: | ||
return False | ||
|
||
|
||
return True | ||
|
||
ans = 0 | ||
while not judge(list): | ||
ans+=1 | ||
flag = True | ||
while flag: | ||
flag = False | ||
for i in range(s): | ||
if list[i] >0: | ||
list[i]-=2 | ||
if list[i]<=0: | ||
flag =True | ||
|
||
# print(list) | ||
print(ans) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 5 3 | ||
# 1 2 3 4 5 | ||
|
||
while True: | ||
s = input() | ||
if s == '': | ||
break | ||
|
||
n, k = s.split(' ') | ||
n = int(n) | ||
k = int(k) | ||
nums = input().split(' ') | ||
list = [int(i) for i in nums] | ||
|
||
start = min(list) | ||
|
||
def judge(num, nums, k): | ||
count = 0 | ||
for i in nums: | ||
if num % i ==0: | ||
count +=1 | ||
|
||
if k <= count: | ||
return True | ||
else: | ||
return False | ||
|
||
while True: | ||
if judge(start, list, k): | ||
break | ||
else: | ||
start+=1 | ||
|
||
print(start) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Solution(object): | ||
def maxScore(self, cardPoints, k): | ||
""" | ||
:type cardPoints: List[int] | ||
:type k: int | ||
:rtype: int | ||
""" | ||
import copy | ||
# cardPoints = [] | ||
zhen = copy.deepcopy(cardPoints) | ||
cardPoints.reverse() | ||
fan = cardPoints | ||
|
||
preSumZ = [0] | ||
cur = 0 | ||
for i in zhen: | ||
cur+=i | ||
preSumZ.append(cur) | ||
|
||
|
||
preSumF = [0] | ||
cur = 0 | ||
for i in fan: | ||
cur+=i | ||
preSumF.append(cur) | ||
|
||
ans = 0 | ||
# print(preSumZ) | ||
# print(preSumF) | ||
for i in range(k+1): | ||
left = preSumZ[i] | ||
right = preSumF[k-i] | ||
tmp = left+ right | ||
# print(tmp) | ||
ans = max(ans,tmp) | ||
|
||
return ans | ||
Solution.maxScore(None,[100,40,17,9,73,75], | ||
3) |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution(object): | ||
def sumOfUnique(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
f ={} | ||
for i in nums: | ||
if i in f: | ||
f[i]+=1 | ||
else: | ||
f[i] =1 | ||
|
||
ans =0 | ||
# print(f) | ||
for i in f: | ||
if f[i] == 1: | ||
ans+= i | ||
|
||
print(ans) | ||
return ans | ||
|
||
Solution.sumOfUnique(None,[1,2,3,2]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution(object): | ||
def maxAbsoluteSum(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
length = len(nums) | ||
dp = [0 for i in range(length)] | ||
dp[0] = nums[0] | ||
for i in range(1, length): | ||
dp[i] = max(dp[i - 1] + nums[i], nums[i]) | ||
|
||
dp2 = [0 for i in range(length)] | ||
dp2[0] = nums[0] | ||
for i in range(1, length): | ||
dp2[i] = min(dp2[i - 1] + nums[i], nums[i]) | ||
|
||
a = max(dp) | ||
b = abs(min(dp2)) | ||
print(a, b) | ||
return max(a,b) | ||
Solution.maxAbsoluteSum(None,[2,-5,1,-4,3,-2]) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class Solution(object): | ||
def minimumLength(self, s): | ||
""" | ||
:type s: str | ||
:rtype: int | ||
""" | ||
|
||
def judge(s): | ||
l = 0 | ||
r = len(s)-1 | ||
|
||
if r == l: | ||
return 1 | ||
# | ||
# if s.count(s[0]) == len(s): | ||
# return 0 | ||
|
||
if s[l] != s[r]: | ||
return len(s) | ||
else: | ||
ans = 0 | ||
c = s[l] | ||
while l <= r: | ||
f = False | ||
if s[l] == c: | ||
l+=1 | ||
ans+=1 | ||
f = True | ||
# print(l,r) | ||
if l > r: | ||
break | ||
if s[r] == c: | ||
r -=1 | ||
ans+=1 | ||
f = True | ||
|
||
if not f: | ||
if s[l] != s[r] or r - l + 1<2: | ||
break | ||
else: | ||
c = s[l] | ||
# print(s[l:r+1]) | ||
if l > r: | ||
return 0 | ||
|
||
return judge(s[l:r+1]) | ||
|
||
ans = judge(s) | ||
print(ans) | ||
return ans | ||
|
||
Solution.minimumLength(None,"acaca") |