Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita2236cf

Browse files
PatOnTheBackAnupKumarPanwar
authored andcommitted
Improve Formatting and Code Quality (#934)
* Improved Formatting of basic_maths.py- Added docstrings.- Improved whitespace formatting.- Renamed functions to match snake_case.* Improved Formatting of factorial_python.py- Added docstrings.- Improved whitespace formatting.- Renamed constants to match UPPER_CASE.* Improved Formatting of factorial_recursive.py- Improved whitespace formatting to meet PyLint standards.* Improved Code to Conform to PyLint- Renamed `max` to `max_num` to avoid redefining built-in 'max' [pylint]- Removed unnecessary parens after 'while' keyword [pylint]* Improved Formatting of factorial_recursive.py- Added docstrings.- Improved whitespace formatting.
1 parentbd40179 commita2236cf

File tree

5 files changed

+71
-57
lines changed

5 files changed

+71
-57
lines changed

‎maths/basic_maths.py‎

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,84 @@
1+
"""Implementation of Basic Math in Python."""
12
importmath
23

3-
defprimeFactors(n):
4+
5+
defprime_factors(n):
6+
"""Find Prime Factors."""
47
pf= []
58
whilen%2==0:
69
pf.append(2)
710
n=int(n/2)
8-
9-
foriinrange(3,int(math.sqrt(n))+1,2):
11+
12+
foriinrange(3,int(math.sqrt(n))+1,2):
1013
whilen%i==0:
1114
pf.append(i)
1215
n=int(n/i)
13-
16+
1417
ifn>2:
1518
pf.append(n)
16-
19+
1720
returnpf
1821

19-
defnumberOfDivisors(n):
22+
23+
defnumber_of_divisors(n):
24+
"""Calculate Number of Divisors of an Integer."""
2025
div=1
21-
26+
2227
temp=1
2328
whilen%2==0:
2429
temp+=1
2530
n=int(n/2)
26-
div=div* (temp)
27-
28-
foriinrange(3,int(math.sqrt(n))+1,2):
31+
div=div* (temp)
32+
33+
foriinrange(3,int(math.sqrt(n))+1,2):
2934
temp=1
3035
whilen%i==0:
3136
temp+=1
3237
n=int(n/i)
3338
div=div* (temp)
34-
39+
3540
returndiv
3641

37-
defsumOfDivisors(n):
42+
43+
defsum_of_divisors(n):
44+
"""Calculate Sum of Divisors."""
3845
s=1
39-
46+
4047
temp=1
4148
whilen%2==0:
4249
temp+=1
4350
n=int(n/2)
4451
iftemp>1:
45-
s*= (2**temp-1)/ (2-1)
46-
47-
foriinrange(3,int(math.sqrt(n))+1,2):
52+
s*= (2**temp-1)/ (2-1)
53+
54+
foriinrange(3,int(math.sqrt(n))+1,2):
4855
temp=1
4956
whilen%i==0:
5057
temp+=1
5158
n=int(n/i)
5259
iftemp>1:
5360
s*= (i**temp-1)/ (i-1)
54-
61+
5562
returns
5663

57-
defeulerPhi(n):
58-
l=primeFactors(n)
64+
65+
defeuler_phi(n):
66+
"""Calculte Euler's Phi Function."""
67+
l=prime_factors(n)
5968
l=set(l)
6069
s=n
6170
forxinl:
62-
s*= (x-1)/x
63-
returns
71+
s*= (x-1)/x
72+
returns
73+
6474

6575
defmain():
66-
print(primeFactors(100))
67-
print(numberOfDivisors(100))
68-
print(sumOfDivisors(100))
69-
print(eulerPhi(100))
70-
76+
"""Print the Results of Basic Math Operations."""
77+
print(prime_factors(100))
78+
print(number_of_divisors(100))
79+
print(sum_of_divisors(100))
80+
print(euler_phi(100))
81+
82+
7183
if__name__=='__main__':
7284
main()
73-
74-

‎maths/factorial_python.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
#Python program to find the factorial of a number provided by the user.
1+
"""Python program to find the factorial of a number provided by the user."""
22

33
# change the value for a different result
4-
num=10
4+
NUM=10
55

66
# uncomment to take input from the user
7-
#num = int(input("Enter a number: "))
7+
#num = int(input("Enter a number: "))
88

9-
factorial=1
9+
FACTORIAL=1
1010

1111
# check if the number is negative, positive or zero
12-
ifnum<0:
13-
print("Sorry, factorial does not exist for negative numbers")
14-
elifnum==0:
15-
print("The factorial of 0 is 1")
12+
ifNUM<0:
13+
print("Sorry, factorial does not exist for negative numbers")
14+
elifNUM==0:
15+
print("The factorial of 0 is 1")
1616
else:
17-
foriinrange(1,num+1):
18-
factorial=factorial*i
19-
print("The factorial of",num,"is",factorial)
17+
foriinrange(1,NUM+1):
18+
FACTORIAL=FACTORIAL*i
19+
print("The factorial of",NUM,"is",FACTORIAL)

‎maths/factorial_recursive.py‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
deffact(n):
2-
"""
3-
Return 1, if n is 1 or below,
4-
otherwise, return n * fact(n-1).
5-
"""
6-
return1ifn<=1elsen*fact(n-1)
2+
"""
3+
Return 1, if n is 1 or below,
4+
otherwise, return n * fact(n-1).
5+
"""
6+
return1ifn<=1elsen*fact(n-1)
7+
78

89
"""
9-
Shown factorial for i,
10+
Show factorial for i,
1011
where i ranges from 1 to 20.
1112
"""
12-
foriinrange(1,21):
13-
print(i,": ",fact(i),sep='')
13+
foriinrange(1,21):
14+
print(i,": ",fact(i),sep='')

‎maths/find_lcm.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
deffind_lcm(num_1,num_2):
77
"""Find the LCM of two numbers."""
8-
max=num_1ifnum_1>num_2elsenum_2
9-
lcm=max
10-
while(True):
8+
max_num=num_1ifnum_1>num_2elsenum_2
9+
lcm=max_num
10+
whileTrue:
1111
if ((lcm%num_1==0)and (lcm%num_2==0)):
1212
break
13-
lcm+=max
13+
lcm+=max_num
1414
returnlcm
1515

1616

‎sorts/pancake_sort.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
#Pancakesort algorithm
1+
"""PancakeSort Algorithm."""
22
# Only can reverse array from 0 to i
33

4+
45
defpancake_sort(arr):
6+
"""Sort Array with Pancake Sort."""
57
cur=len(arr)
68
whilecur>1:
79
# Find the maximum number in arr
810
mi=arr.index(max(arr[0:cur]))
9-
# Reverse from 0 to mi
10-
arr=arr[mi::-1]+arr[mi+1:len(arr)]
11-
# Reverse whole list
12-
arr=arr[cur-1::-1]+arr[cur:len(arr)]
11+
# Reverse from 0 to mi
12+
arr=arr[mi::-1]+arr[mi+1:len(arr)]
13+
# Reverse whole list
14+
arr=arr[cur-1::-1]+arr[cur:len(arr)]
1315
cur-=1
1416
returnarr
1517

18+
1619
if__name__=='__main__':
17-
print(pancake_sort([0,10,15,3,2,9,14,13]))
20+
print(pancake_sort([0,10,15,3,2,9,14,13]))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp