2
\$\begingroup\$

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 and tests <= 10
  • charofstring >= 1 and charofstring <= 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."
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jan 29, 2015 at 2:03
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

By using the Distinct method and comparing lengths you can get the number you want in one line. Here's what such a function could look like:

Function CharsToDelete(Of T)(input As IEnumerable(Of T)) As Integer
 Return input.Count - input.Distinct.Count
End Function

Oops missed the part about consecutive chars. This should do the trick:

Function CharsToDelete(Of T)(input As IEnumerable(Of T)) As Integer
 Return input.Where(Function(x, y) x.Equals(input(y + 1))).Count
End Function

You don't need to split the text into an array. The Lines property of the TextBox already does this:

Dim inputArr As String() = txtInput.Lines
answered Jan 29, 2015 at 2:34
\$\endgroup\$
3
  • \$\begingroup\$ then how can i use it in my code and what should i replace? \$\endgroup\$ Commented Jan 29, 2015 at 3:35
  • \$\begingroup\$ Ok ive tried it and it works when the user input is AABBCCCC however when user input ABABABABA the answer should be 0 because there are no consecutive but the method you gave returns a number. \$\endgroup\$ Commented Jan 29, 2015 at 3:43
  • \$\begingroup\$ @MixAustria - Oops missed the part about consecutive chars. I added more code for you to look at. \$\endgroup\$ Commented Jan 29, 2015 at 4:06
1
\$\begingroup\$

I'll suggest a different method to find the replaceable character count of consecutive characters in a string. I will explain it with the help of code and example execution.

Consider the code:

Dim inputStr As String = "AAAABBBC" '<--- input string 
Dim splitArray As Char() = inputStr.ToCharArray '<--- split the string as character array
Dim replaceCount As Integer = 0 '<-- initialize the variable for counting the replaceable characters
For i As Integer = 0 To splitArray.Length - 1 '<--- take each characters in the array
 If splitArray(i) = splitArray(i + 1) Then '<--- Compare with the next immediate character if comparison gives true then increment the counter
 replaceCount += 1 '<--- increment the counter for each match
 End If
Next
MsgBox(replaceCount) '<--- will display 5 in this situation

If you want to perform this for number of lines then you have to place the code in a looping statement that iterates through each line.

I have another suggestion for reducing the number of replaceable characters:

Let the input string be "AAA" AS per this solution the number of replaceable characters are 2. But only one character is enough, no? If we replace the middle character ("AXA") then the output will not contains any consecutive characters, so that I can reduce the number of replaceable characters to 1.

Coding for doing this operation:

Dim inputStr As String = "AAAABXXC" '<--- input string 
Dim splitArray As Char() = inputStr.ToCharArray '<-- split into array
Dim rnd As New Random '<--- generate a random number for replaceable character
Dim replaceCount As Integer = 0
For i As Integer = 0 To splitArray.Length - 2
 If splitArray(i) = splitArray(i + 1) Then
 replaceCount += 1 ' <-- increment the count 
 splitArray(i + 1) = Chr(rnd.Next(128, 200)) '<-- Replace the character with random special character
 End If
Next 
MsgBox(replaceCount) '<--- it will display 3 for this time
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered Jan 29, 2015 at 6:45
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.