Log In · Register

 
Program this!, Programming thread
mipadi
post Mar 23 2009, 09:27 AM
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:

  1. Write a method named matchesPattern1() that accepts a String as input. This method determines whether or not the input String has the following property:

    The string has a number of c's equal to the total number of a's and b's.

    In other words, every word in this language is of the form:

    anbmc(n + m), where n ≥ 0 and m ≥ 0.

    n and m may be equal, but this is not required.

    Thus, bc, abccac, aabbcccc and abbbbccccc are all valid words in this language.

    Your method should return a boolean value: true if the input string satisfies these requirements, and false if it does not.

    For example, given a String such as aabbcc, your method should return false.
  2. Write a method named matchesPattern2() that accepts a String as input. This method determines whether or not the input String has the following properties:

    The string has a number of c's equal to the total number of a's and b's.
    All a's come before any b's, and all b's come before any c's. (HINT: Can you test for this using one or more boolean variable "flags"?)


    In other words, every word in this language is of the form:

    anbmc(n + m), where n ≥ 0 and m ≥ 0.

    n and m may be equal, but this is not required.

    Thus, aabbcccc and abbbbccccc are valid words in this language, but abaccc and abc are not.

    Your method should return a boolean value: true if the input string satisfies these requirements, and false if it does not.

    For example, given a String such as aabbcc, your method should return false.

    This pattern is identical to the one above, except it imposes a specific ordering requirement on the input string.
  3. Write a method named matchesPattern3() that accepts a String as input. This method determines whether or not the input String has the following properties:

    The string has an equal (nonzero) number of a's and c's, and any number of (zero or more) b's.
    All a's come before any b's, and all b's come before any c's.


    In other words, every word in this language is of the form:

    anb*cn, where n > 0.

    Thus, aabbcc and ac are valid words in this language, but abaccc and aabc are not.

    Your method should return a boolean value: true if the input string satisfies these requirements, and false if it does not.

    For example, given a String such as aabbaa, your method should return false.
 
 
Start new topic
Replies
Uronacid
post Mar 23 2009, 10:21 AM
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.

 
mipadi
post Mar 23 2009, 10:24 AM
Post #3


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



QUOTE(Uronacid @ Mar 23 2009, 11:21 AM) *
I'd love to do this, but "anbmc(n + m), where n ≥ 0 and m ≥ 0." I don't understand this line.

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"
 
Uronacid
post Mar 23 2009, 10:38 AM
Post #4


Senior Member
******

Group: Official Member
Posts: 1,574
Joined: Aug 2007
Member No: 555,438



QUOTE(mipadi @ Mar 23 2009, 11:24 AM) *
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?
 

Posts in this topic
mipadi   Program this!   Mar 23 2009, 09:27 AM
mipadi   Haskell CODEmatchesPattern1 s = mp s (0, 0, 0...   Mar 23 2009, 09:28 AM
mipadi   Erlang CODE-module(matcher). -export(...   Mar 23 2009, 09:29 AM
Uronacid   I'd love to do this, but "anbmc(n + m), w...   Mar 23 2009, 10:21 AM
mipadi   QUOTE(Uronacid @ Mar 23 2009, 11:21 AM) I...   Mar 23 2009, 10:24 AM
Uronacid   QUOTE(mipadi @ Mar 23 2009, 11:24 AM) It...   Mar 23 2009, 10:38 AM
9001   Any specific language we have to use? I might sta...   Mar 23 2009, 10:35 AM
mipadi   QUOTE(9001 @ Mar 23 2009, 11:35 AM) Any s...   Mar 23 2009, 12:09 PM
Uronacid   QUOTE(mipadi @ Mar 23 2009, 01:09 PM) No,...   Mar 23 2009, 01:11 PM
mipadi   QUOTE(Uronacid @ Mar 23 2009, 02:11 PM) y...   Mar 23 2009, 06:41 PM
Uronacid   QUOTE(mipadi @ Mar 23 2009, 07:41 PM) Yea...   Mar 23 2009, 09:25 PM
mipadi   QUOTE(Uronacid @ Mar 23 2009, 10:25 PM) Y...   Mar 23 2009, 09:39 PM
Uronacid   QUOTE(mipadi @ Mar 23 2009, 10:39 PM) Neg...   Mar 23 2009, 10:19 PM
9001   Cool. I'll see if I can somehow get this done...   Mar 23 2009, 12:15 PM
mipadi   Ruby CODEdef matches_pattern_1?(s) if s ...   Mar 23 2009, 12:32 PM
mipadi   C CODE#include <stdio.h> #include <str...   Mar 23 2009, 01:05 PM
mipadi   Python CODEdef matches_pattern_1(s): ...   Mar 23 2009, 07:21 PM
mipadi   Hey! There are more programmers than just the ...   Apr 5 2009, 01:55 PM
9001   QUOTE(mipadi @ Apr 5 2009, 12:55 PM) Hey...   Apr 5 2009, 02:44 PM
mipadi   QUOTE(9001 @ Apr 5 2009, 03:44 PM) At lea...   Apr 5 2009, 02:47 PM
mipadi   Objective-C This one isn't dramatically diffe...   Apr 5 2009, 02:30 PM


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: