Announcement

Collapse
No announcement yet.

Need programming help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need programming help

    Can someone tell me a way to print all possible combinations of a string of letters?

    Eg. if I give the input "abc", it should give the output

    abc
    acb
    bac
    bca
    cab
    cba

    I have a rudimentary knowledge of C/C++, and a (very very) little bit of Lisp/Scheme.

  • #2
    I would be more than happy to help you. I love to program, and I know a lot about C++. I dont think there are any built in functions that will do that for you, but there are some that can help you do this. How long can the string be?
    Donate to the American Red Cross.
    Computer Science or Engineering Student? Compete in the Microsoft Imagine Cup today!.

    Comment


    • #3
      one way you can do this is pure force way, using loops to come up with all possible comibations of those letters. I also think that you can use a spanning tree to do this if you need something that is faster.
      Donate to the American Red Cross.
      Computer Science or Engineering Student? Compete in the Microsoft Imagine Cup today!.

      Comment


      • #4
        Say a limit of 25 characters?

        I know that that would be practically impossible - it works out to 25! combinations - but I'm taking that as the theoretical maximum I'd want.

        Comment


        • #5
          yes that would take a long time, but it would not be 25 combinations, but 25 to the 25 power of combinations . So I think spanning tree is your best bet. I need to go to sleep though, but I will look up some stuff when I get up.
          Donate to the American Red Cross.
          Computer Science or Engineering Student? Compete in the Microsoft Imagine Cup today!.

          Comment


          • #6
            We can use each letter only the number of times it appeared in the original string. If that is the case, then the number of combinations reduces to 25! (= 25x24x23x22....x1), or even less. If we are allowed to use the same element an arbitrary number of times, then it becomes 25^25.

            Comment


            • #7
              I initially thought of a recursive function that iterates over each element, and for each element gives a string with that element removed to itself, thus resulting in the result, but I couldn't work it out.

              Comment


              • #8
                Quick and dirty version in php...


                $chars = Array('a', 'b', 'c');
                $used_indices = Array();

                gen($used_indices);

                function gen(&$used_indices) {
                global $chars;

                if (sizeof($used_indices) < sizeof($chars)) {
                for ($i=0; $i if (!in_array($i, $used_indices)) {
                array_push($used_indices, $i);
                gen($used_indices);
                array_pop($used_indices);
                }
                }
                } else {
                for ($i=0; $i echo $chars[$used_indices[$i]];
                }
                echo "\n";
                }
                }

                http://www.hardware-wiki.com - A wiki about computers, with focus on Linux support.

                Comment


                • #9
                  The problem solved in ML; this solution is nicer.

                  This is programmed using a very functionel strategy, somewhat different from the PHP one used above. It seemed so much nicer that I tried translating it to php afterwards, but I ended up with a PHP program larger and uglier than the PHP program above.

                  Code:
                  val chars = ["a", "b", "c"];
                  
                  fun gen (a::arest) bs       res = (gen (arest@bs) [] (a::res))@(gen arest (a::bs) res)
                    | gen []       []       res = [res]
                    | gen []       bs       res = [];
                  
                  gen     chars    []       [];
                  http://www.hardware-wiki.com - A wiki about computers, with focus on Linux support.

                  Comment


                  • #10
                    Re: Need programming help

                    Originally posted by aneeshm
                    Can someone tell me a way to print all possible combinations of a string of letters?

                    Eg. if I give the input "abc", it should give the output

                    abc
                    acb
                    bac
                    bca
                    cab
                    cba

                    I have a rudimentary knowledge of C/C++, and a (very very) little bit of Lisp/Scheme.
                    Those are called permutations, not combinations. With that knowledge, you should be able to Google an answer in C++ easily.
                    This is Shireroth, and Giant Squid will brutally murder me if I ever remove this link from my signature | In the end it won't be love that saves us, it will be mathematics | So many people have this concept of God the Avenger. I see God as the ultimate sense of humor -- SlowwHand

                    Comment


                    • #11
                      *Smacks head*

                      Damn the incorrect terminology!

                      Comment


                      • #12
                        I would like to see a C/C++ program as short and simple as my ML program. I bet that isn't possible
                        http://www.hardware-wiki.com - A wiki about computers, with focus on Linux support.

                        Comment


                        • #13
                          WTF is ML?
                          THEY!!111 OMG WTF LOL LET DA NOMADS AND TEH S3D3NTARY PEOPLA BOTH MAEK BITER AXP3REINCES
                          AND TEH GRAAT SINS OF THERE [DOCTRINAL] INOVATIONS BQU3ATH3D SMAL
                          AND!!1!11!!! LOL JUST IN CAES A DISPUTANT CALS U 2 DISPUT3 ABOUT THEYRE CLAMES
                          DO NOT THAN DISPUT3 ON THEM 3XCAPT BY WAY OF AN 3XTARNAL DISPUTA!!!!11!! WTF

                          Comment


                          • #14
                            The first programming taught to new students at the Copenhagen University of Computer Science. http://en.wikipedia.org/wiki/ML_programming_language
                            http://www.hardware-wiki.com - A wiki about computers, with focus on Linux support.

                            Comment


                            • #15
                              Originally posted by LordShiva
                              WTF is ML?
                              you didn't have to take the "History of dead programming languages" course?

                              Comment

                              Working...
                              X