Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   a homework need help (http://www.velocityreviews.com/forums/t909116-a-homework-need-help.html)

Camel 12-05-2008 12:38 AM

a homework need help
 
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ürgen Exner 12-05-2008 01:25 AM

Re: a homework need help
 
"Camel" <jiao_he@sbcglobal.net> 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

Tim Greer 12-05-2008 02:26 AM

Re: a homework need help
 
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!

Charlton Wilbur 12-05-2008 02:30 AM

Re: a homework need help
 
>>>>> "C" == Camel <jiao_he@sbcglobal.net> 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
cwilbur@chromatico.net

sln@netherlands.com 12-05-2008 03:32 AM

Re: a homework need help
 
On Thu, 04 Dec 2008 21:30:19 -0500, Charlton Wilbur <cwilbur@chromatico.net> wrote:

>>>>>> "C" == Camel <jiao_he@sbcglobal.net> 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

sln@netherlands.com 12-05-2008 03:44 AM

Re: a homework need help
 
On Fri, 05 Dec 2008 03:32:56 GMT, sln@netherlands.com wrote:

>On Thu, 04 Dec 2008 21:30:19 -0500, Charlton Wilbur <cwilbur@chromatico.net> wrote:
>
>>>>>>> "C" == Camel <jiao_he@sbcglobal.net> 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

Josef Moellers 12-05-2008 12:49 PM

Re: a homework need help
 
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

Charlton Wilbur 12-05-2008 04:22 PM

Re: a homework need help
 
>>>>> "JM" == Josef Moellers <josef.moellers@fujitsu-siemens.com> 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
cwilbur@chromatico.net

Ted Zlatanov 12-05-2008 04:43 PM

Re: a homework need help
 
On Thu, 4 Dec 2008 16:38:52 -0800 "Camel" <jiao_he@sbcglobal.net> 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

Dr.Ruud 12-06-2008 03:14 AM

Re: a homework need help
 
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."


All times are GMT. The time now is 05:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.