Skip to content

Instantly share code, notes, and snippets.

@brunopgalvao
Created November 13, 2011 01:07
Show Gist options
  • Save brunopgalvao/1361415 to your computer and use it in GitHub Desktop.
Save brunopgalvao/1361415 to your computer and use it in GitHub Desktop.
Developer Assignment
' Bruno Galvao
' 11/12/11
'
' Given an array of integers between 1 and 1,000,000.
' One integer is in the array twice. Find the duplicate.
Module Module1
Sub Main()
Dim Numbers() As Integer = {5, 10, 12, 8, 8, 14}
' Unit Test
' Pass array as argument.
Console.WriteLine(findDup(Numbers))
End Sub
Function findDup(ByVal Numbers() As Integer) As Integer
Dim hash As HashSet(Of Integer)
For Each Num In Numbers
' HashSet only accepts unique values
' If we try to add a duplicate value
' then hash.Add(Num) will fail.
' The returned value will be the duplicate.
If (Not hash.Add(Num)) Then
Return (Num)
End If
Next
' No duplicate found.
Return (0)
End Function
End Module
' Bruno Galvao
' 11/12/11
'
' Find the first non-repeating character in a string:("DEFD" -> E )
Module Module2
Sub Main()
' Unit test
' Pass string as argument.
Console.WriteLine(nonRepeat("(""DEFD"" -> E )"))
End Sub
Function nonRepeat(ByVal aString As String) As String
Dim repeated As Integer = 0
For i = 0 To aString.Length - 1
repeated = 0
For j = 0 To aString.Length - 1
' If inner and outer For loops are on the same index then
' inner For loop moves to next index and compares character
' with outer For loop character.
' If characters are equal then set repeated = 1 and Exit inner For loop.
' Otherwise, continue to find repeating character
' If reached end of string without finding repeating character
' then non-repeating character has been found and is returned.
If ((i <> j) AndAlso (aString(i) = aString(j))) Then
' Found repeating character
repeated = 1
Exit For
End If
Next
If (repeated = 0) Then
' Found first non-repeating character
Return aString(i)
End If
Next
Return ("No Non-Reapeating character!")
End Function
End Module
' Bruno Galvao
' 11/12/11
'
' A standard deck of 52 cards is represented in an array.
' Each card is represented as an integer.
' Write a method to shuffle the cards.
Module Module3
Sub Main()
' Create deck of cards
Dim Cards(51) As Integer
' Unit Test
' Pass array as argument.
Console.WriteLine(shuffle(Cards))
End Sub
Function shuffle(ByVal Cards() As Integer)
Dim rand = New Random()
For counter = 0 To Cards.Length - 1
Dim n = rand.Next(counter + 1)
' Swap card with random card in deck
Dim temp = Cards(counter)
Cards(counter) = Cards(n)
Cards(n) = temp
Next
Return (Cards)
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment