In this guide, I’ll walk you through solving five popular `8 kyu level`

challenges with ease. But that’s not all – I’ll also introduce you to smarter techniques that will make you more advanced. Let’s jump in and unravel these challenges together!

**Calculate BMI**

Write function bmi that calculates body mass index (`bmi = weight / height2`

).

- if bmi <=
`18.5`

return “Underweight” - if bmi <=
`25.0`

return “Normal” - if bmi <=
`30.0`

return “Overweight” - if bmi >
`30`

return “Obese”

**Solution**

1
2
3
4
5
6
7
8
9
10

def bmi(weight, height):
bmi = weight / height ** 2
if bmi <= 18.5:
return "Underweight"
elif bmi <= 25:
return "Normal"
elif bmi <= 30:
return "Overweight"
else:
return "Obese"

**Enhanced Solution**

1
2
3
4
5
6

def bmi(weight, height):
bmi = weight / height ** 2
if bmi <= 18.5: return "Underweight"
elif bmi <= 25: return "Normal"
elif bmi <= 30: return "Overweight"
else: return "Obese"

The only difference between the two code snippets is the formatting style. The functionality and logic of both code snippets are identical

**Sum of positive**

You get an array of numbers, return the sum of all of the positives ones.

Example `[1,-4,7,12]`

=> `1 + 7 + 12 = 20`

Note: if there is nothing to sum, the sum is default to `0`

.

**Solution**

1
2
3
4
5

def positive_sum(arr):
p = []
for i in arr:
if i > 0: p.append(i)
return sum(p) if sum(p) > 0 else 0

**Enhanced Solution**

1
2

def positive_sum(arr):
return sum(x for x in arr if x > 0) # Generator Expression

Using a **Generator Expression**, you can tell Python to make stuff when you want it one at a time. This is great for lots of data, you deal with one thing without filling up your computer.

Additionally, there’s also something called **List Comprehension**. It makes lots of things all at once and keeps them in memory. It’s good if you want everything immediately, but it might use more memory, especially for big data.

**tl;dr**

Generator Expression | Uses less memory | Slightly slower |

List Comprehension | Not recommended for big data (uses more memory) | Faster |

*This becomes noticeable only with big data*

**Return Negative**

In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?

Examples

1
2
3

make_negative(1); # return -1
make_negative(-5); # return -5
make_negative(0); # return 0

Notes

- The number can be negative already, in which case no change is required.
- Zero (0) is not checked for any specific sign. Negative zeros make no mathematical sense.

**Solution**

1
2
3

def make_negative(number):
if number < 0: return number
else: return number * -1

**Enhanced Solution**

1
2

def make_negative(number):
return -abs(number)

`abs()`

gives you the absolute value of a given number, while `-abs()`

returns the negative of that number.

**Keep Hydrated!**

Nathan loves cycling. Because Nathan knows it is important to stay hydrated, he drinks 0.5 litres of water per hour of cycling. You get given the time in hours and you need to return the number of litres Nathan will drink, rounded to the smallest value.

For example:

1
2
3

time = 3 -----> litres = 1
time = 6.7 ---> litres = 3
time = 11.8 --> litres = 5

**Solution**

1
2
3

def litres(time):
f = time * 0.5
return f // 1

**Enhanced Solution**

1
2

def litres(time):
return time // 2

When you use `//`

between two numbers, Python divides them and then rounds down to the nearest whole number. It essentially gives you the quotient without any remainder

**Convert boolean values to strings**

Complete the method that takes a boolean value and return a `Yes`

string for `true`

, or a `No`

string for `false`

.

**Solution**

1
2
3
4
5

def bool_to_word(boolean):
if boolean == True:
return "Yes"
else:
return "No"

**Enhanced Solution**

1
2

def bool_to_word(bool):
return "Yes" if bool else "No" # ternary operator

The **Ternary Operator** enables you to express this condition in just one line of code.