Skip to content

Commit 5dcf01e

Browse files
Merge pull request steam-bell-92#864 from nishtha-agarwal-211/feat/expand-tests-788
Feat/expand tests
2 parents e9802b3 + 77cc26c commit 5dcf01e

13 files changed

Lines changed: 817 additions & 408 deletions

File tree

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,64 @@
1-
print("=" * 50)
2-
print("🔢 ARMSTRONG NUMBER CHECKER 🔢")
3-
print("=" * 50)
4-
print("An Armstrong number equals the sum of its digits raised to the power of number of digits.")
5-
print("Example: 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153\n")
1+
def is_armstrong_number(n: int) -> bool:
2+
if n < 0:
3+
return False
4+
num_str = str(n)
5+
num_digits = len(num_str)
6+
total = sum(int(char) ** num_digits for char in num_str)
7+
return total == n
68

7-
while True:
9+
def main() -> None:
810
print("=" * 50)
9-
try:
10-
user_input = input("➡️ Enter a positive number to check: ").strip()
11-
if not user_input:
12-
print("❌ Error: Input cannot be empty.")
13-
continue
14-
15-
num = int(user_input)
16-
if num < 0:
17-
print("❌ Please enter a positive number!")
18-
continue
11+
print("🔢 ARMSTRONG NUMBER CHECKER 🔢")
12+
print("=" * 50)
13+
print("An Armstrong number equals the sum of its digits raised to the power of number of digits.")
14+
print("Example: 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153\n")
15+
16+
while True:
17+
print("=" * 50)
18+
try:
19+
user_input = input("➡️ Enter a positive number to check: ").strip()
20+
if not user_input:
21+
print("❌ Error: Input cannot be empty.")
22+
continue
23+
24+
num = int(user_input)
25+
if num < 0:
26+
print("❌ Please enter a positive number!")
27+
continue
28+
29+
num_str = str(num)
30+
num_digits = len(num_str)
31+
total = sum(int(char) ** num_digits for char in num_str)
32+
33+
print(f"\n📊 Number: {num}")
34+
print(f"📐 Number of digits: {num_digits}")
35+
print(f"\n🔍 Calculation:")
1936

20-
num_str = str(num)
21-
num_digits = len(num_str)
22-
total = 0
23-
24-
# Calculate sum of digits raised to power of num_digits
25-
for char in num_str:
26-
total += int(char) ** num_digits
37+
calculation_parts = [f"{char}^{num_digits}" for char in num_str]
38+
print(f" {' + '.join(calculation_parts)}")
2739

28-
print(f"\n📊 Number: {num}")
29-
print(f"📐 Number of digits: {num_digits}")
30-
print(f"\n🔍 Calculation:")
31-
32-
calculation_parts = [f"{char}^{num_digits}" for char in num_str]
33-
print(f" {' + '.join(calculation_parts)}")
34-
35-
values = [f"{int(char)**num_digits}" for char in num_str]
36-
print(f" = {' + '.join(values)}")
37-
print(f" = {total}")
38-
39-
if total == num:
40-
print(f"\n{num} is an Armstrong Number! 🎉")
41-
else:
42-
print(f"\n{num} is NOT an Armstrong Number.")
40+
values = [f"{int(char)**num_digits}" for char in num_str]
41+
print(f" = {' + '.join(values)}")
42+
print(f" = {total}")
4343

44-
print("\n💡 Some Armstrong Numbers:")
45-
print(" 1-digit: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9")
46-
print(" 3-digit: 153, 370, 371, 407")
47-
print(" 4-digit: 1634, 8208, 9474")
44+
if is_armstrong_number(num):
45+
print(f"\n{num} is an Armstrong Number! 🎉")
46+
else:
47+
print(f"\n{num} is NOT an Armstrong Number.")
48+
49+
print("\n💡 Some Armstrong Numbers:")
50+
print(" 1-digit: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9")
51+
print(" 3-digit: 153, 370, 371, 407")
52+
print(" 4-digit: 1634, 8208, 9474")
53+
54+
except ValueError:
55+
print("⚠️ Oops! That doesn't look like a valid integer. Please try again.\n")
56+
continue
4857

49-
except ValueError:
50-
print("⚠️ Oops! That doesn't look like a valid integer. Please try again.\n")
51-
continue
58+
again = input("\n🔄 Do you want to check another number? (y/n): ").strip().lower()
59+
if again != 'y':
60+
print("\n👋 Thanks for using Armstrong Number Checker! Goodbye!\n")
61+
break
5262

53-
again = input("\n🔄 Do you want to check another number? (y/n): ").strip().lower()
54-
if again != 'y':
55-
print("\n👋 Thanks for using Armstrong Number Checker! Goodbye!\n")
56-
break
63+
if __name__ == "__main__":
64+
main()

math/Bubble-Sort/Bubble-Sort.py

Lines changed: 75 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,80 @@
1-
print("=" * 50)
2-
print("🔄 BUBBLE SORT INTERACTIVE TOOL 🔄")
3-
print("=" * 50)
4-
print("Sort a list of numbers in Ascending or Descending order.\n")
1+
def bubble_sort(arr: list[int], reverse: bool = False) -> list[int]:
2+
"""Sorts a list of integers using the Bubble Sort algorithm.
3+
4+
Args:
5+
arr: The list of integers to sort.
6+
reverse: If True, sorts in descending order. Otherwise, ascending.
7+
8+
Returns:
9+
A new sorted list.
10+
"""
11+
sorted_arr = arr.copy()
12+
n = len(sorted_arr)
13+
14+
for i in range(n):
15+
swapped = False
16+
# Last i elements are already in place
17+
for j in range(0, n - i - 1):
18+
# Ascending Order
19+
if not reverse and sorted_arr[j] > sorted_arr[j + 1]:
20+
sorted_arr[j], sorted_arr[j + 1] = sorted_arr[j + 1], sorted_arr[j]
21+
swapped = True
22+
# Descending Order
23+
elif reverse and sorted_arr[j] < sorted_arr[j + 1]:
24+
sorted_arr[j], sorted_arr[j + 1] = sorted_arr[j + 1], sorted_arr[j]
25+
swapped = True
26+
27+
# If no swaps happened, list is already sorted
28+
if not swapped:
29+
break
30+
31+
return sorted_arr
532

6-
while True:
33+
def main() -> None:
734
print("=" * 50)
8-
try:
9-
user_input = input("➡️ Enter numbers to sort separated by spaces (e.g., 64 34 25): ").strip()
10-
if not user_input:
11-
print("❌ Error: Input cannot be empty!")
12-
continue
35+
print("🔄 BUBBLE SORT INTERACTIVE TOOL 🔄")
36+
print("=" * 50)
37+
print("Sort a list of numbers in Ascending or Descending order.\n")
1338

14-
# Convert string input into list of integers
15-
arr = [int(x) for x in user_input.split()]
16-
17-
print("\nChoose sorting order:")
18-
print("1️⃣ Ascending")
19-
print("2️⃣ Descending")
20-
21-
order_choice = input("🎯 Enter your choice (1 or 2): ").strip()
22-
23-
if order_choice not in ("1", "2"):
24-
print("❌ Invalid sorting choice! Please select 1 or 2.")
25-
continue
39+
while True:
40+
print("=" * 50)
41+
try:
42+
user_input = input("➡️ Enter numbers to sort separated by spaces (e.g., 64 34 25): ").strip()
43+
if not user_input:
44+
print("❌ Error: Input cannot be empty!")
45+
continue
2646

27-
reverse = (order_choice == "2")
28-
29-
# Inline Bubble Sort Algorithm
30-
sorted_arr = arr.copy()
31-
n = len(sorted_arr)
32-
33-
for i in range(n):
34-
swapped = False
35-
# Last i elements are already in place
36-
for j in range(0, n - i - 1):
37-
# Ascending Order
38-
if not reverse and sorted_arr[j] > sorted_arr[j + 1]:
39-
sorted_arr[j], sorted_arr[j + 1] = sorted_arr[j + 1], sorted_arr[j]
40-
swapped = True
41-
# Descending Order
42-
elif reverse and sorted_arr[j] < sorted_arr[j + 1]:
43-
sorted_arr[j], sorted_arr[j + 1] = sorted_arr[j + 1], sorted_arr[j]
44-
swapped = True
47+
# Convert string input into list of integers
48+
arr = [int(x) for x in user_input.split()]
49+
50+
print("\nChoose sorting order:")
51+
print("1️⃣ Ascending")
52+
print("2️⃣ Descending")
53+
54+
order_choice = input("🎯 Enter your choice (1 or 2): ").strip()
55+
56+
if order_choice not in ("1", "2"):
57+
print("❌ Invalid sorting choice! Please select 1 or 2.")
58+
continue
59+
60+
reverse = (order_choice == "2")
4561

46-
# If no swaps happened, list is already sorted
47-
if not swapped:
48-
break
49-
50-
print(f"\n📊 Original list: {arr}")
51-
if reverse:
52-
print(f"✅ Sorted list (Descending): {sorted_arr}")
53-
else:
54-
print(f"✅ Sorted list (Ascending): {sorted_arr}")
55-
56-
except ValueError:
57-
print("❌ Error: Please enter valid integers only.")
58-
continue
59-
60-
again = input("\n🔄 Do you want to sort another list? (y/n): ").strip().lower()
61-
if again != 'y':
62-
print("\n👋 Thanks for using Bubble Sort Tool! Goodbye!\n")
63-
break
62+
sorted_arr = bubble_sort(arr, reverse)
63+
64+
print(f"\n📊 Original list: {arr}")
65+
if reverse:
66+
print(f"✅ Sorted list (Descending): {sorted_arr}")
67+
else:
68+
print(f"✅ Sorted list (Ascending): {sorted_arr}")
69+
70+
except ValueError:
71+
print("❌ Error: Please enter valid integers only.")
72+
continue
73+
74+
again = input("\n🔄 Do you want to sort another list? (y/n): ").strip().lower()
75+
if again != 'y':
76+
print("\n👋 Thanks for using Bubble Sort Tool! Goodbye!\n")
77+
break
78+
79+
if __name__ == "__main__":
80+
main()

math/Happy-Number/Happy-Number.py

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,19 @@
11
import tkinter as tk
22

3-
print("=" * 50)
4-
print("🔢 HAPPY NUMBER CHECKER & VISUALIZER 🔢")
5-
print("=" * 50)
6-
print("A happy number eventually reaches 1 when replaced by the sum of square of its digits.\n")
7-
8-
while True:
9-
print("=" * 50)
10-
try:
11-
user_input = input("➡️ Enter a number: ").strip()
12-
if not user_input:
13-
print("❌ Error: Input cannot be empty.")
14-
continue
15-
N = int(user_input)
16-
if N <= 0:
17-
print("❌ Please enter a positive number!")
18-
continue
19-
except ValueError:
20-
print("❌ Error: Please enter a valid positive integer.")
21-
continue
22-
3+
def is_happy_number(n: int) -> tuple[bool, list[int]]:
234
seen = set()
245
sequence = []
25-
num = N
6+
num = n
267

278
while num != 1 and num not in seen:
289
seen.add(num)
2910
sequence.append(num)
3011
num = sum(int(digit) ** 2 for digit in str(num))
3112

3213
sequence.append(num)
33-
is_happy = (num == 1)
34-
35-
if is_happy:
36-
print(f"\n{N} is a HAPPY number!")
37-
else:
38-
print(f"\n{N} is NOT a happy number!")
39-
40-
print("➡️ Sequence:", " → ".join(map(str, sequence)))
41-
print("\n🖥️ Opening Visualizer window... Close the window to continue.")
14+
return num == 1, sequence
4215

16+
def run_visualizer(n: int, is_happy: bool, sequence: list[int]) -> None:
4317
# ---------------- TKINTER VISUALIZER ---------------- #
4418
root = tk.Tk()
4519
root.title("Happy Number Visualizer")
@@ -104,7 +78,7 @@ def mouse_scroll(event):
10478
canvas.create_line(x + 45, y, next_x - 45, y, arrow=tk.LAST, width=3, fill="#444")
10579

10680
# Result text
107-
result = f"{N} is a HAPPY Number 🎉" if is_happy else f"{N} is NOT a Happy Number ❌"
81+
result = f"{n} is a HAPPY Number 🎉" if is_happy else f"{n} is NOT a Happy Number ❌"
10882
canvas.create_text(
10983
max(500, start_x + len(sequence) * spacing // 2),
11084
420,
@@ -118,7 +92,43 @@ def mouse_scroll(event):
11892

11993
root.mainloop()
12094

121-
again = input("\n🔄 Do you want to check another number? (y/n): ").strip().lower()
122-
if again != 'y':
123-
print("\n👋 Thanks for using Happy Number Checker! Goodbye!\n")
124-
break
95+
def main() -> None:
96+
print("=" * 50)
97+
print("🔢 HAPPY NUMBER CHECKER & VISUALIZER 🔢")
98+
print("=" * 50)
99+
print("A happy number eventually reaches 1 when replaced by the sum of square of its digits.\n")
100+
101+
while True:
102+
print("=" * 50)
103+
try:
104+
user_input = input("➡️ Enter a number: ").strip()
105+
if not user_input:
106+
print("❌ Error: Input cannot be empty.")
107+
continue
108+
n = int(user_input)
109+
if n <= 0:
110+
print("❌ Please enter a positive number!")
111+
continue
112+
except ValueError:
113+
print("❌ Error: Please enter a valid positive integer.")
114+
continue
115+
116+
is_happy, sequence = is_happy_number(n)
117+
118+
if is_happy:
119+
print(f"\n{n} is a HAPPY number!")
120+
else:
121+
print(f"\n{n} is NOT a happy number!")
122+
123+
print("➡️ Sequence:", " → ".join(map(str, sequence)))
124+
print("\n🖥️ Opening Visualizer window... Close the window to continue.")
125+
126+
run_visualizer(n, is_happy, sequence)
127+
128+
again = input("\n🔄 Do you want to check another number? (y/n): ").strip().lower()
129+
if again != 'y':
130+
print("\n👋 Thanks for using Happy Number Checker! Goodbye!\n")
131+
break
132+
133+
if __name__ == "__main__":
134+
main()

0 commit comments

Comments
 (0)