How to create an alphabet rangoli (geometric and character pattern) of size N in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Module AlphabetRangoli

    Sub PrintAlphabetRangoli(n As Integer)
        If n <= 0 Then Return

        ' Total width of the grid based on character and hyphen spacing
        Dim totalWidth As Integer = 4 * n - 3

        ' Loop from -(n-1) to (n-1) to leverage geometric symmetry
        For i As Integer = -(n - 1) To (n - 1)
            Dim currentRowDist As Integer = Math.Abs(i)
            
            Dim lineChars As New List(Of String)()
            Dim maxChars As Integer = n - currentRowDist

            ' 1. Build the left/descending side of characters
            For j As Integer = 0 To maxChars - 1
                Dim targetChar As Char = Convert.ToChar(Convert.ToInt32("a"c) + n - 1 - j)
                lineChars.Add(targetChar.ToString())
            Next

            ' 2. Build the right/ascending side of characters
            For j As Integer = maxChars - 2 To 0 Step -1
                Dim targetChar As Char = Convert.ToChar(Convert.ToInt32("a"c) + n - 1 - j)
                lineChars.Add(targetChar.ToString())
            Next

            ' 3. Interleave hyphens between characters
            Dim formattedRow As String = String.Join("-", lineChars)

            ' 4. Calculate hyphen padding on each side and print
            Dim paddingSize As Integer = (totalWidth - formattedRow.Length) \ 2
            Dim sideHyphens As New String("-"c, paddingSize)

            Console.WriteLine(sideHyphens & formattedRow & sideHyphens)
        Next
    End Sub

    Sub Main()
        Dim n As Integer = 5
        PrintAlphabetRangoli(n)
    End Sub

End Module



' run:
'
' --------e--------
' ------e-d-e------
' ----e-d-c-d-e----
' --e-d-c-b-c-d-e--
' e-d-c-b-a-b-c-d-e
' --e-d-c-b-c-d-e--
' ----e-d-c-d-e----
' ------e-d-e------
' --------e--------
'

 



answered Jul 12 by avibootz
...