ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Okay, I have the numbers 1 to 10. I need to do tests on the numbers, that is I need to add the numbers in different combinations so that they equal the same end values. So the numbers are assigned to variables, thus.
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
j = 10
Right,
I have the tests all written but it is the next series of numbers that I'm having difficulty generating.
I need to write a program that can generate all possible order permutations of the above numbers using each number only once.
So the the next combination could be....
a = 1
b = 10
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
j = 2
Okay, I have the numbers 1 to 10. I need to do tests on the numbers, that is I need to add the numbers in different combinations so that they equal the same end values. So the numbers are assigned to variables, thus.
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
j = 10
Right,
I have the tests all written but it is the next series of numbers that I'm having difficulty generating.
I need to write a program that can generate all possible order permutations of the above numbers using each number only once.
So the the next combination could be....
a = 1
b = 10
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
j = 2
Ideas?
Cheers,
Sniff
I have some python functions that can take a sequence of elements and output all the possible permutations of those elements. Would that help?
If you have two numbers, you can have the permutations by switching them: a, b; b,a.
When you have three, you take one of them (first, then second, then third) and are back to the problem with two (the 'taken' one will be first).
With four, you can take one of them (one by one) and you have the problem with three.
Such recursive solution should work nicely. That's first idea, probably there are better
This is obviously a homework problem. The point of it that you should learn from it. Asking for hints is usually ok, asking for complete solutions isn't. Don't you have a TA or some class- or coursemates you can ask?
If indeed it isn't a homework problem, then I'm sorry.
Its not a homework problem I'm a 23 year old genetic engineer. Its is just me fiddling about with stuff. Carl the python stuff would be useful I program in C but I'm sure that it will point me in the right direction, thanks. I have also found a Donald Knuth paper that looks like it might be helpful. I'm not after a spoon fed answer just a nudge in the right direction.
Its not a homework problem I'm a 23 year old genetic engineer. Its is just me fiddling about with stuff. Carl the python stuff would be useful I program in C but I'm sure that it will point me in the right direction, thanks. I have also found a Donald Knuth paper that looks like it might be helpful. I'm not after a spoon fed answer just a nudge in the right direction.
This originally came from the activestate web site.
I had translated this to C++ once upon a time, though I don't know where the translation got to. If I can find it, I will let you know.
Code:
#!/usr/bin/env python
__version__ = "1.0"
"""xpermutations.py
Generators for calculating a) the permutations of a sequence and
b) the combinations and selections of a number of elements from a
sequence. Uses Python 2.2 generators.
Similar solutions found also in comp.lang.python
Keywords: generator, combination, permutation, selection
See also: http://aspn.activestate.com/ASPN/Coo.../Recipe/105962
See also: http://aspn.activestate.com/ASPN/Coo...n/Recipe/66463
See also: http://aspn.activestate.com/ASPN/Coo...n/Recipe/66465
"""
from __future__ import generators
def xcombinations(items, n):
if n==0: yield []
else:
for i in xrange(len(items)):
for cc in xcombinations(items[:i]+items[i+1:],n-1):
yield [items[i]]+cc
def xuniqueCombinations(items, n):
if n==0: yield []
else:
for i in xrange(len(items)):
for cc in xuniqueCombinations(items[i+1:],n-1):
yield [items[i]]+cc
def xselections(items, n):
if n==0: yield []
else:
for i in xrange(len(items)):
for ss in xselections(items, n-1):
yield [items[i]]+ss
def xpermutations(items):
return xcombinations(items, len(items))
if __name__=="__main__":
print "Permutations of 'love'"
for p in xpermutations(['l','o','v','e']): print ''.join(p)
print
print "Combinations of 2 letters from 'love'"
for c in xcombinations(['l','o','v','e'],2): print ''.join(c)
print
print "Unique Combinations of 2 letters from 'love'"
for uc in xuniqueCombinations(['l','o','v','e'],2): print ''.join(uc)
print
print "Selections of 2 letters from 'love'"
for s in xselections(['l','o','v','e'],2): print ''.join(s)
print
print map(''.join, list(xpermutations('done')))
Actually, the C++ code I did turned out to be based on the *combinations* part of the python script. I did recall the standard C++ library actually has an algorithm called "next_permutation", though. It might be just what you are looking for if you could link to C++.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.