Skip to content

Commit d3caf5d

Browse files
committed
Refactor and update various Python scripts
- Removed obsolete configuration files (.vscode/launch.json, .vscode/settings.json). - Deleted unused binary files (class.dat, studrec.dat). - Added new script for NumPy array exponentiation with detailed docstring and examples. - Enhanced Random Number Guessing Game with structured functions and improved documentation. - Introduced Snake-Water-Gun game with clear rules and structured gameplay. - Updated Tic-Tac-Toe game with type hints and doctests for better clarity and testing. - Removed old calculator script and replaced it with a new simple calculator module. - Implemented a password guessing simulation script with random character choices. - Improved sum of digits calculation with input validation and structured main function. - Cleaned up and removed unnecessary files and scripts to streamline the project.
1 parent bd6073e commit d3caf5d

21 files changed

+680
-522
lines changed

.vscode/c_cpp_properties.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

.vscode/launch.json

Lines changed: 0 additions & 31 deletions
This file was deleted.

.vscode/settings.json

Lines changed: 0 additions & 66 deletions
This file was deleted.
-471 Bytes
Binary file not shown.
-257 Bytes
Binary file not shown.

NumPy Array Exponentiation.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
NumPy Array Exponentiation
3+
4+
Check if two arrays have the same shape and compute element-wise powers
5+
with and without np.power.
6+
7+
Example usage:
8+
>>> import numpy as np
9+
>>> x = np.array([1, 2])
10+
>>> y = np.array([3, 4])
11+
>>> get_array(x, y) # doctest: +ELLIPSIS
12+
Array of powers without using np.power: [ 1 16]
13+
Array of powers using np.power: [ 1 16]
14+
"""
15+
16+
import numpy as np
17+
18+
19+
def get_array(x: np.ndarray, y: np.ndarray) -> None:
20+
"""
21+
Compute element-wise power of two NumPy arrays if their shapes match.
22+
23+
Parameters
24+
----------
25+
x : np.ndarray
26+
Base array.
27+
y : np.ndarray
28+
Exponent array.
29+
30+
Returns
31+
-------
32+
None
33+
Prints the element-wise powers using both operator ** and np.power.
34+
35+
Example:
36+
>>> import numpy as np
37+
>>> a = np.array([[1, 2], [3, 4]])
38+
>>> b = np.array([[2, 2], [2, 2]])
39+
>>> get_array(a, b) # doctest: +ELLIPSIS
40+
Array of powers without using np.power: [[ 1 4]
41+
[ 9 16]]
42+
Array of powers using np.power: [[ 1 4]
43+
[ 9 16]]
44+
"""
45+
if x.shape == y.shape:
46+
np_pow_array = x**y
47+
print("Array of powers without using np.power: ", np_pow_array)
48+
print("Array of powers using np.power: ", np.power(x, y))
49+
else:
50+
print("Error: Shape of the given arrays is not equal.")
51+
52+
53+
if __name__ == "__main__":
54+
import doctest
55+
56+
doctest.testmod()
57+
58+
# 0D array
59+
np_arr1 = np.array(3)
60+
np_arr2 = np.array(4)
61+
# 1D array
62+
np_arr3 = np.array([1, 2])
63+
np_arr4 = np.array([3, 4])
64+
# 2D array
65+
np_arr5 = np.array([[1, 2], [3, 4]])
66+
np_arr6 = np.array([[5, 6], [7, 8]])
67+
68+
get_array(np_arr1, np_arr2)
69+
print()
70+
get_array(np_arr3, np_arr4)
71+
print()
72+
get_array(np_arr5, np_arr6)

RandomNumberGame.py

Lines changed: 105 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,117 @@
11
"""
2-
hey everyone it is a basic game code using random . in this game computer will randomly chose an number from 1 to 100 and players will have
3-
to guess that which number it is and game will tell him on every guss whether his/her guess is smaller or bigger than the chosen number. it is
4-
a multi player game so it can be played with many players there is no such limitations of user till the size of list. if any one wants to modify
5-
this game he/she is most welcomed.
6-
Thank you
2+
Random Number Guessing Game
3+
---------------------------
4+
This is a simple multiplayer game where each player tries to guess a number
5+
chosen randomly by the computer between 1 and 100. After each guess, the game
6+
provides feedback whether the guess is higher or lower than the target number.
7+
The winner is the player who guesses the number in the fewest attempts.
8+
9+
Example:
10+
>>> import builtins, random
11+
>>> random.seed(0)
12+
>>> inputs = iter(["1", "Alice", "50", "49"])
13+
>>> builtins.input = lambda prompt="": next(inputs)
14+
>>> from game import play_game
15+
>>> players, scores, winners = play_game()
16+
>>> players
17+
['Alice']
18+
>>> scores # doctest: +ELLIPSIS
19+
[2]
20+
>>> winners
21+
['Alice']
722
"""
823

9-
import os
1024
import random
25+
from typing import List, Tuple
26+
27+
28+
def get_players(n: int) -> List[str]:
29+
"""
30+
Prompt to enter `n` player names.
1131
12-
players = []
13-
score = []
32+
Args:
33+
n (int): number of players
1434
15-
print(
16-
"\n\tRandom Number Game\n\nHello Everyone ! it is just a game of chance in which you have to guess a number"
17-
" from 0 to 100 and computer will tell whether your guess is smaller or bigger than the acctual number chossen by the computer . "
18-
"the person with less attempts in guessing the number will be winner ."
19-
)
20-
x = input()
21-
os.system("cls")
35+
Returns:
36+
List[str]: list of player names
2237
23-
n = int(input("Enter number of players : "))
24-
print()
38+
Example:
39+
>>> import builtins
40+
>>> inputs = iter(["Alice", "Bob"])
41+
>>> builtins.input = lambda prompt="": next(inputs)
42+
>>> get_players(2)
43+
['Alice', 'Bob']
44+
"""
45+
return [input("Enter name of player: ") for _ in range(n)]
2546

26-
for i in range(0, n):
27-
name = input("Enter name of player : ")
28-
players.append(name)
2947

30-
os.system("cls")
48+
def play_turn(player: str) -> int:
49+
"""
50+
Let a player try to guess a random number.
3151
32-
for i in range(0, n):
33-
orignum = random.randint(1, 100)
34-
print(players[i], "your turn :", end="\n\n")
35-
count = 0
52+
Args:
53+
player (str): player name
54+
55+
Returns:
56+
int: number of attempts taken
57+
58+
Example:
59+
>>> import builtins, random
60+
>>> random.seed(1)
61+
>>> inputs = iter(["30", "15", "9"])
62+
>>> builtins.input = lambda prompt="": next(inputs)
63+
>>> play_turn("Alice") # doctest: +ELLIPSIS
64+
3
65+
"""
66+
target = random.randint(1, 100)
67+
print(f"\n{player}, it's your turn!")
68+
attempts = 0
3669
while True:
37-
ch = int(input("Please enter your guess : "))
38-
if ch > orignum:
39-
print("no! number is smaller...")
40-
count += 1
41-
elif ch == orignum:
42-
print("\n\n\tcongrats you won")
43-
break
70+
guess = int(input("Please enter your guess: "))
71+
attempts += 1
72+
if guess > target:
73+
print("Too high, try smaller...")
74+
elif guess < target:
75+
print("Too low, try bigger...")
4476
else:
45-
print("nope ! number is large dude...")
46-
count += 1
47-
print(" you have taken", count + 1, "attempts")
48-
x = input()
49-
score.append(count + 1)
50-
os.system("cls")
51-
print("players :\n")
52-
for i in range(0, n):
53-
print(players[i], "-", score[i])
54-
55-
print("\n\nwinner is :\n")
56-
for i in range(0, n):
57-
if score[i] == min(score):
58-
print(players[i])
59-
x = input()
77+
print("Congratulations! You guessed it!")
78+
return attempts
79+
80+
81+
def play_game() -> Tuple[List[str], List[int], List[str]]:
82+
"""
83+
Run the multiplayer game.
84+
85+
Returns:
86+
Tuple[List[str], List[int], List[str]]: (players, scores, winners)
87+
88+
Example:
89+
>>> import builtins, random
90+
>>> random.seed(2)
91+
>>> inputs = iter(["1", "Eve", "30", "13"])
92+
>>> builtins.input = lambda prompt="": next(inputs)
93+
>>> players, scores, winners = play_game()
94+
>>> players
95+
['Eve']
96+
>>> scores # doctest: +ELLIPSIS
97+
[2]
98+
>>> winners
99+
['Eve']
100+
"""
101+
n = int(input("Enter number of players: "))
102+
players = get_players(n)
103+
scores = [play_turn(p) for p in players]
104+
min_score = min(scores)
105+
winners = [p for p, s in zip(players, scores) if s == min_score]
106+
print("\nResults:")
107+
for p, s in zip(players, scores):
108+
print(f"{p}: {s} attempts")
109+
print("\nWinner(s):", ", ".join(winners))
110+
return players, scores, winners
111+
112+
113+
if __name__ == "__main__":
114+
import doctest
115+
116+
doctest.testmod()
117+
play_game()

0 commit comments

Comments
 (0)