First of all, this code will ask for a number (first) on how many test the user would like to have. Then the user will input the strings depending on how the number entered.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim input As String = txtInput.Text
Dim inputArr As String() = input.Split(New Char() {Environment.NewLine})
Dim count As Integer = Convert.ToInt32(inputArr(0))
If count >= 1 And count <= 10 Then
If txtInput.Lines.Length() - 1 <= count Then
For x As Integer = 1 To count
If inputArr(x).Length - 1 >= 1 And inputArr(x).Length - 1 <= 10 Then
Dim numDeletes = CountConsecutiveDuplicates(inputArr(x))
txtResult.AppendText(numDeletes & Environment.NewLine)
Else
MessageBox.Show("Hell no!")
End If
Next x
Else
MessageBox.Show("Max test reached!")
End If
Else
MessageBox.Show("Number of cases is limited only from 1 - 10")
End If
End Sub
Shared Function CountConsecutiveDuplicates(Of T)(input As IEnumerable(Of T)) As Int32
Dim count As Int32 = 0
Dim comparer = EqualityComparer(Of T).Default
' optimization for lists and arrays: '
Dim listT = TryCast(input, IList(Of T))
If listT IsNot Nothing Then
If listT.Count <= 1 Then Return 0
For i As Int32 = 0 To listT.Count - 2
If comparer.Equals(listT(i), listT(i + 1)) Then
count += 1
End If
Next
Return count
End If
If Not input.Any() Then Return 0
Dim this As T = input.First()
For Each item As T In input.Skip(1)
If comparer.Equals(this, item) Then
count += 1
End If
this = item
Next
Return count
End Function
Here are the constraints:
tests >= 1
andtests <= 10
charofstring >= 1
andcharofstring <= 10
This code will check the string that user input in a multiline textbox. For example, the input is 4 (as the test counter)(\n) AABBCCC (first string to check)(\n) AAAABBBC (2nd string to check)(\n) ... (4th string)
I need to check on the number of consecutive characters that are needed to delete until no characters are consecutive. This will be the result of the user input.
- 4 (first string because 1A 1B and 2C) - text in the parentheses not included in output
- 5 (2nd string because 3A 2B)
- ...
Is there any code that I do not need? Someone told me that I can still shorten the code I have. How can I modify this code?
- "txtInput = name of input textbox"
- "txtResult = name of output textbox"
- "Button1 = name of button to execute the code."