Generating an ordered selection with repetition of designated length of entry - c++
I am writing a function kind of mimicking unordered_tuple from the sage combinatorial functions available in python.
It differs, though, in that the input set I am using is always [10, 9, 8, 7, 6], and only the number of entry varies (not larger than 10).
So, the desired output for entry = 3 and for entry = 4 is,
unordered_tuples([10,9,8,7,6], 3)
[[6, 6, 6],
[6, 6, 7],
[6, 6, 8],
[6, 6, 9],
[6, 6, 10],
[6, 7, 7],
[6, 7, 8],
[6, 7, 9],
[6, 7, 10],
[6, 8, 8],
[6, 8, 9],
[6, 8, 10],
[6, 9, 9],
[6, 9, 10],
[6, 10, 10],
[7, 7, 7],
[7, 7, 8],
[7, 7, 9],
[7, 7, 10],
[7, 8, 8],
[7, 8, 9],
[7, 8, 10],
[7, 9, 9],
[7, 9, 10],
[7, 10, 10],
[8, 8, 8],
[8, 8, 9],
[8, 8, 10],
[8, 9, 9],
[8, 9, 10],
[8, 10, 10],
[9, 9, 9],
[9, 9, 10],
[9, 10, 10],
[10, 10, 10]]
unordered_tuples([10,9,8,7,6], 4)
[[6, 6, 6, 6],
[6, 6, 6, 7],
[6, 6, 6, 8],
[6, 6, 6, 9],
[6, 6, 6, 10],
[6, 6, 7, 7],
[6, 6, 7, 8],
[6, 6, 7, 9],
[6, 6, 7, 10],
[6, 6, 8, 8],
[6, 6, 8, 9],
[6, 6, 8, 10],
[6, 6, 9, 9],
[6, 6, 9, 10],
[6, 6, 10, 10],
[6, 7, 7, 7],
[6, 7, 7, 8],
[6, 7, 7, 9],
[6, 7, 7, 10],
[6, 7, 8, 8],
[6, 7, 8, 9],
[6, 7, 8, 10],
[6, 7, 9, 9],
[6, 7, 9, 10],
[6, 7, 10, 10],
[6, 8, 8, 8],
[6, 8, 8, 9],
[6, 8, 8, 10],
[6, 8, 9, 9],
[6, 8, 9, 10],
[6, 8, 10, 10],
[6, 9, 9, 9],
[6, 9, 9, 10],
[6, 9, 10, 10],
[6, 10, 10, 10],
[7, 7, 7, 7],
[7, 7, 7, 8],
[7, 7, 7, 9],
[7, 7, 7, 10],
[7, 7, 8, 8],
[7, 7, 8, 9],
[7, 7, 8, 10],
[7, 7, 9, 9],
[7, 7, 9, 10],
[7, 7, 10, 10],
[7, 8, 8, 8],
[7, 8, 8, 9],
[7, 8, 8, 10],
[7, 8, 9, 9],
[7, 8, 9, 10],
[7, 8, 10, 10],
[7, 9, 9, 9],
[7, 9, 9, 10],
[7, 9, 10, 10],
[7, 10, 10, 10],
[8, 8, 8, 8],
[8, 8, 8, 9],
[8, 8, 8, 10],
[8, 8, 9, 9],
[8, 8, 9, 10],
[8, 8, 10, 10],
[8, 9, 9, 9],
[8, 9, 9, 10],
[8, 9, 10, 10],
[8, 10, 10, 10],
[9, 9, 9, 9],
[9, 9, 9, 10],
[9, 9, 10, 10],
[9, 10, 10, 10],
[10, 10, 10, 10]]
and the c++ function I wrote follows.
I am actually not an experienced programmer, and I just tried to come up with the right solution, but it is working correctly, but it gives a lot of repetitive solutions.
Honestly, I wrote the function, but I don't even know what I wrote.
I could use set, but it would be very inefficient and I want to know the correct solution for this problem.
Can anyone fix it so that it gives the output above?
#include<iostream>
#include<string>
#include<cstdlib>
#include<vector>
using namespace std;
vector<vector<int> > ut(int);
int main(int argc, char** argv) {
int entry = atoi(argv[1]);
ut(entry);
return 1;
}
vector<vector<int> > ut(int entry) {
vector<vector<int> > ret;
int upper = 10;
vector<int> v(entry, upper);
ret.push_back(v);
typedef vector<int>::iterator iter_t;
iter_t it = v.begin();
int count=0;
int c = 0;
while(v.back() != 6) {
v = ret[count+c];
while(it != v.end()) {
--(*it);
++it;
ret.push_back(v);
++c;
}
it = v.begin();
c=0;
++count;
}
for(int i=0; i<ret.size(); ++i) {
vector<int> tuple = ret[i];
for(int j=0; j<tuple.size(); ++j) {
cout << tuple[j] << ' ';
}
cout<<endl;
}
cout << endl;
return ret;
}
Look here:
vector<vector<int> > ret;
int upper = 10;
vector<int> v(entry, upper);
ret.push_back(v);
typedef vector<int>::iterator iter_t;
iter_t it = v.begin();
int count=0;
int c = 0;
while(v.back() != 6) {
v = ret[count+c];
while(it != v.end()) {
--(*it);
++it;
ret.push_back(v);
++c;
}
it = v.begin();
c=0;
++count;
}
This is just scary. (I understand that you're a beginner; please understand that my criticism is intended to help.) Usually this kind if dense complexity is unnecessary and serves as a hiding place for bugs. Notice that c and it are set before the loop and at the end of the loop, and never used again; we can set them at the beginning of the loop, and the code will be shorter and clearer:
int count=0;
while(v.back() != 6) {
iter_t it = v.begin();
int c = 0;
v = ret[count+c];
while(it != v.end()) {
--(*it);
++it;
ret.push_back(v);
++c;
}
++count;
}
Now we can see that c is never used except when it's zero. (Look at the original code if you don't believe me.) But what's much worse is that it points into v, and then v is assigned a new value. So it probably points into dead memory, and dereferencing it causes undefined behavior. And it's not clear how this code is intended to work anyway.
Try this:
vector<int> v(n,6);
vector<int>::iterator itr1;
do{
ret.push_back(v);
itr1 = v.begin();
while(++(*itr1)>10){
if(++itr1==v.end())
break;
}
for(vector<int>::iterator itr2 = v.begin(); itr2!=itr1; ++itr2)
*itr2 = *itr1;
}
while(itr1!=v.end());
A good place to start with permutation problems is recursion. Taking this approach, to build all the outputs of length 3, you choose a digit from your set [6, 7, 8, 9, 10], and then append to it all the outputs of length 2, with the input set constrained to start from the digit chosen. So, if, e.g. you chose 7, your input set for the first recursive call would be [ 7, 8, 9, 10]. I.e., the recursive call in this case would be append to [ 7 ] all outputs of length 2 from the input [ 7, 8, 9, 10]
A program that implements this idea is below. I'd be interested to see if anyone can come up with a non-recursive solution.
#include "stdafx.h"
#include <iostream>
#include <vector>
typedef std::vector<int> intvec;
typedef std::vector<intvec> intvecs;
void GenerateUnOrderedIntVecs(
const int* remainingInput, int remainingInputLen,
const intvec& outputSoFar, int remainingOutputLen,
intvecs& output)
{
if (remainingOutputLen == 0) { // base case of recursion
output.push_back(outputSoFar);
return;
}
// For all digits in our input
for(int i=0; i<remainingInputLen; ++i) {
// Add the ith digit to our output so far
intvec outputSoFar2(outputSoFar);
outputSoFar2.push_back(remainingInput[i]);
// The recursion
GenerateUnOrderedIntVecs(
remainingInput + i, // input set constrained to start from chosen digit
remainingInputLen - i, // input set is shorter
outputSoFar2, // one digit longer than the parameter outputSoFar
remainingOutputLen -1, // so we need one digit less as we recurse
output);
}
}
int main(int argc, _TCHAR* argv[])
{
const int nToChooseFrom = 5;
const int nToChooose = 3;
const int input[nToChooseFrom] = { 6, 7, 8, 9, 10 }; // provide input in sorted order (or sort it!)
intvecs output;
GenerateUnOrderedIntVecs(
input, nToChooseFrom,
intvec(), nToChooose,
output);
for(intvecs::const_iterator i=output.begin(); i!=output.end(); ++i) {
std::cout << "[ ";
const intvec& unordered_tuple = *i;
for(intvec::const_iterator j = unordered_tuple.begin(); j!=unordered_tuple.end(); ++j) {
std::cout << *j << " ";
}
std::cout << "]\n";
}
return 0;
}
It seems to work on both your examples (but I only checked the first thoroughly). If you can't see how it works by reading the code, a good approach would be to run it in a debugger (that's what I had to do to get it to work!:)
Related
Sort an integer array by converting an element to its sum of numbers
The question I am given is We are given an array. In one operation we can replace any element of the array with any two elements that sum to that element. For example: array = {4, 11, 7}. In one operation you can replace array[1] with 5 and 6 which sums to 11. So the array becomes array = {4, 5, 6, 7} Return the minimum number of steps in which the whole array can be sorted in non-decreasing order. Along with array in sorted order. For example: array = {3,9,3} I think the answer will be 9 will be converted to 3,3,3 But I cannot think of a general formula of doing it. My thoughts on the solution are Suppose we want to convert number 6 and 9 We use if and else IF we see that we divide a number by 2 and take ceiling but it is greater than the number on it's right side(last example in the question) then we keep subtracting that number(3) until we get integer 0. That is 9 = 3(number on right of 9 in array in last example) - 3 - 3 ELSE simply do ceiling(num / 2) to get first number and then num - ceil(num / 2) to ger second. 7 will be 4 and 3. Please can someone think of a general formula for doing it?
Edy's way (as I interpret it) in Python: def solve(xs): limit = 10**100 out = [] for x in reversed(xs): parts = (x - 1) // limit + 1 limit, extra = divmod(x, parts) out += extra * [limit+1] + (parts - extra) * [limit] print(len(out) - len(xs), out[::-1]) solve([4, 11, 7]) solve([3, 9, 3]) solve([9, 4, 15, 15, 28, 23, 13]) Output showing steps and result array for the three test cases (Try it online!): 1 [4, 5, 6, 7] 2 [3, 3, 3, 3, 3] 8 [3, 3, 3, 4, 5, 5, 5, 7, 8, 9, 9, 10, 11, 12, 13] An output illustrating the progress: [4, 11, 7] = (input) [4, 11, [7]] [4, [5, 6], [7]] [[4], [5, 6], [7]] [3, 9, 3] = (input) [3, 9, [3]] [3, [3, 3, 3], [3]] [[3], [3, 3, 3], [3]] [9, 4, 15, 15, 28, 23, 13] = (input) [9, 4, 15, 15, 28, 23, [13]] [9, 4, 15, 15, 28, [11, 12], [13]] [9, 4, 15, 15, [9, 9, 10], [11, 12], [13]] [9, 4, 15, [7, 8], [9, 9, 10], [11, 12], [13]] [9, 4, [5, 5, 5], [7, 8], [9, 9, 10], [11, 12], [13]] [9, [4], [5, 5, 5], [7, 8], [9, 9, 10], [11, 12], [13]] [[3, 3, 3], [4], [5, 5, 5], [7, 8], [9, 9, 10], [11, 12], [13]] Code for that (Try it online!): def solve(xs): print(xs, '= (input)') limit = 10**100 for i, x in enumerate(reversed(xs)): parts = (x - 1) // limit + 1 limit, extra = divmod(x, parts) xs[~i] = (parts - extra) * [limit] + extra * [limit+1] print(xs) print()
You would want to scan from the right to the left. For convenient explanation, let's mark the right-most element x_0, and the left-most x_{n-1} (n can increase as you split a number into two). If x_{i} > x_{i-1}, you would want to divide x_{i} into ((x_{i} - 1) / x_{i-1}) + 1 parts, where / is integer division, as evenly as possible. So for example: If x_{i} = 15, x_{i-1] = 5, divide x_{i} into (15-1)/5 + 1 = 3 parts: (5, 5, 5). If x_{i} = 19, x_{i-1] = 5, divide x_{i} into (19-1)/5 + 1 = 4 parts: (4, 5, 5, 5). (To divide a number equally into a non-decreasing sequence would require a bit of calculation, which shouldn't be too difficult.) Once you know the sequence, it would be straightforward to repeatedly split a number into 2 to produce that sequence.
How to put in variable as list
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
Dictionary Keys-Repeat (List<int>) in Python#
This is an assignment; I have worked over it and somewhere get stuck; This is the input from text file: min: 1,2,3,5,6 max: 1,2,3,5,6 avg: 1,2,3,5,6 p90: 1,2,3,4,5,6,7,8,9,10 sum: 1,2,3,5,6 min: 1,5,6,14,24 max: 2,3,9 p70: 1,2,3 This is the required output to the text file: The min of [1, 2, 3, 5, 6] is 1 The max of [1, 2, 3, 5, 6] is 6 The avg of [1, 2, 3, 5, 6] is 3.4 The 90th percentile of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is 9 The sum of [1, 2, 3, 5, 6] is 17 The min of [1, 5, 6, 14, 24] is 1 The max of [2, 3, 9] is 9 The 70th percentile of [1, 2, 3] is 2 This is my work-out to the text file: The min of [1, 5, 6, 14, 24] is 1 The max of [2, 3, 9] is 9 The avg of [1, 2, 3, 5, 6] is 3.4 The p90 of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is 9.0 The sum of [1, 2, 3, 5, 6] is 17 The p70 of [1, 2, 3] is 2.1 Logics I wrote a function to read from a file and insert the keys:values into dictionary; Below is the dictionary OrderedDict([('min', [1, 5, 6, 14, 24]), ('max', [2, 3, 9]), ('avg', [1, 2, 3, 5, 6]), ('p90', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), ('sum', [1, 2, 3, 5, 6]), ('p70', [1, 2, 3])]) From here I compute the required and write the results to the file My question; how can I make the keys min and max duplicate in the dictionary as you can see the have been overwritten
The problem is, that the keys in a dictionary are unique. That means, a dictionary can only have one entry with the key 'min'. That's why your first entry with the key 'min' gets overwritten by the second. To solve this I would recommend to change the structure type from Dictionary to something else (like a nested List). list = [] list.append(['min', [1, 2, 3, 5, 6]]) you will get a list of rows, each containing the function (like 'min') and the number array. More about Lists
Find and print equals in a list
Given a list of 50 random integers in the range [n,k], where n is less than k. I would like to find how many numbers are equal to each other and print them.
This can be done with Tally as follows. First, let's generate a test list: list = RandomInteger[{5, 10}, 50] (* ==> {10, 7, 5, 7, 10, 8, 6, 6, 7, 6, 6, 8, 7, 5, 6, 9, 10, 6, 9, 6, 10, 8, 10, 8, 9, 7, 5, 9, 8, 5, 9, 7, 5, 7, 9, 10, 6, 6, 7, 7, 5, 6, 9, 10, 5, 6, 6, 6, 10, 9} *) Then count them: Tally[list] (* ==> {{10, 8}, {7, 9}, {5, 7}, {8, 5}, {6, 13}, {9, 8}} *)
Unknown error in array initialization: invalid in-class initialization of static data member of non- integral type `const unsigned char[256]'
I was trying to make a Intel 8080 CPU emulator (then I'd like to emulate Space Invaders, which use it). I coded nearly complete implementation of this CPU (thanks to MAME and Tickle project (mostly) ;) ) except undocument instructions (0x08, 0x10, 0x18, 0x20, 0x28, 0x30, 0x38, 0x0CB, 0x0D9, 0x0DD, 0x0ED, 0x0FD). I've have only problems when I compile it, I don't know why. This is the code: static const unsigned char cycles_table[256] = { /* 8080's Cycles Table */ /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ /*0*/ 4, 10, 7, 5, 5, 5, 7, 4, 0, 10, 7, 5, 5, 5, 7, 4, /*1*/ 0, 10, 7, 5, 5, 5, 7, 4, 0, 10, 7, 5, 5, 5, 7, 4, /*2*/ 0, 10, 16, 5, 5, 5, 7, 4, 0, 10, 16, 5, 5, 5, 7, 4, /*3*/ 0, 10, 13, 5, 10, 10, 10, 4, 0, 10, 13, 5, 5, 5, 7, 4, /*4*/ 5, 5, 5, 5, 5, 5, 7, 5, 5, 5, 5, 5, 5, 5, 7, 5, /*5*/ 5, 5, 5, 5, 5, 5, 7, 5, 5, 5, 5, 5, 5, 5, 7, 5, /*6*/ 5, 5, 5, 5, 5, 5, 7, 5, 5, 5, 5, 5, 5, 5, 7, 5, /*7*/ 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 7, 5, /*8*/ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, /*9*/ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, /*A*/ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, /*B*/ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, /*C*/ 5, 10, 10, 10, 11, 11, 7, 11, 5, 10, 10, 0, 11, 17, 7, 11, /*D*/ 5, 10, 10, 10, 11, 11, 7, 11, 5, 0, 10, 10, 11, 0, 7, 11, /*E*/ 5, 10, 10, 18, 11, 11, 7, 11, 5, 5, 10, 4, 11, 0, 7, 11, /*F*/ 5, 10, 10, 4, 11, 11, 7, 11, 5, 5, 10, 4, 11, 0, 7, 11 }; g++ takes me this error: 8080.h:521: error: invalid in-class initialization of static data member of non- integral type `const unsigned char[256]' This array is in a class called i8080.
Like it says, you cannot initialize static non-integral types in a class definition. That is, you could do this: static const unsigned value = 123; static const bool value_again = true; But not anything else. What you should do is place this in your class definition: static const unsigned char cycles_table[256]; And in the corresponding source file, place what you have: const unsigned char i8080::cycles_table[256] = // ... What this does is say (in the definition), "Hey, there's gonna be this array." and in the source file, "Hey, here's that array."
Static data members need to be initialised outside of the class.
You cannot initialize a static array embedded within a class like this: class Thing { public: static const int [3] = {1, 2, 3}; }; You have to do it like this: thing.h: class Thing { public: static const int vals[3];; }; thing.cpp: const int Thing::vals[3] = {1, 2, 3};