comparing and extracting elements of 2 nested loops in python 2.7 - python-2.7

I have two 2d arrays.
a=['the flower is red','butterflies are pretty','dog is a man best friend']
b=['231','01','034']
Array a contains sentences, while array b is the indexes of the word that I would like to extract from array a.
For example by comparing the individual elements in b[0] which is 231, I would like to extract is,red,flower where as for b[2], I would like to extract dog, man,best.
So, in order to do that, I have to elements a[] word by word, and then compare with each of the individual elements in b[] (for example to read 2,3,1 individually to compare with the index in a[i][j].)
Hence, I would require two 2d array loops and compare them. [ 4 for loops I think]
for i in a:
x= i.split()
#x=one word
for idx, word in enumerate(x):
#idx= index of one word, word=one word
for i in b:
for y in i:
if y == idx: #comparing y which is a number with the index in a[]
print(word)
the code above is incorrect somehow and i don't know what or where went wrong.
So, what is the code to get the wanted result?

for idx, s in enumerate(b):
r = []
for c in s:
r.append(a[idx].split()[int(c)])
print r

Related

How to print histogram of ordered C++ string array?

I read a given text file and then fill my array with each word in the text file (I do a check to make sure the file is under 100 words, storing the number of words up top). I order them alphabetically (using bubble sort) and then get an array of of a bunch of words in order, occurring different amounts of times so for example:
string stringText[10] = {alpha, alpha, bravo, charlie, charlie, charlie...}
I need to print a histogram where I have each word followed by an 'x' for each occurrence of the word (to create a histogram):
alpha: xx
bravo: x
charlie: xxx
and so on...
My question I guess is, should I edit the array, getting rid of duplicate elements or just print the first occurrence of each unique element followed by how many times it occurs? If I delete duplicate elements, my approach would be to go back into the string I read and just count how many times that word occurs. I'm more inclined towards leaving the array and just printing the first unique occurrence followed by an 'x' for each occurrence but I am not sure how to implement that.
I'm not allowed to map/use vectors etc.
The code below requires sorting and no limitation on number of words.
const int wordCount = 6;
string stringText[wordCount] = {"alpha", "alpha", "bravo", "charlie", "charlie","charlie"};
int counter = 0;
while(counter<wordCount)
{
cout<<stringText[counter];
cout<<" : x";
for(int i=counter+1;i<wordCount;++i)
{
if(stringText[i]==stringText[counter])
{
cout<<"x";
counter++;
}
}
cout<<endl;
counter++;
}
And the output is :
alpha : xx
bravo : x
charlie : xxx
Could be easy with a map ... But if you can't do an int* sorted like that :
int count[nb_word];
Where
count[0]
represent the first word (in your exemple alpha) number of occurence
Just keep previous element if it's the same as current one, print x, if not print the new element with 1st occurrence mark.
I am not giving implementation nor completely exact algorithm on purpose, as it seems like an exercise.

C++ : Fastest Way to find number of elements of one array in another array

I have two arrays, one sorted array int b[] and other unsorted array int a[n] having n elements . The sorted array is made of some or all elements of unsorted array. Now there are M queries. For each query values of l and r are given. In each query I need to find the number of elements of a[n] which are present in b[].
For eg -
N=5 ,M=2
a= [2 5 1 2 3]
b=[3 2 1]
for each m:
l=1 r=5 ->a[1]=1, a[5]=5 -> answer should be 3 as all elements of b i.e 1,2,3 are present in a
l=2 r=4 ->a[2]=5 , a[4]=2 ->answer should be 2 as only 1 and 2 are there in b for given value of l and r for array.
How to find the answer with not more than O(M * LOGN) time complexity ?
NOTE:
Array is not necessary. Vector can also be used that is if it helps in reducing time complexity or easier to implement the code.
Well i think you can do something like this
std::map<int,int> c;
for(int i = 0;i<b.length.i++){
c[b[i]] = 0;
}
for(int i = l; i<=r; i++){
int number = a[i];
c[number]++;
}
//Iterate through c with b index and get all number which different than 0. The left is for you
The purpose of this is creating a map hold index of B. Then while iterating A you can increase the c value. So that after that you can check whether each element in C has value different than zero mean that A has hold the number of B.
You can use array instead of map if C starting from zero and increase by 1 for better performance. Make sure to check if a[i] can throw out of bounds exception if you use array.

C++ Arrays and overflow

I am mis-understanding something about the code below. From my understanding the tester declaration should return a pointer to the first array of two elements, i.e. [1,2], and so *(tester+1) should return [3,4], which only has 2 elements so how does it make sense to call (*(tester + 1))[2] . This example prints the number 5 by the way. Any clarifications much appreciated.
int main() {
int tester[][2]{ 1,2,3,4,5,6 };
cout << (*(tester + 1))[2] << endl;
return 0;
}
When you declare a 2-dimensional array, all the elements are contiguous. The entire array is a single object, so you're not going out of bounds when you just exceed one of the row limits, so longer as you're still in the array. So the next element after tester[1,1] is tester[2,0], and that's what (*(tester + 1))[2] accesses.
[2] is higher than the highest element at [1] because the index starts at [0]
There are three arrays.
[1,2] positions 0,0 and 0,1
[3,4] positions 1,0 and 1,2
[5,6] positions 2,0 and 2,1
Since all of the arrays are next to each other in the data segment, going out of bounds on the second array (tester + 1) bleeds over into the third array giving you the first element of the third array.
i.e. position 1,2 is the same as 2,0
int tester[][2]{ 1,2,3,4,5,6 } creates a 3 by 2 array.
tester[0][0] = 1
tester[0][1] = 2
tester[1][0] = 3
tester[1][1] = 4
tester[2][0] = 5
tester[2][1] = 6
The compiler creates an array using the least amount of memory possible based on your specifications. [][2] explicit says to organize the data in such a fashion that there a x rows and 2 columns. Because you put 6 elements into the tester array, the compiler decides the number of rows to assign by dividing the number of total elements by the number of columns.
Furthermore, (*(tester + 1))[2], is equivalent to tester[2], which would access the element in the first column of the third row. Thus returning 5.
It is another way to write this code which means you define vector but it acts like 2-dimensional array.
int main() {
int tester[3][2]{ {1,2},{3,4},{5,6} };
std::cout<< tester[1][2] <<std::endl;
return 0;
}

C++ Find all combinations of elements in multiple arrays in Breadth-first search manner

I've got multiple arrays and want to find the permutations of all the elements in these arrays. Each element also carries a weight, and these arrays are sorted decreasing by weight. I've got an array with weight that mimics the arrays with he values themselves. I want my search to find permutations with the greatest weight to the lowest weight.
However, each element in an array has a weight associated with it so I want to run my search with those with the highest weight first.
Example:
arr0 = [A, B, C, D]
arr0_weight = [11, 7, 4, 3]
arr1 = [W, X, Y]
arr1_weight = [10, 9, 4]
Thus, the ideal output would be:
AW (11+10=21)
AX (11+9=20)
BW (7+10=17)
BX (7+9=16)
AY (11+4=15)
...
If I did just a for loop like this:
for (int i = 0; i < sizeof(arr0)/4; i++) {
for (int j = 0; j < sizeof(arr1)/4; j++) {
cout << arr0[i] << arr1[j] << endl; }}
I would get:
AW (11+10=21)
AX (11+9=20)
AY (11+4=15)
BW (7+10=17)
BX (7+9=16)
BZ (7+4=11)
Which isn't what I want because 17 > 15 and 16 > 15.
Also, what's a good way to do this for n arrays? If I don't know how many arrays I will have, and their size might not all be the same?
I've looked into putting the values into vectors but I can't find a way to do what I want (a sorted Cartesian product). Any help? Pseudo-code is fine if you don't have time - I'm just really stuck.
Thanks so much.
Your question is about algorithm, not C++.
You want to sort all tuples in Cartesian product from heaviest to lightest.
Easiest way is to find all tuples and sort them by their weight.
If you need sequential access, your should do following. Since weight of tuple is sum of weights of its elements, I think, greediness is optimal here. Let's move to arbitrary number of arrays of arbitrary dimensions. Create set of indices. Initially, it's contains zeros. First tuple that it represents is obviously heaviest. Find one of indices to increment: choose index that loses least weight, that has least difference with next element. Don't forget to keep track of exhausted arrays. When all vectors are exhausted, you're done.
To implement it in C++, you should employ vector<pair<element_t, weight_t>> for input data and set<pair<weight_difference_t, index_t>> as set of indices. All types are probably integers but I used custom types to show which data should be there. Your should also know how pair is compared.

How to list all the possible sums of combinations in an array for C++?

I have my homework assignment and I have no idea how to start with the code for this kind of problem.
Let say I have an integer array with consist of n elements,
[A][B][C][D][E] (We have 5 elements for example)
I want to list out all the sum of possibility such that I want to print out the
sum of all combination (ABCDE, ABCD, ABCE, ACDE, BCDE, ABC, ABD, ABE, ACE, ADE, BDE, CDE, AB, AC, AD, AE, BC, BD, BE, CD, CE, DE, A, B, C, D and E)
Another example would be 4 elements in an array ([A][B][C][D])
I want to list all sum of combination of (ABCD, ABC, ABD, ACD, BCD, AB, AC, AD, BC, BD, CD, A, B, C and D).
Well, here's a simple rule to follow:
The set of all combinations of "ABCDE" is composed of those combinations which contain (and thus start with) "A" and those which don't contain "A". In both cases, all combinations of "BCDE" can occur. Of course, combinations of "BCDE" can be treated in the same way.
When you say "list out all the sum of possibility" do you mean you want to know how many combinations are actually possible?
If so, then search on combinations of N items taken K at a time; there are pages on this very site addressing this problem. You would then simply add the number of combinations of (by 5) + (by 4) + (by 3) + (by 2) + (by 1) to get your total "sum of possibilities".
Or do you mean you have an array of values and you literally want to print out the different sums represented by different combinations of the elements? In that case you need to actually enumerate all the combinations and evaluate the sums.
So given an array of { 1, 2, 3, 4, 5 } you can encode this as "A", "B", "C", "D", "E". Examples of tuples would be:
ABCDE = 1+2+3+4+5
ABE = 1+2+5
BCE = 2+3+5
etc, where you use the encoded enumeration to select the addends for your sum. Note that deciding whether to allow or disallow duplicates (i.e., is "DE" different from "ED") will have a very large effect on your results; in most cases this will probably not be what you want.
If you have 3 elements, you may imagine each element placed at a certain position from 1 to 3 (or 0 to 2) and a boolean array representing whether the element is contained in a certain set.
ABC remark
--- ---------------------
000 no element in the set
001 one element, C
010 one element, b
100 ...
011 two elements, b and c
...
111 all elements contained
Now if you calculate the number of solutions, which is 2³ in this case, and generate a function, which does a mapping from a binary representation to a set, from 011 to (b, c) for example, then you can easily program a loop, which iterates from 0 to max-1 and returns all sets, produced by your mapping function.