# Day 1 - Python basics
Some exercises about basic python concepts.
## Exercise 1.1 - The factorial of a number (Easy)
Recursion is a powerful method at the foundation of many algorithms. The terms recursion means that a function during its execution call itself.
Using this idea, write a simple function to compute the [factorial](https://en.wikipedia.org/wiki/Factorial) of an integer number. Using the created function compute the factorial of 9.
**Solution:**
```python=
def factorial(n):
return 1 if n<=1 else n*factorial(n-1)
print(factorial(9))
```
**Output:**
```
362880
```
## Exercise 1.2 - Summing odd numbers (Medium)
Write a simple program to compute the sum of the first 20 odd numbers that cannot be divided exactly by 3. In writing the script it may be useful to know that `==` can be used to check identity while `!=` performs the opposite task.
As an example:
```
4 != 5
```
Will evaluate to `True` since the number are different.
**Solution:**
```python=
value, current, counter = 0, 0, 0
while counter<20:
current += 1
if current%2==1 and current%3 != 0:
value += current
counter += 1
print(value)
```
**Output:**
```
600
```
## Exercise 1.3 - Printing a pattern (Medium)
The `range` function accepts 3 possible arguments:
* `start`: (optional). An integer number specifying at which position to start. Default is 0
* `stop`: Required. An integer number specifying at which position to stop (not included).
* `step`: (optional). An integer number specifying the incrementation. Default is 1 `start`, `stop` e `step`.
Try the effect of changing the arguments of the function and using what you have learend about strings and `for` cycles, write a script capable of printing the following pattern:
```
o
oo
ooo
oooo
ooo
oo
o
```
**Solution:**
```python=
N = 4
for i in range(1, N):
text = ""
for j in range(i):
text += "o"
print(text)
for i in range(N):
text = ""
for j in range(i, N):
text += "o"
print(text)
```
## Exercise 1.4 - Computing $\pi$ with Monte Carlo (Advanced)
The `random` module contains the definition of the `random` function capable of generating pseudo-random numbers uniformly distributed in the range from $0$ to $1$.
<img title="MonteCarloPI" src="https://onlyphysics.org/wp-content/uploads/2020/09/12_file.gif">
A quarter of circle of radius $1$ can be inscribed in a square of side $1$. The area of the square is $1$ while the area of the quarter of the circle is $\pi/4$. If a set of random points is uniformly generated over the whole area of the square, the ratio between the number of points falling inside the circle and the total number of points can be used to approximate the ratio between the areas of the two figures. This method of integration is known as Monte Carlo method.
Write a python script to estimate the value of $\pi$ using the Monte Carlo method.
**Solution:**
```python=
from random import random
hits = 0
shots = int(1e7)
for n in range(shots):
x, y = random(), random()
if x**2 + y**2 <= 1.:
hits += 1
print(4*hits/shots)
```
**Output:**
Note: *The output may vary depending on the seed value.*
```
3.1411188
```