Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So, I need to find unique quadruples in C++. Any idea would help
Input 1 : [1, 0, 2, 3], [2, 0, 1, 3], [4, 5, 6, 7], [8, 9, 10, 11]
Output 1 : [2, 0, 1, 3], [4, 5, 6, ,7], [8, 9, 10, 11]
As [1,0,2,3] and [2,0,1,3] both contain same elements so either one can be in the output
Input 2 : [2, 0, 1, 3], [4, 5, 6, ,7], [8, 9, 10, 11], [15,16,17,18]
Output 2 : [2, 0, 1, 3], [4, 5, 6, ,7], [8, 9, 10, 11], [15,16,17,18]
I cannot initalize set (int,int,int,int). Any idea on how to get unique ones?
Update for people who asked for defining the question more:
A quadruple is a combination of 4 integers for the problem. Problem states to find unique quadruples from all the given quadruples. A quadruple (a,b,c,d) is unique , if no other quadruple exists with all the elements same as this one, i.e. any quadruple formed from the permutation of (a,b,c,d) is not unique. Quadruples (a,b,c,d) and (a,d,b,c) are the same, where as quadruples (a,b,c,d) and (a,e,f,b) are not. Quadruples are unique if they contain atleast 1 element which is not common to both.
Write a comparator that sorts the integers in the quadruples before comparing them.
struct CompareQuads
{
bool operator()(Quad x, Quad y) const
{
// sort x integers
...
// sort y integers
...
// return true if x < y (lexicographically)
...
}
};
Use the comparator in std::set to eliminate duplicates.
std::set<Quad, CompareQuads> s;
Add all the quads to s and the duplicates will be removed. Iterate through s and print the ones that remain.
Related
I'm trying to make a program that for a sublist of numbers, uses index as a variable and selects each number from the list of lists
so if my numbest = [[1, 2, 3, 4, 5], [2, 4, 6, 8, 10], [3, 5, 7, 9, 11]]
I want to be able to call the function like this
column_sum(2, [[1, 2, 3, 4, 5], [2, 4, 6, 8, 10], [3, 5, 7, 9, 11]]) will add the numbers at index 2 in each sublist (3, 6, and 7) and will return the number 16."
I can't for the life of me figure out how to print
for i in numlist:
print numbest[index]
Looks like Python, so imma say that all you need to do is have a variable that is a running total, add up all the numbers that are the values at the index you specify, and then return that value.
Alexander is also right and if his way is easier for you, you can find resources https://www.w3schools.com/python/ref_func_sum.asp and https://www.w3schools.com/python/python_lists_comprehension.asp
I have a ndarray like this one:
number_of_rows = 3
number_of_columns = 3
a = np.arange(number_of_rows*number_of_columns).reshape(number_of_rows,number_of_columns)
a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
But I want something like this:
array([[0, 100, 101],
[3, 102, 103],
[6, 7, 8]])
To do that I want to avoid to do it one by one, I rather prefer to do it in arrays or matrices, because later I want to extend the code.
Nothe I have change a submatrix of the initial matrix (in mathematical terms, in terms of this example ndarray). In the example the columns considered are [1,2] and the rows [0,1].
columns_to_keep = [1,2]
rows_to_keep = [0,1]
My first try was to do:
a[rows_to_keep,:][:,columns_to_keep] = np.asarray([[100,101],[102,103]])
However this doesn't modify the initial a, I am not having any error, so a=
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
So I have implemented a piece of code that goes do the job:
b = [[100, 101],[102, 103]]
for i in range(len(rows_to_keep)):
a[i,columns_to_keep] = b[i]
Al thought the previous lines do the job I am wondering how to do it slicing and in a faster fashion. Also in a way that with:
columns_to_keep = [0,2]
rows_to_keep = [0,2]
the desired output is
array([[100, 1, 101],
[3, 4, 5],
[102, 7, 103]]).
Many thanks!
Indexing with lists like [1,2] is called advanced indexing. By itself it produces a copy, not a view. You have to use one indexing expression, not two to assign or change values. That is a[[1,2],:] is a copy, a[[1,2],:][:,[1,2]] += 100 modifies that copy, not the original a.
In [68]: arr = np.arange(12).reshape(3,4)
Indexing with slices; this is basic indexing:
In [69]: arr[1:,2:]
Out[69]:
array([[ 6, 7],
[10, 11]])
In [70]: arr[1:,2:] += 100
In [71]: arr
Out[71]:
array([[ 0, 1, 2, 3],
[ 4, 5, 106, 107],
[ 8, 9, 110, 111]])
Doing the same indexing with lists requires arrays that 'broadcast' against each other. ix_ is a handy way of generating these:
In [73]: arr[np.ix_([1,2],[2,3])]
Out[73]:
array([[106, 107],
[110, 111]])
In [74]: arr[np.ix_([1,2],[2,3])] -= 100
In [75]: arr
Out[75]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
Here's what ix_ produces - a tuple of arrays, one is (2,1) in shape, the other (1,2). Together they index a (2,2) block:
In [76]: np.ix_([1,2],[2,3])
Out[76]:
(array([[1],
[2]]), array([[2, 3]]))
For the continuous rows and columns case, you can use basic slicing like this:
In [634]: a
Out[634]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
In [635]: b = np.asarray([[100, 101],[102, 103]])
In [636]: a[:rows_to_keep[1]+1, columns_to_keep[0]:] = b
In [637]: a
Out[637]:
array([[ 0, 100, 101],
[ 3, 102, 103],
[ 6, 7, 8]])
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Let's say, for example, that I have 2 variables __m256i called rows and cols, the values inside them are:
rows: 0, 2, 7, 5, 7, 2, 3, 0
cols: 1, 2, 7, 5, 7, 2, 2, 6
Now, these values represent the x and y positions for 8 points, so, in this case I would have these points:
p0: [0, 1], p1: [2, 2], p2: [7, 7], p3: [5, 5]
p4: [7, 7], p5: [2, 2], p6: [3, 2], p7: [0, 6]
I also have an array called lut that will have values of int type:
lut: [0, 1, 2, 3, ..., 60, 61, 62, 63]
What I want to do, is to use these positions values from rows and cols variables, access the lut array with it and create a new __m256i value with the lut accessed values.
The way I know of how to do that would be to store rows and cols values in two int arrays of size 8, then read the values from lut array one at a time and then use _mm256_set_epi32() to create the new _m256i value.
This works, but it seems to me to be very inefficient.. So my question is if there is some way to do it faster.
Note that these values are just for a more concrete example, and lut doesn't need to have ordered values or size 64.
thanks!
You can build a solution using an avx2 gather instruction, like so
// index = (rows << 3) + cols;
const __m256i index = _mm256_add_epi32( _mm256_slli_epi32(rows, 3), cols);
// result = lut[index];
const __m256i result = _mm256_i32gather_epi32(lut, index, 4);
Be aware that on current CPUs gather instructions have quite huge latency, so unless you can interleave some instructions before actually using result, this may not be worth using.
To explain the factor of 4: The scale factor in
__m256i _mm256_i32gather_epi32 (int const* base_addr, __m256i vindex, const int scale)
is considered as actual byte-offset, i.e., the returned value for each index is:
*(const int*)((const char*) base_addr + scale*index)
I don't know if there are many use-cases for that behavior (perhaps this is to make it possible to access a LUT with 1byte or 2byte entries (requiring some masking afterwards)). Perhaps this was just allowed, because scaling by 4 is possible, while scaling by 1/4 or 1/2 would not be (in case someone really needed that).
I'm trying to write a 4X4 grid in python where the last two rows contain the same numbers as the first two rows.
The end result should be exactly this:
0 1 2 3
4 5 6 7
0 1 2 3
4 5 6 7
The goal is to make a game where the above grid is traversable. I've tried list comprehensions and concatenating two lists and it's not producing the right answers.
Concatenating two lists should work. The code for concatenating two lists
l1 = [1, 2, 3, 4]
l2 = [5, 6, 7, 8]
l3 = [l1 , l2];
l4 = l3+l3
print l4
should yield [[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4], [5, 6, 7, 8]]
Basically, wanted to iterate over a list of numerical data to change it's contents, where the numerical at the start of the list is moved to the last, and then the data is shifted to the left. Whilst I have achieved this, as the printed contents of the loop gives the desired results, when trying to append the contents of said loop to said dictionary, it only does this for the final iteration. Here's my code:
minor=[1,2,3,4,5,6]
MNP = {'scale degree' : []
}
def patterns(scale):
for i in scale:
print (scale)
scale.insert(len(scale),scale[0])
del(scale[0])
MNP['scale degree'].append(scale)
using the function patterns, this is the output:
>>> patterns(minor)
the list, minor, is at the top of the page by the way.
output:
[1, 2, 3, 4, 5, 6]
[2, 3, 4, 5, 6, 1]
[3, 4, 5, 6, 1, 2]
[4, 5, 6, 1, 2, 3]
[5, 6, 1, 2, 3, 4]
[6, 1, 2, 3, 4, 5]
Yet when I try to print the contents of the list, scale degree, in the MNP dict, the result is:
MNP['scale degree']
[[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]
I am very perplexed by this result, it's as if the output changes depending on the operation called upon it?
Thank you for any help in advance. It's also worth noting that I've been stuck with this for a good amount of time, so if there's any resources out there that may help me understand similar occurrences i certainly wouldn't pass that up.
The reason this happens is because what you store in MNP['scale degree'] is only a reference to scale. So when you change scale, so do the entries in MNP['scale degree']. What you need to do to avoid this is copying scale each time you append it (i.e. creating a new list instead of adding a reference). You can do this with the copy module:
import copy
minor=[1,2,3,4,5,6]
MNP = {'scale degree' : []
}
def patterns(scale):
for i in scale:
print (scale)
scale.insert(len(scale),scale[0])
del(scale[0])
MNP['scale degree'].append(copy.copy(scale))
patterns(minor)
print(MNP['scale degree'])