|
Hi,
no code, just an approach:
if you were to use a ternary number representation (i.e. digits 0, 1, and 2; and digit weights that are powers of 3), then each combination could be written as a number, and incrementing such number would yield the next combination, so you would get 000, 001, 002, 010, 011, 012, 020, 021, 022, 100, ... 222
|
|
|
|
|
But if they have 15 schedules each then it wont be easy to solve it in that way or ??
Plus sometime the persons have different numbers of schedules.
|
|
|
|
|
the same principles apply:
1. if all persons have N schedules, use base=N notation and increment the number;
2. if all persons have different numbers of schedules, use an inhomogeneous base (each digit now has as weight the product of all numbers of schedules to the right of it, somewhat harder to implement)
|
|
|
|
|
using System;
using System.IO;
namespace Schedule
{
class Program
{
static void Main(string[] args)
{
GetThreeSchedules("schedule3.txt", 7, 6, 10);
GetFiveSchedules("schedule5.txt", 2, 3, 4, 1, 6);
}
public static void GetThreeSchedules(string filename, int one, int two, int three)
{
for (int a = 1; a <= one; a++)
{
for (int b = 1; b <= two; b++)
{
for (int c = 1; c <= three; c++)
{
File.AppendAllText(filename, "P1 S" + a + " P2 S" + b + " P3 S" + c + "\r\n");
}
}
}
}
public static void GetFiveSchedules(string filename, int one, int two, int three, int four, int five)
{
for (int a = 1; a <= one; a++)
{
for (int b = 1; b <= two; b++)
{
for (int c = 1; c <= three; c++)
{
for (int d = 1; d <= four; d++)
{
for (int e = 1; e <= five; e++)
{
File.AppendAllText(filename, "P1 S" + a + " P2 S" + b + " P3 S" + c + " P4 S" + d + " P5 S" + e + "\r\n");
}
}
}
}
}
}
}
}
Here is some code. I do not know how to generalize it further, though.
|
|
|
|
|
take this, if you have N persons and they all have a collection of schedules (0 .... N), how would you solve it dynamically so to speak. I mean your example works fine if it is a fixed nr of persons and schedules but thats not the case here.
Person 1 has 3 schedules
Person 2 has 2 schedules
Person 3 has 4 schedules
Person 4 has 3 schedules
etc etc etc up to Person N
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Person person1 = new Person(3);
Person person2 = new Person(2);
Person person3 = new Person(4);
Person person4 = new Person(3);
// code - write to file all unique combinations
}
}
class Person
{
public static int counter = 0;
public int Id;
public List<int> schedules = new List<int>();
public Person(int nrOfSchedules)
{
Id = counter++;
// create schedules for person
for (int i = 0; i < nrOfSchedules ; i++)
{
// think each object in the collection as an schedule id
schedules.Add(i);
}
}
}
}
|
|
|
|
|
I already gave you most of the solution.
the number of combinations N equals the product of the individual number of schedules.
so you need a loop from 0 to N-1
|
|
|
|
|
I understand the solution but i've worked with it for a couple of days now and i can't implement it to code.
|
|
|
|
|
Hi,
something along these lines:
int persons=3;
int[] max=new int[] {4,2,3};
int prod=1;
for (int p=0; p< persons; p++) prod*=max[p];
for (int comb=0; comb< prod; comb++) {
int val=comb;
string s="";
for (int p=persons-1; p>=0; p--) {
int maxp=max[p];
int mod=val%maxp;
s=mod.ToString()+" "+s;
val=val/maxp;
}
Console.WriteLine(s);
}
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
modified on Monday, May 11, 2009 8:57 AM
|
|
|
|
|
Thanks for the example but i cant run the code.
i think something is wrong on you first for-loop.
|
|
|
|
|
HTML monster in action again.
fixed.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Thank you it worked fine
|
|
|
|
|
I needed something similar and came across this[^]. However, at 13 elements or more, it fails due to 13!. I still have not found anything perfect.
|
|
|
|
|
Hi,
Can somebody tell me an algorithm to determine if the set of points form a concave or convex Polygon.I want to code it to use for commercial purpose
Thanks
|
|
|
|
|
Hi,
this should do it:
- take one edge (two consecutive points) of the polygon
- find its equation in the form f(x,y) = a*x + b*y + c = 0
(there is one freedom left, a scale factor in each of the coefficients)
- now feed each of the other points in the f(x,y) function; all the results need to have the same sign for the poly to be convex.
- repeat for each edge.
There may be simpler, I am not aware of it though.
|
|
|
|
|
Luc's suggestions is nearly there, but:
1. You should use the orientation predicate (sign of the 3 point determinant (0,+,-)), instead of the half-plane equation.
2. Instead of testing each edge against each other point (via half-plain which btw shouldn't work), what you do is test the orientation of the next point of polygon against the current edege.
3. if the orientation is the same as the previously calculated orientation, or if the orientation is zero and the point is not in the AABB of the current edge then continue onto next edge, if any of those conditions fail for the current edge assume conconcave polygon and break from loop.
This should have a time complexity of O(n).
|
|
|
|
|
Hi,
I looked into your suggestions and have some comments.
1. The orientation predicate, a simple determinant, seems to compute the same thing I described, in a more abstract but more direct manner. An advantage is there are no divisions involved, hence it is faster and without any danger for overflows and divides by zero.
2. Testing only one point against each edge reduces complexity from O(n^2) to O(n), but IMO it may cause problems for certain shapes, such as this 9-point star[^]
3. yes, collinear points could be present, and then one might be strict on how to interpret the situation. Is a polygon with 4 or more collinear points where some go back and forth along an edge still convex? the convex hull remains the same, so I would be inclined to say yes, although one could argue there are some 180 degree angles involved.
|
|
|
|
|
Thanks
I feel that i can go ahead with the logic/algorithm that u have suggested
|
|
|
|
|
Regarding:
Point 2
That is definitely correct, if that is a case you wish your routine to handle which would be quite common then the simplest solution would be computing the convex hull (better than the half-plane approach) and testing against it. Computing the hull would be O(nlogn) and testing it would be O(n) (find an anchor point then traverse edges, or compute the areas of the polygons - if they match then convex ye be.) which would result in a final complexity of O(nlogn) less than n^2.
Now there is this issue of the difference between simple and non-simple polygons, for example the 9 point star is an example of a non-simple self intersecting polygon, perhaps the OP could have better specified the classes of polygon he was interested in as that would help narrow down the most optimal algorithm he could use for his expected input.
Point 3
These are really edge cases you're mentioning but nonetheless possible. I guess if the general shape of the polygon is convex some may call it convex, but if a strict path-wise approach is taken then it would certainly be concave, I personally would attempt to simplify such polygons as running times of future operations performed upon the polygons such as (point in polygon etc...) will become needlessly long. At the end of the day the requirements of the task matter more than the pedantic/ambiguous nature of the mathematical definitions.
|
|
|
|
|
I was thinking about this this morning. Would an epicycloid be considered convex? If the polygon were composed of line segments whose endpoints lie on the epicycloid, what would you do with the little area that overlapped the bigger area? This is sort of like the center of the 9 point star.
Dave.
|
|
|
|
|
I think assuming the Jordan curve theorem, as long as your "boundary" has distinct inside and outside areas it can be considered a polygon, however in this case the epicycloid would definitely be a non-simple self-intersecting polygon of type concave.
Those are all "simple" cases, now consider a polygon that is essentially concentric circles (aka polygon with holes and islands) would that be considered convex?
|
|
|
|
|
well I have been working on an assigment and it states::
A program has to be developed, and coded in C language, to decipher a document written
in Italian that is encoded using a secret key. The secret key is obtained as random
permutation of all the uppercase letters, lowercase letters, numbers and blank space. As
an example, let us consider the following two strings:
Plain: “ABCDEFGHIJKLMNOPQRSTUVXWYZabcdefghijklmnopqrstuvwxyz0123456789 ”
Code: “BZJ9y0KePWopxYkQlRjhzsaNTFAtM7H6S24fC5mcIgXbnLOq8Uid 3EDv1ruVGw”
The secret key modifies only letters, numbers, and spaces of the original document, while
the remaining characters are left unchanged. The document is stored in a text file whose
length is unknown.
The program has to read the document, find the secret key (which by definition is
unknown; the above table is just an example and it is not the key used for preparing the
sample files available on the web course) using a suitable decoding algorithm, and write
the decoded document to a new text file.
And I know that I have to upload an English dictionary into the program but I don't why it has been asked.(may be not in that statement but I have to dO THAT). My question is , while I can do that program using simple encryption/decryption algorithm then what's the use of uploading the english dictionary in our program? So is there any decryption algorithm that uses a dictionary to decrypt an encrypted file? or can somebody tell me what approach or algorithm should I use to solve that problem???
An early reply (and also authentic one) will be highly appreciated from you.
Thank you guys.
|
|
|
|
|
I don't know why it's wanted in your program, but have a look at this.
http://en.wikipedia.org/wiki/Dictionary_attack[^]
Found by googling 'dictionary attack' - easy isn't it!
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
ETOALIN....
The encryption algorithm is very weak, and you are expected to attack it using letter-frequency analysis. For example, in an substitution cipher for english the most common letter would correspond to E.
I'd assume that they have given you an Italian dictionary text file to use.
I guess they want you to try with the English dictionary to see it not work (or maybe work), so you can discuss it.
|
|
|
|
|
I need an alogithm to calculate all the points through which a bezier curve passes.
|
|
|
|
|
Since it will pass through an infinite number of points, I recommend you use a Turing_machine[^], which can store those points on its infinite tape.
|
|
|
|
|