|
3 | 3 | ## Day 2 Recap
|
4 | 4 | [Control Flow: Conditionals, Loops, Jump statements, functions](day3/recap.py)
|
5 | 5 |
|
| 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 | + |
6 | 161 | ## More on Functions
|
7 | 162 | [Code](day3/functions.py)
|
8 | 163 |
|
|
0 commit comments