We want to ship bars of aluminum. We will create a function that accepts an integer representing the requested kilograms of aluminum for the package to be shipped. To fulfill these orders, we have small bars (1 kilogram each) and big bars (5 kilograms each). Return the least number of bars needed.

868    Asked by Nainapandey in Data Science , Asked on Jan 7, 2020
Answered by Naina pandey

Let us find the number of bars by creating a function bar_count

bar_count <- function(pack){

    amount_of_ones = pack %% 5

    amount_of_fives = (pack - amount_of_ones)/5

    return(amount_of_ones+amount_of_fives)

}

Let us check how many bars required for 6kg load

bar_count(6)

Output:

[1] 2 # meaning 2 bars are required for 6 kg load.



Your Answer

Interviews

Parent Categories