Skip to content

Commit dbf052d

Browse files
author
Sahil
committed
Add recap Day 2 notes
1 parent 0bd89ff commit dbf052d

File tree

2 files changed

+268
-0
lines changed

2 files changed

+268
-0
lines changed

day3/recap.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"""
2+
program to check if the number (say 'n') given as input is a prime
3+
a prime no. is the one which has only 1 and the numbers itself as its divisors
4+
1 is not a prime no.
5+
eg. n = 2, 3, 5, 7, ...
6+
7+
idea: start a loop from 2 to n/2, if n is divisble by the divisor => not prime
8+
because a no. cannot be divided by a no. > n/2 except n
9+
"""
10+
11+
n = int(input())
12+
checked = 0
13+
for divisor in range(2, n//2 + 1):
14+
if (n % divisor == 0):
15+
print("No is not prime!")
16+
checked = 1
17+
# no need checking further, can break the loop here itself
18+
break
19+
20+
# the above code doesn't say that 1 is not a prime because loop won't run even once
21+
# to handle that case, we can add the extra condition:
22+
if (n == 1):
23+
print("No is not prime!")
24+
checked = 1
25+
26+
# in cases where checked is false or equal to 0, can say it is prime!
27+
if (not checked):
28+
print("No is prime!")
29+
30+
31+
"""
32+
program to print the following pattern:
33+
34+
*
35+
* *
36+
* * *
37+
* * * *
38+
...
39+
40+
idea: As you can observer, we are looping in two directions, row wise and column wise
41+
1st row has 1 star
42+
2nd row has 2 stars
43+
3rd row has 3 stars and so on...
44+
45+
the outer loop would represent looping over the rows
46+
and in the inner loop, we will loop over the elements in the rows, i.e. columns
47+
48+
Always better to use loop variable 'r' or 'row' for looping on rows
49+
and the loop variable 'c' or 'col' to loop on columns
50+
for easy understanding and debugging of your code !
51+
"""
52+
53+
num_rows = n
54+
for row in range(1, num_rows + 1):
55+
# r_th row will have r stars
56+
for col in range(1, row + 1):
57+
print('*', end=' ')
58+
# endline
59+
print('')
60+
61+
# is it possible to do the same thing using one loop, think?
62+
63+
# idea is the same: print 1 char in 1st row, 2 chars in 2nd row, ...
64+
line_num = 1
65+
printed_on_cur_row = 0
66+
while (line_num <= num_rows):
67+
if (printed_on_cur_row < line_num):
68+
print('*', end=' ')
69+
printed_on_cur_row += 1
70+
else:
71+
print('')
72+
# reset count of stars on cur_row to be zero
73+
printed_on_cur_row = 0
74+
# increment the line_num, i.e. move to next row
75+
line_num += 1
76+
77+
"""
78+
function to return maximum of three numbers
79+
80+
idea: let numbers be a, b, c
81+
when can a be the max? when a >= b and a >= c
82+
similarly for b and c
83+
so, write three conditions, and return the answer if that condition is true
84+
also, you can use if elif else as well!
85+
"""
86+
87+
def maximum_of_three(num_one, num_two, num_three):
88+
if (num_one >= num_two and num_one >= num_three):
89+
return num_one
90+
if (num_two >= num_one and num_two >= num_three):
91+
return num_two
92+
if (num_three >= num_one and num_three >= num_two):
93+
return num_three
94+
return -1
95+
96+
n1 = 3
97+
n2 = 7
98+
n3 = 1
99+
100+
res = maximum_of_three(n1, n2, n3)
101+
print(res)
102+
103+
# idea: assign max = a, now check if b >= max, max = b, again if c >= max, max = c
104+
def maximum_of_three_simple(n1, n2, n3):
105+
maxi = n1
106+
if (n2 >= maxi):
107+
maxi = n2
108+
if (n3 >= maxi):
109+
maxi = n3
110+
return maxi
111+
112+
res = maximum_of_three(n1, n2, n3)
113+
print(res)

lecture3.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,161 @@
33
## Day 2 Recap
44
[Control Flow: Conditionals, Loops, Jump statements, functions](day3/recap.py)
55

6+
### Check if given number is a prime
7+
8+
<details>
9+
<summary>
10+
Code
11+
</summary>
12+
13+
```py
14+
"""
15+
program to check if the number (say 'n') given as input is a prime
16+
a prime no. is the one which has only 1 and the numbers itself as its divisors
17+
1 is not a prime no.
18+
eg. n = 2, 3, 5, 7, ...
19+
20+
idea: start a loop from 2 to n/2, if n is divisble by the divisor => not prime
21+
because a no. cannot be divided by a no. > n/2 except n
22+
"""
23+
24+
n = int(input())
25+
checked = 0
26+
for divisor in range(2, n//2 + 1):
27+
if (n % divisor == 0):
28+
print("No is not prime!")
29+
checked = 1
30+
# no need checking further, can break the loop here itself
31+
break
32+
33+
# the above code doesn't say that 1 is not a prime because loop won't run even once
34+
# to handle that case, we can add the extra condition:
35+
if (n == 1):
36+
print("No is not prime!")
37+
checked = 1
38+
39+
# in cases where checked is false or equal to 0, can say it is prime!
40+
if (not checked):
41+
print("No is prime!")
42+
```
43+
44+
</details>
45+
46+
### Printing star pattern
47+
48+
<details>
49+
<summary>
50+
Code
51+
</summary>
52+
53+
```py
54+
"""
55+
program to print the following pattern:
56+
57+
*
58+
* *
59+
* * *
60+
* * * *
61+
...
62+
63+
idea: As you can observer, we are looping in two directions, row wise and column wise
64+
1st row has 1 star
65+
2nd row has 2 stars
66+
3rd row has 3 stars and so on...
67+
68+
the outer loop would represent looping over the rows
69+
and in the inner loop, we will loop over the elements in the rows, i.e. columns
70+
71+
Always better to use loop variable 'r' or 'row' for looping on rows
72+
and the loop variable 'c' or 'col' to loop on columns
73+
for easy understanding and debugging of your code !
74+
"""
75+
76+
num_rows = n
77+
for row in range(1, num_rows + 1):
78+
# r_th row will have r stars
79+
for col in range(1, row + 1):
80+
print('*', end=' ')
81+
# endline
82+
print('')
83+
84+
# is it possible to do the same thing using one loop, think?
85+
86+
# idea is the same: print 1 char in 1st row, 2 chars in 2nd row, ...
87+
line_num = 1
88+
printed_on_cur_row = 0
89+
while (line_num <= num_rows):
90+
if (printed_on_cur_row < line_num):
91+
print('*', end=' ')
92+
printed_on_cur_row += 1
93+
else:
94+
print('')
95+
# reset count of stars on cur_row to be zero
96+
printed_on_cur_row = 0
97+
# increment the line_num, i.e. move to next row
98+
line_num += 1
99+
```
100+
101+
</details>
102+
103+
### Find max of three numbers
104+
105+
<details>
106+
<summary>
107+
Function 1
108+
</summary>
109+
110+
```py
111+
"""
112+
function to return maximum of three numbers
113+
114+
idea: let numbers be a, b, c
115+
when can a be the max? when a >= b and a >= c
116+
similarly for b and c
117+
so, write three conditions, and return the answer if that condition is true
118+
also, you can use if elif else as well!
119+
"""
120+
121+
def maximum_of_three(num_one, num_two, num_three):
122+
if (num_one >= num_two and num_one >= num_three):
123+
return num_one
124+
if (num_two >= num_one and num_two >= num_three):
125+
return num_two
126+
if (num_three >= num_one and num_three >= num_two):
127+
return num_three
128+
return -1
129+
130+
n1 = 3
131+
n2 = 7
132+
n3 = 1
133+
134+
res = maximum_of_three(n1, n2, n3)
135+
print(res)
136+
```
137+
138+
</details>
139+
140+
<details>
141+
<summary>
142+
Function 2
143+
</summary>
144+
145+
```py
146+
# idea: assign max = a, now check if b >= max, max = b, again if c >= max, max = c
147+
def maximum_of_three_simple(n1, n2, n3):
148+
maxi = n1
149+
if (n2 >= maxi):
150+
maxi = n2
151+
if (n3 >= maxi):
152+
maxi = n3
153+
return maxi
154+
155+
res = maximum_of_three(n1, n2, n3)
156+
print(res)
157+
```
158+
159+
</details>
160+
6161
## More on Functions
7162
[Code](day3/functions.py)
8163

0 commit comments

Comments
 (0)