John is developing a system to report fuel usage but needs help with the coding.

First, he needs you to write a function that, given the actual consumption (in l/100 km) and remaining amount of petrol (in l), will give you how many kilometers you'll be able to drive.

Second, he needs you to write a function that, given a distance (in km), a consumption (in l/100 km), and an amount of petrol (in l), will return one of the following: If you can't make the distance without refueling, it should return the message "You will need to refuel". If you can make the distance, the function will check every 100 km and produce an array with [1:kilometers already driven. 2: kilometers till end. 3: remaining amount of petrol] and return all the arrays inside another array ([[after 100km], [after 200km], [after 300km]...])

PLEASE NOTE: any of the values with decimals that you return should be rounded to 2 decimals.

Difficulty: introductory

Starter code:

def total_kilometers(cons, petrol):
	

Solutions:
def total_kilometers(cons, petrol):
    return round(100*petrol/cons, 2)

def check_distance(dist, cons, petrol):
    return ("You will need to refuel" if dist > total_kilometers(cons, petrol) else
            [ [n*100, dist-100*n, round(petrol-cons*n, 2)] for n in range(dist//100+1)])

def total_kilometers(cons, petrol): 
    distance = round(((petrol/cons) * 100), 2)
    return distance


def check_distance(distance, cons, petrol):
    outp = []
    track = 0
    driven_dist = 0
    if distance > ((petrol/cons) * 100):
        return 'You will need to refuel'
    while track <= distance:
        outp.append([driven_dist, distance - driven_dist, round(petrol, 2)])
        driven_dist += 100
        petrol -= cons
        track += 100
    return outp



def total_kilometers(cons, petrol):
    return round(petrol * 100.0 / cons, 2)

def check_distance(distance, cons, petrol):
    if total_kilometers(cons, petrol) < distance:
        return 'You will need to refuel'
    return [[round(x, 2) for x in (i * 100, distance - i * 100, petrol - cons * i)] for i in range(int(distance / 100) + 1)]

def total_kilometers(cons, petrol):
    return round(float(petrol) / cons * 100, 2)

def check_distance(distance, cons, petrol):
    if distance * cons > petrol * 100: return "You will need to refuel"
    return [[i, distance - i, round(petrol - i * cons / 100, 2)] for i in xrange(0, distance + 1, 100)]

def total_kilometers(cons, petrol):
    return round(petrol * 100.0 / cons,2)

def check_distance(distance, cons, petrol):
    return [[(100 * n), distance - (100 * n), round(petrol - (cons * n),2)] for n in range(distance/100 + 1)] if total_kilometers(cons, petrol) >= distance else "You will need to refuel"

def total_kilometers(cons, petrol):
    return round(petrol / float(cons) * 100, 2)


def check_distance(distance, cons, petrol):
    if total_kilometers(cons, petrol) < distance:
        return 'You will need to refuel'
    km_driven = 0
    result = []
    while distance >= 0:
        result.append([km_driven, distance, petrol])
        distance -= 100
        km_driven += 100
        petrol = round(petrol - cons, 2)
    return result


def total_kilometers(cons, petrol):
    result = round(float(petrol) / float(cons) * 100.0, 2)
    print(f'total_kilometers({cons}, {petrol}) => {result}')
    return result

def check_distance(distance, cons, petrol):
    if petrol / cons * 100 < distance:
        return "You will need to refuel"
    
    result = [[0, distance, petrol]]
    
    fuel = petrol
    since_start = 0
    to_end = distance
    for _ in range(to_end // 100):
        fuel -= cons
        since_start += 100
        to_end -= 100
        result.append([since_start, to_end, round(fuel, 2)])
    print(f'check_distance({distance}, {cons}, {petrol}) => {result}')
    return result

def total_kilometers(cons, petrol):
    return round(petrol/cons*100,2)

def check_distance(distance, cons, petrol):
    if total_kilometers(cons, petrol)<distance:
        return "You will need to refuel"
    return[[i*100,distance-i*100,round(petrol-cons*i,2)]for i in range(0,distance//100+1)]

def total_kilometers(cons, petrol):
    return round(100*petrol/cons,2)

def check_distance(distance, cons, petrol):
    if total_kilometers(cons,petrol) < distance: return 'You will need to refuel'
    r = [[0,distance,petrol]]
    x = 0
    while distance >= 100:
        petrol -= cons
        distance -= 100
        x += 100
        r.append([x,distance,round(petrol,2)])
    return r

def total_kilometers(cons, petrol):
    return round((petrol * 100)/ cons, 2)

def check_distance(distance, cons, petrol):
    if distance > (petrol * 100)/ cons:
        return "You will need to refuel"
    else:
        l = []
        length = 0
        for i in range(1 + distance//100):
            l.append([length, distance, round(petrol, 2)])
            length    += 100
            distance  -= 100
            petrol    -= cons
        return l