Skip to content

Commit 6512b4b

Browse files
author
曹繁
committed
format code
1 parent 9b945cb commit 6512b4b

File tree

284 files changed

+5487
-5097
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

284 files changed

+5487
-5097
lines changed

.travis.yml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
language: python
22
cache: pip
33
python:
4-
- 2.7
5-
- 3.6
6-
#- nightly
7-
#- pypy
8-
#- pypy3
4+
- 2.7
5+
- 3.6
6+
#- nightly
7+
#- pypy
8+
#- pypy3
99
matrix:
10-
allow_failures:
11-
- python: nightly
12-
- python: pypy
13-
- python: pypy3
10+
allow_failures:
11+
- python: nightly
12+
- python: pypy
13+
- python: pypy3
1414
install:
15-
#- pip install -r requirements.txt
16-
- pip install flake8 # pytest # add another testing frameworks later
15+
#- pip install -r requirements.txt
16+
- pip install flake8 # pytest # add another testing frameworks later
1717
before_script:
18-
# stop the build if there are Python syntax errors or undefined names
19-
- flake8 . --count --select=E9,F63,F72,F82 --show-source --statistics
20-
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
21-
- flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
18+
# stop the build if there are Python syntax errors or undefined names
19+
- flake8 . --count --select=E9,F63,F72,F82 --show-source --statistics
20+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
21+
- flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
2222
script:
23-
- true # pytest --capture=sys # add other tests here
23+
- true # pytest --capture=sys # add other tests here
2424
notifications:
25-
on_success: change
26-
on_failure: change # `always` will be the setting once code changes slow down
25+
on_success: change
26+
on_failure: change # `always` will be the setting once code changes slow down

analysis/compression_analysis/psnr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import cv2
1010
import numpy as np
1111

12+
1213
def psnr(original, contrast):
1314
mse = np.mean((original - contrast) ** 2)
1415
if mse == 0:

arithmetic_analysis/bisection.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
1515
return
1616
else:
1717
mid = (start + end) / 2
18-
while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7
18+
while abs(start - mid) > 10 ** -7: # until we achieve precise equals to 10^-7
1919
if function(mid) == 0:
2020
return mid
2121
elif function(mid) * function(start) < 0:
@@ -27,7 +27,8 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
2727

2828

2929
def f(x):
30-
return math.pow(x, 3) - 2*x - 5
30+
return math.pow(x, 3) - 2 * x - 5
31+
3132

3233
if __name__ == "__main__":
3334
print(bisection(f, 1, 1000))
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import math
22

3-
def intersection(function,x0,x1): #function is the f we want to find its root and x0 and x1 are two random starting points
3+
4+
def intersection(function, x0, x1): # function is the f we want to find its root and x0 and x1 are two random starting points
45
x_n = x0
56
x_n1 = x1
67
while True:
7-
x_n2 = x_n1-(function(x_n1)/((function(x_n1)-function(x_n))/(x_n1-x_n)))
8-
if abs(x_n2 - x_n1) < 10**-5:
8+
x_n2 = x_n1 - (function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n)))
9+
if abs(x_n2 - x_n1) < 10 ** -5:
910
return x_n2
10-
x_n=x_n1
11-
x_n1=x_n2
11+
x_n = x_n1
12+
x_n1 = x_n2
13+
1214

1315
def f(x):
14-
return math.pow(x , 3) - (2 * x) -5
16+
return math.pow(x, 3) - (2 * x) - 5
17+
1518

1619
if __name__ == "__main__":
17-
print(intersection(f,3,3.5))
20+
print(intersection(f, 3, 3.5))
Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
# lower–upper (LU) decomposition - https://en.wikipedia.org/wiki/LU_decomposition
22
import numpy
33

4-
def LUDecompose (table):
4+
5+
def LUDecompose(table):
56
# Table that contains our data
67
# Table has to be a square array so we need to check first
7-
rows,columns=numpy.shape(table)
8-
L=numpy.zeros((rows,columns))
9-
U=numpy.zeros((rows,columns))
10-
if rows!=columns:
8+
rows, columns = numpy.shape(table)
9+
L = numpy.zeros((rows, columns))
10+
U = numpy.zeros((rows, columns))
11+
if rows != columns:
1112
return []
12-
for i in range (columns):
13-
for j in range(i-1):
14-
sum=0
15-
for k in range (j-1):
16-
sum+=L[i][k]*U[k][j]
17-
L[i][j]=(table[i][j]-sum)/U[j][j]
18-
L[i][i]=1
19-
for j in range(i-1,columns):
20-
sum1=0
21-
for k in range(i-1):
22-
sum1+=L[i][k]*U[k][j]
23-
U[i][j]=table[i][j]-sum1
24-
return L,U
13+
for i in range(columns):
14+
for j in range(i - 1):
15+
sum = 0
16+
for k in range(j - 1):
17+
sum += L[i][k] * U[k][j]
18+
L[i][j] = (table[i][j] - sum) / U[j][j]
19+
L[i][i] = 1
20+
for j in range(i - 1, columns):
21+
sum1 = 0
22+
for k in range(i - 1):
23+
sum1 += L[i][k] * U[k][j]
24+
U[i][j] = table[i][j] - sum1
25+
return L, U
26+
2527

2628
if __name__ == "__main__":
27-
matrix =numpy.array([[2,-2,1],
28-
[0,1,2],
29-
[5,3,1]])
30-
L,U = LUDecompose(matrix)
29+
matrix = numpy.array([[2, -2, 1],
30+
[0, 1, 2],
31+
[5, 3, 1]])
32+
L, U = LUDecompose(matrix)
3133
print(L)
3234
print(U)
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
# Newton's Method - https://en.wikipedia.org/wiki/Newton%27s_method
22

3-
def newton(function,function1,startingInt): #function is the f(x) and function1 is the f'(x)
4-
x_n=startingInt
5-
while True:
6-
x_n1=x_n-function(x_n)/function1(x_n)
7-
if abs(x_n-x_n1) < 10**-5:
8-
return x_n1
9-
x_n=x_n1
10-
3+
def newton(function, function1, startingInt): # function is the f(x) and function1 is the f'(x)
4+
x_n = startingInt
5+
while True:
6+
x_n1 = x_n - function(x_n) / function1(x_n)
7+
if abs(x_n - x_n1) < 10 ** -5:
8+
return x_n1
9+
x_n = x_n1
10+
11+
1112
def f(x):
12-
return (x**3) - (2 * x) -5
13+
return (x ** 3) - (2 * x) - 5
14+
1315

1416
def f1(x):
15-
return 3 * (x**2) -2
17+
return 3 * (x ** 2) - 2
18+
1619

1720
if __name__ == "__main__":
18-
print(newton(f,f1,3))
21+
print(newton(f, f1, 3))
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
# Implementing Newton Raphson method in Python
22
# Author: Haseeb
33

4-
from sympy import diff
54
from decimal import Decimal
65

6+
from sympy import diff
7+
8+
79
def NewtonRaphson(func, a):
810
''' Finds root from the point 'a' onwards by Newton-Raphson method '''
911
while True:
10-
c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) )
11-
12+
c = Decimal(a) - (Decimal(eval(func)) / Decimal(eval(str(diff(func)))))
13+
1214
a = c
1315

1416
# This number dictates the accuracy of the answer
15-
if abs(eval(func)) < 10**-15:
16-
return c
17-
17+
if abs(eval(func)) < 10 ** -15:
18+
return c
19+
1820

1921
# Let's Execute
2022
if __name__ == '__main__':
2123
# Find root of trigonometric function
2224
# Find value of pi
23-
print ('sin(x) = 0', NewtonRaphson('sin(x)', 2))
24-
25+
print('sin(x) = 0', NewtonRaphson('sin(x)', 2))
26+
2527
# Find root of polynomial
26-
print ('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))
27-
28+
print('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))
29+
2830
# Find Square Root of 5
29-
print ('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))
31+
print('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))
3032

3133
# Exponential Roots
32-
print ('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
33-
34-
35-
36-
34+
print('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))

binary_tree/basic_binary_tree.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
class Node: # This is the Class Node with constructor that contains data variable to type data and left,right pointers.
1+
class Node: # This is the Class Node with constructor that contains data variable to type data and left,right pointers.
22
def __init__(self, data):
33
self.data = data
44
self.left = None
55
self.right = None
66

7-
def display(tree): #In Order traversal of the tree
87

9-
if tree is None:
8+
def display(tree): # In Order traversal of the tree
9+
10+
if tree is None:
1011
return
1112

1213
if tree.left is not None:
@@ -19,7 +20,8 @@ def display(tree): #In Order traversal of the tree
1920

2021
return
2122

22-
def depth_of_tree(tree): #This is the recursive function to find the depth of binary tree.
23+
24+
def depth_of_tree(tree): # This is the recursive function to find the depth of binary tree.
2325
if tree is None:
2426
return 0
2527
else:
@@ -31,7 +33,7 @@ def depth_of_tree(tree): #This is the recursive function to find the depth of bi
3133
return 1 + depth_r_tree
3234

3335

34-
def is_full_binary_tree(tree): # This functions returns that is it full binary tree or not?
36+
def is_full_binary_tree(tree): # This functions returns that is it full binary tree or not?
3537
if tree is None:
3638
return True
3739
if (tree.left is None) and (tree.right is None):
@@ -42,7 +44,7 @@ def is_full_binary_tree(tree): # This functions returns that is it full binary t
4244
return False
4345

4446

45-
def main(): # Main func for testing.
47+
def main(): # Main func for testing.
4648
tree = Node(1)
4749
tree.left = Node(2)
4850
tree.right = Node(3)

0 commit comments

Comments
 (0)