## python – Divide by bins with pandas

I have a database in pandas that looks like below. The index is a date-time object, categorized by day, divided into 5-minute slices. I have a column called "col1". So if I do it

``````df['col1']
``````

I receive:

``````Date and hour
2008-04-28 09:40:00 300.0
2008-04-28 09:45:00 -800.0
2008-04-28 09:50:00 0.0
2008-04-28 09:55:00 -100.0
2008-04-28 10:00:00 0.0
2008-04-29 09:40:00 500.0
2008-04-29 09:45:00 800.0
2008-04-29 09:50:00 100.0
2008-04-29 09:55:00 -100.0
2008-04-29 10:00:00 0.0
``````

I have another database in pandas obtained using groupby 5 minutes in the original database that generates:

``````                                            col2
09:40:00 4603.585657
09:45:00 5547.011952
09:50:00 8532.007952
09:55:00 6175.298805
10:00:00 4236.055777
``````

What I would like to do is split col1 by col2 for each of the 5-minute bins without using a for loop. To better explain, for every day, for each bin, divide col1 by col2. For example, divide all values ​​9:40:00 in col1 by 9:40:00 in col2.

I do not know how to start doing this without a loop, but I have the impression that it should be doable with pandas.

## ACT NW 2018 5, 1: find the least amount of bins needed to hold all items

This is a problem of computer programming competition from last year. The problems were sent home with the competitors, so I would consider them "released."

The problem is: "You want to choose the minimum number of shelves for all your book collections, so that each book collection is not divided between each shelf." Each shelf is at the same price, and the collections and the shelves can have different sizes. "To emphasize, the books can not be divided on separate shelves if they are in the same collection. There may be several collections on the same shelf.

Input example (first line: size of each book shelf, second line: size of each book collection):

150 150 300 150 150

70 76 99 75 105

Expected results (number of shelves needed):

2

This was given at a beginner level competition. So I suppose that they want the competitors to make some sort of brute-force solution to that, like recursing through all the different possible selections (and then another recursion to understand how to put the books on the shelves properly) up to a minimal solution is found. Is there another algorithm that can be used for that which is not brute force?

## links bins of different languages

Hello. I manage a site in English, but I also have some link links that come from sites in Russian (Russian) but with a relevant niche. Is it good or bad?

## probability – Bins and Balls – pick up balls

I asked myself questions about this question for a long time, but I was not able to find a good answer.

let $$k , n$$ to be two positive integers such as $$k the n$$, we throw $$n$$ balls in $$2n$$ (in a uniform and independent way), we buy as few bins as possible, so that the total number of bales in the bins purchased is at least equal to $$k$$, let $$A$$ the number of bins purchased

what is the relationship between $$k$$ and $$mathbb E[A]$$ (Asymptotically)?

it seems that for $$k = n$$ we will need $$(1- frac {1} { sqrt e}) n$$ bins (the number of non-empty bins) giving us $$mathbb {E}[A]= Theta (k)$$

but for $$k = frac { log n} {2 log log }$$ we will need only one bin w.h.p (maximum load in the bins and bins model), so $$mathbb {E}[A]= Theta ( frac {k log log n} {; log n})$$

I am more interested in $$k le sqrt n$$and when does the transition begin? $$mathbb {E}[A]= O (k)$$ at $$mathbb {E}[A]= Theta (k)$$

## algorithms – Distribute repeated values ​​in bins as evenly as possible

Preface

I've already asked a very similar question about stack overflow in a different wording and got a satisfying answer assuming it's impossible to go through all the possibilities (np-hard).

After further research (because this is my bottleneck), I stumbled upon this similar question, but again, it is assumed that the problem is not working and that it is not exactly what I research. However, it seems that the question is better suited to this site than SO (correct me if I'm wrong).

I am pretty sure that this can be solved in "brute force" (optimal) with the right algorithm in a short time for the given constraints. More on that later.

Problem

I have a small set of repeated values, for example.

``````values ​​= 10 x [18] (ten times the value 18); 5 x [30], 6 x [55]
``````

which I need to distribute as much as possible evenly on a fixed number of bins with a maximum difference between bins (maximum difference between the sum of the elements in the bins, see below).

constraints:

The reason I think this can be brutally forced are my constraints. I will never have a large number of bins (for the moment, the maximum is 3, suppose it is 4 or 5 if). The maximum difference between the two tanks is 2 for atm. We can deduce that it stays there if it helps, because I can adjust this parameter arbitrarily if necessary.

Example

For a small example, let's say `values ​​= [[18,18,18,18,18],[30,30,30]]`, `number_of_bins = 2` and `max_difference_between_bins = 2`.

The algorithm to distribute them would be something of the kind

``````for the rest in the range (0.8) #try distributing all the packets first,
# then one less, then two less, etc.
generate possible distributions for the values ​​in the bins
add distributions to check for differences, if any
# is below the threshold of max max_difference_between_bins, break
``````

L & # 39; try:

Rest = 0

Divide 5 times 18 in two bins:

``````(0.90), (18.72), (36.36), (72.18), (90.0)
``````

Divide 3 times 30 in two bins:

``````(0.90), (30.60), (60.30), (90.0)
``````

See that the example is bad because it works in the first run, in any case, adding to them returns:

``````(0.90) -> (0.180), (30.150), (60.120), (90.90)
(18.72) -> (18.162), (48.132), (78.102) (108.72)
(36,36) -> (36,126), (66,96), (96,66) (126,36)
(72.18) -> (72.108), (102.78), (132.48), (162.18)
(90.0) -> (90.90), (120.60), (150.30), (180.0)
``````

As you can see, half of them are obsolete because they are duplicates.

If there was no solution (90,90), it would continue at the next execution by trying the same thing for `values ​​= [[18,18,18,18],[30,30,30]]` and `values ​​= [[18,18,18,18,18],[30,30]]` and `rest = [18]` or `rest = [30]`.

Runtime

So, for a very big example of 70 values ​​with 5 different sizes in 3 bins, the possible solutions should be for the first time:

Let's say that 50 values ​​of the same size are divided into 3 trays, then we get 50 + 3-1 out of 3 = 1326 possibilities or 316251 for 5 trays (no?) (Assuming that the trays are differentiable, which is not the case. is not necessary, but then it would be necessary to increase the size later for the combination with the other values).

Distribute the other 20 with, say, 8, 5, 4, 3 values ​​each worth 45, 21, 15, 10 values. Multiplied, that's 190 million possibilities. However, most of these combinations can not be a good solution. Some in-depth research or division and conquest should be able to break them down.

Of course, these are then multiplied by 5 for the first pass with a remainder of 1, 120 for a passage with a remainder of 2, and so on. But these solutions can also be generated from old solutions and again, it should be possible to go through a fraction of them.

Question

Basically, how to find a solution to this problem in a short time? Pseudocode or even python would be great, but not necessary, I can understand the coding myself.

We can make many assumptions about the number of different values ​​and the maximum number of values ​​you want. So if you plan to reduce the number of solutions by 190 million times, whatever the chosen solution, for example: maximum 3 boxes, maximum 3 different values ​​occurring at most, for example, 20 times (not 20 times, 20 times, but 10 times, 5 times, 5 times).

## Theory of complexity – Transforming the problem of packaging bins into a problem of weight distribution

I am working on a problem and I need help to go over the details and move it forward.

P1:

There is $$n$$ items with weights $$w1, w2, .., wn$$ and $$K$$ people ($$p1, p2, .., pk)$$ must wear these items. We must spread these weights as evenly as possible.

Let $$Total (ft)$$ to designate the total weight assigned to the person $$pi$$.

I wrote this problem as:

Problem of optimization: $$minimize (max (T (pi)) – min (T (pj)))$$

Decision problem:
Given the number M, is it possible to find an assignment such that
$$max (T (pi)) – min (T (pj)) <= M$$

Now, I have to turn the decision version of Bin Packing Problem into a decision version of this problem. I have a hard time doing it because P1 does not have a concept of tray capacity. What would be a rough idea of ​​this transformation?
Thank you!