<a href="https://colab.research.google.com/github/jabnik/Python_Course/blob/master/Assignment_1_ipynb_NJ.ipynb" target="_parent"></a>

## 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
x[0]+x[1:].lower().replace(" ", "")[27:]
```




    '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(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
w, h = input("Adam's weight and height:").split(); bmi = (float(w)/float(h) ** 2); print(bmi)
```

    Adam's weight and height:68.4 1.71
    23.39181286549708


#### 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
bmi = 23.39181286549708

if bmi < 18.5 :
  print ("Underweight")
elif 18.5 <= bmi < 24.9:
    print ("Normal weight")
elif 25.0 <= bmi < 29.9:
    print ("Pre-obesity")
elif 30.0 <= bmi < 34.9:
    print ("Obesity class I")
elif 35.0 <= bmi < 39.9:
    print ("Obesity class II")
elif 40 <= bmi :
      print ("Obesity class III")
 
```

    Normal weight


#### 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
num=int(input("Enter a number to find factorial: "))

factorial=1 

if num<0:
   print("Factorial does not defined for negative integer");
elif(num==0):
    print("The factorial of 0 is 1");
else:
  while(num>0):
        factorial=factorial*num
        
        num=num-1
        
  print("factorial of the given number is: ")
  print(factorial)
```

    Enter a number to find factorial: 5
    factorial of the given number is: 
    120


#### 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
x=int(input("Enter a number: "))

while x != -1 :
  print(x, "is bigger than -1")
  if x > -1 :
    x = x -1
  else :
    x = x + 1
```

    Enter a number: 5
    5 is bigger than -1
    4 is bigger than -1
    3 is bigger than -1
    2 is bigger than -1
    1 is bigger than -1
    0 is bigger than -1


#### 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
def weeklyPaid(employee, hours_worked, wage_per_hour):
    if hours_worked > 40:
        return 40 * wage_per_hour + (hours_worked - 40) * wage_per_hour * 1.5
    else:
        return hours_worked * wage_per_hour
  
employee = "Matilde"  
hours_worked = 50
wage_per_hour = 120
  
pay = weeklyPaid(employee, hours_worked, wage_per_hour)
  
print(f"Total gross pay of {employee} : EUR {pay:.2f} ")
```

    Total gross pay of Matilde : EUR 6600.00 


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


```python
A, B, C = 1.1, 2.2, 3.3 
```

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


```python
base = int(A)
height = int(C)
Area = (base * height) / 2

print("The area of the triangle rectangle =", Area)
```

    The area of the triangle rectangle = 1.5


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


```python
radius = int(C)
pi = 3.14159
Area = pi * radius * radius
print("Area of the circle = " + str(Area))
```

    Area of the circle = 28.274309999999996


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


```python
base_1 = int(A)
base_2 = int(B)
height = int(C)
Area = (base_1+base_2)*height/2
print("Area of the trapezoid = " + str(Area))
```

    Area of the trapezoid = 4.5


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


```python
side = int(B) 
Area = side ** 2
print("Area of the square = " + str(Area))
```

    Area of the square = 4


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


```python
width = int(A)
height = int(B)
Area = width * height
print("The area of the rectangle = " + str(Area))
```

    The area of the rectangle = 2


#### 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
a, b, c = 0.23, 1.45, 3.77

r = b**2 - 4*a*c

if r > 0:
    num_roots = 2
    x1 = (((-b) + sqrt(r))/(2*a))     
    x2 = (((-b) - sqrt(r))/(2*a))
    print("There are 2 roots: %f and %f" % (x1, x2))
elif r == 0:
    num_roots = 1
    x = (-b) / 2*a
    print("There is one root: ", x)
else:
    num_roots = 0
    print("There are no real roots.")

```

    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
x1, x2, y1, y2 = 1.11, 2.32, 4.56, 5.23

result= ((((x2 - x1 )**2) + ((y2-y1)**2) )**0.5)

print("distance(d) between",(x1,x2),"and",(y1,y2),"is : ",result)
```

    distance(d) between (1.11, 2.32) and (4.56, 5.23) is :  1.3831124321616086


#### 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
def quadrant(x, y):
	if (x > 0 and y > 0):
		print ("lies in First quadrant")
	elif (x < 0 and y > 0):
		print ("lies in Second quadrant")
	elif (x < 0 and y < 0):
		print ("lies in Third quadrant")
	elif (x > 0 and y < 0):
		print ("lies in Fourth quadrant")
	elif (x == 0 and y > 0):
		print ("lies at positive y axis")
	elif (x == 0 and y < 0):
		print ("lies at negative y axis")
	elif (y == 0 and x < 0):
		print ("lies at negative x axis")
	elif (y == 0 and x > 0):
		print ("lies at positive x axis")
	else:
		print ("lies at origin")

x = 1
y = 1
quadrant(x, y)
```

    lies in First quadrant


#### 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
Country_calling_code = int(input('Enter a number: '))
CountryCall_dict = {43:'Austria',
             32:'Belgium',
             359:'Bulgaria',
             385:'Croatia',
             357:'Cyprus',
             420:'Czech Republic',
             45:'Denmark',
             372:'Estonia',
             359:'Finland',
             33:'France',
             49: 'Germany'}

if Country_calling_code in CountryCall_dict.keys():
    print(CountryCall_dict[Country_calling_code])
else:
    print('No result.')
```

    Enter a number: 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
import statistics
 
data1 = [1, 3, 4, 5, 7, 9]
 
x = statistics.mean(data1)
print(f"Mean is: {x:.1f}")
```

    Mean is: 4.8


#### 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
def printSquares(n):
 
    square = 0; prev_x = 0;
    for x in range(1, n+1):
        square = (square + x + prev_x)
        print(square, end = " ")
        prev_x = x

n = 6;
printSquares(n)
```

    1 4 9 16 25 36 

#### 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
num = int(input("Enter a number: "))
if num >= 0:
   if num == 0:
       print("Zero")
   else:
       print("Positive number")
else:
   print("Negative number")
  
if (num % 2) == 0:
   print("{0} is Even".format(num))
else:
   print("{0} is Odd".format(num))
```

    Enter a number: 10
    Positive number
    10 is Even


## 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

```

b5aa1b60ab58baf1f7b739026b570f10188f991b
ipynb
Jupyter Notebook
Assignment_1_ipynb_NJ.ipynb
jabnik/Python_Course
ca7b8605df6af8535b23e9c8766d0196c57239b1
Apache-2.0
Assignment_1_ipynb_NJ.ipynb
jabnik/Python_Course
ca7b8605df6af8535b23e9c8766d0196c57239b1
Apache-2.0
Assignment_1_ipynb_NJ.ipynb
jabnik/Python_Course
ca7b8605df6af8535b23e9c8766d0196c57239b1
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES