Results 1 to 1 of 1

Thread: Pascal's Triangle

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Pascal's Triangle

    An implementation of Pascal's Triangle.

    vb Code:
    1. Class PascalsTriangle
    2.     Private Class triRow
    3.         Property value As Integer = 0
    4.         Property aboveRow As Integer = -1
    5.         Property aboveColLeft As Integer = -1
    6.         Property aboveColRight As Integer = -1
    7.  
    8.         Public Function ValueStringLength() As Integer
    9.             Return Me.value.ToString.Length
    10.         End Function
    11.     End Class
    12.  
    13.     Private Property triRows As New List(Of List(Of triRow))
    14.  
    15.     Public Sub New(numberOfRows As Integer)
    16.         For rowNum As Integer = 0 To numberOfRows - 1
    17.             Me.addRow(rowNum)
    18.         Next
    19.     End Sub
    20.  
    21.     Private Sub addRow(rowNum As Integer)
    22.         Dim aRow As New List(Of triRow)
    23.         If rowNum > 0 Then
    24.             For col As Integer = 0 To rowNum
    25.                 Dim aCol As New triRow
    26.                 aCol.aboveRow = rowNum - 1
    27.                 aCol.aboveColLeft = col - 1
    28.                 aCol.aboveColRight = rowNum - (col + 1)
    29.                 If aCol.aboveColLeft >= 0 Then
    30.                     aCol.value = Me.triRows(aCol.aboveRow)(aCol.aboveColLeft).value
    31.                 End If
    32.                 If aCol.aboveColRight >= 0 Then
    33.                     aCol.value += Me.triRows(aCol.aboveRow)(aCol.aboveColRight).value
    34.                 End If
    35.                 aRow.Add(aCol)
    36.             Next
    37.         Else
    38.             Dim aCol As New triRow
    39.             aCol.value = 1
    40.             aRow.Add(aCol)
    41.         End If
    42.         Me.triRows.Add(aRow)
    43.     End Sub
    44.  
    45.     Private Function maxColWidth() As Integer
    46.         Dim rv As Integer = 0
    47.         For Each r As triRow In Me.triRows(Me.triRows.Count - 1)
    48.             Dim colL As Integer = r.ValueStringLength
    49.             If colL > rv Then
    50.                 rv = colL
    51.             End If
    52.         Next
    53.         Return rv
    54.     End Function
    55.  
    56.     Public Overrides Function ToString() As String
    57.         Dim pad As Integer = Me.maxColWidth + 2
    58.         Dim rv As New System.Text.StringBuilder
    59.         Dim lines As New List(Of String)
    60.         For r As Integer = 0 To Me.triRows.Count - 1
    61.             rv.Length = 0
    62.             For Each rw As triRow In Me.triRows(r)
    63.                 rv.AppendFormat("{0}", rw.value.ToString.PadRight(pad, " "c))
    64.             Next
    65.             lines.Add(rv.ToString.Trim)
    66.         Next
    67.  
    68.         'make it look like triangle
    69.         'max string length
    70.         Dim maxL As Integer = lines(lines.Count - 1).Length
    71.  
    72.         For x As Integer = 0 To lines.Count - 1
    73.             'pad left
    74.             Dim ldif As Integer = (maxL - lines(x).Length) \ 2
    75.             Dim s As String = New String(" "c, ldif)
    76.             lines(x) = lines(x).Insert(0, s)
    77.         Next
    78.         Return String.Join(Environment.NewLine, lines)
    79.     End Function
    80. End Class
    Last edited by dbasnett; Nov 12th, 2014 at 07:40 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width