Skip to content

Commit 896bd97

Browse files
committed
fix typing
1 parent 67ddb7d commit 896bd97

File tree

12 files changed

+77
-22
lines changed
  • Logic Building
    • Find the number closest to n and divisible by m
    • Nth term of AP from First Two Terms
    • Program for Sum of squares of first n natural numbers
    • Program to find sum of first n natural numbers
    • Swap Two Numbers
    • The dice problem

12 files changed

+77
-22
lines changed

Logic Building/Find the number closest to n and divisible by m/1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def closest_number(n:int,m:int) -> int:
99
>>> closest_number(15674.246)
1010
15675
1111
"""
12-
closest = 0
13-
min_diff = float('inf')
12+
closest: int = 0
13+
min_diff: float = float('inf')
1414

1515
for i in range(n-abs(m), n+abs(m)+1):
1616
if i % m == 0:

Logic Building/Find the number closest to n and divisible by m/2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def closest_number(n:int, m:int ) -> int:
1313
"""
1414
# Find the quotient
1515
# q = n // m
16-
q = int (n / m)
16+
q: int = int (n / m)
1717

1818
# First possible closest number
19-
n1 = m * q
19+
n1: int = m * q
2020

2121
# Second possible closest number
2222
if n * m > 0:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Using for Loop"""
2+
3+
4+
def nth_term_of_AP(a1: int, a2: int, n: int) -> int:
5+
"""
6+
>>> nth_term_of_AP(2, 4, 5)
7+
10
8+
>>> nth_term_of_AP(1, 3, 10)
9+
19
10+
>>> nth_term_of_AP(5, 10, 1)
11+
5
12+
"""
13+
nth_term: int = a1
14+
d: int = a2 - a1
15+
for i in range(1, n):
16+
nth_term += d
17+
return nth_term
18+
19+
20+
if __name__ == "__main__":
21+
from doctest import testmod
22+
23+
testmod(verbose=True)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
""" Using the Formula for nth Term """
2+
def nth_term_of_AP(a1: int, a2: int, n: int) -> int:
3+
"""
4+
>>> nth_term_of_AP(2, 4, 5)
5+
10
6+
>>> nth_term_of_AP(1, 3, 10)
7+
19
8+
>>> nth_term_of_AP(5, 10, 1)
9+
5
10+
"""
11+
12+
return a1 + (n - 1) * (a2 - a1)
13+
if __name__ == "__main__":
14+
from doctest import testmod
15+
testmod(verbose=True)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## nth_term_of_AP Method
2+
- Using for Loop
3+
- Using the Formula for nth Term
4+
5+
**AP** stands for Arithmetic Progression

Logic Building/Program for Sum of squares of first n natural numbers/1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Adding One By One"""
22

33

4-
def sum_of_squares(n):
4+
def sum_of_squares(n: int) -> int:
55
"""
66
>>> sum_of_squares(5)
77
55
@@ -13,7 +13,7 @@ def sum_of_squares(n):
1313
return sum([i**2 for i in range(1, n + 1)])
1414

1515

16-
def sum_of_squares_not_overflow(n):
16+
def sum_of_squares_not_overflow(n: int) -> int:
1717
"""
1818
>>> sum_of_squares_not_overflow(5)
1919
55

Logic Building/Program to find sum of first n natural numbers/1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def find_sum(n: int) -> int:
99
>>> find_sum(1000)
1010
500500
1111
"""
12-
sum = 0
12+
sum: int = 0
1313
x = 1
1414
while x <= n:
1515
sum += x

Logic Building/Swap Two Numbers/1.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ def main() -> None:
77
a = 2 b = 3
88
a = 3 b = 2
99
"""
10-
a = 2
11-
b = 3
10+
a: int = 2
11+
b: int = 3
1212
print("a =", a, "b =", b)
1313

14-
temp = a
14+
temp: int = a
1515
a = b
1616
b = temp
1717
print("a =", a, "b =", b)

Logic Building/Swap Two Numbers/2.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
1-
""" Without using Third Variable """
1+
"""Without using Third Variable"""
2+
3+
24
def swap_by_arithmetic() -> None:
35
"""
46
>>> swap_by_arithmetic()
57
a = 2 b = 3
68
a = 3 b = 2
79
"""
8-
a = 2
9-
b = 3
10+
a: int = 2
11+
b: int = 3
1012
print("a =", a, "b =", b)
1113

1214
a = a + b
1315
b = a - b
1416
a = a - b
1517
print("a =", a, "b =", b)
1618

19+
1720
def swap_by_bitwise() -> None:
1821
"""
1922
>>> swap_by_bitwise()
2023
a = 2 b = 3
2124
a = 3 b = 2
2225
"""
23-
a = 2
24-
b = 3
26+
a: int = 2
27+
b: int = 3
2528
print("a =", a, "b =", b)
2629

2730
a = a ^ b
2831
b = a ^ b
2932
a = a ^ b
3033
print("a =", a, "b =", b)
34+
35+
3136
if __name__ == "__main__":
3237
from doctest import testmod
33-
testmod(verbose=True)
38+
39+
testmod(verbose=True)

Logic Building/Swap Two Numbers/3.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
""" Built-in swap """
1+
"""Built-in swap"""
2+
3+
24
def main() -> None:
35
"""
46
>>> main()
57
a = 2 b = 3
68
a = 3 b = 2
79
"""
8-
a = 2
9-
b = 3
10+
a: int = 2
11+
b: int = 3
1012
print("a =", a, "b =", b)
1113

1214
a, b = b, a
1315
print("a =", a, "b =", b)
16+
17+
1418
if __name__ == "__main__":
1519
from doctest import testmod
1620

17-
testmod(verbose=True)
21+
testmod(verbose=True)

0 commit comments

Comments
 (0)