Integer Repetition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Beany
    New Member
    • Nov 2006
    • 173

    Integer Repetition

    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
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    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.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      hehehe.... seems that the thought of having to write this very simple code his/her self was a bit too much to contemplate.

      Comment

      • Beany
        New Member
        • Nov 2006
        • 173

        #4
        KevinADC,

        sorry mate for the late reply, well busy.

        ok i think ive cracked it with 5digits:

        Code:
        print "please insert a 5-digit integer: \n";
        
        $string = <STDIN>;
        
        $var = substr($string,0,1) x 1;
        $var2 = substr($string,1,1) x 2;
        $var3 = substr($string,2,1) x 3;
        $var4 = substr($string,3,1) x 4;
        $var5 = substr($string,4,1) x 5;
        
        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

        Comment

        • nithinpes
          Recognized Expert Contributor
          • Dec 2007
          • 410

          #5
          To reduce the number of lines, you could have used for loop:
          Code:
          print "please insert a 5-digit integer: \n"; 
            
          $string = <STDIN>; 
          for($i=1;$i<=5;$i++) {  
          my $var = substr($string,$i-1,1) x $i; 
          print "$var\n";
          }
          Another way of doing it would be :
          Code:
          print "please insert a 5-digit integer: \n"; 
          $string = <STDIN>; 
          $i=1;
          print $1x$i++."\n" while($string=~/(\d)/g); #works for any no. of digits

          Comment

          • BeemerBiker
            New Member
            • Jul 2008
            • 87

            #6
            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".

            Comment

            • Beany
              New Member
              • Nov 2006
              • 173

              #7
              Originally posted by nithinpes
              To reduce the number of lines, you could have used for loop:
              Code:
              print "please insert a 5-digit integer: \n"; 
                
              $string = <STDIN>; 
              for($i=1;$i<=5;$i++) {  
              my $var = substr($string,$i-1,1) x $i; 
              print "$var\n";
              }
              Another way of doing it would be :
              Code:
              print "please insert a 5-digit integer: \n"; 
              $string = <STDIN>; 
              $i=1;
              print $1x$i++."\n" while($string=~/(\d)/g); #works for any no. of digits

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

              thanks

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by Beany
                KevinADC,

                sorry mate for the late reply, well busy.

                ok i think ive cracked it with 5digits:

                Code:
                print "please insert a 5-digit integer: \n";
                
                $string = <STDIN>;
                
                $var = substr($string,0,1) x 1;
                $var2 = substr($string,1,1) x 2;
                $var3 = substr($string,2,1) x 3;
                $var4 = substr($string,3,1) x 4;
                $var5 = substr($string,4,1) x 5;
                
                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.

                Comment

                • Beany
                  New Member
                  • Nov 2006
                  • 173

                  #9
                  Originally posted by KevinADC
                  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

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    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.
                    Code:
                            // Amount of columns and rows.
                    	$limitX = 7;
                    	$limitY = 10;
                    	$out	= '*';
                    	
                    	// Loop through the columns.
                    	for( $y = 1; $y <= $limitY; ++$y )
                    	{
                    		// Inner loop for rows.
                    		for ( $x = 1; $x <= $limitX; ++$x )
                    		{
                    			// Check to see if we are at the end of the row and need a newline char (\n)
                    			if ( $x === $limitX )
                    			{
                    				echo "*\n";
                    			}
                    			else
                    			{
                    				echo "*";
                    			}
                    		}
                    	}
                    Hope this helps.

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      That can be simplified:

                      Code:
                      my $x = 7;
                      my $y = 7;
                      for (1..$x) {
                          print "*" x $y, "\n";
                      }
                      Could also look into format() or perlform:

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        Originally posted by KevinADC
                        That can be simplified:

                        Code:
                        my $x = 7;
                        my $y = 7;
                        for (1..$x) {
                            print "*" x $y, "\n";
                        }
                        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?

                        Comment

                        • eWish
                          Recognized Expert Contributor
                          • Jul 2007
                          • 973

                          #13
                          Originally posted by Markus
                          Sorry to go off-topic, but what does the 'x' operator do?
                          From the perl docs.

                          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

                          Comment

                          • KevinADC
                            Recognized Expert Specialist
                            • Jan 2007
                            • 4092

                            #14
                            Yes, 'x' is a perl operator, don't know if PHP has something similar.

                            Comment

                            • Beany
                              New Member
                              • Nov 2006
                              • 173

                              #15
                              Originally posted by KevinADC
                              That can be simplified:

                              Code:
                              my $x = 7;
                              my $y = 7;
                              for (1..$x) {
                                  print "*" x $y, "\n";
                              }
                              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

                              Comment

                              Working...