|
| 1 | + |
| 2 | + |
| 3 | +# Question 6 |
| 4 | + |
| 5 | +### **Question** |
| 6 | + |
| 7 | +>***Write a program to compute:*** |
| 8 | +``` |
| 9 | +f(n)=f(n-1)+100 when n>0 |
| 10 | +and f(0)=1 |
| 11 | +``` |
| 12 | +>***with a given n input by console (n>0).*** |
| 13 | +
|
| 14 | +>***Example: |
| 15 | +If the following n is given as input to the program:*** |
| 16 | +``` |
| 17 | +5 |
| 18 | +``` |
| 19 | +>***Then, the output of the program should be:*** |
| 20 | +``` |
| 21 | +500 |
| 22 | +``` |
| 23 | +>***In case of input data being supplied to the question, it should be assumed to be a console input.*** |
| 24 | +
|
| 25 | +---------------------- |
| 26 | +### Hints |
| 27 | +> ***We can define recursive function in Python.*** |
| 28 | +
|
| 29 | +---------------------- |
| 30 | + |
| 31 | +**Main author's Solution: Python 2** |
| 32 | +```python |
| 33 | +def f(n): |
| 34 | + if n==0: |
| 35 | + return 0 |
| 36 | + else: |
| 37 | + return f(n-1)+100 |
| 38 | + |
| 39 | +n=int(raw_input()) |
| 40 | +print f(n) |
| 41 | +``` |
| 42 | +---------------- |
| 43 | +**My Solution: Python 3** |
| 44 | +```python |
| 45 | +#to be written |
| 46 | + |
| 47 | +``` |
| 48 | +--------------------- |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +# Question 61 |
| 53 | + |
| 54 | +### **Question** |
| 55 | + |
| 56 | +>***The Fibonacci Sequence is computed based on the following formula:*** |
| 57 | +``` |
| 58 | +f(n)=0 if n=0 |
| 59 | +f(n)=1 if n=1 |
| 60 | +f(n)=f(n-1)+f(n-2) if n>1 |
| 61 | +``` |
| 62 | +>***Please write a program to compute the value of f(n) with a given n input by console.*** |
| 63 | +
|
| 64 | +>***Example: |
| 65 | +If the following n is given as input to the program:*** |
| 66 | +``` |
| 67 | +7 |
| 68 | +``` |
| 69 | +>***Then, the output of the program should be:*** |
| 70 | +``` |
| 71 | +13 |
| 72 | +``` |
| 73 | +>***In case of input data being supplied to the question, it should be assumed to be a console input.*** |
| 74 | +
|
| 75 | +---------------------- |
| 76 | +### Hints |
| 77 | +> ***We can define recursive function in Python.*** |
| 78 | +
|
| 79 | +---------------------- |
| 80 | + |
| 81 | +**Main author's Solution: Python 2** |
| 82 | +```python |
| 83 | +def f(n): |
| 84 | + if n == 0: return 0 |
| 85 | + elif n == 1: return 1 |
| 86 | + else: return f(n-1)+f(n-2) |
| 87 | + |
| 88 | +n=int(raw_input()) |
| 89 | +print f(n) |
| 90 | +``` |
| 91 | +---------------- |
| 92 | +**My Solution: Python 3** |
| 93 | +```python |
| 94 | +#to be written |
| 95 | + |
| 96 | +``` |
| 97 | +--------------------- |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +# Question 62 |
| 102 | + |
| 103 | +### **Question** |
| 104 | + |
| 105 | +>***The Fibonacci Sequence is computed based on the following formula:*** |
| 106 | +``` |
| 107 | +f(n)=0 if n=0 |
| 108 | +f(n)=1 if n=1 |
| 109 | +f(n)=f(n-1)+f(n-2) if n>1 |
| 110 | +``` |
| 111 | +>***Please write a program to compute the value of f(n) with a given n input by console.*** |
| 112 | +
|
| 113 | +>***Example: |
| 114 | +If the following n is given as input to the program:*** |
| 115 | +``` |
| 116 | +7 |
| 117 | +``` |
| 118 | +>***Then, the output of the program should be:*** |
| 119 | +``` |
| 120 | +0,1,1,2,3,5,8,13 |
| 121 | +``` |
| 122 | +>***In case of input data being supplied to the question, it should be assumed to be a console input.*** |
| 123 | +
|
| 124 | +---------------------- |
| 125 | +### Hints |
| 126 | +>***We can define recursive function in Python. |
| 127 | +Use list comprehension to generate a list from an existing list. |
| 128 | +Use string.join() to join a list of strings.*** |
| 129 | + |
| 130 | +---------------------- |
| 131 | + |
| 132 | +**Main author's Solution: Python 2** |
| 133 | +```python |
| 134 | +def f(n): |
| 135 | + if n == 0: return 0 |
| 136 | + elif n == 1: return 1 |
| 137 | + else: return f(n-1)+f(n-2) |
| 138 | + |
| 139 | +n=int(raw_input()) |
| 140 | +values = [str(f(x)) for x in range(0, n+1)] |
| 141 | +print ",".join(values) |
| 142 | + |
| 143 | +``` |
| 144 | +---------------- |
| 145 | +**My Solution: Python 3** |
| 146 | +```python |
| 147 | +#to be written |
| 148 | + |
| 149 | +``` |
| 150 | +--------------------- |
| 151 | + |
| 152 | + |
| 153 | +# Question 63 |
| 154 | + |
| 155 | +### **Question** |
| 156 | + |
| 157 | +>***Please write a program using generator to print the even numbers between 0 and n in comma separated form while n is input by console.*** |
| 158 | +
|
| 159 | +>***Example: |
| 160 | +If the following n is given as input to the program:*** |
| 161 | +``` |
| 162 | +10 |
| 163 | +``` |
| 164 | +>***Then, the output of the program should be:*** |
| 165 | +``` |
| 166 | +0,2,4,6,8,10 |
| 167 | +``` |
| 168 | +>***In case of input data being supplied to the question, it should be assumed to be a console input.*** |
| 169 | +
|
| 170 | +---------------------- |
| 171 | +### Hints |
| 172 | +> ***Use yield to produce the next value in generator.*** |
| 173 | +
|
| 174 | +---------------------- |
| 175 | + |
| 176 | +**Main author's Solution: Python 2** |
| 177 | +```python |
| 178 | +def EvenGenerator(n): |
| 179 | + i=0 |
| 180 | + while i<=n: |
| 181 | + if i%2==0: |
| 182 | + yield i |
| 183 | + i+=1 |
| 184 | + |
| 185 | + |
| 186 | +n=int(raw_input()) |
| 187 | +values = [] |
| 188 | +for i in EvenGenerator(n): |
| 189 | + values.append(str(i)) |
| 190 | + |
| 191 | +print ",".join(values) |
| 192 | + |
| 193 | +``` |
| 194 | +---------------- |
| 195 | +**My Solution: Python 3** |
| 196 | +```python |
| 197 | +#to be written |
| 198 | + |
| 199 | +``` |
| 200 | +--------------------- |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | +# Question 64 |
| 205 | + |
| 206 | +### **Question** |
| 207 | + |
| 208 | +>***Please write a program using generator to print the numbers which can be divisible by 5 and 7 between 0 and n in comma separated form while n is input by console.*** |
| 209 | +
|
| 210 | +>***Example: |
| 211 | +If the following n is given as input to the program:*** |
| 212 | +``` |
| 213 | +100 |
| 214 | +``` |
| 215 | +>***Then, the output of the program should be:*** |
| 216 | +``` |
| 217 | +0,35,70 |
| 218 | +``` |
| 219 | +>***In case of input data being supplied to the question, it should be assumed to be a console input.*** |
| 220 | +
|
| 221 | +---------------------- |
| 222 | +### Hints |
| 223 | +> ***Use yield to produce the next value in generator.*** |
| 224 | +
|
| 225 | +---------------------- |
| 226 | + |
| 227 | +**Main author's Solution: Python 2** |
| 228 | +```python |
| 229 | +def NumGenerator(n): |
| 230 | + for i in range(n+1): |
| 231 | + if i%5==0 and i%7==0: |
| 232 | + yield i |
| 233 | + |
| 234 | +n=int(raw_input()) |
| 235 | +values = [] |
| 236 | +for i in NumGenerator(n): |
| 237 | + values.append(str(i)) |
| 238 | + |
| 239 | +print ",".join(values) |
| 240 | +``` |
| 241 | +---------------- |
| 242 | +**My Solution: Python 3** |
| 243 | +```python |
| 244 | +#to be written |
| 245 | + |
| 246 | +``` |
| 247 | +--------------------- |
| 248 | + |
| 249 | + |
0 commit comments