Topic 1:
Implementation 2:
def funOne(n):
you = 0xaaaaaaaa & n
ji = 0x55555555 & n
print(bin(n), bin(you), bin(ji))
print((you>>1)^(ji<<1));
Topic 2:
Implementation 2:
def funThree(n):
res = 0
count = 0
while n != 0:
if n >= 1:
res = res + 1 / (10**count)
n -= 1
else:
n = n * 2
count += 1
print(res)
if __name__ == "__main__":
funThree(0.3)
Topic 3:
This topic should be a bit of a problem. The k here should be an integer multiple of 3
Implementation 3:
def fun():
ls = [1,1,1,4,4,4,5,5,5,13,2,2,2,9,9,9]
res = []
maxLen = 0
for i in range(len(ls)):
s = transform(ls[i])
if len(s) > maxLen:
maxLen = len(s)
res.append(s)
for i in range(len(res)):
if len(res[i]) < maxLen:
for j in range(maxLen - len(res[i])):
res[i].insert(0, 0)
myRes = []
for i in range(len(res[0])):
mySum = 0
for j in range(len(res)):
mySum = (mySum + res[j][i]) % 3
myRes.append(mySum)
result = 0
myRes.reverse()
print(myRes)
for i in range(len(myRes)):
result += myRes[i] * (3 **(i))
print("最后结果为:", result)
Topic 4:
Find the factorial of n
Implementation 4:
def f(n):
if n == 1:
return 1
return n*f(n-1)
if __name__ == "__main__":
print(f(4))
Topic 5:
print [ i , j ] left closed right closed
Implementation 5:
def fOne(i, j):
if i > j:
return
print(i)
fOne(i+1,j)
if __name__ == "__main__":
fOne(11, 10)
Topic 6:
array sum
Implementation 6:
def fTwo(ls):
if len(ls) == 0:
return 0
return ls[0]+fTwo(ls[1:])
if __name__ == "__main__":
print(fTwo(list(range(1, 101))))
Topic 7:
The two methods of string flip recursion are similar
Implementation 7:
def fThree(string, left, right):
if left > right:
return ""
if left == right:
return string[left]
return string[right] + fThree(string, left+1, right-1) + string[left]
def fFour(string, end):
if(end == 0):
return ""+string[end]
return string[end]+fFour(string, end-1)
if __name__ == "__main__":
print(fThree("abcde", 0, 4))
print(fFour("abcde", 4))