a homework need help

Discussion in 'Perl Misc' started by Camel, Dec 5, 2008.

  1. Camel

    Camel Guest

    I have no clue with my homework, can anyone help me?

    Three digits "1,2,3", how many 3-digit numbers can be made? print all the
    numbers

    I know how to calculate the total numbers and using loop to print all the
    numbers. The problem is how to write a generic program that takes any number
    of digits. For example, if given "1,2,3,4,5" digits, you still can use your
    program to print all the combinations of 5-digit numbers without modifing
    it.

    Thanks in advance.
     
    Camel, Dec 5, 2008
    #1
    1. Advertising

  2. "Camel" <> wrote:
    >I have no clue with my homework, can anyone help me?
    >
    >Three digits "1,2,3", how many 3-digit numbers can be made? print all the
    >numbers
    >
    >I know how to calculate the total numbers and using loop to print all the
    >numbers. The problem is how to write a generic program that takes any number
    >of digits.


    Quite simple. If you really know how to do the loop for a constant
    number like e.g. 3, then just use a variable instead of that fixed
    number 3 everywhere where you make any assumptions about 3.

    E.g. instead of
    for $i (1..3) {
    use
    for $i(1..$n){
    and preset $n to the desired length.

    jue
     
    Jürgen Exner, Dec 5, 2008
    #2
    1. Advertising

  3. Camel

    Tim Greer Guest

    Camel wrote:

    > I have no clue with my homework, can anyone help me?
    >



    What do you have so far? What have you tried?
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Dec 5, 2008
    #3
  4. >>>>> "C" == Camel <> writes:

    C> I have no clue with my homework, can anyone help me? Three
    C> digits "1,2,3", how many 3-digit numbers can be made? print all
    C> the numbers

    C> I know how to calculate the total numbers and using loop to print
    C> all the numbers. The problem is how to write a generic program
    C> that takes any number of digits. For example, if given
    C> "1,2,3,4,5" digits, you still can use your program to print all
    C> the combinations of 5-digit numbers without modifing it.

    Recursion.

    #!/usr/bin/perl

    sub f
    {
    my ($p, @l) = ref $_[1] ? ($_[0], @{$_[1]}) : ('', @_);
    return $_[0] if @l == 0;
    return map { $p . $_ }
    map { f(@$_) }
    map { [ $::x = $_, [ grep { $::x ne $_ } @l ]] } @l;
    }

    print "12: ", join ('; ', f(1..2)), "\n";
    print "12345: ", join ('; ', f(1..5)), "\n";
    print "abc: ", join ('; ', f(qw/a b c/)), "\n";

    (I strongly recommend against turning in this code. Your professor will
    be able to tell you didn't write it.)

    Charlton





    --
    Charlton Wilbur
     
    Charlton Wilbur, Dec 5, 2008
    #4
  5. Camel

    Guest

    On Thu, 04 Dec 2008 21:30:19 -0500, Charlton Wilbur <> wrote:

    >>>>>> "C" == Camel <> writes:

    >
    > C> I have no clue with my homework, can anyone help me? Three
    > C> digits "1,2,3", how many 3-digit numbers can be made? print all
    > C> the numbers


    Do you know number series? Can you describe mathmatically the relavent
    expression? This is not a Perl issue.

    sln
     
    , Dec 5, 2008
    #5
  6. Camel

    Guest

    On Fri, 05 Dec 2008 03:32:56 GMT, wrote:

    >On Thu, 04 Dec 2008 21:30:19 -0500, Charlton Wilbur <> wrote:
    >
    >>>>>>> "C" == Camel <> writes:

    >>
    >> C> I have no clue with my homework, can anyone help me? Three
    >> C> digits "1,2,3", how many 3-digit numbers can be made? print all
    >> C> the numbers

    >
    >Do you know number series? Can you describe mathmatically the relavent
    >expression? This is not a Perl issue.
    >

    Hey, you know once I used to be a mechanical engineer. On my first
    job, it was told to me by some old timer drafter of a problem that
    perplexed some minds there.

    Seems there was a circle problem they couldn't solve without a nightmare
    computer program.

    The problem was as such:

    There is a circle of unknown diameter, that needed to be divided into
    3 equal horizontal areas (2 horizontal lines drawn).

    I took out my pencil/paper wrote down every circle equation I knew,
    grabbed my HP calculator (programmable) plugged in an itterative equation,
    and solved in emphirically right there on the spot in 5 minutes.

    From what I heard it took 4 weeks to write a program that took 8 hours to
    do it (given a diameter).

    So, whats the moral of the story?

    sln
     
    , Dec 5, 2008
    #6
  7. Camel wrote:
    > I have no clue with my homework, can anyone help me?
    >
    > Three digits "1,2,3", how many 3-digit numbers can be made? print all the
    > numbers
    >
    > I know how to calculate the total numbers and using loop to print all the
    > numbers. The problem is how to write a generic program that takes any number
    > of digits. For example, if given "1,2,3,4,5" digits, you still can use your
    > program to print all the combinations of 5-digit numbers without modifing
    > it.


    The answer is: as many as there are permutations of the <n> digits.

    As Tim Greer has pointed out: we don't do other people's homework, but
    we're willing to help get over some problem or the other. So post
    whatever you have, tell us what you expect the program to do and whereit
    doesn't work as expected, and we may be able to help you.

    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
     
    Josef Moellers, Dec 5, 2008
    #7
  8. >>>>> "JM" == Josef Moellers <> writes:

    JM> As Tim Greer has pointed out: we don't do other people's
    JM> homework,

    Oh, but we do -- just not in any sort of useful way.

    Charlton


    --
    Charlton Wilbur
     
    Charlton Wilbur, Dec 5, 2008
    #8
  9. Camel

    Ted Zlatanov Guest

    On Thu, 4 Dec 2008 16:38:52 -0800 "Camel" <> wrote:

    C> I have no clue with my homework, can anyone help me?
    C> Three digits "1,2,3", how many 3-digit numbers can be made? print all the
    C> numbers

    C> I know how to calculate the total numbers and using loop to print all the
    C> numbers. The problem is how to write a generic program that takes any number
    C> of digits. For example, if given "1,2,3,4,5" digits, you still can use your
    C> program to print all the combinations of 5-digit numbers without modifing
    C> it.

    I'll give you a hint: if you need the combinations of N digits, you can
    generate them with the combinations of N-1 digits. Print out the list
    of 1-digit numbers and compare it to the list of 2-digit and 3-digit
    numbers and you'll see what I mean.

    0 1 2 3 4 5 6 7 8 9

    compare to:

    00 01 02 03 04 ... 09
    10 11 12 13 14 ... 19
    ....
    90 91 92 93 94 ... 99

    and then

    000 001 002 003 004 ... 099
    100 101 102 103 104 ... 199
    ....
    900 901 902 903 904 ... 999

    Do you see the pattern? It should be obvious by now.

    Ted
     
    Ted Zlatanov, Dec 5, 2008
    #9
  10. Camel

    Dr.Ruud Guest

    Camel schreef:

    > Three digits "1,2,3", how many 3-digit numbers can be made? print all
    > the numbers
    >
    > I know how to calculate the total numbers and using loop to print all
    > the numbers. The problem is how to write a generic program that takes
    > any number of digits. For example, if given "1,2,3,4,5" digits, you
    > still can use your program to print all the combinations of 5-digit
    > numbers without modifing it.


    $ echo 123 |perl -anlF// -e'$"=",";print for glob "{@F}"x@F'

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Dec 6, 2008
    #10
  11. Camel

    David Combs Guest

    In article <ghb7vq$u21$-siemens.com>,
    Josef Moellers <> wrote:
    >Camel wrote:
    >> I have no clue with my homework, can anyone help me?
    >>
    >> Three digits "1,2,3", how many 3-digit numbers can be made? print all the
    >> numbers
    >>
    >> I know how to calculate the total numbers and using loop to print all the
    >> numbers. The problem is how to write a generic program that takes any number
    >> of digits. For example, if given "1,2,3,4,5" digits, you still can use your
    >> program to print all the combinations of 5-digit numbers without modifing
    >> it.

    >
    >The answer is: as many as there are permutations of the <n> digits.
    >


    Followed by stripping out any leftmost zeros.

    David
     
    David Combs, Dec 15, 2008
    #11
  12. Camel

    skeldoy Guest

    On Dec 5, 1:38 am, "Camel" <> wrote:
    > Three digits "1,2,3", how many 3-digit numbers can be made? print all the
    > numbers


    Found what you're looking for in the obfuscation-contests archives:

    #!/usr/bin/perl
    G: *S=sub{goto shift};*T=sub{exit shift};*U=sub{print shift};
    H: my $A="";my $C=0;my $D=0;my $E=0;my $F=0;my $G=0;my $H=0;my @I;
    I: if(!defined($A=$ARGV[0])){U(qw(ARGV[0]?));U("\n");T(1)}$C=length
    ($A);
    U("-$A-\n");$D=0;
    J: $F=0;$I[$D]=0;if($D!=$C){S(K)}for($G=0;$G<$C;$G++){U(substr($A,$I
    [$G],1))
    }$H++;U("\t");$H%8||U("\n");S(M);
    K: $F=$D;if($F!=0){S(N)}$E=$I[0];if($E==$C){U("\n---\n$H\n");T(0)}
    L: $D++;S(J);
    M: $D--;$I[$D]++;S(K);
    N: $F=$I[$D];if($F==$C){S(M)}$E=$D-1;
    O: if($F==$I[$E]){S(P)}$E--;if($E!=-1){S(O)}S(L);
    P: $I[$D]++;S(N);


    Hand that in ;)

    Sverre
     
    skeldoy, Dec 17, 2008
    #12
  13. Camel

    Pilcrow Guest

    On Thu, 4 Dec 2008 16:38:52 -0800, "Camel" <>
    wrote:

    >I have no clue with my homework, can anyone help me?
    >
    >Three digits "1,2,3", how many 3-digit numbers can be made? print all the
    >numbers
    >
    >I know how to calculate the total numbers and using loop to print all the
    >numbers. The problem is how to write a generic program that takes any number
    >of digits. For example, if given "1,2,3,4,5" digits, you still can use your
    >program to print all the combinations of 5-digit numbers without modifing
    >it.
    >
    >Thanks in advance.
    >



    perldoc -q permute
     
    Pilcrow, Jan 10, 2009
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    0
    Views:
    692
    Guest
    Feb 25, 2004
  2. Rhino

    need help with homework

    Rhino, Nov 16, 2003, in forum: Java
    Replies:
    1
    Views:
    309
    Roedy Green
    Nov 16, 2003
  3. Allens Mail

    Need Help On Homework

    Allens Mail, Jul 13, 2003, in forum: C++
    Replies:
    1
    Views:
    380
    Aggro
    Jul 14, 2003
  4. Mike
    Replies:
    1
    Views:
    333
    Phlip
    Nov 23, 2004
  5. Karl Heinz Buchegger

    Re: I need help with my homework!! Emergency!!!

    Karl Heinz Buchegger, Nov 23, 2004, in forum: C++
    Replies:
    1
    Views:
    395
    Raymond Martineau
    Nov 23, 2004
Loading...

Share This Page