473,414 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

A problem to get your teeth stuck into..

Given a set of intervals {i1, i2, i3, ...} a list is produced; the base
items (0) are placed one at a time into the stream and after the ix one
a interval item (1, 2, 3, etc) of the correct type is created. When
intervals intersect the order is kept (most frequent first).

For example, given the intervals {2, 5, 10} I produce a stream of
objects as follows;

0 0 1 0 0 1 0 2 0 1 0 0 1 0 0 1 2 3 0 0 1 0 0 1 0 2 0 1...

So the object at index 0 in the list is of type 0, the object at index
2 in the list is of type 1, the object at index 16 in the list is of
type 3.

The assumption can be made that intervals specified in the supplied
list are ordered most frequent first - although the same interval could
appear twice. There could be any number of intervals in the list.

Can anyone write an algorithm to solve the following,

Given the list index and the set of intervals return the object
expected at that position?

In the above example, I'd expect;

calcExpectedValue(0, {2, 5, 10}) to return 0.
calcExpectedValue(2, {2, 5, 10}) to return 1.
calcExpectedValue(16, {2, 5, 10}) to return 3.

I'm thinking it should be fairly simple but I've not been able to come
up with an elegant solution? I've obviously abstracted the problem but
it does have a bearing on work I am doing (it's to check the results of
a test case are in expected order).

Nov 17 '05 #1
3 1569
mdb
ge*****************@yahoo.co.uk wrote in news:1120217133.260792.306840
@g49g2000cwa.googlegroups.com:
Given a set of intervals {i1, i2, i3, ...} a list is produced; the base
items (0) are placed one at a time into the stream and after the ix one
a interval item (1, 2, 3, etc) of the correct type is created. When
intervals intersect the order is kept (most frequent first).

For example, given the intervals {2, 5, 10} I produce a stream of
objects as follows;

0 0 1 0 0 1 0 2 0 1 0 0 1 0 0 1 2 3 0 0 1 0 0 1 0 2 0 1...

So the object at index 0 in the list is of type 0, the object at index
2 in the list is of type 1, the object at index 16 in the list is of
type 3.

The assumption can be made that intervals specified in the supplied
list are ordered most frequent first - although the same interval could
appear twice. There could be any number of intervals in the list.

Can anyone write an algorithm to solve the following,

Given the list index and the set of intervals return the object
expected at that position?

In the above example, I'd expect;

calcExpectedValue(0, {2, 5, 10}) to return 0.
calcExpectedValue(2, {2, 5, 10}) to return 1.
calcExpectedValue(16, {2, 5, 10}) to return 3.

I'm thinking it should be fairly simple but I've not been able to come
up with an elegant solution? I've obviously abstracted the problem but
it does have a bearing on work I am doing (it's to check the results of
a test case are in expected order).


First of all, you have a small error in your stated outputs...

calcExpectedValue(16, {2, 5, 10}) should be 2, not 3... while
calcExpectedValue(17, {2, 5, 10}) IS 3.

Anyway...

There are two things to realize here...

1) Since you are inserting data, you have an "offset" problem where you
can't represent the output based on a F(p) that doesn't involve sums.
This means you can't use a one-line expression. :) It would help if you
think of the output as a "zero at every position" with the other objects
being inserted "between" posistions. Consider the table below... the
numbers across the top are "nominal" positions (not the actual position
in the stream), and the numbers down the left represent the index of the
object being inserted in the stream. (Index '0' is the '0' object).

0 1 2 3 4 5 6 7 8 9
0 x x x x x x x x x x
1 x x x x x
2 x x
3 x

In this case, there are 18 outputs. From this point on we will assume
the object outputs are included with the "previous" nominal position.
You can see that there is now a regular and predictable pattern, but the
position isn't the position in the stream so we still have a problem...
BUT this shows that...

2) The type of thing you are doing will result in a repeating pattern
after LCM(2, 5, 10) zero's have been output and all subsequent values
have been output. In other words, after the 18th value (real zero-
indexed position 17 - the last output), there will have 10 0's output (10
is the LCM of 2,5,10), and all of the objects have been output. After
this point, the pattern will repeat, so we never have to work with any
position in the stream > LCM. If p > NumOutputs, we simply scale p back
to the position in the pattern: P = p % NumOutputs. (p is the actual
file position you are asking about, while P is the real pattern
position).

The question now is how do we figure out the output at any real position
in the stream pattern. Lets work backwards...

At nominal position N, there have been:
Object 0: N + 1
Object 1: (N+1) / 2 (integer division)
Object 2: (N+1) / 5 (integer division)
Object 3: (N+1) / 10 (integer division)

Thus, AFTER outputs have been written for nominal position N (including
the objects), the file position P is...

P = N + 1 + (N+1)/2 + (N+1)/5 + (N+1)/10 (all integer division)

We would like to solve for N, but because we are using integer division,
we can't do this like normal. But we don't really have to because we are
going to write an algorithm, not a formula. Unfortunately I can't pin
down any easy way to do this except follow the mechanics of what I've
already described.

So as long as I've convinced you of the repeating pattern aspect, it
should be easy now... The way I would do it is:

Build a chart like the one above...
a) Calculate the number of nominal positions - this is the LCM of each
of your intervals, and create an array (or arraylist) with this number of
elements
b) The contents of each element will be another arrayList, the
contents of which will be object 0 followed by the objects that should be
output at that nominal position

Then to figure out the output at position p, count the elements until you
get to position p and return that element.

So for example:

using System;
using System.Collections;

namespace Intervals
{
public class MyClass
{
static int[] Intervals = new int[] { 2, 5, 10 };
static ArrayList[] NomPs = null;
static int NumOutputs = 0;
public static void Main()
{

// Build the chart
int lcm = LCM(Intervals);
NomPs = new ArrayList[lcm];

// Populate the chart
for(int i=0; i<lcm; i++)
{
Console.Write(i.ToString() + " ");
NomPs[i] = new ArrayList();
NomPs[i].Add(0);
NumOutputs++;
Console.Write("0 ");
for(int j=0; j<Intervals.Length; j++)
{
if ((i+1) % Intervals[j] == 0)
{
Console.Write(Intervals[j].ToString() +
" ");
NomPs[i].Add(Intervals[j]);
NumOutputs++;
}
}
Console.WriteLine(string.Empty);
}

for(int i=0; i<NomPs.Length; i++)
{
for(int j=0; j<NomPs[i].Count; j++)
{
Console.Write(NomPs[i][j] + " ");
}
}
Console.WriteLine();
Console.WriteLine("TEST:");
for(int i=0; i<18; i++)
{
Console.Write(GetObjAt(i).ToString() + " ");
}
Console.WriteLine();
for(int i=18; i<36; i++)
{
Console.Write(GetObjAt(i).ToString() + " ");
}
Console.WriteLine();

Console.ReadLine();
}

private static int GetObjAt(int p)
{
p = p % NumOutputs;
for(int i=0; i<NomPs.Length; i++)
{
if (p >= NomPs[i].Count) p -= NomPs[i].Count;
else return (int)NomPs[i][p];
}
return -1;
}

private static int LCM(int[] vals)
{
int lcm = 1;
for(int i=0; i<vals.Length-1; i++)
{
lcm = LCM(lcm, vals[i]);
}
return lcm;
}

private static int LCM(int x, int y)
{
return (x * y) / GCD(x, y);
}

private static int GCD(int x, int y)
{
do
{
int n1 = Math.Max(x, y);
int n2 = Math.Min(x, y);
n1 = n1 % n2;
if (n1 == 0) return n2;
x = n1;
y = n2;
} while (true);
}
}
}

--
-mdb
Nov 17 '05 #2
mdb
mdb <m_b_r_a_y@c_t_i_u_s_a__d0t__com> wrote in
news:Xn****************************@207.46.248.16:
private static int LCM(int[] vals)
{
int lcm = 1;
for(int i=0; i<vals.Length-1; i++)
{
lcm = LCM(lcm, vals[i]);
}
return lcm;
}


Ooops I had a little bug... this function should be:

private static int LCM(int[] vals)
{
int lcm = 1;
for(int i=0; i<vals.Length; i++)
{
lcm = LCM(lcm, vals[i]);
}
return lcm;
}

I didn't catch it because LCM(2,5) == LCM(2,5,10). Sorry!

--
-mdb
Nov 17 '05 #3
Many thanks for taking the time to do that, I hadn't expected such a
complete answer.
calcExpectedValue(16, {2, 5, 10}) should be 2, not 3... while
calcExpectedValue(17, {2, 5, 10}) IS 3.


You're correct - I took that from my working notes rather than program
output and I can't read my own writing.

The way you've done it is in fact the same way I'd solved it; populate
an array with the group that repeats, mod the required index by the
length of that array and then use the result to get an index in to the
array to get the repeated element.

Seeing as you've come to a similar solution - makes me feel happier
with doing it this way.

Again - many thanks!

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ron | last post by:
have been stuck on this for several days now. I am trying to create a reverse polish calculator and I'm stuck at an intermediate stage. This is what I know I have to do (just not sure how to do it...
5
by: Stuart Palmer | last post by:
Hi, I am trying to get cdo.message working on my home development machine, however, when I send it it appears to get stuck in the queue and never actually gets sent. If I don't have a domain...
0
by: Inspired | last post by:
hi guys.. im writing an application that uses windows service to listen (as a tcplistener) for any data sent (by a tcpclient) ... one kind of the requests might be a file sent by client and the...
2
by: Mike Curry | last post by:
I have run into a problem, I have 2 fields in my database, both key fields: Table 1 ===== Field X <key> Field Y <key> In field X, there are say about 3 records for each unique Field Y. I...
1
by: colleen1980 | last post by:
When I run my query it stuck is it possible that i can debug it SELECT TOP 1 tblCredits.OPR, Min(tbl_CRCDollars.SumOfAMOUNT) AS MinOfSumOfAMOUNT, Avg(tblCredits.Credit) AS AvgOfCredit,...
0
by: skip | last post by:
After much wailing and gnashing of teeth the past couple of days, I managed to move (most of?) the content from the MacPython wiki to the main Python wiki (*). All pages were created as subpages...
11
by: dave6502 | last post by:
Newbe C++ programmer. I am trying to bug fix a simple KDE application that uses C++. There is a main directory with .cpp & .h files in it. I need to write some additional code and split it up...
4
by: billb | last post by:
I installed a perl extension for PHP to use some perl inside my php primarily because I have perl working with oracle and not php and oracle. So I want to use my old perl scripts, and use the...
103
by: Tom | last post by:
How do we get out of the browser infinite loop quicksand when we navigate to web pages designed to lock us in and force us to hit the "pay me" button (whatever they want to force you to do)? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.