Program this!, Programming thread |
Program this!, Programming thread |
![]()
Post
#1
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 2,648 Joined: Apr 2008 Member No: 639,265 ![]() |
I'm of the understand that there are a few Technology forum aficionados that are programmers, or are at least interested in programming, so I thought I'd start a thread -- a sort of game. The game was inspired by this thread. It's pretty simple: just implement the following program in whatever language you desire. If someone's already posted a program in your favorite language, see if you can do better! You get bragging rights if you implement the program in a weird/unusual/rare language.
Here are the guidelines for the program:
|
|
|
![]() |
![]()
Post
#2
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Official Member Posts: 1,574 Joined: Aug 2007 Member No: 555,438 ![]() |
I'd love to do this, but "anbmc(n + m), where n ≥ 0 and m ≥ 0." I don't understand this line.
|
|
|
![]()
Post
#3
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 2,648 Joined: Apr 2008 Member No: 639,265 ![]() |
|
|
|
![]()
Post
#4
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Official Member Posts: 1,574 Joined: Aug 2007 Member No: 555,438 ![]() |
It's a string in which the total number of a's and b's equals the total number of c's. Valid: "aabbcccc", "acbc", "abaccc" Invalid: "abc", "aabbc", "cabb" vbscript CODE strTest = "ccccc" wscript.echo "1:" & pattern1(strTest) & " 2:" & pattern2(strTest) & " 3:" & pattern2(strTest) Private Function pattern1(strValue) numAB = 0 numC = 0 numPos = 1 strPos = " " Do Until numPos = Len(strValue) + 1 strPos = Mid(strValue,numPos,1) If StrComp(strPos, "a", vbTextCompare) = 0 OR StrComp(strPos, "b", vbTextCompare) = 0 Then numAB = numAB + 1 ElseIf StrComp(strPos, "c", vbTextCompare) = 0 Then numC = numC + 1 End If numPos = numPos + 1 Loop If numC => numAB Then pattern1 = "true" Else pattern1 = "false" End If End Function Private Function pattern2(strValue) numAB = 0 numC = 0 numPos = 1 strPos = Mid(strValue,numPos,1) blnA = true blnB = false blnC = true If StrComp(strPos, "b", vbTextCompare) = 0 OR StrComp(strPos, "c", vbTextCompare) = 0 Then pattern2 = "false" Exit Function End If Do Until numPos = Len(strValue) + 1 strPos = Mid(strValue,numPos,1) If StrComp(strPos, "a", vbTextCompare) = 0 Then If blnA = true Then blnA = true blnB = true blnC = true Else pattern2 = "false" Exit Function End If ElseIf StrComp(strPos, "b", vbTextCompare) = 0 Then If blnB = true Then blnA = false blnB = true blnC = true Else pattern2 = "false" Exit Function End If ElseIf StrComp(strPos, "c", vbTextCompare) = 0 Then If blnC = true Then blnA = false blnB = false blnC = true Else pattern2 = "false" Exit Function End If End If If StrComp(strPos, "a", vbTextCompare) = 0 OR StrComp(strPos, "b", vbTextCompare) = 0 Then numAB = numAB + 1 ElseIf StrComp(strPos, "c", vbTextCompare) = 0 Then numC = numC + 1 End If numPos = numPos + 1 Loop If numC => numAB Then pattern2 = "true" Else pattern2 = "false" End If End Function Private Function pattern3(strValue) numAB = 0 numC = 0 numPos = 1 strPos = Mid(strValue,numPos,1) blnA = true blnB = false blnC = false If StrComp(strPos, "b", vbTextCompare) = 0 OR StrComp(strPos, "c", vbTextCompare) = 0 Then pattern3 = "false" Exit Function End If Do Until numPos = Len(strValue) + 1 strPos = Mid(strValue,numPos,1) If StrComp(strPos, "a", vbTextCompare) = 0 Then If blnA = true Then blnA = true blnB = true blnC = true Else pattern3 = "false" Exit Function End If ElseIf StrComp(strPos, "b", vbTextCompare) = 0 Then If blnB = true Then blnA = false blnB = true blnC = true Else pattern3 = "false" Exit Function End If ElseIf StrComp(strPos, "c", vbTextCompare) = 0 Then If blnC = true Then blnA = false blnB = false blnC = true Else pattern3 = "false" Exit Function End If End If If StrComp(strPos, "a", vbTextCompare) = 0 OR StrComp(strPos, "b", vbTextCompare) = 0 Then numAB = numAB + 1 ElseIf StrComp(strPos, "c", vbTextCompare) = 0 Then numC = numC + 1 End If numPos = numPos + 1 Loop If numC => numAB Then If numAB > 0 Then pattern2 = "true" Else pattern2 = "false" End If Else pattern2 = "false" End If End Function Like that? |
|
|
![]() ![]() |