Vector behaving wierdly - c++

I was trying to find super lucky numbers that is numbers that contain only 4 and 7 as their digit (lucky number) and the number of 4's and 7's must be same (super lucky number). I used recursion to find the lucky numbers and stored them in vector. Then i looped over all lucky elements to check if it is super lucky. But while doing this when i was copying each vector element to another variable for checking its digits, the copy is not working properly. I don't know why it is copying wrong number .
The code goes like:
#include<iostream>
#include<vector>
using namespace std;
vector<long long int> v;
void comb(long long int x){
v.push_back(x);
if(x<10000000){
comb(x*10+4);
comb(x*10+7);
}
}
int main(){
int n,f,s;
long long int n1;
cin>>n;
comb(0);
for(long long int i=1;i<v.size();i++){
n1=v[i]; //here i copied the element to variable
f=0;
s=0;
cout<<n1<<" "; // But when i checked the value of variable it is not equal to element
while(n1>0LL){
if(n1%10LL==4LL){
f++;
}else s++;
n1/=10LL;
}
if(f!=s){
v.erase(v.begin()+i);
}
cout<<v[i]<<" "<<f<<" "<<s<<'\n';
}
return 0;
}
The output goes like:
4 44 1 0 // n1 v[i] f s
444 4444 3 0
44444 444444 5 0
4444444 44444444 7 0
44444447 4444447 7 1
44444474 44444477 7 1
444447 4444474 5 1
44444744 44444747 7 1
4444477 44444774 5 2
44444777 44447 5 3
444474 4444744 5 1
44447444 44447447 7 1
4444747 44447474 5 2
44447477 444477 5 3
4444774 44447744 5 2
44447747 4444777 5 3
44447774 44447777 5 3
4447 44474 3 1
444744 4447444 5 1
44474444 44474447 7 1
4447447 44474474 5 2
44474477 444747 5 3
4447474 44474744 5 2
44474747 4447477 5 3
44474774 44474777 5 3
44477 444774 3 2
4447744 44477444 5 2
44477447 4447747 5 3
44477474 44477477 5 3
444777 444777 3 3
4447774 44477744 4 3
44477747 44477747 4 4
4447777 44477774 3 4
44477777 447 3 5
4474 44744 3 1
447444 4474444 5 1
44744444 44744447 7 1
4474447 44744474 5 2
44744477 447447 5 3
4474474 44744744 5 2
44744747 4474477 5 3
44744774 44744777 5 3
44747 447474 3 2
4474744 44747444 5 2
44747447 4474747 5 3
44747474 44747477 5 3
447477 447477 3 3
4474774 44747744 4 3
44747747 44747747 4 4
4474777 44747774 3 4
44747777 4477 3 5
44774 447744 3 2
4477444 44774444 5 2
44774447 4477447 5 3
44774474 44774477 5 3
447747 447747 3 3
4477474 44774744 4 3
44774747 44774747 4 4
4477477 44774774 3 4
44774777 44777 3 5
447774 447774 3 3
4477744 44777444 4 3
44777447 44777447 4 4
4477747 44777474 3 4
44777477 447777 3 5
4477774 44777744 3 4
44777747 4477777 3 5
44777774 44777777 3 5
47 47 1 1
474 4744 2 1
47444 474444 4 1
4744444 47444444 6 1
47444447 4744447 6 2
47444474 47444477 6 2
474447 4744474 4 2
47444744 47444747 6 2
4744477 47444774 4 3
47444777 47444777 4 4
47447 474474 3 2
4744744 47447444 5 2
47447447 4744747 5 3
47447474 47447477 5 3
474477 474477 3 3
4744774 47447744 4 3
47447747 47447747 4 4
4744777 47447774 3 4
47447777 4747 3 5
47474 474744 3 2
4747444 47474444 5 2
47474447 4747447 5 3
47474474 47474477 5 3
474747 474747 3 3
4747474 47474744 4 3
47474747 47474747 4 4
4747477 47474774 3 4
47474777 47477 3 5
474774 474774 3 3
4747744 47477444 4 3
47477447 47477447 4 4
4747747 47477474 3 4
47477477 474777 3 5
4747774 47477744 3 4
47477747 4747777 3 5
47477774 47477777 3 5
477 4774 1 2
47744 477444 3 2
4774444 47744444 5 2
47744447 4774447 5 3
47744474 47744477 5 3
477447 477447 3 3
4774474 47744744 4 3
47744747 47744747 4 4
4774477 47744774 3 4
47744777 47747 3 5
..........................................
Why is this behaving like this?

You print n1 whatever value it is. Then if it isn't super lucky number, you remove v[i] which is same as n1 then print new v[i], which is the next value of original v[i].

Related

Given that I have a list of vector <1, 2, 3, 4>, can next_permutation in algorithm library help me arrange 4P2 arrangement? In C++

can next_permutation avoid the duplication as what I want is to skip 2th and 4th as only change in first 2 character is important to me.
do {
//Do something
} while(next_permutation(s.begin(), s.end()));
this will get 4! = 24 solution, while I only wanted 4P2 = 12 solution.
The above coding will give me.
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
While actually I only want
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3
do {
// Do something with the first two entries of `s`.
std::prev_permutation(s.begin()+2, s.end());
} while(std::next_permutation(s.begin(), s.end()));
This will essentially skip all permutations of the last two items, so it won't iterate all permutations unnecessarily and be relatively efficient even if you change the length of the vector or the number of items you are interested in (the magic number 2 in the code above).

My selection sort replaces some numbers in the array with 0

This is the complete code that I wrote:
( [numsin the function] and [arrin main] is the array that needs to be sorted, sizeis the amount of numbers in the array, minis the smallest number in the unsorted part)
#include <iostream>
#include <vector>
using namespace std;
void sort(vector <int> &nums, int size){
int min = 0;
for(int i=0;i<size;i++){
min = i;
for(int j=i+1;j<size;j++){
if(nums[j]<nums[min]){
min = j; //comparing
}
}
nums[i] = nums[min] + nums[i]; //swaping
nums[min] = nums[i] - nums[min];
nums[i] = nums[i] - nums[min];
}
}
int main(){
cout<<"\nEnter Numbers:\n";
vector <int> arr;
int num;
while(cin>>num){
arr.push_back(num);
}
sort(arr,arr.size());
cout<<"\nSorted:\n";
for(int i=0;i<arr.size();i++){
cout<<arr[i]<<" ";
}
}
I'm writing a code that simply sorts the given array. But after trying to debug and find solutions online, I can't figure out which part is wrong. These are some examples of my results:
Enter Numbers:
9 8 7 6 1 2 3 4 5 ^Z
Sorted:
1 2 3 4 5 6 0 0 0
Enter Numbers:
6 4 8 7 2 3 5 ^Z
Sorted:
2 3 4 5 0 7 0
Enter Numbers:
9 8 7 6 5 4 3 2 1 ^Z
Sorted:
1 2 3 4 0 0 0 0 0
This is the result when I added a for loop under the swapping part to show what every round has done to the array:
Enter Numbers:
9 8 7 6 1 2 3 4 5 ^Z
1 8 7 6 9 2 3 4 5
1 2 7 6 9 8 3 4 5
1 2 3 6 9 8 7 4 5
1 2 3 4 9 8 7 6 5
1 2 3 4 5 8 7 6 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 0 8 9
1 2 3 4 5 6 0 0 9
1 2 3 4 5 6 0 0 0
Sorted:
1 2 3 4 5 6 0 0 0
Enter Numbers:
6 4 8 7 2 3 5 ^Z
2 4 8 7 6 3 5
2 3 8 7 6 4 5
2 3 4 7 6 8 5
2 3 4 5 6 8 7
2 3 4 5 0 8 7
2 3 4 5 0 7 8
2 3 4 5 0 7 0
Sorted:
2 3 4 5 0 7 0
9 8 7 6 5 4 3 2 1 ^Z
1 8 7 6 5 4 3 2 9
1 2 7 6 5 4 3 8 9
1 2 3 6 5 4 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 0 6 7 8 9
1 2 3 4 0 0 7 8 9
1 2 3 4 0 0 0 8 9
1 2 3 4 0 0 0 0 9
1 2 3 4 0 0 0 0 0
Sorted:
1 2 3 4 0 0 0 0 0
Please help, thanks.
Your swapping logic doesn't handle the case where the smallest remaining element is the first unsorted element (i.e., i == min). Consider what each line does in this case:
nums[i] = nums[min] + nums[i]; //nums[i] will be doubled
nums[min] = nums[i] - nums[min]; // nums[min] is subtracted from itself, making it 0
nums[i] = nums[i] - nums[min]; // nums[i] is subtracted from itself gain, but 0-0 is still 0
The goal of avoiding a temporary isn't bad by itself, but you do have this nasty edge case. You'd either have to detect the edge case or just bite the bullet and deal with a temporary. You could also call std::swap, but that likely uses a temporary as well.
The advantage to using a temporary or std::swap is that this code would be easier to make generic for other types (especially via std::swap). In addition, std::swap can be specialized for types to avoid temporaries if possible, and if this is actually a bottleneck.

Insertion in std::vector vs. insertion in std::deque

I was solving the puzzles in 2017 Advent of Code. It was necessary to fill in circular buffer using certain algorithm. For the buffer implementation I first used vector, and then I tried with deque. I am getting different results when printing values of the vector and the queue. Here's the code:
#include <iostream>
#include <vector>
void PrintBuffer(std::vector<int> a_CircularBuffer)
{
for (std::vector<int>::iterator it = a_CircularBuffer.begin(); it != a_CircularBuffer.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
int main()
{
std::vector<int> circularBuffer;
circularBuffer.reserve(20);
circularBuffer.push_back(0);
circularBuffer.push_back(1);
std::vector<int>::iterator currentPosition = circularBuffer.begin() + 1;
for (int i = 2; i < 20; ++i) {
int steps = 378;
if (steps >= i) {
steps = (steps % i);
}
if ((circularBuffer.end() - currentPosition) <= steps) {
currentPosition = circularBuffer.begin() + (((currentPosition - circularBuffer.begin()) + steps) % i);
circularBuffer.insert(currentPosition, i);
}
else {
currentPosition = currentPosition + steps;
circularBuffer.insert(currentPosition, i);
}
PrintBuffer(circularBuffer);
}
return 0;
}
This is the result when using vector:
0 2 1
0 3 2 1
0 3 2 4 1
0 5 3 2 4 1
0 6 5 3 2 4 1
0 7 6 5 3 2 4 1
0 7 6 8 5 3 2 4 1
0 7 6 9 8 5 3 2 4 1
0 10 7 6 9 8 5 3 2 4 1
0 10 7 6 9 11 8 5 3 2 4 1
0 10 7 6 9 11 8 5 3 2 4 12 1
0 10 7 6 9 11 8 5 3 2 4 12 13 1
0 10 7 6 9 11 8 5 3 2 4 12 14 13 1
15 0 10 7 6 9 11 8 5 3 2 4 12 14 13 1
...
and this is when using deque (just change "vector" to "deque" and comment out circularBuffer.reserve(20) line):
0 2 1
0 3 2 1
0 3 2 4 1
0 5 3 2 4 1
0 5 6 3 2 4 1
0 5 6 7 3 2 4 1
0 5 6 7 3 8 2 4 1
0 5 6 7 3 9 8 2 4 1
0 5 6 10 7 3 9 8 2 4 1
0 5 6 10 7 3 9 8 11 2 4 1
0 5 12 6 10 7 3 9 8 11 2 4 1
0 5 12 6 13 10 7 3 9 8 11 2 4 1
0 5 12 6 13 14 10 7 3 9 8 11 2 4 1
0 5 12 6 13 14 10 7 3 15 9 8 11 2 4 1
...
Why there are different results for vector and deque?
You get undefined behaviour when you insert an element causing reallocation, and then use the old iterator again.
Anything can happen.
Use index to store current position and it'll work the same way.

Stripping off the zeros from a list containing a panda dataframe

i have a list
[0,0,0, DataFrame1,0,0,DataFrame2,0,0, DataFrame3]
where Dataframe is a "Panda Dataframe".
now what i am trying to do is to strip of the '0' zeros (being integers). Is there any way i can do this without using a loop. I tried to use set function, but it is not working with panda Dataframes.
My answer should resemble like this
[ DateFrame1, DataFrame2, DataFrame3]
As #Zero, suggest in comment.
l = []
df = pd.DataFrame(np.random.randint(0,10,(5,5))
l.append(0)
l.append(0)
l.append(df)
l.append(0)
l.append(0)
l.append(df)
print(l)
[0, 0, 0 1 2 3 4
0 5 4 9 6 7
1 9 9 2 7 3
2 4 9 4 8 3
3 4 6 2 5 5
4 8 1 2 1 8, 0, 0, 0 1 2 3 4
0 5 4 9 6 7
1 9 9 2 7 3
2 4 9 4 8 3
3 4 6 2 5 5
4 8 1 2 1 8]
[x for x in l if isinstance(x,pd.DataFrame)]
Output:
[ 0 1 2 3 4
0 5 4 9 6 7
1 9 9 2 7 3
2 4 9 4 8 3
3 4 6 2 5 5
4 8 1 2 1 8, 0 1 2 3 4
0 5 4 9 6 7
1 9 9 2 7 3
2 4 9 4 8 3
3 4 6 2 5 5
4 8 1 2 1 8]

openCV: Plotting a colormap from a matrix

I have a 50 x 50 matrix having certain values from 1 to 50. I need to plot a color map for the same using OpenCV .
I have already done this in MATLAB (with help from this answer):
>> A = randi([10,60],100,100);
>> colormap('hot')
>> imagesc(A)
>> colorbar
And the output comes like .
I would like to do the same using openCV.
How do I proceed for this ?
I am unable to understand how do I implement the function of "hot" which appears like this (see only hot)
:
Additionally I would also be interested in putting a color bar as shown in the first image.
Update (Final code I am running)
Unfortunately still I am unable to plot the correct color map: Here is the MATLAB code:
Note : A is a 10 by 100 matrix.
A=[0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 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 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
]
colormap('hot');
imagesc(A)
colorbar
And here is the output:
Now I created a text file with same data, and my text file looks like this:
And used the following code to achieve the same result as of MATLAB:
int main()
{
ifstream Read("myfile.txt");
vector<int> nums;
while ( !Read.eof() ) {
int n;
Read >> n;
nums.push_back(n);
}
// now make a Mat from the vector:
Mat mat(nums);
cout<<mat<<endl; //for testing
Mat mat1d(nums);
Mat mat2d = mat1d.reshape(1, 10);
//
Mat image; //create an empty image. (you can leave it empty ;)
//// Apply the colormap, but on the 2d mat, not on the 2d vector, please:
applyColorMap( mat2d, image, COLORMAP_JET );
// Show the result:
imshow("colormap", image);
waitKey(0);
return 0;
}
But the output from the above is meaningless.
I need to match the results from MATLAB and Opencv.
you will have to reshape the 1d vector, to a 2d mat :
//
// before doing anything else, CLEAN UP YOUR DAMN TXTFILE.
// it should contain nothing than numbers and spaces.
ifstream Read("m.txt");
// be extra picky about the type here.
// we are reading in a 8bit grayscale map.
vector<uchar> nums;
for (int i=0; (i<100*10)&&(!Read.eof()); i++ ) {
int n;
Read >> n;
nums.push_back(n);
}
// now make a Mat from the vector:
Mat mat1d(nums);
Mat mat2d = mat1d.reshape(1, 10);
// your data is in the [0..9] range, so scale up to [0..255] uchar range
mat2d *= (255/10);
cout<<mat2d<<endl; //for testing
//
Mat image; //create an empty image. (you can leave it empty ;)
//// Apply the colormap, but on the 2d mat, not on the 2d vector, please:
applyColorMap( mat2d, image, COLORMAP_HOT );
// Show the result:
imshow("colormap", image);
waitKey(0);