## 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
# first try. there might be a way where I don't need to create now variables for each step? 
xnew = x.replace(" ", "") 
xnew_stripped = xnew[27:]
xnew_stripped_lower = xnew_stripped.lower()
xfinal = xnew_stripped.capitalize()
print(xfinal)
```

    Dichlorodiphenyltrichloroethane



```python
#other solution presented in class(not my own work): 
y = "DiClOrod  IFeNi  lTRicLOr oETaNo  DiChlorod iPHeny lTrichL oroEThaNe"
y.replace(" ", "")[27: ].title()

```




    '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
#so second try with tuple: 
A, B, C, D = "word", 15, 3.14, "list"
print(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
x,y = input("Enter your weight in kg and heigt in m separated by a space").split()
bmi = float(x)/float(y)**2
print("This is your BMI:", bmi)
```

#### 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
#x,y = input("Enter your weight in kg and heigt in m separated by a space").split()
#bmi = float(x)/float(y)**2
#print("This is your BMI:", bmi)

if (bmi < 18.5): 
        print ("Your nutritional status is 'Underweight'") 
elif (bmi >= 18.5) and (bmi <= 24.9):
         print ("Your nutritional status is 'Normal weight'")
elif (bmi >= 25.0) and (bmi <= 29.9):
         print("Your nutritional status is 'Pre-obesity'")
elif (bmi >= 30.0) and (bmi <= 34,9):
        print("Your nutritional status is 'Obersity class I'")
elif (bmi >= 35.0) and (bmi <= 39,9):
        print("Your nutritional status is 'Obersity class II'") 
elif (bmi >= 40):
        print("Your nutritional status is 'Obersity class III'")
else:
        print("your BMI is to low to calculate")
```

#### 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
value = int(input())
factorial = 1
for number in range(1,value+1):
    factorian = factorial * number
print(factorial)
```

     3


    1


#### 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
num = 0
sum_num = 0

while num != -1: 
    sum_num = sum_num + num
    num = int(input())
print(f"Sum= {sum_num}")
```

     3
     4
     5
     1
     -1


    Sum= 13


#### 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
#first try. only one decimal place
x = ("Nora", 40, 10)
(name, hours, salery) = x
name = x[0]
hours = x[1]
salery= x[2]

salerytotal = float(hours*salery)
print(salerytotal)
```

    400.0


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


```python
numbers = input("Select three numbers: ").split()
A = int(numbers[0])
B = int(numbers[1])
C = int(numbers[2])
```

    Select three numbers:  1 3 4


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


```python
#A=h*b/2

area_triangle = (A*C)/2
print(area_triangle)
```

    2.0


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


```python
#A=h*b/2

area_triangle = (A*C)/2
print(area_triangle)
```

    2.0


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


```python
#A=pi*r^2

area_circle = 3.14159*C**2
print(area_circle)
```

    50.26544


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


```python
#A = (a+b/2)*h

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

    8.0


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


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

    3


#### 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
numbers = input("Select three numbers: ").split()
a = int(numbers[0])
b = int(numbers[1])
c = int(numbers[2])

# Python program to find roots of quadratic equation
import math 
  
# function for finding roots
def roots( a, b, c): 
  
    # calculating discriminant using formula
    dis = b * b - 4 * a * c 
    sqrt = math.sqrt(abs(dis)) 
      
    # checking condition for discriminant
    if dis > 0: 
        print(" real and different roots ") 
        print((-b + sqrt)/(2 * a)) 
        print((-b - sqrt)/(2 * a)) 
      
    elif dis == 0: 
        print(" real and equal roots") 
        print(-b / (2 * a)) 
      
    # when discriminant is less than 0
    else:
        print("There are no real roots")

roots(a, b, c)
```

    Select three numbers:  3 5 6


    There are no real roots


#### 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
numbers = input("Select four values for two coordinates: ").split()
x1 = float(numbers[0])
y1 = float(numbers[1])
x2 = float(numbers[2])
y2 = float(numbers[3])

d = math.sqrt((x1 - x2)**2 + (y1-y2)**2)
print(d)
```

    Select four values for two coordinates:  2 3 4 5


    2.8284271247461903


#### 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
numbers = input("Select two numbers: ").split()
x = float(numbers[0])
y = float(numbers[1])


#check for quadrants: 
if x > 0 and y > 0: 
    print("q1")
elif x < 0 and y > 0: 
    print("q2")
elif x < 0 and y < 0: 
    print("q3")
elif x > 0 and y < 0: 
    print("q4")
elif x == 0 and y ==0: 
    print("origin")
elif x and y == 0: 
    print ("origin")
elif x == 0 and y != 0: 
    print("x axis")
elif x != 0 and y == 0: 
    print("y axis")
```

    Select two numbers:  1 2


    q1


#### 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
some_value = input("Specify the phone code: ")
my_subset = df["Country calling code"] == some_value
country = df.loc[my_subset]["Country"].values.tolist()[0]
print(country)
```

    Specify the phone 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
numbers = [float(y) for y in input("Give me six values separates by space").split()]
sum_numbers = 0
positiv = 0
for i in numbers: 
    sum_numbers = sum_numbers + i
    if i > 0: 
        positiv = positiv +1
print(f"Count of positives: {positiv}")
print(f"Average: {sum_numbers/6}")
```

    Give me six values separates by space 1 4 5 -2 4 3


    Count of positives: 5
    Average: 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
n = input("Give me an integer: ")
N = int(n[0])
for x in range(1, N+1): 
    if x%2 == 0:
        print(x**2)
```

    Give me an integer:  8


    4
    16
    36
    64


#### 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
#first try. 
def even_odd(num):
    num_int = input("Provide a numeric: ")
    num = int(num_int)
    if num%2 == 0 and num > 0:
        return "even positive"
    if num%2 != 0 and num > 0:
        return "odd positive"
    if num%2 == 0 and num < 0:
        return "even negative"
    if num%2 != 0 and num < 0:
        return "odd negative"
    if num == 0:
        return "null"
    
even_odd(x)
```

    Provide a numeric:  7





    'odd positive'




```python
# now with classes, but needed google to figure out the tenary operation. 
num = int(input("Provide a numeric: "))
group1 = "even" if num%2 == 0 else "odd"
group2 = "positive" if num > 0 else "negative"
if number == 0: 
    print ("null")
else:
    print(group1, group2)
    
```

    Provide a numeric:  7


    odd positive


## 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
#does not work...
def bubbelsort(array):
    n = len(array)
    for i in range(n -1):
        for j in (0, n - i - 1):
            if array[j] > array[j + 1] :
                array[j], array[j + 1] = array[j + 1], array[j]
    print(array.sort(reverse = Treu))

array = [4, 2, 5, 6, 8, 1]
bubbelsort(array)
```


```python

```

8be12d93583bd3fc481f7a389c988c8e3bed744a
ipynb
Jupyter Notebook
Assignments/Assignment_1_final.ipynb
NorLins/Python_Course
966477571b46703050da995b54a90819cd9c729e
Apache-2.0
Assignments/Assignment_1_final.ipynb
NorLins/Python_Course
966477571b46703050da995b54a90819cd9c729e
Apache-2.0
Assignments/Assignment_1_final.ipynb
NorLins/Python_Course
966477571b46703050da995b54a90819cd9c729e
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES