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
mipadi
post Apr 5 2009, 02:30 PM
Post #2


Senior Member
******

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



Objective-C

This one isn't dramatically different from the C version...but for such a low-level task, that's to be expected, I guess.

CODE
/* Compile:
*   $ gcc -std=c99 -o matcher-objc -framework Foundation matcher.m
*   # ./matcher-objc
*/

#import <Foundation/Foundation.h>


#define foreach(s)     for (unsigned int i = 0; i < [s length]; i++)


static BOOL matchesPattern1(NSString *s)
{
    struct { unsigned int a; unsigned int b; unsigned int c; } sums = {0,0,0};
    
    foreach(s) {
        switch ([s characterAtIndex:i]) {
            case 'a': sums.a++; break;
            case 'b': sums.b++; break;
            case 'c': sums.c++; break;
        }
    }
    
    return sums.a + sums.b == sums.c;
}


static BOOL matchesPattern2(NSString *s)
{
    struct { unsigned int a; unsigned int b; unsigned int c; } sums = {0,0,0};
    struct { BOOL a; BOOL b; BOOL c; } saw = {NO,NO,NO};
    
    foreach(s) {
        switch ([s characterAtIndex:i]) {
            case 'a':
                if (saw.a || saw.b) return NO;
                saw.a = YES;
                sums.a++;
                break;
            case 'b':
                if (saw.c) return NO;
                saw.b = YES;
                sums.b++;
                break;
            case 'c':
                saw.c = YES;
                sums.c++;
                break;
        }
    }
    
    return sums.a + sums.b == sums.c;
}


static BOOL matchesPattern3(NSString *s)
{
    struct { unsigned int a; unsigned int c; } sums = {0,0};
    struct { BOOL a; BOOL b; BOOL c; } saw = {NO,NO,NO};
    
    foreach(s) {
        switch ([s characterAtIndex:i]) {
            case 'a':
                if (saw.b || saw.c) return NO;
                saw.a = YES;
                sums.a++;
                break;
            case 'b':
                if (saw.c) return NO;
                saw.b = YES;
                break;
            case 'c':
                saw.c = YES;
                sums.c++;
                break;
        }
    }
    
    return sums.a == sums.c && sums.a > 0;
}


int main(int argc, char **argv)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    char buf[81];
    
    while (fscanf(stdin, "%s\n", buf) != EOF) {
        NSString *s = [[NSString alloc] initWithCString:buf];
        printf("%s\n", [s UTF8String]);
        printf("  matches pattern 1? %s\n", (matchesPattern1(s) ? "true" : "false"));
        printf("  matches pattern 2? %s\n", (matchesPattern2(s) ? "true" : "false"));
        printf("  matches pattern 3? %s\n", (matchesPattern3(s) ? "true" : "false"));
        [s release];
    }
    
    [pool release];
    return 0;
}

Source: matcher.m
 

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: