Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 10 additions & 42 deletions test_playground/basics/boss.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,20 @@
choice = 'y'

while choice =='y' : # make 'Y' valid too
while choice in ('y', 'Y'): # make 'Y' valid too
try:
# typecast the below 2 to a list

numbers =input("Enter the input numbers separated by spaces: ")
operators = input("Enter operators between them: ")

# check length matching

if len(numbers) != len(operators): # this seems odd... u might say it's ... off by one
print("What u doin fam ? ") # replace wiht better message :)
expr = input("Enter arithmetic expression: ").strip()
if not expr:
print("Please enter a valid expression.")
continue

flag = False # huh this seems inverted
for i in range(len(numbers)): # indexing range fix
a, b, op = numbers[i-1], numbers[i], operators[i]
# correct the ops
match op:
case '+':
c = a + b
case '-':
c = a * b
case '*':
c = a / b
case '/':
c = a % b
case '%':
c = a - b
case '//':
c = a ** b
case '**':
c = a // b
case _:
flag = True
if not flag:
print("Invalid ops vro")
break

numbers[i-1] = c
if not flag:
continue
print(f"Output: numbers[-1]")
except Exception:
print(f"Exception: ...") # print exception
# Safe-ish eval for educational use: no builtins, only arithmetic expression support.
result = eval(expr, {"__builtins__": {}}, {})
print(f"Output: {result}")
except Exception as e:
print(f"Exception: {e}")
finally:
choice = input("Do you want to continue? [y/n] : ") # always ask before ending

# can you make the code shorter and with improved answer?
# like handling any basic arithmetic equation (that may have brackets too) ?
# u might wanna find a special function in python
# u might wanna find a special function in python
45 changes: 26 additions & 19 deletions test_playground/basics/conditionals.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
# correct if else ladder to check if person is underage, normal citizen or senior citizen
# [0,18) -> underage, [18,60) normal age, [60,inf) senior citizen
# bonus, can you reduce ladder to a one liner?
age = input("Enter age") # ahh yes age is str , definitely

if age <= 0:
print("Lil bro")
elif age > 100:
print("Pay up taxes, person")
age = int(input("Enter age: "))

if age < 0:
print("Invalid age")
elif age < 18:
print("Underage")
elif age < 60:
print("Normal citizen")
else:
print("U still good, unc?")
print("Senior citizen")


# complete the match

day = input("Enter the day number") # dont forget to typecast to int
day = int(input("Enter the day number: ")) # dont forget to typecast to int

print("Today is: ") # how can you avoid printing newline here?
print("Today is: ", end="") # how can you avoid printing newline here?

match day:
case 1:
print("Monday")
# fill in the rest
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case 4:
print("Thursday")
case 5:
print("Friday")
case 6:
print("Saturday")
case 7:
print("Sunday")
case _:
print("Funday !")
print("Invalid day")

# implement try catch

try:
print(1/0)
except IndentationError: # ahh fix the syntax, also when u don't know the error what will u use?
print("what u tryna do bro")
except Exception as e: # ahh fix the syntax, also when u don't know the error what will u use?
print(f"what u tryna do bro: {e}")
finally:
print("So u done?")






2 changes: 1 addition & 1 deletion test_playground/basics/hello.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# yayy
"Hello World"(print)
print("Hello World")
17 changes: 6 additions & 11 deletions test_playground/basics/input_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,29 @@
# note: all inputs are strs by default


integer = input("Enter an integer: ") # change only this line
integer = int(input("Enter an integer: "))

print(type(integer)) # should output 'int'

number = input("Enter a number (floating point allowed): ") # change only this line
number = float(input("Enter a number (floating point allowed): "))

print(type(number)) # should output 'float'

array = input("Enter an array of numbers: ") # change only this line
array = list(map(float, input("Enter an array of numbers: ").split()))

print(type(array)) # should output 'list'

nums = [1,2,3,4]

print(nums) # print it as a string joined by commas : 1,2,3,4
print(",".join(map(str, nums))) # print it as a string joined by commas : 1,2,3,4


name = input("Enter your name: ")

print(f"Hello, name") # complete f string
print(f"Hello, {name}") # complete f string

x,y,z = 67, 420 , 9000


# 6 print statements is too much, can you get the same output in one print statement ?
print(x)
print('\n')
print(y)
print('\n')
print(z)

print(x, y, z, sep="\n\n")
11 changes: 6 additions & 5 deletions test_playground/basics/loops.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# print all multiples of 5 in [1,100]

for i in range(3,99,4): # start, stop, step , range is [start,stop)
for i in range(5, 101, 5): # start, stop, step , range is [start,stop)
print(i,end=" ")

print()

names = ["Avanish","Awwab","Nathan"]
nicknames = ["Amar","Akbar","Anthony"]

for name,nickname in zip(names, nicknames): # wow cool new function
print("Name: ..., Nickname: ...") # fill this at least
print(f"Name: {name}, Nickname: {nickname}")

# try zip for adding this array to the above 2 and printing all 3 in loop
hobbies = ["Marvel","Anime","Games"]
for name, nickname, hobby in zip(names, nicknames, hobbies):
print(f"Name: {name}, Nickname: {nickname}, Hobby: {hobby}")

choice = 'y'

while choice == 'y': # can you make this case insensitive with one more condition?
while choice in ('y', 'Y'): # can you make this case insensitive with one more condition?
choice = input("Enter choice [y/n] : ")

16 changes: 6 additions & 10 deletions test_playground/basics/vars_and_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@
x = 10
y = "5"

print(x+y) # come on think, this ain't javascript
print(x + int(y)) # come on think, this ain't javascript

num1 , num2 = 6 , 7

print(num1 // num2) # huh this shouldnt output 0, as a bonus can u also round to 2 decimal places?
print(round(num1 / num2, 2)) # huh this shouldnt output 0, as a bonus can u also round to 2 decimal places?

a , n = 1, 31

for i in range(n):
a *= 2 # can you replace this loop with a one liner?
a = 2 ** n


# match the correct statements wrt bitwise operators

print("AND operator:", " | ")
print("OR operator:", " ^ ")
print("XOR operator:", " & ")



print("AND operator:", " & ")
print("OR operator:", " | ")
print("XOR operator:", " ^ ")
Loading