algorithm

XOR swap algorithm in python?

I tried to implement XOR swap in python. x,y= 10,20 x,y,x = x^y,x^y,x^y print('%s , %s'%(x,y)) OUTPUT: 30 , 30 I am not new to python but I am unable to explain this output. It should have been 20,10. What is going on under the hood? ...

Algorithm for 2D Interpolation

I have two shapes which are cross sections of a channel. I want to calculate the cross section of an intermediate point between the two defined points. What's the simplest (relatively simple?) algorithm to use in this situation? P.S.: I came across several algorithms like natural neighbor and poisson, which seemed complex. I'm looking f...

Finding the index of a given value in a pre-sorted array

Today, I went for an interview and the interviewer asked me how I would find the index of a given value (number) in a pre-sorted array like this: $preSortedArr=array(23,32,36,41,45,54); He also said that using recursion is not allowed. I think the function should look like this: function findIndexByValue($preSortedArray,$value){ ...

Data Structures

There is a large stream of numbers coming in such as 5 6 7 2 3 1 2 3 .. What kind of data structure is suitable for this problem given the constraints that elements must be inserted in descending order and duplicates should be eliminated. I am not looking for any code just ideas? I was thinking of a Self-Balancing BST where we could ad...

Using base64 encoding as a mechanism to detect changes

Is it possible to detect changes in the base64 encoding of an object to detect the degree of changes in the object. Suppose I send a document attachment to several users and each makes changes to it and emails back to me, can I use the string distance between original base64 and the received base64s to detect which version has the most...

What's a good way to add a large number of small floats together?

Say you have 100000000 32-bit floating point values in an array, and each of these floats has a value between 0.0 and 1.0. If you tried to sum them all up like this result = 0.0; for (i = 0; i < 100000000; i++) { result += array[i]; } you'd run into problems as result gets much larger than 1.0. So what are some of the ways to mor...

How to find whole graph coverage path in dynamic state-flow diagram?

Hello, As I've been researching algorithms for path finding in graph, I found interesting problem. Definition of situation: 1)State diagram can have p states, and s Boolean Fields, and z Int Fields 2)Every state can have q ingoing and r outgoing transitions, and h Int fields (h belongs to z - see above) 3)Every transition can have on...

Fast, Vectorizable method of taking floating point number modulus of special primes?

Is there a fast method for taking the modulus of a floating point number? With integers, there are tricks for Mersenne primes, so that its possible to calculate y = x MOD 2^31-1 without needing division. integer trick Can any similar tricks be applied for floating point numbers? Preferably, in a way that can be converted into vect...

CODE1 Spoj - cannot solve it

I am trying to solve the problem Secret Code and it's obviously math problem. The full problem For those who are lazy to go and read, it's like this: a0,a1,a2,...,an - sequence of N numbers B - some number known to us X = a0 + a1*B + a2*(B^2) + a3*(B^3) + ... + an*(B^n) So if you are given B and X, you should find a0,a1,..an. I don...

Given a 2d array sorted in increasing order from left to right and top to bottom, what is the best way to search for a target number?

I was recently given this interview question and I'm curious what a good solution to it would be. Say I'm given a 2d array where all the numbers in the array are in increasing order from left to right and top to bottom. What is the best way to search and determine if a target number is in the array? Now, my first inc...

What problem/s does a Rule Engine Algorithm solves?

What problem/s does a Rule Engine Algorithm solve? Which are the recommended ones? ...

Finding All Ways in FlowChart diagram ?

Hi All... I made an FlowChart diagram editor on Java. It It drows flowscharts and connect them each other and creates me two array. One of it shows connection nodes and lines , other shows connnecting elements eachother. I have to find all ways from starting Begin Two And . For example if I have some diamond for decision I have two sepe...

Is there a Perl implementation of Factor Graphs sum-product scheduling?

Are there good example codes of implementations of factor graph sum-product scheduling? I am new to the concept, and would like to see how it gets implemented. ...

Elegant Algorithm for Parsing Data Stream Into Record

I am interfacing with a hardware device that streams data to my app over Wifi. The data is streaming in just fine. The data contains a character header (DATA:) that indicates a new record has begun. The issues is that the data I receive doesn't necessarily fall on the header boundary, so I have to capture the data until what I've capture...

FIlling a Java Bean tree structure from a csv flat file

Hi, I'm currently trying to construct a list of bean classes in Java from a flat description file formatted in csv. Concretely : Here is the structure of the csv file : MES_ID;GRP_PARENT_ID;GRP_ID;ATTR_ID M1 ; ;G1 ;A1 M1 ; ;G1 ;A2 M1 ;G1 ;G2 ;A3 M1 ;G1 ;G2 ;A4 M1 ;...

Why is fisher yates the most useful shuffling algorithm?

Would you say modern version of fisher yates is the most unbiased shuffling algorithm? How would you explain that each element in the array has a probability of 1/n being in its original spot? ...

question about sorting

Bubble sort is O(n) at best, O(n^2) at worst, and its memory usage is O(1) . Merge sort is always O(n log n), but its memory usage is O(n). Which algorithm we would use to implement a function that takes an array of integers and returns the max integer in the collection, assuming that the length of the array is less than 1000. What if ...

How to find smallest substring which contains all characters from a given string?

I have recently come across an interesting question on strings. Suppose you are given following: Input string1: "this is a test string" Input string2: "tist" Output string: "t stri" So, given above, how can I approach towards finding smallest substring of string1 that contains all the characters from string 2? ...

What are some good algorithms for drawing lines between graph nodes?

What I'm specifically grappling with is not just the layout of a graph, but when a user selects a graph node and starts to drag it around the screen area, the line has to constantly be redrawn to reflect what it would look like if the user were to release the node. I suppose this is part of the layout algorithm? Also some applications ...

Techniques and algorithms for precise and efficient graph plotting

I'd like to plot mathematical functions and I'm looking for techniques and algorithms for precise and efficient plotting. 2D plotting should suffice. I focus on techniques and algorithms, not libraries or languages. Thanks in advance! ...