Distributing values equally inside an array
Moderator: General Moderators
Distributing values equally inside an array
I have an array with 240 elements.
Inside it there are either single elements having a value, either groups of 2, 3, or 4 elements with the same value. Also, there are null values. Equal to zero.
I want to distribute the single elements and the groups equally in the array so there will be the same number of "zeros" between two groups or elements. (Each group or single element holds, as its value, the unique id of a column from a mysql database. So the groups can be identified and distinguished by a variable called $positions. - each member occupies 1, 2, 3, or 4 positions in the 240 elements array.)
An example would be: [1; 1; 1; 0; 0; 0; 0; 0; 5; 5; 0; 0; 0; 0; 3; 3; 0; 0; 0; 0; 0; 0; 0; 0; 0; 16; 0; 0; 0; ... and so on, till 240.] This should be sorted. I mean distributed somehow. How exactly, I, personally, do not know. I have tried some complicated algorithms with 1 billion FOR loops and didn't work. Please help. I'm quite desperate.
In the end, all of this should be exported to a text file in a different format. But I can handle that.
If anyone could help, I would really appreciate and love him/her for the rest of my cursed life.
Dan.
Inside it there are either single elements having a value, either groups of 2, 3, or 4 elements with the same value. Also, there are null values. Equal to zero.
I want to distribute the single elements and the groups equally in the array so there will be the same number of "zeros" between two groups or elements. (Each group or single element holds, as its value, the unique id of a column from a mysql database. So the groups can be identified and distinguished by a variable called $positions. - each member occupies 1, 2, 3, or 4 positions in the 240 elements array.)
An example would be: [1; 1; 1; 0; 0; 0; 0; 0; 5; 5; 0; 0; 0; 0; 3; 3; 0; 0; 0; 0; 0; 0; 0; 0; 0; 16; 0; 0; 0; ... and so on, till 240.] This should be sorted. I mean distributed somehow. How exactly, I, personally, do not know. I have tried some complicated algorithms with 1 billion FOR loops and didn't work. Please help. I'm quite desperate.
In the end, all of this should be exported to a text file in a different format. But I can handle that.
If anyone could help, I would really appreciate and love him/her for the rest of my cursed life.
Dan.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
What on earth is this supposed to be for?
Also your descriptions are very confusing because you are talking about groups of things (multiple dimensions) but your examples show nothing of the sort.
Also your descriptions are very confusing because you are talking about groups of things (multiple dimensions) but your examples show nothing of the sort.
Last edited by Ollie Saunders on Tue Dec 05, 2006 7:21 am, edited 1 time in total.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Code: Select all
00[1:::]
01[:::2]
02[::::]
03[:3::]
04[::4:]
05[::::]
06[:5::]
07[:::14]
08[::::]
09[:1::]
10[::::]
11[2:::]
12[::3:]
13[:::4]
14[::::]
15[::5:]
16[::::]
17[14:::]
18[::1:]
19[::::]
20[:2::]
21[:::3]
22[::::]
23[4:::]
24[:::5]
25[::::]
26[:14::]
27[:::1]
28[::::]
29[::2:]
30[::::]
31[3:::]
32[:4::]
33[::::]
34[5:::]
35[::14:]
36[::::]
37[1:::]
38[:::3]
39[::::]
40[4:::]
41[:::5]
42[::::]
43[:14::]
44[:::1]
45[::::]
46[::3:]
47[:::4]
48[::::]
49[::14:]
50[::::]
51[3:::]
52[:14::]
53[:::3]
54[:::4]
55[::::]
56[::14:]
57[::::]
58[3:::]
59[:14::]- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
its quite complicated. each video file has a minimum length of 15 seconds and a maximum of 60.
so there are 4 maximum "positions" in each minute. the colon separates 4 positions
if there are empty spaces between colons it means that in that minute there is only one video file played either because there are few video files to be distributed in one hour or because the video file has 4 positions(46->60 sec) and there are no other files that could go in that minute.
I repeat, this is the desired result
) I still didn't find it.
so there are 4 maximum "positions" in each minute. the colon separates 4 positions
if there are empty spaces between colons it means that in that minute there is only one video file played either because there are few video files to be distributed in one hour or because the video file has 4 positions(46->60 sec) and there are no other files that could go in that minute.
I repeat, this is the desired result
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Right OK, beginning to make sense now.
For this data (i've changed the filenames to letters to avoid confusion):You will probably want to use an array structure like this:
For this data (i've changed the filenames to letters to avoid confusion):
Code: Select all
00[a:::]
01[:::b]
02[::::]
03[:c::]Code: Select all
$minutes = array(
array(0 => 'a'),
array(3 => 'b'),
array(),
array(1 => 'c')
);that's not really how I did it. I just used one big array with 240 elements, each representing one "position"
initially, all elements are equal to zero, and i made an algorithm that fills the array with (plays per hour) times the filename(numeric) of the video. And then i extract from there.. and export it to .txt.
But now i have a problem... there are times when this happens:
the problem here is that if video 3 has 3 positions and video 4 lets say 2, its a total of 5 and the mplayer will stop playing the fifth part of the video and pass to the next minute. And that's bad. I have to check somewhere if the sum is larger than 4. And do something. I dont know exactly what. I guess move the file further
initially, all elements are equal to zero, and i made an algorithm that fills the array with (plays per hour) times the filename(numeric) of the video. And then i extract from there.. and export it to .txt.
But now i have a problem... there are times when this happens:
Code: Select all
00[3::4:]
01[::::]
02[:6::]
03
04
and so on...- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Degro, send me some files so I can understand what you mean: klinger.ofir@gmail.com