LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-20-2005, 02:57 PM   #1
sniff
Member
 
Registered: Jan 2003
Location: Durham UK
Distribution: openSUSE/Debian/ubuntu
Posts: 362

Rep: Reputation: 42
Number series generator


Hello,

Bit of help required.

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
 
Old 08-20-2005, 03:49 PM   #2
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Re: Number series generator

Quote:
Originally posted by sniff
Hello,

Bit of help required.

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?
 
Old 08-20-2005, 03:53 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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
 
Old 08-20-2005, 08:26 PM   #4
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
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.

--Jonas
 
Old 08-21-2005, 02:37 PM   #5
sniff
Member
 
Registered: Jan 2003
Location: Durham UK
Distribution: openSUSE/Debian/ubuntu
Posts: 362

Original Poster
Rep: Reputation: 42
Hello,

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.
 
Old 08-21-2005, 02:55 PM   #6
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Quote:
Originally posted by sniff
Hello,

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')))
 
Old 08-21-2005, 03:09 PM   #7
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
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++.
 
Old 08-21-2005, 03:53 PM   #8
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Quote:
an algorithm called "next_permutation"
.. or you could learn the algorithm
 
Old 08-23-2005, 03:26 AM   #9
sniff
Member
 
Registered: Jan 2003
Location: Durham UK
Distribution: openSUSE/Debian/ubuntu
Posts: 362

Original Poster
Rep: Reputation: 42
I might learn the algorithm but it depends on how much I want to make of this project, its only a bit of fun.

Thanks for all your help chaps.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
does GNU's g77 come with a random number generator? terrence Programming 1 08-30-2005 09:29 PM
what is a major number and a minor number ? Menestrel Linux - Newbie 2 10-11-2004 07:53 AM
how do you edit your virtual console number? (or VT number 3) jjorloff1 Linux - General 2 04-03-2004 07:21 PM
why there is a need for minor number and major number for monitor/keyboard in pc? tripathi Solaris / OpenSolaris 1 11-07-2003 09:36 AM
Random number generator for linux Steve_Taylor Programming 12 09-30-2001 04:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration