Connecting Tech Pros Worldwide Help | Site Map

Integer Repetition

Familiar Sight
 
Join Date: Nov 2006
Posts: 168
#1: Feb 25 '09
Hi All,

i need help with the following:

i need to write a simple program that asks the user to input a number (minimum of 3 digits), which i seperate into individual digits and print the 1st digit 1 time, 2nd digit 2 times, 3rd digit 3 times and so forth...

I cannot get the last bit working, what bit of code can i use for the repetition part?

many thanks
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Feb 25 '09

re: Integer Repetition


Post the code you have wrttien so far to try and solve this problem. Even if it does not work. We can then see where you are making a mistake and help you better. Don't expect anyone to write code for you based upon your programming requirements, we will want to see effort on your part first to solve the problem.
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Feb 26 '09

re: Integer Repetition


hehehe.... seems that the thought of having to write this very simple code his/her self was a bit too much to contemplate.
Familiar Sight
 
Join Date: Nov 2006
Posts: 168
#4: Feb 26 '09

re: Integer Repetition


KevinADC,

sorry mate for the late reply, well busy.

ok i think ive cracked it with 5digits:

Expand|Select|Wrap|Line Numbers
  1. print "please insert a 5-digit integer: \n";
  2.  
  3. $string = <STDIN>;
  4.  
  5. $var = substr($string,0,1) x 1;
  6. $var2 = substr($string,1,1) x 2;
  7. $var3 = substr($string,2,1) x 3;
  8. $var4 = substr($string,3,1) x 4;
  9. $var5 = substr($string,4,1) x 5;
  10.  
  11. print $var."\n", $var2."\n", $var3."\n", $var4."\n", $var5."\n";

Im new to Perl, so im sure this can be written in a smaller way..

Can you please check this for me?

thanks
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#5: Feb 26 '09

re: Integer Repetition


To reduce the number of lines, you could have used for loop:
Expand|Select|Wrap|Line Numbers
  1. print "please insert a 5-digit integer: \n"; 
  2.  
  3. $string = <STDIN>; 
  4. for($i=1;$i<=5;$i++) {  
  5. my $var = substr($string,$i-1,1) x $i; 
  6. print "$var\n";
  7. }
  8.  
Another way of doing it would be :
Expand|Select|Wrap|Line Numbers
  1. print "please insert a 5-digit integer: \n"; 
  2. $string = <STDIN>; 
  3. $i=1;
  4. print $1x$i++."\n" while($string=~/(\d)/g); #works for any no. of digits
  5.  
BeemerBiker's Avatar
Member
 
Join Date: Jul 2008
Location: San Antonio, Texas
Posts: 68
#6: Feb 26 '09

re: Integer Repetition


Just a suggestion - If this is an assignment for a programming class, and the instructor is an old fortran programmer, he/she will probably want your perl code to look exactly like the fortran code they are familar with and a one-liner will get an "F".
Familiar Sight
 
Join Date: Nov 2006
Posts: 168
#7: Feb 26 '09

re: Integer Repetition


Quote:

Originally Posted by nithinpes View Post

To reduce the number of lines, you could have used for loop:

Expand|Select|Wrap|Line Numbers
  1. print "please insert a 5-digit integer: \n"; 
  2.  
  3. $string = <STDIN>; 
  4. for($i=1;$i<=5;$i++) {  
  5. my $var = substr($string,$i-1,1) x $i; 
  6. print "$var\n";
  7. }
  8.  
Another way of doing it would be :
Expand|Select|Wrap|Line Numbers
  1. print "please insert a 5-digit integer: \n"; 
  2. $string = <STDIN>; 
  3. $i=1;
  4. print $1x$i++."\n" while($string=~/(\d)/g); #works for any no. of digits
  5.  


Can you please provide me some explaination for the above code?

thanks
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#8: Feb 26 '09

re: Integer Repetition


Quote:

Originally Posted by Beany View Post

KevinADC,

sorry mate for the late reply, well busy.

ok i think ive cracked it with 5digits:

Expand|Select|Wrap|Line Numbers
  1. print "please insert a 5-digit integer: \n";
  2.  
  3. $string = <STDIN>;
  4.  
  5. $var = substr($string,0,1) x 1;
  6. $var2 = substr($string,1,1) x 2;
  7. $var3 = substr($string,2,1) x 3;
  8. $var4 = substr($string,3,1) x 4;
  9. $var5 = substr($string,4,1) x 5;
  10.  
  11. print $var."\n", $var2."\n", $var3."\n", $var4."\n", $var5."\n";

Im new to Perl, so im sure this can be written in a smaller way..

Can you please check this for me?

thanks

The above code is probably fine for your assingment. When you ask on a forum you will get solutions that are probably beyond your current lesson in class. You should stick with your own code as long as it works properly.
Familiar Sight
 
Join Date: Nov 2006
Posts: 168
#9: Feb 27 '09

re: Integer Repetition


Quote:

Originally Posted by KevinADC View Post

The above code is probably fine for your assingment. When you ask on a forum you will get solutions that are probably beyond your current lesson in class. You should stick with your own code as long as it works properly.

thanks for the advice,

I'm self studying to learn Perl so have decided to do a chapter a week.

Also, can you please give me a hint or any tips as to where to look (or what function to use) if i wanted to create a similar code but instead the inputted integer prints a square of asterisks..

for eg, if a user inputs an integer of '7' the output would show a square of asterisks (7X7)..

regards
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#10: Mar 2 '09

re: Integer Repetition


The output would not be perfectly square, or square at all because the line height of a character is greater than it's width, or it is with html, at least. :P

Anyway, the logic goes as follows:
  1. Get your limits - you mentioned before a square of 7x7. So, of course, for a square you will only need 1 limit, but for the sake of portability, we'll take a limit for the X column and the Y column. So, limitX = 7 and limitY = 7.
  2. Now we'll need to use an outer-loop for the columns and a loop within that for the actual output. The outer for loop should loop from 1 - LimitY.
  3. Inside this outer-loop we need another for loop to actually output our character of choice - in this case, an asterix. This loop should run from 1 - LimitX, outputting the asterix.
  4. You will also need to check to see if you have reached LimitX so you can output a newline character / line break / whatever.

I don't know Perl syntax, but the below (PHP) should offer some visible logic.
Expand|Select|Wrap|Line Numbers
  1.         // Amount of columns and rows.
  2.     $limitX = 7;
  3.     $limitY = 10;
  4.     $out    = '*';
  5.  
  6.     // Loop through the columns.
  7.     for( $y = 1; $y <= $limitY; ++$y )
  8.     {
  9.         // Inner loop for rows.
  10.         for ( $x = 1; $x <= $limitX; ++$x )
  11.         {
  12.             // Check to see if we are at the end of the row and need a newline char (\n)
  13.             if ( $x === $limitX )
  14.             {
  15.                 echo "*\n";
  16.             }
  17.             else
  18.             {
  19.                 echo "*";
  20.             }
  21.         }
  22.     }
  23.  
Hope this helps.
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#11: Mar 2 '09

re: Integer Repetition


That can be simplified:

Expand|Select|Wrap|Line Numbers
  1. my $x = 7;
  2. my $y = 7;
  3. for (1..$x) {
  4.     print "*" x $y, "\n";
  5. }
Could also look into format() or perlform:

http://perldoc.perl.org/perlform.html
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#12: Mar 2 '09

re: Integer Repetition


Quote:

Originally Posted by KevinADC View Post

That can be simplified:

Expand|Select|Wrap|Line Numbers
  1. my $x = 7;
  2. my $y = 7;
  3. for (1..$x) {
  4.     print "*" x $y, "\n";
  5. }
Could also look into format() or perlform:

http://perldoc.perl.org/perlform.html

Sorry to go off-topic, but what does the 'x' operator do?
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#13: Mar 2 '09

re: Integer Repetition


Quote:

Originally Posted by Markus View Post

Sorry to go off-topic, but what does the 'x' operator do?

From the perl docs.

Quote:
Binary "x" is the repetition operator. In scalar context or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeated the number of times specified by the right operand. In list context, if the left operand is enclosed in parentheses or is a list formed by qw/STRING/, it repeats the list. If the right operand is zero or negative, it returns an empty string or an empty list, depending on the context.
--Kevin
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#14: Mar 3 '09

re: Integer Repetition


Yes, 'x' is a perl operator, don't know if PHP has something similar.
Familiar Sight
 
Join Date: Nov 2006
Posts: 168
#15: Mar 3 '09

re: Integer Repetition


Quote:

Originally Posted by KevinADC View Post

That can be simplified:

Expand|Select|Wrap|Line Numbers
  1. my $x = 7;
  2. my $y = 7;
  3. for (1..$x) {
  4.     print "*" x $y, "\n";
  5. }
Could also look into format() or perlform:

http://perldoc.perl.org/perlform.html

Thanks for all the replies so far, very beneficial.

Kevin, why do you use 'my'?

your above code prints out a 7x7 square which is cool, but if you wanted to print a hollow square? just the asterisks on the outline of the square?

sorry its probably my fault for not making it very clear..

regards
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#16: Mar 3 '09

re: Integer Repetition


I use "my" because I write all my perls script using "strict", so using "my" is a habit, a good one. As far as a hollow square, you have already been given enough information to figure that out. Try something, if you get stuck, post your code,
Reply