Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit fc9c982

Browse files
authored
Merge branch 'master' into sharur7-HCTA
2 parents 36d19dd + 0f4827d commit fc9c982

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1558
-30
lines changed

Basic-Scripts/Simple_calculator.py

+155-29
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,155 @@
1-
operation = input('''
2-
Please type in the math operation you would like to complete:
3-
1 for addition
4-
2 for subtraction
5-
3 for multiplication
6-
4 for division
7-
''')
8-
9-
num = int(input('Enter your first number: '))
10-
num2 = int(input('Enter your second number: '))
11-
12-
if operation == '1':
13-
print('{} + {} = '.format(num, num2))
14-
print(num+num2)
15-
16-
elif operation == '2':
17-
print('{} - {} = '.format(num, num2))
18-
print(num-num2)
19-
20-
elif operation == '3':
21-
print('{} * {} = '.format(num, num2))
22-
print(num*num2)
23-
24-
elif operation == '4':
25-
print('{} / {} = '.format(num, num2))
26-
print(num / num2)
27-
28-
else:
29-
print('Wrong Input! Please Try Again')
1+
operation = input('''
2+
Please type in the math operation you would like to complete:
3+
1 for addition
4+
2 for subtraction
5+
3 for multiplication
6+
4 for division
7+
''')
8+
9+
num = int(input('Enter your first number: '))
10+
num2 = int(input('Enter your second number: '))
11+
12+
if operation == '1':
13+
print('{} + {} = '.format(num, num2))
14+
print(num+num2)
15+
16+
elif operation == '2':
17+
print('{} - {} = '.format(num, num2))
18+
print(num-num2)
19+
20+
elif operation == '3':
21+
print('{} * {} = '.format(num, num2))
22+
print(num*num2)
23+
24+
elif operation == '4':
25+
print('{} / {} = '.format(num, num2))
26+
print(num / num2)
27+
28+
else:
29+
print('Wrong Input! Please Try Again')
30+
=======
31+
32+
33+
"""The previous script becomes very boring for the user as he has to keep on typing just numbers and of wants to interact using
34+
simple english language he is unable to do so. Also the program terminates after one execution and if the user wants to keep on
35+
performing operations it becomes difficult.
36+
I have tried making a more simple and interactive calculator which allows the user to input any statement for performing the tasks. """
37+
38+
39+
40+
41+
def Calculator(num_1,num_2):
42+
#Executing the loop infinite times as we donot know how many times the user will want to run
43+
while(True):
44+
choice= input("Enter what you want to perform: ")
45+
print()
46+
47+
#For Addition user can type any Sentence containing word related to it and will get the output but here we are checking only for the common words
48+
if ("addition" in choice) or ("add" in choice) or ("sum" in choice) or ("plus" in choice):
49+
sum = (num_1) + (num_2)
50+
print("Output....")
51+
print("Adding {} and {} results to {}".format(num_1,num_2,sum))
52+
print()
53+
54+
#For Subtraction user can type any Sentence containing word related to it and will get the output but here we are checking only for the common words
55+
elif ("subtraction" in choice) or ("minus" in choice) or ("difference" in choice) or ("subtract" in choice):
56+
if( num_1 > num_2 ):
57+
diff = (num_1) - (num_2)
58+
print("Output....")
59+
print("Subtracting {} from {} results to {}".format(num_2,num_1,diff))
60+
print()
61+
elif( num_2 > num_1 ):
62+
diff = (num_2) - (num_1)
63+
print("Output....")
64+
print("Subtracting {} from {} results to {}".format(num_1,num_2,diff))
65+
print()
66+
67+
#For Multiplication user can type any Sentence cpntaining word related to it and will get the output but here we are checking only for the common words
68+
elif ("multiplication" in choice) or ("product" in choice) or ("multiply" in choice):
69+
if(num_1==0 or num_2==0):
70+
print("Output....")
71+
print("Multiplying {} and {} results to 0".format(num_1,num_2))
72+
print()
73+
elif(num_1==1):
74+
print("Output....")
75+
print("Multiplying {} and {} results to {}".format(num_1,num_2,num_2))
76+
print()
77+
elif(num_2==1):
78+
print("Output....")
79+
print("Multiplying {} and {} results to {}".format(num_1,num_2,num_1))
80+
print()
81+
else:
82+
mul = (num_1) * (num_2)
83+
print("Output....")
84+
print("Multiplying {} and {} results to {}".format(num_1,num_2,mul))
85+
print()
86+
87+
#For Division user can type any Sentence cpntaining word related to it and will get the output but here we are checking only for the common words
88+
elif("division" in choice) or ("divide" in choice) or ("quotient" in choice):
89+
if( num_1 > num_2 ):
90+
if(num_2==0):
91+
print("Output....")
92+
print("Error: Please try with some other values!")
93+
94+
elif(num_1==0):
95+
print("Output....")
96+
print("Dividing {} from {} results to 0".format(num_1,num_2))
97+
print()
98+
else:
99+
div = (num_1) / (num_2)
100+
print("Output....")
101+
print("Dividing {} from {} results to {}".format(num_1,num_2,div))
102+
print()
103+
elif(num_1==0 and num_2==0):
104+
print("Infinity!")
105+
print()
106+
elif( num_2 > num_1 ):
107+
if(num_1==0):
108+
print("Output....")
109+
print("Error: Please try with some other values!")
110+
print()
111+
112+
elif(num_2==0):
113+
print("Output....")
114+
print("Dividing {} from {} results to 0".format(num_1,num_2))
115+
print()
116+
else:
117+
div = (num_2) / (num_1)
118+
print("Output....")
119+
print("Dividing {} from {} results to {}".format(num_2,num_1,div))
120+
print()
121+
122+
#For exiting the loop user can type any Sentence containing word related to it and it will exit from the loop but here we are checking for the common words
123+
elif ("exit" in choice) or ("stop" in choice) or ("return" in choice):
124+
break
125+
126+
else:
127+
print()
128+
print("Operation not found: Please try again!")
129+
print()
130+
131+
132+
133+
def main():
134+
135+
print()
136+
print(" THIS IS A BASIC USER FRIENDLY CALCULATOR! ")
137+
print()
138+
print("You can type a sentence and interact.")
139+
print()
140+
#inputting two numbers at a time using the split function
141+
num_1,num_2 = input("Enter two numbers: ").split()
142+
num1=float(num_1)
143+
num2=float(num_2)
144+
145+
146+
#printing both the numbers
147+
print("Number 1 is: ",num_1)
148+
print("Number 2 is: ",num_2)
149+
print()
150+
151+
Calculator(num_1,num_2)
152+
153+
154+
if __name__ == "__main__":
155+
main()

Basic-Scripts/balanced_paranthesis.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
openBracketList = ["[", "{", "("]
2+
closeBracketList = ["]", "}", ")"]
3+
4+
5+
def check_parentheses(data: str) -> str:
6+
"""
7+
check_parentheses() : Will take a string as an arguement and each time when an open parentheses is encountered
8+
will push it in the stack, and when closed parenthesis is encountered,
9+
will match it with the top of stack and pop it.
10+
11+
Parameters:
12+
data (str): takes a string.
13+
14+
Returns:
15+
str: Returns a string value whether string passed is balanced or Unbalanced.
16+
"""
17+
stack = []
18+
for index in data:
19+
if index in openBracketList:
20+
stack.append(index)
21+
elif index in closeBracketList:
22+
position = closeBracketList.index(index)
23+
if (len(stack) > 0) and (
24+
openBracketList[position] == stack[len(stack) - 1]
25+
):
26+
stack.pop()
27+
else:
28+
return "Unbalanced"
29+
if len(stack) == 0:
30+
return "Balanced"
31+
else:
32+
return "Unbalanced"
33+
34+
35+
if __name__ == "__main__":
36+
37+
data = input("Enter the string to check:\t")
38+
result = check_parentheses(data)
39+
print(result)
40+

Basic-Scripts/tic-tac-toe-1.py

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
board = [' ' for x in range(10)]
2+
3+
4+
# inserting input to the board
5+
def insertLetter(letter, pos):
6+
board[pos] = letter
7+
8+
9+
# checks if space is free on board
10+
def spaceIsFree(pos):
11+
return board[pos] == ' '
12+
13+
14+
# prints the board interface
15+
def printBoard(board):
16+
print(' | | ')
17+
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
18+
print(' | | ')
19+
print('------------')
20+
print(' | | ')
21+
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
22+
print(' | | ')
23+
print('------------')
24+
print(' | | ')
25+
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
26+
print(' | | ')
27+
28+
29+
# checks if board is full
30+
def isBoardFull(board):
31+
if board.count(' ') > 1:
32+
return False
33+
else:
34+
return True
35+
36+
37+
# checks who's the winner
38+
def IsWinner(b, l):
39+
return ((b[1] == l and b[2] == l and b[3] == l) or
40+
(b[4] == l and b[5] == l and b[6] == l) or
41+
(b[7] == l and b[8] == l and b[9] == l) or
42+
(b[1] == l and b[4] == l and b[7] == l) or
43+
(b[2] == l and b[5] == l and b[8] == l) or
44+
(b[3] == l and b[6] == l and b[9] == l) or
45+
(b[1] == l and b[5] == l and b[9] == l) or
46+
(b[3] == l and b[5] == l and b[7] == l))
47+
48+
49+
# tracks player's move
50+
def playerMove():
51+
run = True
52+
while run:
53+
move = input("please select a position to enter the X between 1 to 9: ")
54+
try:
55+
move = int(move)
56+
if move > 0 and move < 10:
57+
if spaceIsFree(move):
58+
run = False
59+
insertLetter('X', move)
60+
else:
61+
print('Sorry, this space is occupied')
62+
else:
63+
print('please type a number between 1 and 9')
64+
65+
except:
66+
print('Please type a number')
67+
68+
69+
# tracks computer's move
70+
def computerMove():
71+
possibleMoves = [x for x, letter in enumerate(board) if letter == ' ' and x != 0]
72+
move = 0
73+
74+
for let in ['O', 'X']:
75+
for i in possibleMoves:
76+
boardcopy = board[:]
77+
boardcopy[i] = let
78+
if IsWinner(boardcopy, let):
79+
move = i
80+
return move
81+
82+
cornersOpen = []
83+
for i in possibleMoves:
84+
if i in [1, 3, 7, 9]:
85+
cornersOpen.append(i)
86+
87+
if len(cornersOpen) > 0:
88+
move = selectRandom(cornersOpen)
89+
return move
90+
91+
if 5 in possibleMoves:
92+
move = 5
93+
return move
94+
95+
edgesOpen = []
96+
for i in possibleMoves:
97+
if i in [2, 4, 6, 8]:
98+
edgesOpen.append(i)
99+
100+
if len(edgesOpen) > 0:
101+
move = selectRandom(edgesOpen)
102+
return move
103+
104+
105+
# helps computer to choose appropriate move
106+
def selectRandom(li):
107+
import random
108+
ln = len(li)
109+
r = random.randrange(0, ln)
110+
return li[r]
111+
112+
113+
# main function of the game
114+
def main():
115+
print("Welcome to the game!")
116+
printBoard(board)
117+
118+
while not (isBoardFull(board)):
119+
if not (IsWinner(board, 'O')):
120+
playerMove()
121+
printBoard(board)
122+
else:
123+
print("sorry you loose!")
124+
break
125+
126+
if not (IsWinner(board, 'X')):
127+
move = computerMove()
128+
if move == 0:
129+
print(" ")
130+
else:
131+
insertLetter('O', move)
132+
print('computer placed an o on position', move, ':')
133+
printBoard(board)
134+
else:
135+
print("you win!")
136+
break
137+
138+
if isBoardFull(board):
139+
print("Tie game")
140+
141+
142+
# message prompt for the player
143+
while True:
144+
x = input("Do you want to play again? (y/n): ")
145+
if x.lower() == 'y':
146+
board = [' ' for x in range(10)]
147+
print('--------------------')
148+
main()
149+
else:
150+
break

Contribution.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
- Clone the fork [repo](https://github.com/ankitdobhal/Awesome-Python-Scripts)
1616
- git clone https://github.com/ankitdobhal/Awesome-Python-Scripts
17-
- Create new branch
17+
18+
- git clone https://github.com/<Your_Username>/Awesome-Python-Scripts
19+
- Create new branch
1820
- git checkout -b <Your-Branch-Name>
1921

2022
- Add Scripts related to your respective issues.
@@ -24,6 +26,10 @@
2426
- git commit -a -m "<Added your message>"
2527
- Push changes
2628
- git push origin
29+
30+
31+
- Push changes
32+
- git push -u origin <name_of_your_branch>
2733

2834
- Create pull requests
2935
- [Try to Mention the related issue for your PR]

0 commit comments

Comments
 (0)