rakesh
rakesh

Reputation: 123

Arrange the given values from a dataframe with followed rules - pandas

I have a fruit count dataframe.From the dataframe i have total count as 7, these fruit will distributed for 7 days each fruit per day.

fruit  count   
apple  3  
orange 1  
lemon  1  
cherry 1  
banana 1

Rules for splitting the fruit:
1.Apple fruit should not sent on consecutive days,Maintain a gap of at least a day between the apples.
2.orange has to be sent on day7(i.e last day).
3.On day 6 the lemon should not be sent, Maintain a gap of at least a day between orange and lemon
4.Do not accomodate any other fruits with lemon and orange,If fruit count is more than 7 then accomodate with apple or cherry or banana.

Expected_ouput

day fruit
d1   apple
d2   cherry
d3   apple
d4   lemon
d5   apple
d6   banana
d7   orange

case 1 - Less than 7 fruits

fruit  count   
apple  1  
orange 1  
lemon  1  
cherry 1  
banana 1

output expected:

day fruit
d1   apple
d2   cherry
d4   lemon
d6   banana
d7   orange

case 2 - more than 7 fruits:

fruit  count  
apple  3  
orange 1
pomerante 1  
lemon  1  
cherry 1  
banana 1 

Expected_ouput

day fruit
d1   apple
d2   cherry
d3   apple
d3   pomegranate
d4   lemon
d5   apple
d6   banana
d7   orange

I have tried:

apple = 3
orange = 1
week = 7
start_day=1
end_day = 8
days = [f"Day{i}" for i in range(start_day,end_day)]
profile = {key : [] for key in days}
profile["Day7"].append("orange")
c = 6 // apple

apple_day = 1
for i in range(apple):
    profile["Day{}".format(apple_day)].append("apple")
    apple_day += c

Output:

{'Day1': ['apple'],
 'Day2': [],
 'Day3': ['apple'],
 'Day4': [],
 'Day5': ['apple'],
 'Day6': [],
 'Day7': ['orange']}

Upvotes: 2

Views: 188

Answers (1)

Vojtech Stas
Vojtech Stas

Reputation: 741

This is my state of the art solution, hopefully you'll accept this answer.

It works with any extreme example, that has solution:

basket_of_fruits = ['apple','apple','apple','cherry','cherry','banana','banana','orange','lemon','kiwi','kiwi']
n_days = 7
# Create day plan
plan = {}
for i in range(n_days):
    plan['Day_'+str(i+1)] = []
# Set startiong values
apple_day = n_days % 2 +1
occupy = 1
days_occupied = []
# solve 1 orange
if 'orange' in basket_of_fruits:
    plan['Day_'+str(n_days)].append('orange')
    basket_of_fruits.remove('orange')
    days_occupied.append(n_days)
# solve 1 lemon
if 'lemon' in basket_of_fruits:
    plan['Day_'+str(n_days-2)].append('lemon')
    basket_of_fruits.remove('lemon')
    days_occupied.append(n_days-2)
# solve other fruit then apple, cherry or banana
basket_of_fruits_rest = basket_of_fruits.copy()
while 'apple' in basket_of_fruits_rest:
    basket_of_fruits_rest.remove('apple')
while 'cherry' in basket_of_fruits_rest:
    basket_of_fruits_rest.remove('cherry')
while 'banana' in basket_of_fruits_rest:
    basket_of_fruits_rest.remove('banana')
for i in range(len(basket_of_fruits_rest)):
    while occupy in days_occupied:
        occupy += 1
        if occupy > n_days:
            days_occupied = [n_days,n_days-2]
            occupy -= n_days
    selected_fruit = basket_of_fruits_rest[i]
    plan['Day_'+str(occupy)].append(selected_fruit)
    basket_of_fruits.remove(selected_fruit)
    days_occupied.append(occupy)
    occupy += 1

# solve cherry or banana
basket_of_fruits_rest = basket_of_fruits.copy()
while 'apple' in basket_of_fruits_rest:
    basket_of_fruits_rest.remove('apple')
for i in range(len(basket_of_fruits_rest)):
    while occupy in days_occupied:
        occupy += 1
        if occupy > n_days:
            days_occupied = [n_days,n_days-2]
            occupy -= n_days
    selected_fruit = basket_of_fruits_rest[i]
    plan['Day_'+str(occupy)].append(selected_fruit)
    basket_of_fruits.remove(selected_fruit)
    days_occupied.append(occupy)
    occupy += 1
    
# solve apples
while 'apple' in basket_of_fruits:
    while ('lemon' in plan['Day_'+str(apple_day)]) | ('orange' in plan['Day_'+str(apple_day)]) :
        apple_day += 2
        if apple_day > n_days:
            apple_day -= n_days-1
    
        
    plan['Day_'+str(apple_day)].append('apple')
    basket_of_fruits.remove('apple')
    apple_day += 2
    if apple_day > n_days:
        apple_day -= n_days-1

returns:

{'Day_1': ['kiwi', 'banana'],
 'Day_2': ['kiwi', 'apple'],
 'Day_3': ['cherry'],
 'Day_4': ['cherry', 'apple'],
 'Day_5': ['lemon'],
 'Day_6': ['banana', 'apple'],
 'Day_7': ['orange']}

Upvotes: 0

Related Questions