Reversed iterator from top to bottom, right to left, instead of completely reversed - c++

I have a std::vector that goes in the following order:
0 3 6
1 4 7
2 5 8
using a reverse iterator I get the following output:
8 5 2
7 4 1
6 3 0
The output I need is:
6 3 0
7 4 1
8 5 2
how would i go about getting that required ordering, at first it seems like i would be able to get a reverse iterator position - vertical size but that gets tricky when my vector is something like this:
0 6 12
1 7 13
2 8 14
3 9 15
4 10 16
5 11 17...

Related

Vector behaving wierdly

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].

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.

Sort rows in a dataframe based on highest values in the whole dataframe

I have a dataframe that has probability values for 3 category columns [A, B, C]. Now I want to sort the rows of this dataframe based on the condition that the row which has the highest probability value in the whole dataframe(irrespective of the columns), should be at the top followed by the row with the second highest probability value and so on.
If someone can help me with this?
In [15]: df = pd.DataFrame(np.random.randint(1, 10, (10,3)))
In [16]: df
Out[16]:
0 1 2
0 9 2 8
1 6 6 9
2 2 4 9
3 2 1 2
4 2 5 3
5 3 4 9
6 8 7 3
7 6 4 1
8 3 3 8
9 7 2 7
In [17]: df.iloc[df.apply(np.max, axis=1).sort_values(ascending=False).index]
Out[17]:
0 1 2
5 3 4 9
2 2 4 9
1 6 6 9
0 9 2 8
8 3 3 8
6 8 7 3
9 7 2 7
7 6 4 1
4 2 5 3
3 2 1 2

Why is srand(time(NULL)) working smoothly even though I repeatedly reset it?

I have a function that creates a vector of size N, and shuffles it:
void rand_vector_generator(int N) {
srand(time(NULL));
vector <int> perm(N);
for (unsigned k=0; k<N; k++) {
perm[k] = k;
}
random_shuffle(perm.begin(),perm.end());
}
I'm calling this from my main function with the loop:
for(int i=0; i<20; i++)
rand_vector_generator(10);
I expected this to not give me sufficient randomness in my shuffling because I'm calling srand(time(NULL)); with every function call and the seed is not too different from successive call to call. My understanding is that I call srand(time(NULL)); once and not multiple times so the seed doesn't "reset".
This thread somewhat affirms what I was expecting the result to be.
Instead, I get:
6 0 3 5 7 8 4 1 2 9
0 8 6 4 2 3 7 9 1 5
8 2 4 9 5 0 6 7 1 3
0 6 1 8 7 4 5 2 3 9
2 5 1 0 3 7 6 4 8 9
4 5 3 0 1 7 2 9 6 8
8 5 2 9 7 0 6 3 4 1
8 4 9 3 1 5 7 0 6 2
3 7 6 0 9 8 2 4 1 5
8 5 2 3 7 4 6 9 1 0
5 4 0 1 2 6 8 7 3 9
2 5 7 9 6 0 4 3 1 8
5 8 3 7 0 2 1 6 9 4
7 4 9 5 1 8 2 3 0 6
1 9 2 3 8 6 0 7 5 4
0 6 4 3 1 2 9 7 8 5
9 3 8 4 7 5 1 6 0 2
1 9 6 5 3 0 2 4 8 7
7 5 1 8 9 3 4 0 2 6
2 9 6 5 4 0 3 7 8 1
These vectors seem pretty randomly shuffled to me. What am I missing? Does the srand call somehow exist on a different scope than the function call so it doesn't get reset every call? Or am I misunderstanding something more fundamental here?
According to standard the use of std::rand in both std::random_shuffle and std::shuffle is implementation-defined (though it is often the case that an std::rand is used this is not guaranteed). Try it on another compiler? Another platform?
If you want to make sure the std::rand is used you should let your code use it explicitly (for example, using lambda expression):
random_shuffle(perm.begin(), perm.end(), []{return std::rand();});
On a somewhat unrelated note, the time()'s precision is one whole second, your code runs way faster than that (I would hope) so those multiple calls to srand() result in resetting to the same-ish seed

Can I use Lists in R as a proxy to data frame having unequal number of columns?

My understanding as far as data frame in R is that it has to be rectangular. It is not possible to have a data frame with unequal column lengths. Can I use the lists in R to achieve this? What are he pros and cons for such an approach?
You can use lists to store whatever you want, even dataframes or other lists! You can indeed assign different length vectors, or even completely different objects. It gives you the same functionality as dataframes in that you can index using the dollar sign:
> fooList <- list(a=1:12, b=1:11, c=1:10)
> fooList$a
[1] 1 2 3 4 5 6 7 8 9 10 11 12
> fooDF <- data.frame(a=1:10, b=1:10, c=1:10)
> fooDF$a
[1] 1 2 3 4 5 6 7 8 9 10
But numeric indexing is different:
> fooList[[1]]
[1] 1 2 3 4 5 6 7 8 9 10 11 12
> fooDF[,1]
[1] 1 2 3 4 5 6 7 8 9 10
as well as the structure and printing method:
> fooList
$a
[1] 1 2 3 4 5 6 7 8 9 10 11 12
$b
[1] 1 2 3 4 5 6 7 8 9 10 11
$c
[1] 1 2 3 4 5 6 7 8 9 10
> fooDF
a b c
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
9 9 9 9
10 10 10 10
Simply said a dataframe is a matrix and a list more of a container.
A list is meant to keep all sorts of stuff together, and a dataframe is the usual data format (a subject/case for each row and a variable for each column). It is used in a lot of analyses, allows to index the scores of a subject, can be more easilly transformed and other things.
However if you have unequal length columns then I doubt each row resembles a subject/case in your data. In that case I guess you don't need much of the functionality of dataframes.
If each row does resemble a subject/case, then you should use NA for any missing values and use a data frame.