Coding Challenge: Print This Pattern

kasun090354t

Well-known member
  • Aug 21, 2011
    24,170
    36,182
    113
    කෑගල්ල
    Python:
    a=9
    for j in range(a):
        for i in range(a):
            if i<1:
                for k in range(a-j-1):
                    print(' ',end=" ")
                print('*',end=" ")
                for k in range(j-1):
                    print(' ',end=" ")
                if j>0 and j<a-1:
                    print('*',end=" ")
            else:
                for k in range(a-2-j):
                    print(' ',end=" ")
                print('*',end=" ")
                for k in range(j-1):
                    print(' ',end=" ")
                if j>0 and j<a-1:
                    print('*',end=" ")
        if j==a-1:
            print('*',end=" ")
        print()
     
    • Like
    Reactions: Schrodinger Cat

    Yakka putha

    Well-known member
  • Dec 15, 2021
    3,439
    5,228
    113
    මන් දැක්ක එකෙක් ඩෝනට් එකක් කැරකෙන විදියට එකෙක් කෝඩ් කරපු එකක් yutube eke
     
    • Love
    Reactions: Error365!

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,703
    113
    සරලම විදිය

    coding.jpg


    C#:
    Console.Write("Size: ");
    int size = Convert.ToInt32(Console.ReadLine());
    for (int i = size; i >= 1; i += -1)
    {
          string line = "*".PadLeft(i, (char)32).PadRight(size - 1, (char)32).PadRight(size, char.Parse("*"));
          Console.WriteLine(line + string.Concat(Enumerable.Repeat(line.Remove(0, 1), size - 1)));
    }
    Console.ReadKey();

    Great solution. (y) Thanks for posting.


    Python:
    a=9
    for j in range(a):
        for i in range(a):
            if i<1:
                for k in range(a-j-1):
                    print(' ',end=" ")
                print('*',end=" ")
                for k in range(j-1):
                    print(' ',end=" ")
                if j>0 and j<a-1:
                    print('*',end=" ")
            else:
                for k in range(a-2-j):
                    print(' ',end=" ")
                print('*',end=" ")
                for k in range(j-1):
                    print(' ',end=" ")
                if j>0 and j<a-1:
                    print('*',end=" ")
        if j==a-1:
            print('*',end=" ")
        print()

    Please check the output, and thanks for posting.
     

    saw1993

    Well-known member
  • Aug 9, 2015
    1,699
    1,705
    113
    Kandy
    Java:
    fun main() {
     
     
        var size:Int = 6
        var vertical:Int =3
        var horizontal:Int=1
     
            for(a in 1..horizontal){
        
            for(b in 1..size){
                println("")
            
                for (e in 1..vertical){
                      for(c in 1..size-b){
                   print(" ")
                }
            
                for(d in 1..b){
                    if(d==1 || d==b){
                        print("*")
                    } else {
                        print(" ")
                    
                    }
                }
              }
            }
        }
    }

    Thanks for the reply.

    මේකේ මචන් සයිස් එක 6 වෙනකොට ඔය ත්‍රිකොණාකාර කොටස 6 පාරක් රීපීට් වෙන්න ඕනේ. තව එක ත්‍රිකෝණයක් ඉවර වෙන තරුවෙන් අනිත් ත්‍රිකෝණය පටන් ගන්න ඕනෑ.

    View attachment 190368
    අහ් එහෙම එකක් තියනවා නේ. දැන් ඒක නොට් උනේ . මේක හරිනේ?
     

    TonyMontana83

    Well-known member
  • Apr 9, 2022
    9,032
    19,515
    113
    Miami
    මට තියෙන ප්‍රශ්නේ මෙච්චර දියුණු පරිගණක තාක්ෂණයක් තියෙද්දි ප්‍රෝග්‍රැමින් කරන්න තාම මාගල් වගේ කෝඩ් කොට කොට දඟලන්න ඕන ඇයි කියලා? ග්‍රැෆිකල් ඉන්ටර්ෆේස් එකකින් ප්‍රෝග්‍රැම් එකක් ඩිසයින් කරන්න මොකක්ද තියෙන බැරි කම? ඇයි ප්‍රෝග්‍රැමින් කියන එක මිනිස්සු අතරට නේන්නෙ? සාමාන්‍ය මනුස්සයෙකුට තමන්ට ඕන කරන සරල පොඩි සොෆ්ට්වෙයා කෑල්ලක් අටවගන්න පුළුවන් වෙන්න එපැයි මේ වෙද්දි.
     

    hesian99

    Well-known member
  • Jul 17, 2020
    3,399
    4,279
    113
    Gampaha
    Bump

    මට තියෙන ප්‍රශ්නේ මෙච්චර දියුණු පරිගණක තාක්ෂණයක් තියෙද්දි ප්‍රෝග්‍රැමින් කරන්න තාම මාගල් වගේ කෝඩ් කොට කොට දඟලන්න ඕන ඇයි කියලා? ග්‍රැෆිකල් ඉන්ටර්ෆේස් එකකින් ප්‍රෝග්‍රැම් එකක් ඩිසයින් කරන්න මොකක්ද තියෙන බැරි කම? ඇයි ප්‍රෝග්‍රැමින් කියන එක මිනිස්සු අතරට නේන්නෙ? සාමාන්‍ය මනුස්සයෙකුට තමන්ට ඕන කරන සරල පොඩි සොෆ්ට්වෙයා කෑල්ලක් අටවගන්න පුළුවන් වෙන්න එපැයි මේ වෙද්දි.
    Basics walin oni deyak krnn plwn
    ------ Post added on Nov 26, 2022 at 8:05 AM
     

    Amith0424

    Well-known member
  • Oct 27, 2010
    14,023
    23,976
    113
    ℒ𝓸𝓬𝓪𝓽𝓲𝓸𝓷
    PHP:
    <?php
    $size=5;
    
    
      for($l=0; $l<$size; $l++) {
       
        for($k=0; $k<$size; $k++) {
       
          for($x=$l+1; $x<$size; $x++) {
            echo " ";
          }
         
          for($x=0; $x<$l; $x++) {
            if($x==0 && $l!=4 || ($l==4 && $k==0 && $x==0)){
              echo "*";
            } else {
              echo " ";
            }
          }
          echo "*";
      }
    
          echo "\n";
      }
     
    ?>
    Update :P
    PHP:
    <?php
    $size=3;
    
    
      for($l=0; $l<$size; $l++) {
        
        for($k=0; $k<$size; $k++) {
        
          if($k==0){
            for($e=$l; $e<$size-1; $e++) {
                echo " ";
            }
          }else {
            for($e=$l; $e<$size-2; $e++) {
              echo " ";
            }
          }
          
          for($x=0; $x<$l; $x++) {
            if($k==0 || $l<$size-1){
              if(($x==0 && $l==$size-1 && $k==0) || $x==0){
                echo "*";
              } else {
                echo " ";
              }
            } else {
              if(($x==0 && $l==$size-2) ){
                echo "*";
              } else if($x==$size-2) {
                echo "";
              } else {
                echo " ";
              }
            }
          }
          echo "*";
      }
    
          echo "\n";
      }
      
    ?>
     

    Prof GHOST

    Junior member
  • Jan 18, 2018
    76
    87
    18
    JavaScript:
    const size = 6;
    
    for (let y = 1; y <= size; y++) {
      let str = "";
      for (let x = 1; x <= size * size; x++) {
        let hasStar = false;
        if (x % size === 0) {
          str += "*";
          hasStar = true;
        }
        if (!hasStar && x === 1 && y === size) {
          str += "*";
          hasStar = true;
        }
    
        if (!hasStar && y !== size && (x % size) + y === size + 1) {
          str += "*";
          hasStar = true;
        }
    
        if (!hasStar) {
          str += " ";
        }
      }
      console.log(str);
    }

    1669438588230.png


    මට තියෙන ප්‍රශ්නේ මෙච්චර දියුණු පරිගණක තාක්ෂණයක් තියෙද්දි ප්‍රෝග්‍රැමින් කරන්න තාම මාගල් වගේ කෝඩ් කොට කොට දඟලන්න ඕන ඇයි කියලා? ග්‍රැෆිකල් ඉන්ටර්ෆේස් එකකින් ප්‍රෝග්‍රැම් එකක් ඩිසයින් කරන්න මොකක්ද තියෙන බැරි කම? ඇයි ප්‍රෝග්‍රැමින් කියන එක මිනිස්සු අතරට නේන්නෙ? සාමාන්‍ය මනුස්සයෙකුට තමන්ට ඕන කරන සරල පොඩි සොෆ්ට්වෙයා කෑල්ලක් අටවගන්න පුළුවන් වෙන්න එපැයි මේ වෙද්දි.
    ඕනි තරන් NO Code Platforms තියනව දැන් මේ වැඩේට.
    ------ Post added on Nov 26, 2022 at 10:29 AM
     

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,703
    113
    අහ් එහෙම එකක් තියනවා නේ. දැන් ඒක නොට් උනේ . මේක හරිනේ?

    Update :P
    PHP:
    <?php
    $size=3;
    
    
      for($l=0; $l<$size; $l++) {
       
        for($k=0; $k<$size; $k++) {
       
          if($k==0){
            for($e=$l; $e<$size-1; $e++) {
                echo " ";
            }
          }else {
            for($e=$l; $e<$size-2; $e++) {
              echo " ";
            }
          }
         
          for($x=0; $x<$l; $x++) {
            if($k==0 || $l<$size-1){
              if(($x==0 && $l==$size-1 && $k==0) || $x==0){
                echo "*";
              } else {
                echo " ";
              }
            } else {
              if(($x==0 && $l==$size-2) ){
                echo "*";
              } else if($x==$size-2) {
                echo "";
              } else {
                echo " ";
              }
            }
          }
          echo "*";
      }
    
          echo "\n";
      }
     
    ?>

    JavaScript:
    const size = 6;
    
    for (let y = 1; y <= size; y++) {
      let str = "";
      for (let x = 1; x <= size * size; x++) {
        let hasStar = false;
        if (x % size === 0) {
          str += "*";
          hasStar = true;
        }
        if (!hasStar && x === 1 && y === size) {
          str += "*";
          hasStar = true;
        }
    
        if (!hasStar && y !== size && (x % size) + y === size + 1) {
          str += "*";
          hasStar = true;
        }
    
        if (!hasStar) {
          str += " ";
        }
      }
      console.log(str);
    }

    View attachment 190384


    ඕනි තරන් NO Code Platforms තියනව දැන් මේ වැඩේට.
    ------ Post added on Nov 26, 2022 at 10:29 AM

    තුන් දෙනාගෙම Solutions හරි (y)
     

    Amith0424

    Well-known member
  • Oct 27, 2010
    14,023
    23,976
    113
    ℒ𝓸𝓬𝓪𝓽𝓲𝓸𝓷
    ග්‍රැෆිකල් ඉන්ටර්ෆේස් එකකින් ප්‍රෝග්‍රැම් එකක් ඩිසයින් කරන්න මොකක්ද තියෙන බැරි කම?
    ඒක හදන්නත් implement කරන්නත් raw code ඕනි වෙනවනෙ. ඔය සයිඩ් එකට දැන් ටික ටික move වෙනව. තව kernel drivers machine lang වලට ඔය ක්‍රමේ හරියන්නෙ නෑ.
     

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,703
    113
    ඇත්තටම මම මේ ප්‍රශ්නය හැදුවේ String Array එකකට දාපු පැටර්න් එකක් රිපීට් කරලා ප්‍රින්ට් කරන්න පුලුවන් කියන අදහස හිතේ තියාගෙන. මට ඕනෑ වුනේ අනිත් ප්‍රෝග්‍රැමර්ස්ලා මේක implement කරන්නේ කොහොමද කියලා බලාගන්න. මට වඩා වෙනස් විදියකට හිතලා හදපු නිර්මාණශීලී solutions කිහිපයක්ම පෝස්ට් කරලා තියෙනවා. මගෙ සම්පූර්ණ solution එක පහලින් දාන්නම්. රිප්ලයි කරපු හැමෝටම ස්තුතියි.

    C#:
        static void PrintPattern(int size)
        {
            string[] lines = new string[size];
    
            for (int i = 0; i < size; i++)
            {
                lines[i] = "";
                for (int j = 0; j < size; j++)
                    if (j == size - 1 || j == size - 1 - i)
                        lines[i] += '*';
                    else
                        lines[i] += ' ';
            }
    
            for (int i = 0; i < size; i++)
            {
                Console.Write(lines[i]);
                for (int j = 1; j < size; j++)
                    Console.Write(lines[i].Substring(1));
                Console.WriteLine();
            }
        }