## First Assignment

#### 1) Apply the appropriate string methods to the **x** variable (as '.upper') to change it exactly to: "$Dichlorodiphenyltrichloroethane$".


```python
x = "DiClOrod  IFeNi  lTRicLOr oETaNo  DiChlorod iPHeny lTrichL oroEThaNe"
```


```python
result = x.replace("DiClOrod  IFeNi  lTRicLOr oETaNo  ", "").lower().replace(" ", "").title()
if result == "Dichlorodiphenyltrichloroethane":
    print(result)
```

    Dichlorodiphenyltrichloroethane


#### 2) Assign respectively the values: 'word', 15, 3.14 and 'list' to variables A, B, C and D in a single line of code. Then, print them in that same order on a single line separated by a space, using only one print statement.


```python
A, B, C, D = "word", 15, 3.14, "list"
print(f'{A} {B} {C} {D}')
```

    word 15 3.14 list


#### 3) Use the **input()** function to receive an input in the form **'68.4 1.71'**, that is, two floating point numbers in a line separated by space. Then, assign these numbers to the variables **w** and **h** respectively, which represent an individual's weight and height (hint: take a look at the '.split()' method). With this data, calculate the individual's Body Mass Index (BMI) from the following relationship: 
    
\begin{equation}
BMI = \dfrac{weight}{height^2}
\end{equation}


```python
def bmi(response = 0):
    response = input('Enter your weight and height')
    response1 = list(response.split())
    w = response1[0]
    h = response1[1]    
    if len(response1) > 2 or not str(w).isdigit() or not str(h).isdigit():
        print('Invlid input')
        bmi()
        return   
    else:
        BMI = float(w)/((float(h)/100)**2)
        print('Your body mass index (BMI) is {}'.format(BMI))
```


```python
bmi()
```

    Enter your weight and height 62 120


    Your body mass index (BMI) is 43.05555555555556 - Obesity class III


#### This value can also be classified according to ranges of values, following to the table below. Use conditional structures to classify and print the classification assigned to the individual. 

<center><\center>  


(source: https://healthtravelguide.com/bmi-calculator/)


```python
def bmi(response = 0):
    response = input('Enter your weight and height')
    response1 = list(response.split())
    w = response1[0]
    h = response1[1]    
    if len(response1) > 2 or not str(w).isdigit() or not str(h).isdigit():
        print('Invlid input')
        bmi()
        return   
    else:
        BMI = float(w)/((float(h)/100)**2)
        if BMI < 18.5:
            print('Your body mass index (BMI) is {} - underweight'.format(BMI))
        elif BMI >= 18.5 and  BMI <= 24.9:
            print('Your body mass index (BMI) is {} - Normal weight'.format(BMI))
        elif BMI >= 25.0 and  BMI <= 29.9:    
            print('Your body mass index (BMI) is {} - Pre-obesity'.format(BMI))
        elif BMI >= 30.0 and  BMI <= 34.9:
            print('Your body mass index (BMI) is {} - Obesity class I'.format(BMI))
        elif BMI >= 35.0 and  BMI <= 39.9:    
            print('Your body mass index (BMI) is {} - Obesity class II'.format(BMI))
        elif BMI >= 40.0:    
            print('Your body mass index (BMI) is {} - Obesity class III'.format(BMI))
```


```python
bmi()
```

    Enter your weight and height 45 175


    Your body mass index (BMI) is 14.693877551020408 - underweight


#### 4) Receive an integer as an input and, using a loop, calculate the factorial of this number, that is, the product of all the integers from one to the number provided. 


```python
def factorial(x = 0):
    x = int(input('Enter a number'))
    y = 1    
    for i in range(2, x + 1):
        y *= i 
    return y
```


```python
factorial()
```

    Enter a number 6





    720



#### 5) Using a while loop and the input function, read an indefinite number of integers until the number read is -1. Present the sum of all these numbers in the form of a print, excluding the -1 read at the end. 


```python
def wow(my_number=0):
    y = 0
    my_number = int(input('Enter a number'))
    while my_number != -1:  
        y += my_number
        my_number = int(input('Enter a number'))
    print(y)
```


```python
wow()
```

    Enter a number 5
    Enter a number 4
    Enter a number -1


    9



```python
## that is probably NOT what the task is, but in any case...
def aha(summa=0):
    my_number = int(input('Enter a number: '))
    all_numbers = [*range(-1, my_number+1)]
    summa = sum(all_numbers[1:])
    print(all_numbers)
    print(summa)
```


```python
aha()
```

    Enter a number:  13


    [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    91


#### 6) Read the **first name** of an employee, his **amount of hours worked** and the **salary per hour** in a single line separated by commas. Next, calculate the **total salary** for this employee and show it to two decimal places.


```python

name, working_hours, salary_per_hour = 'John', 40, 15.56
salary = working_hours * salary_per_hour
salary_decimal = "{:.2f}".format(salary)
print(salary_decimal)
```

    622.40



```python
data = input ('Enter name, amount_of_hours_worked, salary_ per_hour')
data_1 = data.replace(" ", "")
data_2 = data_1.split(',')
print(data_2)
name = data_2[0]
working_hours = float(data_2[1])
salary_per_hour = float(data_2[2])
salary = working_hours * salary_per_hour
salary_decimal = "{:.2f}".format(salary)
print(salary_decimal)
```

    Enter name, amount_of_hours_worked, salary_ per_hour peter, 30    , 15


    ['peter', '30', '15']
    450.00


#### 7) Read three floating point values **A**, **B** and **C** respectively. Then calculate items a, b, c, d and e: 


```python
A = float(input('Enter 1st floating point'))
B = float(input('Enter 2nd floating point'))
C = float(input('Enter 3rd floating point'))
print(A, B, C)
```

    Enter 1st floating point 13
    Enter 2nd floating point 12
    Enter 3rd floating point 23


    13.0 12.0 23.0


    a) the area of the triangle rectangle with A as the base and C as the height.


```python
s = (A * C) / 2
print(s)
```

    149.5


    b) the area of the circle of radius C. (pi = 3.14159)  


```python
pi = 3.14159
surface_circle = pi * C **2
print(surface_circle)
```

    38.4844775


    c) the area of the trapezoid that has A and B for bases and C for height. 


```python
area_tra = ((A + B) * C) / 2
print(area_tra)
```

    17.5


    d) the area of the square that has side B. 


```python
area_square = B * B
print(area_square)
```

    30.25


    e) the area of the rectangle that has sides A and B. 


```python
area_rectangle = A * B
print(area_rectangle)
```

    24.75


#### 8) Read **the values a, b and c** and calculate the **roots of the second degree equation** $ax^{2}+bx+c=0$ using [this formula](https://en.wikipedia.org/wiki/Quadratic_equation). If it is not possible to calculate the roots, display the message **“There are no real roots”**. 


```python
### That is too much math for me, sorry.
```

#### 9) Read four floating point numerical values corresponding to the coordinates of two geographical coordinates in the cartesian plane. Each point will come in a line with its coordinates separated by space. Then calculate and show the distance between these two points. 

(obs: $d=\sqrt{(x_1-x_2)^2 + (y_1-y_2)^2}$)


```python
locus1 = input('enter lat and lon of place 1: ')
locus2 = input('enter lat and lon of place 2: ')
coordinates_1 = locus1.split(' ')
coordinates_2 = locus2.split(' ')
coordinates1_float = [float(i) for i in coordinates_1]
coordinates2_float = [float(i) for i in coordinates_2]

from math import sin, cos, sqrt, atan2, radians


R = 6373.0 # radius of earth in km

lat_1 = radians(coordinates1_float[0])
lon_1 = radians(coordinates1_float[1])
lat_2 = radians(coordinates2_float[0])
lon_2 = radians(coordinates2_float[1])

dlon = lon_2 - lon_1
dlat = lat_2 - lat_1

a = sin(dlat / 2)**2 + cos(lat_1) * cos(lat_2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))

distance = R * c

print("Result:", distance)
```

    enter lat and lon of place 1 42 23
    enter lat and lon of place 2 48 16


    42 23
    Result: 864.4150003885633


#### 10) Read **two floating point numbers** on a line that represent **coordinates of a cartesian point**. With this, use **conditional structures** to determine if you are at the origin, printing the message **'origin'**; in one of the axes, printing **'x axis'** or **'y axis'**; or in one of the four quadrants, printing **'q1'**, **'q2**', **'q3'** or **'q4'**. 


```python

```

#### 11) Read an integer that represents a phone code for international dialing.  
#### Then, inform to which country the code belongs to, considering the generated table below:
(You just need to consider the first 10 entries)  


```python
import pandas as pd
df = pd.read_html('https://en.wikipedia.org/wiki/Telephone_numbers_in_Europe')[1]
df = df.iloc[:,:2]
df.head(20)

```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Country</th>
      <th>Country calling code</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>Austria</td>
      <td>43</td>
    </tr>
    <tr>
      <th>1</th>
      <td>Belgium</td>
      <td>32</td>
    </tr>
    <tr>
      <th>2</th>
      <td>Bulgaria</td>
      <td>359</td>
    </tr>
    <tr>
      <th>3</th>
      <td>Croatia</td>
      <td>385</td>
    </tr>
    <tr>
      <th>4</th>
      <td>Cyprus</td>
      <td>357</td>
    </tr>
    <tr>
      <th>5</th>
      <td>Czech Republic</td>
      <td>420</td>
    </tr>
    <tr>
      <th>6</th>
      <td>Denmark</td>
      <td>45</td>
    </tr>
    <tr>
      <th>7</th>
      <td>Estonia</td>
      <td>372</td>
    </tr>
    <tr>
      <th>8</th>
      <td>Finland</td>
      <td>358</td>
    </tr>
    <tr>
      <th>9</th>
      <td>France</td>
      <td>33</td>
    </tr>
    <tr>
      <th>10</th>
      <td>Germany</td>
      <td>49</td>
    </tr>
    <tr>
      <th>11</th>
      <td>Greece</td>
      <td>30</td>
    </tr>
    <tr>
      <th>12</th>
      <td>Hungary</td>
      <td>36</td>
    </tr>
    <tr>
      <th>13</th>
      <td>Iceland</td>
      <td>354</td>
    </tr>
    <tr>
      <th>14</th>
      <td>Ireland</td>
      <td>353</td>
    </tr>
    <tr>
      <th>15</th>
      <td>Italy</td>
      <td>39</td>
    </tr>
    <tr>
      <th>16</th>
      <td>Latvia</td>
      <td>371</td>
    </tr>
    <tr>
      <th>17</th>
      <td>Liechtenstein</td>
      <td>423</td>
    </tr>
    <tr>
      <th>18</th>
      <td>Lithuania</td>
      <td>370</td>
    </tr>
    <tr>
      <th>19</th>
      <td>Luxembourg</td>
      <td>352</td>
    </tr>
  </tbody>
</table>
</div>




```python
codes = {'Austria' : 43, 'Belgium' : 32, 'Bulgaria' : 359, 'Croatia' : 385, 'Cyprus' : 357, 'Czech' : 420, 'Denmark' : 45}
response = int(input('What is your code?'))
for key, value in codes.items():
    if value == response:
        print(key)
```

    What is your code? 43


    Austria


#### 12) Write a piece of code that reads 6 numbers in a row. Next, show the number of positive values entered. On the next line, print the average of the values to one decimal place. 


```python
def numbers(n1 = 0):
    n1 = input('Enter a number')
    n2 = input('Enter a number')
    n3 = input('Enter a number')
    n4 = input('Enter a number')
    n5 = input('Enter a number')
    n6 = input('Enter a number')
    response1 = [n1, n2, n3, n4, n5, n6]
    response2 = [i for i in response1 if i.isdigit()]
    import numpy as np
    x = np.array(response1)
    y = x.astype(np.float)
    z = [k for k in y if k>0]
    w = sum(y) / len(y)
    j = sum(z) / len(z)
    print('All positive values:', z)
    print('The average of all values is: ', w)
    print('The average of all positive values is: ', j)
```


```python
numbers()
```

    Enter a number -96
    Enter a number -20
    Enter a number 4
    Enter a number 2
    Enter a number 3
    Enter a number 1


    All positive values: [4.0, 2.0, 3.0, 1.0]
    The average of all values is:  -17.666666666666668
    The average of all positive values is:  2.5


#### 13) Read an integer **N**. Then print the **square of each of the even values**, from 1 to N, including N, if applicable, arranged one per line. 


```python

```

#### 14) Using **input()**, read an integer and print its classification as **'even / odd'** and **'positive / negative'** . The two classes for the number must be printed on the same line separated by a space. In the case of zero, print only **'null'**. 


```python
def number(x=0):
    x = input('Enter a number')
    if len(x.split())!=1 or not x.isdigit:    
        print('Bad input')
        number()
        return
    else:
        y = int(x)
        print('Your number is:', y)
        if (y % 2) == 0 and y !=0 and y < 0:
            print('even negative')
        elif (y % 2) == 0 and y !=0 and y>0:
            print('even negative')
        elif (y % 2) != 0 and y !=0 and y < 0:
            print('odd negative')
        elif (y % 2) !=0  and y !=0 and y > 0:
            print('odd positive')
        elif (y == 0):
            print('null')
```


```python
number()
```

    Enter a number -5


    Your number is: -5
    odd negative


## Challenge
#### 15) Ordering problems are recurrent in the history of programming. Over time, several algorithms have been developed to fulfill this function. The simplest of these algorithms is the [**Bubble Sort**](https://en.wikipedia.org/wiki/Bubble_sort), which is based on comparisons of elements two by two in a loop of passes through the elements. Your mission, if you decide to accept it, will be to input six whole numbers ramdonly ordered. Then implement the **Bubble Sort** principle to order these six numbers **using only loops and conditionals**.  
#### At the end, print the six numbers in ascending order on a single line separated by spaces. 


```python

```

c00e5bb1f00dd2eaaee7a6358ef27107cecc3b14
ipynb
Jupyter Notebook
Assignments/Assignment_1.ipynb
ivanadob/Python_Course
661bc573b656f191ee0676114793752d4c3844c5
Apache-2.0
Assignments/Assignment_1.ipynb
ivanadob/Python_Course
661bc573b656f191ee0676114793752d4c3844c5
Apache-2.0
Assignments/Assignment_1.ipynb
ivanadob/Python_Course
661bc573b656f191ee0676114793752d4c3844c5
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES