# a homework need help

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

1. ### CamelGuest

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.

Camel, Dec 5, 2008

2. ### Jürgen ExnerGuest

"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.

for \$i (1..3) {
use
for \$i(1..\$n){
and preset \$n to the desired length.

jue

Jürgen Exner, Dec 5, 2008

3. ### Tim GreerGuest

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
4. ### Charlton WilburGuest

>>>>> "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
5. ### 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
6. ### 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
7. ### Josef MoellersGuest

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
8. ### Charlton WilburGuest

>>>>> "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
9. ### Ted ZlatanovGuest

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
10. ### Dr.RuudGuest

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
11. ### David CombsGuest

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
12. ### skeldoyGuest

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
13. ### PilcrowGuest

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.
>
>

perldoc -q permute

Pilcrow, Jan 10, 2009