a homework need help

C

Camel

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

Jürgen Exner

Camel said:
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
 
C

Charlton Wilbur

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
 
S

sln

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
 
S

sln

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
 
J

Josef Moellers

Camel said:
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.
 
C

Charlton Wilbur

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
 
T

Ted Zlatanov

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
 
D

Dr.Ruud

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'
 
S

skeldoy

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
 
P

Pilcrow

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top