Combination without repetition in Mathematica - wolfram-mathematica

How could I obtain 48 random pairs of two different elements drawn from Range[96] with no repetition ? That is the 96 elements are only used once.
While I tried Tuples/Subsets combined with Select, I feel there must be a more straightforward way to achieve this.

Please tell me if this is correct:
Partition[RandomSample#Range#96, 2]

Partition[RandomSample[Range[96]],2]

Related

how to find the elements from an array which are not in another array using golang?

i have two array A and B ,A["anusha","kirthy","reema"],B["anusha","raju","sudha"].i need to get the elements of an array A which are not in array B .result will be ["kirthy","reema"] in golang .please help me
Here's a Playground solving your problem (Quick and Dirty, there may be better solutions out there, but this one works.): https://play.golang.org/p/7vTXLtaGNh
This Problem sounds like a simple homework, with a bit tinkering you could've solved it yourself i guess.

Hashes generated by Rabin Karp Rolling Hash not reflecting on the Text

Note: Lots of Possible duplicates, but nothing seems to be solving my problem.
I am working on a Plagiarism detection based on MOSS.
After successfully implementing a filter which strips out all the necessary details(comments,punctuations etc) I hash the content using a Rolling Hash Implementation(Rabin Karp)
However the Hashes which match in two text-files of source code, have very different underlying text(No plagiarism and yet same hashes)
The Algorithm I implemented(Ruby) -->
(Partial Snippet)
#Preprocessing from RobinKarp Algorithm
for c in 0...k do
text_hash=(radix*text_hash+text_to_process[c].ord)%q
end
#Main loop
for c in 0...loop do
text_hash=((radix*text_hash-(text_to_process[c].ord)*highorder)+(text_hash[c+k].ord))%q
Is there an issue with my implementation? Or the Parameters I specify can be at fault?
I take radix=34
( I am not sure if it is the right value, I am assuming the stripped out text will only contain alphabets+some special charcters like '+','-','*','/' so a rough estimate of total 34 characters)
I am taking q(prime) to be 101
Is this a collision issue I am dealing with? Any pointers as to how to tackle the problem?
I note that with q = 101, there are only 101 possible hash values - 0, 1, 2...100. Have you tried increasing q? Another approach would be to look and see if the hash values look like they are randomly chosen values within the possible values of 0,1..q-1.
You should of course also test your program on cases where there are repeated strings for it to find - a failure there could be another symptom of any problem that is also causing collisions, and it would be easier to find and debug.

Constrained Random Number Generation

I need to generate 500 numbers, 250 1s and 250 0s, randomly located. Below is what I do now. But it does not feel right while the output is correct.
trialNo=500
RandomSample#Flatten[Table[#, {trialNo/2}] & /# {0, 1}]
I'd actually do something slightly different. Since you're looking for a random permutation of Flatten[{ConstantArray[0,250], ConstantArray[1,250]}], I'd generate the permutation and use Part to get the list you're looking for. As follows,
perm = RandomSample[Range[trialNo]];
Flatten[{ConstantArray[0, trialNo/2], ConstantArray[1, trialNo/2]}][[ perm ]]
This isn't operationally different than what you're doing, but I think it captures mathematically what your trying to accomplish better.
Here is another way to do this.
Round[Ordering[1~RandomReal~#] / N##]& # 500
Now with more magic for the guys in Chat.
Mod[RandomSample#Range##, 2] & # 500

How to prepend a column to a matrix?

Ok, imagine I have this Matrix: {{1,2},{2,3}}, and I'd rather have {{4,1,2},{5,2,3}}. That is, I prepended a column to the matrix. Is there an easy way to do it?
My best proposal is this:
PrependColumn[vector_List, matrix_List] :=
Outer[Prepend[#1, #2] &, matrix, vector, 1]
But it obfuscates the code and constantly requires loading more and more code. Isn't this built in somehow?
Since ArrayFlatten was introduced in Mathematica 6 the least obfuscated solution must be
matrix = {{1, 2}, {2, 3}}
vector = {{4}, {5}}
ArrayFlatten#{{vector, matrix}}
A nice trick is that replacing any matrix block with 0 gives you a zero block of the right size.
I believe the most common way is to transpose, prepend, and transpose again:
PrependColumn[vector_List, matrix_List] :=
Transpose[Prepend[Transpose[matrix], vector]]
I think the least obscure is the following way of doing this is:
PrependColumn[vector_List, matrix_List] := MapThread[Prepend, {matrix, vector}];
In general, MapThread is the function that you'll use most often for tasks like this one (I use it all the time when adding labels to arrays before formating them nicely with Grid), and it can make things a lot clearer and more concise to use Prepend instead of the equivalent Prepend[#1, #2]&.
THE... ABSOLUTELY.. BY FAR... FASTEST method to append or prepend a column from my tests of various methods on array RandomReal[100,{10^8,5}] (kids, don't try this at home... if your machine isn't built for speed and memory, operations on an array this size are guaranteed to hang your computer)
...is this: Append[tmp\[Transpose], Range#Length#tmp]\[Transpose].
Replace Append with Prepend at will.
The next fastest thing is this: Table[tmp[[n]]~Join~{n}, {n, Length#tmp}] - almost twice as slow.

Connecting points in Mathematica

I have a collection of points displayed in a graphic:
alt text http://img69.imageshack.us/img69/874/plc1k1lrqynuyshgrdegvfy.jpg
I'd like to know if there is any command that will connect them automatically along the xx and yy axis. This can be better understood looking at the following picture:
alt text http://img341.imageshack.us/img341/5926/tr53exnkpeofcuiw40koyks.jpg
(I am not asking how to implement the algorithm myself!).
Thanks
I suspect the answer is no, there's no such command. It would be interesting to write something to do that though, ie, given a list of points, output the corresponding lines. I guess that would just be a matter of:
For each unique x-coordinate get the list of y-coordinates for points with that x-coordinate and make a line from the min to the max y-coordinate. Then repeat for the y-coordinates.
If you do that, it would be interesting to post it here as a follow-up. Or if you want to make that the question, I'm sure you'll get some nice solutions.
I vote for dreeves' suggestion. It doesn't use a "built-in" function, but it's a one-liner using functional programming and level specifications. An implementation is:
gridify[pts : {{_?NumericQ, _?NumericQ} ...}] :=
Map[Line, GatherBy[pts, #]& /# {First, Last}, {2}]
Some of what you are looking for is in the ComputationalGeometry Package. In particular, ConvexHull will give you the outer points listed in counterclockwise direction. At which point you can use Line to connect them together. The inner paths are a bit trickier, and I don't think there is an exact match. But, a DelaunayTriangulation comes closest. It essentially breaks your list of points up into sets of triangles. I don't know of a built in function that would break it into rectangles, though.

Resources