Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Subject
- Posted on
- a homework need help
- 12-05-2008
posted on
December 5, 2008, 12:38 am
December 5, 2008, 12:38 am
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.
Re: a homework need help
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
Re: a homework need help
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!
Re: a homework need help
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], @) : ('', @_);
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
cwilbur@chromatico.net
Re: a homework need help
On Fri, 05 Dec 2008 03:32:56 GMT, sln@netherlands.com wrote:
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
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
Re: a homework need help
Camel wrote:
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
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
Re: a homework need help
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
Re: a homework need help
Found what you're looking for in the obfuscation-contests archives:
#!/usr/bin/perl
G: *S=3Dsub{goto shift};*T=3Dsub{exit shift};*U=3Dsub{print shift};
H: my $A=3D"";my $C=3D0;my $D=3D0;my $E=3D0;my $F=3D0;my $G=3D0;my $H=3D0;=
my @I;
I: if(!defined($A=3D$ARGV[0]))$C=3Dlength
($A);
U("-$A-\n");$D=3D0;
J: $F=3D0;$I[$D]=3D0;if($D!=3D$C)for($G=3D0;$G<$C;$G++){U(substr($A,=
$I
[$G],1))
}$H++;U("\t");$H%8||U("\n");S(M);
K: $F=3D$D;if($F!=3D0)$E=3D$I[0];if($E=3D=3D$C){U("\n---\n$H\n");T(0=
)}
L: $D++;S(J);
M: $D--;$I[$D]++;S(K);
N: $F=3D$I[$D];if($F=3D=3D$C)$E=3D$D-1;
O: if($F=3D=3D$I[$E])$E--;if($E!=3D-1)S(L);
P: $I[$D]++;S(N);
Hand that in ;)
Sverre
Site Timeline
- » FAQ 8.3 How do I do fancy stuff with the keyboard/screen/mouse?
- — Next thread in » PERL Discussions
- » a simple control in an nntp client
- — Previous thread in » PERL Discussions
- » s suffix question
- — Newest thread in » PERL Discussions
- » Adblock Testscript problem
- — The site's Newest Thread. Posted in » HTML Markup Language