CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Suggestions

 



dianazhengdz
Novice

Jun 23, 2005, 11:32 PM

Post #1 of 9 (1295 views)
Suggestions Can't Post

Now i have another problem...

The problem is when we enter a number(that is powerset), we will have to print out the subset of that power set. Eg input

Code
 2



My output is like this

Code
 NULL  
1
2
1,2

Because the powerset of 2 is

P(0) = { NULL }
P(1) = { NULL, {1} }
P(2) = { NULL, {1}, {2}, {1,2} }

Any suggestion on how can i do that??
Thanks


KevinR
Veteran


Jun 24, 2005, 2:02 PM

Post #2 of 9 (1286 views)
Re: [dianazhengdz] Suggestions [In reply to] Can't Post

What is the output you want if the number 2 is "entered"? How is the data being stored? Text file or database of some type?
-------------------------------------------------


dianazhengdz
Novice

Jun 24, 2005, 3:31 PM

Post #3 of 9 (1284 views)
Re: [KevinR] Suggestions [In reply to] Can't Post

The input is a textfile that contain a number


Code
  

2



The program should do calculate and return the result (which is the powerset of 2) in this manner


Code
  

NULL
1
2
1,2



And the table for powerset will be something like this

P(0) = { NULL }
P(1) = { NULL, {1} }
P(2) = { NULL, {1}, {2}, {1,2} }

P(3) = { NULL, {1}, {2}, {3}, {1,2}, {1,3}, {1,2,3} }


so if my input is

Code
1

the output will be


Code
  

NULL

1



KevinR
Veteran


Jun 24, 2005, 7:13 PM

Post #4 of 9 (1283 views)
Re: [dianazhengdz] Suggestions [In reply to] Can't Post


Quote
And the table for powerset will be something like this

P(0) = { NULL }
P(1) = { NULL, {1} }
P(2) = { NULL, {1}, {2}, {1,2} }

P(3) = { NULL, {1}, {2}, {3}, {1,2}, {1,3}, {1,2,3} }


"Something like this"? Or is the data in the textfile (the powersets) exactly like you show it? I see no reason for all those curly braces or any other characters/symbols if it's just a text file, could just be like this:

NULL
NULL 1
NULL 1 2 1,2
NULL 1 2 3 1,2 1,3 1,2,3

and it could probably even be simpler. What is a "powerset" anyway?
-------------------------------------------------


dianazhengdz
Novice

Jun 25, 2005, 9:59 PM

Post #5 of 9 (1276 views)
Re: [KevinR] Suggestions [In reply to] Can't Post

There is no input data (the powerset table), the input data from standard in will just be a number....

From that number the program will do the calculation and then print the output to standard output. The table is just be a reference as to how the poweset of a number is being produce....

----------------------------------------------------------------------

Before this can i ask you another question for another program that i am doing, because i couldn't get the correct output, maybe my algorithm is wrong.

The question is that with this sample input (the input is not fixed with just this few lines, it might have more than this few lines, but with a limit of less than 50 lines)


Code
  

L1 Line A
L2 Line B
#1
L3 Line C
#5
L4 Line D



output


Code
  

L1 Line A
L2 Line B
L1 Line A
L3 Line C
L4 Line D
L4 Line D



when there is a "#" in the first place, if the second number matches any other previous number, it will display it (eg L1 will be Line A) otherwise it will dislay the last line in the input (this case: L4 Line D).



This is what i have for the moment, not very sure where i have gone wrong... Maybe my matching is wrong, can you help me to correct that... Thanks..


Code
  

%hashname = ();

while(<STDIN>){
# @fromIn contain orginal input
push (@fromIn, $_);
# @input contain orginal split into three parts
@input = split (/\s+/, $_);

# Split $input[0] into two parts, $firstCol contain "L & #",

# $secCol contain number
($firstCol, $secCol) = split (//, $input[0]);

# hash table contain 1A 2B 3C 4D...
$hashname{$secCol} = $secCol.$input[2];


}

foreach(@fromIn){
if($firstCol =~ '#'){
if(exists $hashname{$secCol}){
# print "L$secCol $input[1] \n";
}
# print "$secCol $hashname{$secCol}\n";

}
else{
# print "$_\n";
}
}



rork
User

Jun 26, 2005, 8:32 AM

Post #6 of 9 (1272 views)
Re: [dianazhengdz] Suggestions [In reply to] Can't Post


Quote
$hashname{$secCol} = $secCol.$input[2];


This resets the hash everytime a number is repeated

so when L1 Line A is evaluated it is set to 1 => "1A"
but when #1 is evaluated it is reset: 1 => ""

You should skip updating hashname if $firstCol eq #:


Code
unless ($firstCol eq "#") { 
$hashname{$secCol} = $secCol.$input[2];
}

Or even quicker, write after "push (@fromIn, $_);"

Code
next if /^#/;


And there are more problems: You don't use strict (I think)

In the foreach loop you use the scalars $firstCol and $secCol scince these are created as locals in the while loop they don't exists anymore. You should split the info again. If you had used strict the script wouldn't have run and gives an error.

If you declared @input, $firstCol and $lastCol as globals they still contain the values they had when the while loop was finished.

I suggest you rewrite the foreach loop, use strict and declare variables. Oh, and open a new thread for new questions, makes a discussion more readable
--
Don't reinvent the wheel, use it, abuse it or hack it.


davorg
Thaumaturge / Moderator

Jun 28, 2005, 9:54 AM

Post #7 of 9 (1251 views)
Re: [dianazhengdz] Suggestions [In reply to] Can't Post


In Reply To
Now i have another problem...

The problem is when we enter a number(that is powerset), we will have to print out the subset of that power set.


Please explain the algorithm used to calculate a "powerset".

And please try to use more descriptive subjects for your postings.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


davorg
Thaumaturge / Moderator

Jun 28, 2005, 9:56 AM

Post #8 of 9 (1250 views)
Re: [dianazhengdz] Suggestions [In reply to] Can't Post


In Reply To
Before this can i ask you another question for another program that i am doing


You can ask us as many questions as you like, but it will help us all if you start a new discussion thread when you post a new question.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


rork
User

Jul 4, 2005, 12:22 AM

Post #9 of 9 (1219 views)
Re: [dianazhengdz] Suggestions [In reply to] Can't Post

A short search on cpan showed this module

List::Powerset

I think this is what you need.
--
Don't reinvent the wheel, use it, abuse it or hack it.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives