Skip to content

Commit 519d8c8

Browse files
committed
Basic and intermediate exercises done
1 parent 6409b0b commit 519d8c8

File tree

19 files changed

+1047
-0
lines changed

19 files changed

+1047
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
choice = 'y'
2+
3+
while choice =='y' or choice == 'Y': # make 'Y' valid too
4+
try:
5+
# typecast the below 2 to a list
6+
numbers = list(input("Enter the input numbers separated by spaces: ").split())
7+
operators = list(input("Enter operators between them: ").split())
8+
9+
# check length matching
10+
11+
if len(numbers) != len(operators) + 1: # this seems odd... u might say it's ... off by one
12+
print("Number of opwrators should be one less than the number of numbers") # replace wiht better message :)
13+
continue
14+
15+
flag = True # huh this seems inverted
16+
for i in range(len(operators)): # indexing range fix
17+
a, b, op = float(numbers[i]), float(numbers[i+1]), operators[i]
18+
# correct the ops
19+
match op:
20+
case '+':
21+
c = a + b
22+
case '-':
23+
c = a - b
24+
case '*':
25+
c = a * b
26+
case '/':
27+
c = a / b
28+
case '%':
29+
c = a % b
30+
case '//':
31+
c = a // b
32+
case '**':
33+
c = a ** b
34+
case _:
35+
flag = False
36+
if not flag:
37+
print("Invalid operator!")
38+
break
39+
40+
numbers[i+1] = str(c)
41+
if not flag:
42+
continue
43+
print(f"Output: {numbers[-1]}")
44+
except Exception as e:
45+
print(f"Exception: {e}") # print exception
46+
finally:
47+
choice = input("Do you want to continue? [y/n] : ") # always ask before ending
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# correct if else ladder to check if person is underage, normal citizen or senior citizen
2+
# [0,18) -> underage, [18,60) normal age, [60,inf) senior citizen
3+
# bonus, can you reduce ladder to a one liner?
4+
age = int(input("Enter age")) # ahh yes age is str , definitely
5+
6+
if age < 18:
7+
print("Underage")
8+
elif age < 60:
9+
print("Normal citizen")
10+
else:
11+
print("Senior citizen")
12+
13+
# one liner
14+
category = "Underage" if age < 18 else "Normal citizen" if age < 60 else "Senior citizen"
15+
16+
17+
# complete the match
18+
19+
day = int(input("Enter the day number")) # dont forget to typecast to int
20+
21+
print("Today is: ", end="") # how can you avoid printing newline here?
22+
23+
match day:
24+
case 1:
25+
print("Monday")
26+
case 2:
27+
print("Tuesday")
28+
case 3:
29+
print("Wednesday")
30+
case 4:
31+
print("Thursday")
32+
case 5:
33+
print("Friday")
34+
case 6:
35+
print("Saturday")
36+
case 7:
37+
print("Sunday")
38+
case _:
39+
print("Funday !")
40+
41+
# implement try catch
42+
43+
try:
44+
print(1/0)
45+
except Exception: # ahh fix the syntax, also when u don't know the error what will u use?
46+
print("what u tryna do bro")
47+
finally:
48+
print("So u done?")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# yayy
2+
print("Hello World")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# typecast all inputs as prompted
2+
3+
# note: all inputs are strs by default
4+
5+
6+
integer = int(input("Enter an integer: ")) # change only this line
7+
8+
print(type(integer)) # should output 'int'
9+
10+
number = float(input("Enter a number (floating point allowed): ")) # change only this line
11+
12+
print(type(number)) # should output 'float'
13+
14+
array = list(input("Enter an array of numbers: ").split()) # change only this line
15+
16+
print(type(array)) # should output 'list'
17+
18+
nums = [1,2,3,4]
19+
20+
print(','.join(map(str, nums))) # print it as a string joined by commas : 1,2,3,4
21+
22+
23+
name = input("Enter your name: ")
24+
25+
print(f"Hello, {name}") # complete f string
26+
27+
x,y,z = 67, 420 , 9000
28+
29+
30+
# 6 print statements is too much, can you get the same output in one print statement ?
31+
print(x, '\n', y, '\n', z)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# print all multiples of 5 in [1,100]
2+
3+
for i in range(5, 101, 5): # start, stop, step , range is [start,stop)
4+
print(i, end=" ")
5+
6+
7+
names = ["Avanish", "Awwab", "Nathan"]
8+
nicknames = ["Amar", "Akbar", "Anthony"]
9+
hobbies = ["Marvel", "Anime", "Games"]
10+
for name, nickname, hobby in zip(names, nicknames, hobbies): # wow cool new function
11+
print(f"Name: {name}, Nickname: {nickname}, Hobby: {hobby}") # fill this at least
12+
13+
# try zip for adding this array to the above 2 and printing all 3 in loop
14+
15+
16+
choice = 'y'
17+
18+
while choice == 'y' or choice == 'Y': # can you make this case insensitive with one more condition?
19+
choice = input("Enter choice [y/n] : ")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# declare some variables
2+
3+
x = 10
4+
y = "5"
5+
6+
print(x + int(y)) # come on think, this ain't javascript
7+
8+
num1, num2 = 6, 7
9+
10+
print(num1 / num2) # huh this shouldnt output 0
11+
print(round(num1 / num2, 2)) # bonus: rounded to 2 decimal places
12+
13+
a, n = 1, 31
14+
15+
a = 2 ** n # can you replace this loop with a one liner?
16+
17+
18+
# match the correct statements wrt bitwise operators
19+
20+
print("AND operator:", " & ")
21+
print("OR operator:", " | ")
22+
print("XOR operator:", " ^ ")

0 commit comments

Comments
 (0)