Say the original list is
1 2 3 4 5 6 7 8 9 10
and u shift it so that it becomes
5 6 7 8 9 10 1 2 3 4
so say i want to check if 7 is in the array. how would i do this efficiently.
If I have a straight line that mesures from 0 to 1, then I have colorA(255,0,0) at 0 on the line, then at 0.3 I have colorB(20,160,0) then at 1 on the line I have colorC(0,0,0). How could I find the color at 0.7?
Thanks
I have the following dilemma: I have a list of strings, and I want to find the set of string which start with a certain prefix. The list is sorted, so the naive solution is this:
Perform binary search on the prefixes of the set, and when you find an element that starts with the prefix, traverse up linearly until you hit the top of the subset.
This runs in linear time, however, and I was wondering if anyone can suggest a more efficient way to do it.
I want to implement a code that loops inside an array that its size is set by the user that means that the size isn't constant.
for example:
A=[1,2,3,4,5]
then I want the output to be like this:
[1],[2],[3],[4],[5]
[1,2],[1,3],[1,4],[1,5]
[2,3],[2,4],[2,5]
[3,4],[3,5]
[4,5]
[1,2,3],[1,2,4],[1,2,5]
[1,3,4],[1,3,5]
and so on
[1,2,3,4],[1,2,3,5]
[2,3,4,5]
[1,2,3,4,5]
Can you help me implement this code?
My task is to search for a string or a pattern in a list of documents that are very short (say 200 characters long). However, say there are 1 million documents of such time. What is the most efficient way to perform this search?. I was thinking of tokenizing each document and putting the words in hashtable with words as key and document number as value, there by creating a bag of words. Then perform the word search and retrieve the list of documents that contained this word. From what I can see is this operation will take O(n) operations. Is there any other way? may be without using hash-tables?.
Also, is there a python library or third party package that can perform efficient searches?
I have a largish array of string that I want to use as a lookup.
I a using in_array(), but I suspect its doing a simple loop through - does anyone know whether the in_array() algo uses a bsearch algo?
I'm working on a research problem out of curiosity and I don't know how to program the logic that I've in mind. Let me explain it to you :
I've 4 vectors say for example,
v1 = 1 1 1 1
v2 = 2 2 2 2
v3 = 3 3 3 3
v4 = 4 4 4 4
Now what I want to do is to add them combination-wise. i.e
v12 = v1+v2
v13 = v1+v3
v14 = v1+v4
v23 = v2+v3
v24 = v2+v4
v34 = v3+v4
Till this step it is just fine. The problem/trick is now, at the end of each iteration I give the obtained vectors into a black box function and it returns only few of the vectors say v12, v13 and v34. Now, I want to add each of these vectors one vector from v1,v2,v3,v4 which it hasn't added before. For example v3 and v4 hasn't been added to v12 so I want to create v123 and v124. similarly for all the vectors like,
v12 should become :
v123 = v12+v3
v124 = v12+v4
v13 should become :
v132 // this should not occur because I already have v123
v134 = v13+v4;
v14,v23 and v24 cannot be considered
because it was deleted in the black
box function so all we have in our
hands to work with is v12,v13 and v34.
v34 should become :
v341 // cannot occur because we have 134
v342 = v34+v2
It is important that I do not do all at one step at the start like for example I can do (4 choose 3) 4C3 and finish it off but I want to do it step by step at each iteration.
I've asked a modified version of this question before (without including the black box function) and got answers here. Can anybody tell me how to do it when the black box function is included ? A modification of the previous answer would also be great.
Thanks in advance.
What is really the difference between the algorithms remove and remove_if and the member function erase?
Does both of them result in a call to the removed objects destructor?
Hello Hackerz,
Here is the idea
A user can set a day alert for a birthday. (We do not care about the year of birth)
He also picks if he wants to be alerted 0, 1, 2, ou 7 days (Delta) before the D day.
Users have a timezone setting.
I want the server to send the alerts at 8 am on the the D day - deleta +- user timezone
Example:
12 jun, with "alert me 3 days before" will give 9 of Jun.
My idea was to have a trigger_datetime extra field saved on the 'recurrent event' object.
Like this a cron Job running every hour on my server will just check for all events matching irs current time hour, day and month and send to the alert.
The problem from a year to the next the trigger_date could change !
If the alert is set on 1st of March, with a one day delay that could be either 28 or 29 of February ..
Maybe i should not use the trigger date trick and use some other kind of scheme.
All plans are welcome.
1st number: 50
2. 30
3. 70
4. 40
5. 11
and other number is 33
I need to calculate which two numbers the last number is between (using php) .. any help?
The problem is to learn computer to do addition.
Computer have as input a knowladge of a numbers: he "knows" that after 1 goes 2, after 2 goes 3 and so on... Having that data computer can easyly get next number.
Next, computer have knowlandge as input that x+0=x and x+(y+1)=(x+1)+y. This axioms let computer to do addition. For example, to add 5 and 3, computer makes following: 5+3 = 5+(2+1) = (5+1)+2 = 6+2 = 6+(1+1) = (6+1)+1 = 7+1 = 8.
But this is too long to add numbers in such way. The problem is to develop program which can improve this way of addition using the rules of mathemetics and logic. The goal addition must executed in O(log(N)) time, not O(N) time, N is magnitude of added numbers.
Have this program any science value? Is any program can do such things?
please help this is very important problem for me i am going to get job and need such kind of practise implement heaps priortiy queue and so on what is wrong in my java code please tell
i want insert number with heap property and return minimum element what is wrong explain please
look
http://stackoverflow.com/questions/2902781/priority-queue-implementation/2903288#2903288
How good is this book?
Seems good to me but I would like to know your opinion about it before I buy it.
Has anybody used this book?
Introduction to algorithms - CLRS
Thanks
i have done folloing code from progrmming pearls here is code
import java.util.*;
public class select {
public static int select1(int x[],int l,int u,int k){
//pre l<=k<=u
//post x[l..k-1]<=x[k]<=x[k+1..u]
Random r=new Random();
int t=r.nextInt(u-1-l)+l;
if (l>=u) return -1 ;
swap(l,t);
int s=x[l];
int i=l;
int j=u+1;
while (true){
do
{
i++;
}while (i<=u && x[i]<t);
do
{
j--;
}while (x[j]>t);
if (i>j) break;
int temp=x[i]; x[i]=x[j];x[j]=t;
swap(l,j);
if (j<k){
return select1(x,j+1,u,k);
}
}
return select1(x,l,j-1,k);
}
public static void main(String[] args) {
int x[]=new int[]{4,7,9,3,2,12,13,10,20};
select1(x,0,x.length-1,5);
}
public static void swap(int i,int j){
int c=i;
i=j;
j=c;
}
}
but here is mistake
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at select.select1(select.java:21)
at select.main(select.java:36)
Java Result: 1
please help
FYI: no boost, yes it has this, I want to reinvent the wheel ;)
Is there some form of a selective iterator (possible) in C++? What I want is to seperate strings like this:
some:word{or other
to a form like this:
some : word { or other
I can do that with two loops and find_first_of(":") and ("{") but this seems (very) inefficient to me. I thought that maybe there would be a way to create/define/write an iterator that would iterate over all these values with for_each. I fear this will have me writing a full-fledged custom way-too-complex iterator class for a std::string.
So I thought maybe this would do:
std::vector<size_t> list;
size_t index = mystring.find(":");
while( index != std::string::npos )
{
list.push_back(index);
index = mystring.find(":", list.back());
}
std::for_each(list.begin(), list.end(), addSpaces(mystring));
This looks messy to me, and I'm quite sure a more elegant way of doing this exists. But I can't think of it. Anyone have a bright idea? Thanks
PS: I did not test the code posted, just a quick write-up of what I would try
I'm trying to port the following Java snippet to Scala. It takes a list of MyColor objects and merges all of the ones that are within a delta of each other. It seems like a problem that could be solved elegantly using some of Scala's functional bits. Any tips?
List<MyColor> mergedColors = ...;
MyColor lastColor = null;
for(Color aColor : lotsOfColors) {
if(lastColor != null) {
if(lastColor.diff(aColor) < delta) {
lastColor.merge(aColor);
continue;
}
}
lastColor = aColor;
mergedColors.add(aColor);
}
Given a N-dimensional vector of small integers is there any simple way to map it with one-to-one correspondence to a large integer number?
Say, we have N=3 vector space. Can we represent a vector X=[(int32)x1,(int32)x2,(int32)x3] using an integer (int48)y? The obvious answer is "Yes, we can". But the question is: "What is the fastest way to do this and its inverse operation?"
A flipping coin on table will slow down and drop to the table surface, facing up or down. How can I calculate the flip-per-second declaration rate over time?
For example,
assuming the coin is at 10 flipping per second when it starts
how long will it take to stop?
For each second (9, 8, 7, 6 ... 3, 2, 1, stop), how is the flipping rate changed?
Friction can be approximated as some real world objects (say, a metallic coin on a wooden table). Thanks!
i have question how write program which calculates following procedures
http://en.wikipedia.org/wiki/Tetration
i have exponential program which returns x^n here is code
public class Exp{
public static long exp(long x,long n){
long t=0;
if (n==0){
t= 1;
}
else{
if (n %2==0){
t= exp(x,n/2)* exp(x,n/2);
}
else{
t= x*exp(x,n-1);
}
}
return t;
}
public static void main(String[]args){
long x=5L;
long n=4L;
System.out.println(exp(x,n));
}
}
but how use it in Tetration program?please help
I have a vector<unsigned int> vec of size n. Each element in vec is in the range [0, m], no duplicates, and I want to sort vec. Is it possible to do better than O(n log n) time if you're allowed to use O(m) space? In the average case m is much larger than n, in the worst case m == n.
Ideally I want something O(n).
I get the feeling that there's a bucket sort-ish way to do this:
unsigned int aux[m];
aux[vec[i]] = i;
Somehow extract the permutation and permute vec.
I'm stuck on how to do 3.
In my application m is on the order of 16k. However this sort is in the inner loops and accounts for a significant portion of my runtime.
Hey everybody. Given a set of n integers, i need to find the product of each of the (n-1) subsets i.e:
ith subset = the product of all the elements except the ith element itself. e.g:
S = {1,6,3,2};
subset_product(1) = 6*3*2=36;
subset_product(2) = 1*3*2=6; etc.
So Any help?