This is a follow-up question to this answer. I am trying to build a loop that produces a set of three random numbers until they match a particular pre-defined set of three arbitrary chosen numbers.
I'm still trying to figure out what operators to use for the program to accept the random numbers in any order but without any results.
I tried to your
!(first==one && second==two && third==three)
but it doesn't seem to work in c++. Thanks for your answer.
The condition that you tried implies that first, second, and third are in the same specific order as one, two, and three. You could try all six permutations, but that would make for a rather unreadable program. A better solution would be to add values to vectors, sort them, and then compare for equality, like this:
vector<int> a;
a.push_back(first);
a.push_back(second);
a.push_back(third);
vector<int> b;
b.push_back(one);
b.push_back(two);
b.push_back(three);
sort(a.begin(), a.end());
sort(b.begin(), b.end());
if (a == b) ... // values match
Here is a link to this snippet on ideone.
Related
i'm trying to get better performance from a pattern check in a really wide list of strings.
i need the 5 first occurences that would match a given pattern.
i was wondering if
list.where(pattern in string).take(5)
was lazily computed and stops after 5 occurences found or
does it compute all the where and then takes the 5 first ? ( in that case, is there a whereXfirstOccurences method where X is a number ? )
thank you,
Edit:
i did some investigation
myList.where((element) {bool isSuggestion = the conditions ;
if (isSuggestion) index++;
return isSuggestion;
})
.take(x)
.toList();
print(index);
the index is always at most equal to x so i guess it's lazy evaluation as mentionned below, Thank you :)
Iterables are lazy.
If you do list.where(computation).take(5), it:
Doesn't do anything at all, until you start iterating.
It doesn't do anything except when you call moveNext on the iterator.
And it stops doing anything once moveNext has returned false, which it does after five elements here, because of the take(5).
If you just use for (var v in list.where(...).take(5)) ... you won't see those steps, but they are still there. The loop stops after finding five values, and no further elements are looked at than the ones needed to find the first five satisfying the where condition.
That might still be a lot of strings looked at, if the condition is very picky. If there are only four matching strings in the input, you will go through all of the input when looking for the first five matches.
Optimizing the pattern itself can definitely be valuable as well.
I need writing a function which takes as input
a = [12,39,48,36]
and produces as output
b=[4,4,4,13,13,13,16,16,16,12,12,12]
where the idea is to repeat one element three times or two times (this should be variable) and divided by 2 or 3.
I tried doing this:
c=[12,39,48,36]
a=size(c)
for i in a
repeat(c[i]/3,3)
end
You need to vectorize the division operator with a dot ..
Additionally I understand that you want results to be Int - you can vectorizing casting to Int too:
repeat(Int.(a./3), inner=3)
Przemyslaw's answer, repeat(Int.(a./3), inner=3), is excellent and is how you should write your code for conciseness and clarity. Let me in this answer analyze your attempted solution and offer a revised solution which preserves your intent. (I find that this is often useful for educational purposes).
Your code is:
c = [12,39,48,36]
a = size(c)
for i in a
repeat(c[i]/3, 3)
end
The immediate fix is:
c = [12,39,48,36]
output = Int[]
for x in c
append!(output, fill(x/3, 3))
end
Here are the changes I made:
You need an array to actually store the output. The repeat function, which you use in your loop, would produce a result, but this result would be thrown away! Instead, we define an initially empty output = Int[] and then append! each repeated block.
Your for loop specification is iterating over a size tuple (4,), which generates just a single number 4. (Probably, you misunderstand the purpose of the size function: it is primarily useful for multidimensional arrays.) To fix it, you could do a = 1:length(c) instead of a = size(c). But you don't actually need the index i, you only require the elements x of c directly, so we can simplify the loop to just for x in c.
Finally, repeat is designed for arrays. It does not work for a single scalar (this is probably the error you are seeing); you can use the more appropriate fill(scalar, n) to get [scalar, ..., scalar].
I am new to C++, and I need to write a code that can find a binary code by checking a bitset against inputs and excluding unnecessary options.
Example:
input-000000000 = 6 correct
This implies that there must be 3 ones present.
I need the code to only use strings that contain 6 zeros and three ones and print one attempting to narrow down the code.
Example:
000000111 = 5 correct
This must mean that two of those ones are correct and one must be a zero with a one somewhere in the first six digits.
How do I approach this problem?
I have so far the function:
string index2code (int i)
{
return bitset <9>(i).to_string();
}
But I am still struggling to think of the logic, and how to use it.
I want to match numbers that are present in two arrays (not of equal length) and output them to another array if there is a match. The numbers are floating point.
Currently I have a working program in Python but it is slow when I run it for large datasets. What I've done is two nested for loops.
The first nested for loop runs through array1 and checks if any numbers from array2 are in array 1. If there is a match, I write it to an array called arrayMatch1.
I then check array2 and see if there is a match with arrayMatch1. And output the final result to arrayFinal.
arrayFinal will have all numbers that exist within both array1, array2.
My problem:
Two nested for loops give me a complexity of O(n^2). This method works fine for data sets under an array length of 25000 but slows down significant if greater. How can I make it more efficient. The numbers are floating point and always are in this format ######.###
I want to speed up my program but keep using Python because of the simplicity. Are there better ways to find matches between two arrays?
Why not just find the interesection of two lists?
a = [1,2,3,4.3,5.7,9,11,15]
b = [4.3,5.7,6.3,7.9,8.1]
def intersect(a, b):
return list(set(a) & set(b))
print intersect(a, b)
Output:
[5.7, 4.3]
Gotten from this question.
So what you're basically trying to do is find intersection(logically correct term) of 2 list.
First you need to eliminate the duplicate form the list itself, set is great way to do that, then you can just & those lists and you will be good to go.
a = [23.3213,23.123,43.213,12.234] #List First
b = [12.234,23.345,34.224] #List Second
def intersect(a, b):
return list(set(a) & set(b))
print intersect(a, b)
For sorting item names, I want to support numbers correctly. i.e. this:
1 Hamlet
2 Ophelia
...
10 Laertes
instead of
1 Hamlet
10 Laertes
2 Ophelia
...
Does anyone know of a comparison functor that already supports that?
(i.e. a predicate that can be passed to std::sort)
I basically have two patterns to support: Leading number (as above), and number at end, similar to explorer:
Dolly
Dolly (2)
Dolly (3)
(I guess I could work that out: compare by character, and treat numeric values differently. However, that would probably break unicode collaiton and whatnot)
That's called alphanumeric sorting.
Check out this link: The Alphanum Algorithm
i think u can use a pair object and then make vector > and then sort this vector.
Pairs are compared based on their first elements. So, this way you can get the sort you desire.