Skip to content

Commit eee6bfc

Browse files
committed
Add Python Graded Assignment IITM
1 parent de41995 commit eee6bfc

11 files changed

Lines changed: 2010 additions & 3 deletions

content/exercises/graded-assignments/python/GrPA1-Arithemetic.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: GrPA 1 Numbers (Arithemetic)
33
label: Graded
44
weight: 1
55
subject: programming
6-
subtitle: GrPA 1
6+
subtitle: Week 1 GrPA
77
categories:
88
- Python Graded Assignment
99
---
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
---
2+
title: GrPA 1 While Loop
3+
label: Graded
4+
weight: 1
5+
subject: programming
6+
subtitle: Week 3 GrPA
7+
categories:
8+
- Python Graded Assignment
9+
---
10+
11+
---
12+
13+
14+
## GrPA 1 While Loop Graded 👨‍💻
15+
16+
{{< border >}}
17+
18+
{{< tabs items="QUESTION,TEST CASES,SOLUTION" >}}
19+
20+
{{< tab >}}
21+
22+
{{< details title="Instructions" closed="true" >}}
23+
24+
{{< /details >}}
25+
26+
{{< /tab >}}
27+
28+
{{< /tabs >}}
29+
30+
{{< /border >}}
31+
32+
## Question ❓
33+
34+
Implement different parts of a multi-functional program based on an initial input value. Each part of the program will handle various tasks related to accumulation, filtering, mapping, and combinations of these operations. None of the tasks should use explicit loops for definite repetitions, and the program should handle indefinite inputs gracefully.
35+
36+
## Python Code 🐍
37+
38+
```python {linenos=table,linenostart=1}
39+
# Note this prefix code is to verify that you are not using any for loops in this exercise. This won't affect any other functionality of the program.
40+
with open(__file__) as f:
41+
content = f.read().split("# <eoi>")[2]
42+
if "for " in content:
43+
print("You should not use for loop or the word for anywhere in this exercise")
44+
45+
# This is the first line of the exercise
46+
task = input()
47+
# <eoi>
48+
49+
if task == "sum_until_0":
50+
total = 0
51+
n = int(input())
52+
while ...: # the terminal condition
53+
... # add n to the total
54+
... # take the next n form the input
55+
print(total)
56+
57+
elif task == "total_price":
58+
total_price = 0
59+
while ...: # repeat forever since we are breaking inside
60+
line = input()
61+
if ...: # The terminal condition
62+
break
63+
quantity, price = line.split() # split uses space by default
64+
quantity, price = ... # convert to ints
65+
... # accumulate the total price
66+
print(total_price)
67+
elif task == "only_ed_or_ing":
68+
...
69+
70+
elif task == "reverse_sum_palindrome":
71+
...
72+
73+
elif task == "double_string":
74+
...
75+
76+
elif task == "odd_char":
77+
...
78+
79+
elif task == "only_even_squares":
80+
...
81+
82+
elif task == "only_odd_lines":
83+
...
84+
85+
```
86+
87+
## Python Code Solution 🧠
88+
89+
```python {linenos=table,linenostart=1}
90+
# ...existing code...
91+
92+
if task == "sum_until_0":
93+
total = 0
94+
n = int(input())
95+
while n != 0: # the terminal condition
96+
total += n # add n to the total
97+
n = int(input()) # take the next n from the input
98+
print(total)
99+
100+
elif task == "total_price":
101+
total_price = 0
102+
while True: # repeat forever since we are breaking inside
103+
line = input()
104+
if line == "END": # The terminal condition
105+
break
106+
quantity, price = line.split() # split uses space by default
107+
quantity, price = int(quantity), int(price) # convert to ints
108+
total_price += quantity * price # accumulate the total price
109+
print(total_price)
110+
111+
elif task == "only_ed_or_ing":
112+
s = input()
113+
while s.lower() != "stop":
114+
if s.lower().endswith("ed") or s.lower().endswith("ing"):
115+
print(s)
116+
s = input()
117+
118+
elif task == "reverse_sum_palindrome":
119+
n = input()
120+
while n != "-1":
121+
try:
122+
num = int(n)
123+
rev = int(str(num)[::-1])
124+
total = num + rev
125+
if str(total) == str(total)[::-1]:
126+
print(n)
127+
except:
128+
pass
129+
n = input()
130+
131+
elif task == "double_string":
132+
s = input()
133+
while s != "":
134+
print(s + s)
135+
s = input()
136+
137+
elif task == "odd_char":
138+
result = []
139+
while True:
140+
s = input()
141+
result.append(s[::2])
142+
if s.endswith("."):
143+
break
144+
print(" ".join(result))
145+
146+
elif task == "only_even_squares":
147+
s = input()
148+
while s != "NAN":
149+
try:
150+
n = int(s)
151+
if n % 2 == 0:
152+
print(n * n)
153+
except:
154+
pass
155+
s = input()
156+
157+
elif task == "only_odd_lines":
158+
idx = 1
159+
lines = []
160+
s = input()
161+
while s != "END":
162+
if idx % 2 == 1:
163+
lines.insert(0, s)
164+
idx += 1
165+
s = input()
166+
print("\n".join(lines))
167+
```
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
title: GrPA 2 For Loop
3+
label: Graded
4+
weight: 1
5+
subject: programming
6+
subtitle: Week 3 GrPA
7+
categories:
8+
- Python Graded Assignment
9+
---
10+
11+
---
12+
13+
14+
## GrPA 2 For Loop Graded 👨‍💻
15+
16+
{{< border >}}
17+
18+
{{< tabs items="QUESTION,TEST CASES,SOLUTION" >}}
19+
20+
{{< tab >}}
21+
22+
{{< details title="Instructions" closed="true" >}}
23+
24+
{{< /details >}}
25+
26+
{{< /tab >}}
27+
28+
{{< /tabs >}}
29+
30+
{{< /border >}}
31+
32+
## Question ❓
33+
34+
Write a multi functional program that takes input task from standard input and does the corresponding taks accordingly. Note that the useage of for loop is not allowed in this exercise.
35+
36+
## Python Code 🐍
37+
38+
```python {linenos=table,linenostart=1}
39+
# Note this prefix code is to verify that you are not using any for loops in this exercise. This won't affect any other functionality of the program.
40+
with open(__file__) as f:
41+
content = f.read().split("# <eoi>")[2]
42+
if "while " in content:
43+
print("You should not use while loop or the word while anywhere in this exercise")
44+
45+
# your code should not use more than 7 for loops
46+
# assuming one for loop per problem
47+
if content.count("for ")>7:
48+
print("You should not use more than 7 for loops")
49+
50+
# This is the first line of the exercise
51+
task = input()
52+
# <eoi>
53+
54+
if task == 'factorial':
55+
n = int(input())
56+
result = 1
57+
i = 1
58+
while i <=n:
59+
result*=i
60+
i+=1
61+
62+
print(result)
63+
elif task == 'even_numbers':
64+
n = ...
65+
while i< n+1:
66+
print(i)
67+
i+=2
68+
69+
elif task == 'power_sequence':
70+
n = ...
71+
result = 1
72+
while i<n:
73+
print(result)
74+
result*=2
75+
i+=1
76+
77+
elif task == 'sum_not_divisible':
78+
...
79+
80+
elif task == 'from_k':
81+
...
82+
83+
elif task == 'string_iter':
84+
...
85+
86+
elif task == 'list_iter':
87+
lst = eval(input()) # this will load the list from input
88+
89+
else:
90+
print("Invalid")
91+
92+
```
93+
94+
## Python Code Solution ✅
95+
96+
```python {linenos=table,linenostart=1}
97+
if task == 'factorial':
98+
n = int(input())
99+
result = 1
100+
for i in range(1, n + 1):
101+
result *= i
102+
print(result)
103+
104+
elif task == 'even_numbers':
105+
n = int(input())
106+
for i in range(0, n + 1, 2):
107+
print(i)
108+
109+
elif task == 'power_sequence':
110+
n = int(input())
111+
result = 1
112+
for i in range(n):
113+
print(result)
114+
result *= 2
115+
116+
elif task == 'sum_not_divisible':
117+
n = int(input())
118+
total = 0
119+
for i in range(1, n):
120+
if i % 4 != 0 and i % 5 != 0:
121+
total += i
122+
print(total)
123+
124+
elif task == 'from_k':
125+
# Accept n and k either as "n k" in one line or as two lines
126+
inp = input().split()
127+
if len(inp) == 2:
128+
n, k = map(int, inp)
129+
else:
130+
n = int(inp[0])
131+
k = int(input())
132+
count = 0
133+
for num in range(k, 99, -1):
134+
print("Checking:", num) # Debug
135+
s = str(num)
136+
if '5' not in s and '9' not in s and num % 2 == 1:
137+
print(s[::-1])
138+
count += 1
139+
if count == n:
140+
break
141+
142+
elif task == 'string_iter':
143+
s = input()
144+
prev = 1
145+
for ch in s:
146+
print(int(ch) * prev)
147+
prev = int(ch)
148+
149+
elif task == 'list_iter':
150+
lst = eval(input()) # this will load the list from input
151+
for element in lst:
152+
print(f"{element} - type: {type(element)}")
153+
154+
else:
155+
print("Invalid")
156+
```

0 commit comments

Comments
 (0)