# Programming and Data Analysis

> Homework 0

Kuo, Yao-Jen <yaojenkuo@ntu.edu.tw> from [DATAINPOINT](https://www.datainpoint.com)

## Instructions

- We've imported necessary modules at the beginning of each exercise.
- We've put necessary files(if any) in the working directory of each exercise.
- We've defined the names of functions/inputs/parameters for you.
- Write down your solution between the comments `### BEGIN SOLUTION` and `### END SOLUTION`.
- It is NECESSARY to `return` the answer, tests will fail by just printing out the answer.
- Do not use `input()` function, it will halt the notebook while running tests.
- Running tests to see if your solutions are right: Kernel -> Restart & Run All -> Restart and Run All Cells.
- You can run tests after each question or after finishing all questions.


```python
import unittest
```

## 01. Define a function named `convert_fahrenheit_to_celsius(x)` which converts Fahrenheit degrees to Celsius degrees.

\begin{equation}
Celsius^{\circ} C = (Fahrenheit^{\circ} F - 32) \times \frac{5}{9}
\end{equation}

- Expected inputs：a numeric `x`.
- Expected outputs：a numeric.


```python
def convert_fahrenheit_to_celsius(x):
    """
    >>> convert_fahrenheit_to_celsius(212)
    100.0
    >>> convert_fahrenheit_to_celsius(32)
    0.0
    """
    ### BEGIN SOLUTION
    C = (x - 32)*5/9
    return C
    ### END SOLUTION
```

## 02. Define a function named `calculate_bmi(height, weight)` which calculates BMI according to heights in meters and weights in kilograms.

\begin{equation}
BMI = \frac{weight_{kg}}{height_{m}^2}
\end{equation}

Source: <https://en.wikipedia.org/wiki/Body_mass_index>

- Expected inputs：2 numerics `height` and `weight`.
- Expected outputs：a numeric.


```python
def calculate_bmi(height, weight):
    """
    >>> calculate_bmi(216, 147) # Shaquille O'Neal in his prime
    31.507201646090532
    >>> calculate_bmi(206, 113) # LeBron James
    26.628334433028563
    >>> calculate_bmi(211, 110) # Giannis Antetokounmpo
    24.70744143213315
    """
    ### BEGIN SOLUTION
    BMI = weight / ((height * .01)**2)
    return BMI
    ### END SOLUTION
```

## 03. Define a function named `show_big_mac_index(country, currency, price)` which returns the Big Mac Index given a country, its currency, and the price of a Big Mac. 

- Expected inputs：2 strings and a numeric.
- Expected outputs：a string.


```python
def show_big_mac_index(country, currency, price):
    """
    >>> show_big_mac_index('US', 'USD', 5.65)
    A Big Mac costs 5.65 USD in US.
    >>> show_big_mac_index('South Korea', 'Won', 6520)
    A Big Mac costs 6,520.00 Won in South Korea.
    >>> show_big_mac_index('Taiwan', 'NTD', 72)
    A Big Mac costs 72.00 NTD in Taiwan.
    """
    ### BEGIN SOLUTION
    sentence = "A Big Mac costs {:0,.2f} {} in {}.".format(price, currency, country)
    return sentence
    ### END SOLUTION
```

## 04. Define a function named `is_a_divisor(x, y)` which returns whether `x` is a is_a_divisor of `y` or not.

- Expected inputs：2 integers.
- Expected outputs：a boolean.


```python
def is_a_divisor(x, y):
    """
    >>> is_a_divisor(1, 3)
    True
    >>> is_a_divisor(2, 3)
    False
    >>> is_a_divisor(3, 3)
    True
    >>> is_a_divisor(1, 4)
    True
    >>> is_a_divisor(2, 4)
    True
    >>> is_a_divisor(3, 4)
    False
    >>> is_a_divisor(4, 4)
    True
    """
    ### BEGIN SOLUTION
    divisor = not(bool(y%x))
    return divisor
    ### END SOLUTION
```

## 05. Define a function named `contains_vowels(x)` which returns whether x contains one of the vowels: a, e, i, o, u or not.

- Expected inputs：a string.
- Expected outputs：a boolean.


```python
def contains_vowels(x):
    """
    >>> contains_vowels('pythn')
    False
    >>> contains_vowels('ncnd')
    False
    >>> contains_vowels('rtclt')
    False
    >>> contains_vowels('python')
    True
    >>> contains_vowels('anaconda')
    True
    >>> contains_vowels('reticulate')
    True
    """
    ### BEGIN SOLUTION
    if x.find('a') != -1:
        return True
    elif x.find('e') != -1:
        return True
    elif x.find('i') != -1:
        return True
    elif x.find('o') != -1:
        return True
    elif x.find('u') != -1:
        return True
    else:
        return False
    ### END SOLUTION
```

## Run tests!

Kernel -> Restart & Run All. -> Restart And Run All Cells.


```python
class TestHomeworkZero(unittest.TestCase):
    def test_01_convert_fahrenheit_to_celsius(self):
        self.assertAlmostEqual(convert_fahrenheit_to_celsius(212), 100.0)
        self.assertAlmostEqual(convert_fahrenheit_to_celsius(32), 0.0)
    def test_02_calculate_bmi(self):
        self.assertTrue(calculate_bmi(216, 147) > 31)
        self.assertTrue(calculate_bmi(216, 147) < 32)
        self.assertTrue(calculate_bmi(206, 113) > 26)
        self.assertTrue(calculate_bmi(206, 113) < 27)
        self.assertTrue(calculate_bmi(211, 110) > 24)
        self.assertTrue(calculate_bmi(211, 110) < 25)
    def test_03_show_big_mac_index(self):
        self.assertEqual(show_big_mac_index('US', 'USD', 5.65), 'A Big Mac costs 5.65 USD in US.')
        self.assertEqual(show_big_mac_index('South Korea', 'Won', 6520), 'A Big Mac costs 6,520.00 Won in South Korea.')
        self.assertEqual(show_big_mac_index('Taiwan', 'NTD', 72), 'A Big Mac costs 72.00 NTD in Taiwan.')
    def test_04_is_a_divisor(self):
        self.assertTrue(is_a_divisor(1, 2))
        self.assertTrue(is_a_divisor(2, 2))
        self.assertTrue(is_a_divisor(1, 3))
        self.assertFalse(is_a_divisor(2, 3))
        self.assertTrue(is_a_divisor(1, 4))
        self.assertTrue(is_a_divisor(2, 4))
        self.assertFalse(is_a_divisor(3, 4))
        self.assertTrue(is_a_divisor(4, 4))
    def test_05_contains_vowels(self):
        self.assertFalse(contains_vowels('pythn'))
        self.assertFalse(contains_vowels('ncnd'))
        self.assertFalse(contains_vowels('rtclt'))
        self.assertTrue(contains_vowels('python'))
        self.assertTrue(contains_vowels('anaconda'))
        self.assertTrue(contains_vowels('reticulate'))

suite = unittest.TestLoader().loadTestsFromTestCase(TestHomeworkZero)
runner = unittest.TextTestRunner(verbosity=2)
test_results = runner.run(suite)
number_of_failures = len(test_results.failures)
number_of_errors = len(test_results.errors)
number_of_test_runs = test_results.testsRun
number_of_successes = number_of_test_runs - (number_of_failures + number_of_errors)
```

    test_01_convert_fahrenheit_to_celsius (__main__.TestHomeworkZero) ... ok
    test_02_calculate_bmi (__main__.TestHomeworkZero) ... ok
    test_03_show_big_mac_index (__main__.TestHomeworkZero) ... ok
    test_04_is_a_divisor (__main__.TestHomeworkZero) ... ok
    test_05_contains_vowels (__main__.TestHomeworkZero) ... ok
    
    ----------------------------------------------------------------------
    Ran 5 tests in 0.006s
    
    OK



```python
print("You've got {} successes among {} questions.".format(number_of_successes, number_of_test_runs))
```

    You've got 5 successes among 5 questions.


dd87510b3244ae1530e730cacc6e40420a1af23b
ipynb
Jupyter Notebook
exercises.ipynb
KuYunHsuan/Programming_and_Data_Analysis
2569837bd59333f6dde1dfb4d23d82a8131393b1
MIT
exercises.ipynb
KuYunHsuan/Programming_and_Data_Analysis
2569837bd59333f6dde1dfb4d23d82a8131393b1
MIT
exercises.ipynb
KuYunHsuan/Programming_and_Data_Analysis
2569837bd59333f6dde1dfb4d23d82a8131393b1
MIT
Qwen/Qwen-72B
1. YES
2. YES