Skip to content

Commit 2c05963

Browse files
committed
Linting to Mathematics
1 parent d5424f4 commit 2c05963

13 files changed

+421
-138
lines changed

Diff for: Mathematics/2.1.3.count_digit_conversion.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
COUNT DIGITS METHOD - I: Iterative Method
2+
COUNT DIGITS METHOD - III: TYPE CONVERSION
33
44
Given a number count the number of digits
55
@@ -9,13 +9,22 @@
99
IP: 787878
1010
OP: 6
1111
12-
12+
TC: O(1)
13+
SC: O(digits in N)
1314
"""
1415

15-
def count_digit(n):
16-
""" Conversion """
17-
# n = len(str(n))
18-
# return n
16+
def count_digit(num: int)-> int:
17+
""" Type Conversion Method
18+
19+
Args:
20+
num (int): number to count digits of
21+
22+
Returns:
23+
int: Number of digits in a number
24+
"""
25+
return len(str(num))
26+
1927

20-
n = 23965465
21-
print("The number of digits in ", n, "is", count_digit(n))
28+
if __name__ == '__main__':
29+
N = int(input('Enter the number: '))
30+
print("The number of digits in ", N, "is", count_digit(N))

Diff for: Mathematics/2.2.1.palindrome_number.py

+38-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1-
num = int(input('Enter the number:'))
2-
rev = 0
3-
temp = num
4-
while temp > 0:
5-
dig = temp % 10
6-
rev = rev*10+dig
7-
temp = temp // 10
8-
if num == rev:
9-
print('YES')
10-
else:
11-
print('NO')
1+
"""
2+
PALINDROME NUMBER | METHOD - I: NAIVE
3+
4+
Check whether the given number is palindrome or not
5+
6+
IP:565655
7+
OP: NO
8+
9+
IP: 89998
10+
OP: YES
11+
12+
TC: O(log n)
13+
SC: O(1)
14+
"""
15+
16+
def check_palindrome(num: int)-> bool:
17+
"""check_palindrome: Naive
18+
19+
Args:
20+
num (int): Number to check
21+
22+
Returns:
23+
bool: True if the number is palindrome, False otherwise
24+
"""
25+
rev = 0
26+
temp = num
27+
while temp > 0:
28+
dig = temp % 10
29+
rev = rev * 10 + dig
30+
temp = temp // 10
31+
return bool(num == rev)
32+
33+
if __name__ == '__main__':
34+
N = int(input("Enter the number: "))
35+
if check_palindrome(N):
36+
print("YES")
37+
else:
38+
print("NO")

Diff for: Mathematics/2.2.2.palindromeNoM2.py

-7
This file was deleted.

Diff for: Mathematics/2.2.2.palindrome_number_m2.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
PALINDROME NUMBER | METHOD - II: CONVERT TO STRING AND REVERSE CHECK
3+
4+
Check whether the given number is palindrome or not
5+
6+
IP:565655
7+
OP: NO
8+
9+
IP: 89998
10+
OP: YES
11+
12+
TC: O(log n)
13+
SC: O(1)
14+
"""
15+
16+
def check_palindrome(num: int)-> bool:
17+
"""check_palindrome: Naive
18+
19+
Args:
20+
num (int): Number to check
21+
22+
Returns:
23+
bool: True if the number is palindrome, False otherwise
24+
"""
25+
num_str = str(num)
26+
rev = num_str[::-1]
27+
return bool(rev == num_str)
28+
29+
if __name__ == '__main__':
30+
N = int(input("Enter the number: "))
31+
if check_palindrome(N):
32+
print("YES")
33+
else:
34+
print("NO")

Diff for: Mathematics/2.3.1.factorial_of_num_recursive.py

+33-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
1-
def fact(num):
2-
if num == 1 or num == 0:
1+
"""
2+
FACTORIAL OF A NUMBER | METHOD - I: RECURSIVE
3+
4+
Find the factorial of the given number.
5+
6+
Base Condition:
7+
1! = 1
8+
0! = 1
9+
10+
IP: N = 6
11+
OP:720
12+
Explaination: ans = 6*5*4*3*2*1 = 720
13+
14+
TC: O(n)
15+
SC: O(n)
16+
"""
17+
18+
def fact(num: int)-> int:
19+
"""factorial of a number
20+
21+
Args:
22+
num (int): to find factorial of
23+
24+
Returns:
25+
int: factorial of a number
26+
"""
27+
# Base Condition
28+
if num in (1, 0):
329
return 1
4-
else:
5-
return num*fact(num-1)
630

7-
num = int(input("Enter the number:"))
8-
print(f"{num}! = {fact(num)}")
31+
return num*fact(num-1)
32+
33+
if __name__ == '__main__':
34+
N = int(input("Enter the number:"))
35+
print(f"{N}! = {fact(N)}")

Diff for: Mathematics/2.3.2.factorial_of_num.py

-8
This file was deleted.

Diff for: Mathematics/2.3.2.factorial_of_num_iterative.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
FACTORIAL OF A NUMBER | METHOD - II: ITERATIVE
3+
4+
Find the factorial of the given number.
5+
6+
Base Condition:
7+
1! = 1
8+
0! = 1
9+
10+
IP: N = 6
11+
OP:720
12+
Explaination: ans = 6*5*4*3*2*1 = 720
13+
14+
TC: O(n)
15+
SC: O(1)
16+
"""
17+
18+
def fact(num: int)-> int:
19+
"""factorial of a number
20+
21+
Args:
22+
num (int): to find factorial of
23+
24+
Returns:
25+
int: factorial of a number
26+
"""
27+
factorial = 1
28+
for i in range(1, num + 1):
29+
factorial = factorial * i
30+
return f"{num}! = {factorial}"
31+
32+
33+
if __name__ == '__main__':
34+
N = int(input("Enter the number:"))
35+
if N < 0:
36+
print("Factorial of -ve number does not exists...")
37+
else:
38+
print(fact(N))

Diff for: Mathematics/2.4.1.gcd_naive.py

+48-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
1-
def gcd(m, n):
2-
fm = []
3-
for i in range(1, m+1):
4-
if (m%i)==0:
5-
fm.append(i)
6-
7-
fn = []
8-
for j in range(1, n+1):
9-
if (n%j)==0:
10-
fn.append(j)
11-
12-
cf = []
13-
for f in fm:
14-
if f in fn:
15-
cf.append(f)
16-
return cf[-1]
17-
18-
19-
m = int(input("Enter the first number: "))
20-
n = int(input("Enter the second number: "))
21-
print(f"gcd({m}, {n}) = {gcd(m,n)}")
22-
1+
"""
2+
GCD of TWO NUMBERS | METHOD - I: NAIVE
3+
4+
Calculate the greatest common divisior of two number
5+
6+
IP: M = 8, N = 6
7+
OP: 2
8+
Explaination:
9+
8 = 4*2*1
10+
6 = 3*2*1
11+
GCD(8, 6) = 2
12+
13+
TC: O(min(m,n))
14+
SC: O(1)
15+
"""
16+
17+
def gcd(num1: int, num2: int)-> int:
18+
"""GCD of two number - Naive
19+
20+
Args:
21+
num1 (int): 1st number
22+
num1 (int): 2nd number
23+
24+
Returns:
25+
int: GCD(num1, num2)
26+
"""
27+
28+
factor_num1 = []
29+
for i in range(1, num1+1):
30+
if num1%i:
31+
factor_num1.append(i)
32+
33+
factor_num2 = []
34+
for j in range(1, num2+1):
35+
if num2 % j:
36+
factor_num2.append(j)
37+
38+
common_factors = []
39+
for i in factor_num1:
40+
if i in factor_num2:
41+
common_factors.append(i)
42+
return common_factors[-1]
43+
44+
if __name__ == '__main__':
45+
M = int(input("Enter the first number: "))
46+
N = int(input("Enter the second number: "))
47+
48+
print(f"gcd({M}, {N}) = {gcd(M, N)}")

Diff for: Mathematics/2.4.2.gcd.py

+61-31
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,63 @@
1+
"""
2+
GCD of TWO NUMBERS | OTHER METHODS
3+
4+
Calculate the greatest common divisior of two number
5+
6+
IP: M = 8, N = 6
7+
OP: 2
8+
Explaination:
9+
8 = 4*2*1
10+
6 = 3*2*1
11+
GCD(8, 6) = 2
12+
13+
TC: O(log(min(m,n)))
14+
SC: O(log(min(m,n)))
15+
"""
16+
17+
def gcd(num1: int,num2: int)-> int:
18+
"""GCD of two number
19+
20+
Args:
21+
num1 (int): 1st number
22+
num1 (int): 2nd number
23+
24+
Returns:
25+
int: GCD(num1, num2)
26+
"""
27+
if num1 < num2:
28+
(num1, num2) = (num2, num1)
29+
30+
if num1 % num2:
31+
return num2
32+
33+
return gcd(num2, num1 % num2)
34+
35+
36+
if __name__ == '__main__':
37+
M = int(input("Enter the first number: "))
38+
N = int(input("Enter the second number: "))
39+
40+
print(f"gcd({M}, {N}) = {gcd(M, N)}")
41+
42+
# OTHER METHODS
43+
144
# Euclid Algorithm...
2-
# def gcd(m, n):
3-
# if m < n: # Assume m >= n
4-
# (m, n) = (n, m)
5-
6-
# if (m%n) == 0:
7-
# return n
45+
# def gcd(num1: int, num2: int)-> int:
46+
# if num1 < num2: # Assume num1 >= num2
47+
# (num1, num2) = (num2, num1)
48+
49+
# if num1 % num2:
50+
# return num2
851
# else:
9-
# diff = m-n
10-
# return gcd(max(n, diff), min(n, diff))
11-
12-
# def gcd(m,n):
13-
# if m < n:
14-
# (m, n)=(n,m)
15-
# while (m%n)!=0:
16-
# diff = m-n
17-
# (m, n) = (max(n, diff), min(n, diff))
18-
19-
# return n
20-
21-
def gcd(m,n):
22-
if m < n:
23-
(m, n) = (n, m)
24-
25-
if m%n == 0:
26-
return n
27-
else:
28-
return gcd(n, m%n)
29-
30-
31-
m = int(input("Enter the first number: "))
32-
n = int(input("Enter the second number: "))
33-
print(f"gcd({m}, {n}) = {gcd(m,n)}")
52+
# diff = num1 - num2
53+
# return gcd(max(num2, diff), min(num2, diff))
54+
55+
# def gcd(num1: int,num2: int)-> int:
56+
# if num1 < num2:
57+
# (num1, num2) = (num2, num1)
58+
59+
# while num1 % num2:
60+
# diff = num1 - num2
61+
# (num1, num2) = (max(num2, diff), min(num2, diff))
62+
63+
# return num2

0 commit comments

Comments
 (0)