Find all combination in group sorting - combinations

I'm trying to find a code that will help me find all combinations in drawing groups.
I have 12 people to sort into 3 groups of 4. But I have some conditions: some people cannot end up in the same group as some others:
1 cannot be with 2, 3 with 4, 5 with 6, 7 with 8, 9 with 10. 11 and 12 can be with anyone.
I'm not trying to obtain the total number of possible combinations but all possibilities listed. I hope I'm being clear...
Any idea?

Related

How to find the number of subsequences with GCD=1 using BOTTOM-UP DP approach?

I came across a question where we are given with a sequence of numbers, and we have to find the number of subsequences with gcd=1.
Eg:
2 3 5 7
count: 11
6 10 15
count: 1
I have solved this question using the top-down approach by the recurrence relation in which we either consider the element or not which goes like -
func(position,current_gcd) = func(position+1,current_gcd) + func(position+1,GCD(curr_gcd,element[position]) ;
Can someone please suggest a tabulation or bottom-up approach for the same..?

Is it possible to get a list of strings from a regex?

Is there a Linux command or software library that returns a list of strings matching a regex it is given as input?
For example, \d would return 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
Given that regexes are already quite power-consuming I'd be really suprised if this was the case. Still, if such a solution does not exist, what alternatives are there?
If using a python parsing module is an option, there is a pyparsing script to expand a regular expression into "all" possible matching strings.
Using its invert(regex) function, the code
for text in invert('\d'):
print(text)
will have the output
0
1
2
3
4
5
6
7
8
9

bash sort so that results are in numeric order as well as string order

Let's say I was comparing two adjacent lines to each other after running sort -u on a file. I find they both match n-characters over from the left side, then begin to disagree at some point, and where the disagreement begins, the first line had a digit "0" to "9". The second line has a non-digit. I want the two lines to swap positions. Why do I want this? Because the digit in the first line meand it is a longer number, and needs to go behind the other, so that these lines, regardless of the digit value, will rearrange from this:
xxxx-xxxx-xxxxxxx.xxxxxxx.xxxx.DD-xx.x.x.x
xxxx-xxxx-xxxxxxx.xxxxxxx.xxxx.D-xx.x.x.x
to this:
xxxx-xxxx-xxxxxxx.xxxxxxx.xxxx.D-xx.x.x.x
xxxx-xxxx-xxxxxxx.xxxxxxx.xxxx.DD-xx.x.x.x
And this:
1
10
11
12
13
14
15
2
3
4
5
6
7
8
9
becomes this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
because it forces numeric values with the same number of digits to be
compared with each other, as those grouped from the left with more digits are moved behind those with fewer digits.
My logic might break down at some point, but until I can code it, I can't check the results returned. So does anybody know how to do this in bash?
sort -g (general numeric sort) should do the trick.

Project Euler # 51 c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to solve problem 51 in project euler.
Here is the problem statement : Project Euler Problem 51
I tried the approach given below.
Generate all the prime numbers between 2 and 10 power 8 using Sieve of Eratosthenes
Gather all n-digit numbers from sieve (i.e .. first i will get all 3 digits, then 4 digits .. so on and compute on it)
check if the number has repeated digit (as per problem statement we have 2 *s of same digit) . If yes , convert into string
if yes, find the position where it repeats and save it as a key (for ex: 12341 will result 04 as key since 1 is the number which is repeated in 0 and 4th position)
now based on the key , insert into the bucket (Bucket is a multimap which contains key as repeated position (04) and value as the prime number)
For each key in bucket, delete the repeated position . If am taking 04 bucket key , then all the prime numbers in that bucket would have the strings repeated in the positions 0 and 4. Am deleting 0th and 4th position of the string which would result my number (12341) as 234 and insert it into a map , which will store the occurence as (234, 8 being the trimmed number and number of occurences).
Now, if the key 234 is repeated 8 times, find the numbers which got trimmed and resulted 234. Those are the answers.
I ran this algorithm in c++ , and for 7 primes 56003, 56113, 56333, 56443, 56663, 56773, and 56993 is resulting less than a second ..
but , for 8 digits , i crossed 10 power 8 primes and still it didnt result any answer. I believe answer is above that limit.
And when i try to generate primes between 2 and 10 power 9 its aborting , since i am storing all the numbers in vector.
My question is ,
Is there any way by which we can fine tune the above mentioned steps and find the answer,
or i need to think some other way to find the answer .
Note: Just for an example i took 12341.
There is atleast one issue in your brute force solution. You are assuming exactly 2 digits are * but question never mentions this. There may be 1 or 3 or more digits which when replaced with the same digits 0-9 still generate primes.
It is impossible to have 8 prime with 1 or 2 * for the following reason:
If you use just 1 *, and let's say replace it with 1 to get a prime(let's call this prime p). Now if p % 3 = 1, you can not replace * with 0, 3 and 6 otherwise the number would become composite(divisible by 3). Throwing away 3 candidates makes it impossible to generate another prime. Next case if p % 3 = 2 you can not replace * with 2, 5 and 8 for the same reason. Making 8-primes with one * impossible for any number of digits.
If you use just 2 *, and let's say replace both with 1 to get a prime(let's call this prime p). Now if p % 3 = 2, you can not replace both * with 0, 3 and 6 otherwise the number would become composite(divisible by 3). Throwing away 3 candidates makes it impossible to generate another prime. Next case if p % 3 = 1 you can not replace * with 2, 5 and 8 for the same reason. Making 8-primes with two * impossible for any number of digits.
This is the reason why your code does not give the required output. You should perhaps try with 3 * characters.

MATLAB IF VALUE LESS THAN

Hi I am writing a matlab code at the moment. I am trying to compare the values in a list to the number 10 and if the value is less than 10 add 1 to the total. However I cannot seem to get the code right. My code so far
tot = 0
for i=1:n
if(x(i)<10)
tot = +1
else
y=0;
end
end
tot
The value I get for tot always = 1 and never increases? Can someone help edit this or if not provide a solution to the problem?
I would agree with the answer mentioned above, that one should avoid for loops for this. There can be a faster solution. Since, he is just interested in the counts, and not value of numbers, so there is no need to index things back.
Given:
a = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
Computing numbers less than 10 (you could put any number here)
answer = sum(a<10);
Good luck!
In languages like MATLAB and R, you really should not use for loops like this, even as an exercise. Each variable can be a vector, and operations can occur on the whole vector at once, rather than element-by-element.
Given:
x = [ 1 2 3 4 11 12 13 14 15 16 ]
To generate a list of all x less than 10 you would say:
x(x<10)
So to count them:
total = length(x(x<10))
No loop needed or wanted!