Skip to content

Commit e8fd3ce

Browse files
committed
Refactor code for improved performance
1 parent 7a6159f commit e8fd3ce

18 files changed

+337
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: main","url":"/Users/pulkitmathur/Documents/python/main.py","tests":[{"id":1708059957916,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"/Users/pulkitmathur/Documents/python/main.py","group":"local","local":true}

.vscode/c_cpp_properties.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "macos-clang-arm64",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"compilerPath": "/usr/bin/clang",
9+
"cStandard": "${default}",
10+
"cppStandard": "${default}",
11+
"intelliSenseMode": "macos-clang-arm64",
12+
"compilerArgs": [
13+
""
14+
]
15+
}
16+
],
17+
"version": 4
18+
}

.vscode/launch.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "C/C++ Runner: Debug Session",
6+
"type": "lldb",
7+
"request": "launch",
8+
"args": [],
9+
"cwd": "/Users/pulkitmathur/Documents/Python Programming",
10+
"program": "/Users/pulkitmathur/Documents/Python Programming/build/Debug/outDebug"
11+
}
12+
]
13+
}

.vscode/settings.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"C_Cpp_Runner.cCompilerPath": "clang",
3+
"C_Cpp_Runner.cppCompilerPath": "clang++",
4+
"C_Cpp_Runner.debuggerPath": "lldb",
5+
"C_Cpp_Runner.cStandard": "",
6+
"C_Cpp_Runner.cppStandard": "",
7+
"C_Cpp_Runner.msvcBatchPath": "",
8+
"C_Cpp_Runner.useMsvc": false,
9+
"C_Cpp_Runner.warnings": [
10+
"-Wall",
11+
"-Wextra",
12+
"-Wpedantic",
13+
"-Wshadow",
14+
"-Wformat=2",
15+
"-Wcast-align",
16+
"-Wconversion",
17+
"-Wsign-conversion",
18+
"-Wnull-dereference"
19+
],
20+
"C_Cpp_Runner.msvcWarnings": [
21+
"/W4",
22+
"/permissive-",
23+
"/w14242",
24+
"/w14287",
25+
"/w14296",
26+
"/w14311",
27+
"/w14826",
28+
"/w44062",
29+
"/w44242",
30+
"/w14905",
31+
"/w14906",
32+
"/w14263",
33+
"/w44265",
34+
"/w14928"
35+
],
36+
"C_Cpp_Runner.enableWarnings": true,
37+
"C_Cpp_Runner.warningsAsError": false,
38+
"C_Cpp_Runner.compilerArgs": [],
39+
"C_Cpp_Runner.linkerArgs": [],
40+
"C_Cpp_Runner.includePaths": [],
41+
"C_Cpp_Runner.includeSearch": [
42+
"*",
43+
"**/*"
44+
],
45+
"C_Cpp_Runner.excludeSearch": [
46+
"**/build",
47+
"**/build/**",
48+
"**/.*",
49+
"**/.*/**",
50+
"**/.vscode",
51+
"**/.vscode/**"
52+
],
53+
"C_Cpp_Runner.useAddressSanitizer": false,
54+
"C_Cpp_Runner.useUndefinedSanitizer": false,
55+
"C_Cpp_Runner.useLeakSanitizer": false,
56+
"C_Cpp_Runner.showCompilationTime": false,
57+
"C_Cpp_Runner.useLinkTimeOptimization": false,
58+
"C_Cpp_Runner.msvcSecureNoWarnings": false
59+
}

binary_divisibility.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def check_binary_divisibility(bin_numbers):
2+
divisible_by_5 = []
3+
for num in bin_numbers.split(','):
4+
if int(num, 2) % 5 == 0:
5+
divisible_by_5.append(num)
6+
return ','.join(divisible_by_5)
7+
8+
if __name__ == "__main__":
9+
input_numbers = input("Enter comma separated 4 digit binary numbers: ")
10+
result = check_binary_divisibility(input_numbers)
11+
print(result)

calculate_distance.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import math
2+
3+
def calculate_distance(movements):
4+
position = {'x': 0, 'y': 0}
5+
6+
for move in movements:
7+
direction, steps = move.split()
8+
steps = int(steps)
9+
10+
if direction.upper() == "UP":
11+
position['y'] += steps
12+
elif direction.upper() == "DOWN":
13+
position['y'] -= steps
14+
elif direction.upper() == "LEFT":
15+
position['x'] -= steps
16+
elif direction.upper() == "RIGHT":
17+
position['x'] += steps
18+
19+
distance = round(math.sqrt(position['x']**2 + position['y']**2))
20+
return distance
21+
22+
if __name__ == "__main__":
23+
movements = []
24+
while True:
25+
movement = input("Enter movement (or 'STOP' to end input): ")
26+
if movement.upper() == 'STOP':
27+
break
28+
movements.append(movement)
29+
print(calculate_distance(movements))

calculate_electricity_bill.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def calculate_electricity_bill(units):
2+
if units <= 50:
3+
bill = units * 0.50
4+
elif units <= 150:
5+
bill = (50 * 0.50) + ((units - 50) * 0.75)
6+
elif units <= 250:
7+
bill = (50 * 0.50) + (100 * 0.75) + ((units - 150) * 1.20)
8+
else:
9+
bill = (50 * 0.50) + (100 * 0.75) + (100 * 1.20) + ((units - 250) * 1.50)
10+
11+
surcharge = bill * 0.20
12+
total_bill = bill + surcharge
13+
14+
return total_bill
15+
16+
if __name__ == "__main__":
17+
units = float(input("Enter the number of units consumed: "))
18+
total = calculate_electricity_bill(units)
19+
print(f"The total electricity bill is: Rs. {total:.2f}")

calculate_gross_salary.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def calculate_gross_salary(basic_salary):
2+
if basic_salary <= 10000:
3+
hra = basic_salary * 0.2
4+
da = basic_salary * 0.8
5+
elif basic_salary <= 20000:
6+
hra = basic_salary * 0.25
7+
da = basic_salary * 0.9
8+
else:
9+
hra = basic_salary * 0.3
10+
da = basic_salary * 0.95
11+
12+
net_salary = basic_salary + hra + da
13+
ppf_deduction = net_salary * 0.1
14+
gross_salary = net_salary - ppf_deduction
15+
16+
print(f"Basic Salary : {basic_salary:.2f}\nDA : {da:.2f}\nHRA : {hra:.2f}")
17+
print("-----------------------------------------")
18+
print(f"Net salary : {net_salary:.2f}\nPPF : -{ppf_deduction:.2f}")
19+
print("-----------------------------------------")
20+
print(f"Gross Salary : {gross_salary:.2f}")
21+
22+
if __name__ == "__main__":
23+
basic_salary = float(input("Basic salary: "))
24+
calculate_gross_salary(basic_salary)

character_checker.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def check_character_type(char):
2+
if char.isalpha():
3+
print(f"The character '{char}' is an alphabet.")
4+
elif char.isdigit():
5+
print(f"The character '{char}' is a digit.")
6+
else:
7+
print(f"The character '{char}' is a special symbol.")
8+
9+
if __name__ == "__main__":
10+
user_input = input("Enter a single character: ")
11+
if len(user_input) == 1:
12+
check_character_type(user_input)
13+
else:
14+
print("Please enter a single character.")

check_number_range.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def check_number_range(number):
2+
if number <= 20:
3+
print(f"{number} is less than or equal to 20.")
4+
elif number <= 40:
5+
print(f"{number} is less than or equal to 40.")
6+
elif number <= 60:
7+
print(f"{number} is less than or equal to 60.")
8+
else:
9+
print(f"{number} is greater than 60.")
10+
11+
if __name__ == "__main__":
12+
num = int(input("Enter a number: "))
13+
check_number_range(num)

check_voting_eligibility.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def check_voting_eligibility(age):
2+
return age >= 18
3+
4+
def main():
5+
age = int(input("Enter your age: "))
6+
if check_voting_eligibility(age):
7+
print("You have the right to vote.")
8+
else:
9+
print("You do not have the right to vote.")
10+
11+
if __name__ == "__main__":
12+
main()

determine_triangle_type.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def determine_triangle_type(side1, side2, side3):
2+
if side1 == side2 == side3:
3+
print("Equilateral triangle")
4+
elif side1 == side2 or side2 == side3 or side1 == side3:
5+
print("Isosceles triangle")
6+
else:
7+
print("Scalene triangle")
8+
9+
if __name__ == "__main__":
10+
sides = input("Enter the lengths of the three sides of the triangle, separated by spaces: ").split()
11+
sides = [int(side) for side in sides]
12+
determine_triangle_type(*sides)

is_leap_year.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def is_leap_year(year):
2+
"""
3+
This function checks if a given year is a leap year.
4+
5+
A leap year is exactly divisible by 4 except for century years (years ending with 00).
6+
The century year is a leap year only if it is perfectly divisible by 400.
7+
"""
8+
if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):
9+
return True
10+
else:
11+
return False
12+
13+
if __name__ == "__main__":
14+
year = int(input("Enter a year: "))
15+
if is_leap_year(year):
16+
print(f"{year} is a leap year.")
17+
else:
18+
print(f"{year} is not a leap year.")

largest_number.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def find_largest_of_four(num1, num2, num3, num4):
2+
if num1 >= num2 and num1 >= num3 and num1 >= num4:
3+
return num1
4+
elif num2 >= num1 and num2 >= num3 and num2 >= num4:
5+
return num2
6+
elif num3 >= num1 and num3 >= num2 and num3 >= num4:
7+
return num3
8+
else:
9+
return num4
10+
11+
if __name__ == "__main__":
12+
numbers = input("Enter four numbers separated by spaces: ").split()
13+
numbers = [int(num) for num in numbers]
14+
largest = find_largest_of_four(*numbers)
15+
print(f"The largest number is: {largest}")

main.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Get input from the user
2+
user_input = input("Enter integers separated by spaces: ")
3+
4+
# Split the input string into individual string integers
5+
input_strings = user_input.split()
6+
7+
# Convert string integers to integers and print each one
8+
for string in input_strings:
9+
try:
10+
integer = int(string)
11+
print(integer)
12+
except ValueError:
13+
print(f"'{string}' is not an integer.")

quadratic_equation.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import math
2+
3+
def find_roots(a, b, c):
4+
# Calculate the discriminant
5+
discriminant = b**2 - 4*a*c
6+
7+
# Check if the discriminant is positive, negative, or zero
8+
if discriminant > 0:
9+
# Two distinct real roots
10+
root1 = (-b + math.sqrt(discriminant)) / (2*a)
11+
root2 = (-b - math.sqrt(discriminant)) / (2*a)
12+
return (root1, root2)
13+
elif discriminant == 0:
14+
# One real root
15+
root = -b / (2*a)
16+
return (root,)
17+
else:
18+
# Complex roots
19+
real_part = -b / (2*a)
20+
imaginary_part = math.sqrt(-discriminant) / (2*a)
21+
return (real_part + imaginary_part * 1j, real_part - imaginary_part * 1j)
22+
23+
if __name__ == "__main__":
24+
print("Enter the coefficients of the quadratic equation (ax^2 + bx + c):")
25+
a = float(input("Enter coefficient a: "))
26+
b = float(input("Enter coefficient b: "))
27+
c = float(input("Enter coefficient c: "))
28+
29+
roots = find_roots(a, b, c)
30+
31+
if len(roots) == 1:
32+
print(f"The quadratic equation has one real root: {roots[0]}")
33+
elif isinstance(roots[0], complex):
34+
print(f"The quadratic equation has two complex roots: {roots[0]} and {roots[1]}")
35+
else:
36+
print(f"The quadratic equation has two real roots: {roots[0]} and {roots[1]}")

rps.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def determine_winner(player1, player2):
2+
if player1 == player2:
3+
return "It's a tie."
4+
elif (player1 == "rock" and player2 == "scissors") or \
5+
(player1 == "scissors" and player2 == "paper") or \
6+
(player1 == "paper" and player2 == "rock"):
7+
return "Player 1 wins."
8+
else:
9+
return "Player 2 wins."
10+
11+
if __name__ == "__main__":
12+
player1_input = input("Player 1? ").lower()
13+
player2_input = input("Player 2? ").lower()
14+
result = determine_winner(player1_input, player2_input)
15+
print(result)

vowel_checker.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def check_vowel_or_consonant(letter):
2+
vowels = 'aeiou'
3+
if letter.lower() in vowels:
4+
print(f"'{letter}' is a vowel.")
5+
elif letter.isalpha():
6+
print(f"'{letter}' is a consonant.")
7+
else:
8+
print(f"'{letter}' is not a letter.")
9+
10+
if __name__ == "__main__":
11+
user_input = input("Enter a single letter: ")
12+
if len(user_input) == 1:
13+
check_vowel_or_consonant(user_input)
14+
else:
15+
print("Please enter a single letter.")

0 commit comments

Comments
 (0)