RUSES
Would you like to react to this message? Create an account in a few clicks or log in to continue.
May 2024
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  

Calendar Calendar


Daily Programming Challenge #2 23/02/12

2 posters

Go down

Daily Programming Challenge #2 23/02/12 Empty Daily Programming Challenge #2 23/02/12

Post  Alex Fri Feb 24, 2012 12:38 am

(Solutions for Challenge #1 are still being accepted for points until later today which is when I'll post my solution)

These are examples of creatures called Flombs \(O_O)/ \(O_O\) /(O_O)/

All the above Flombs have two eyes OO, two arms \/, two ears (), one mouth _

These are other examples of people which don't follow the same structure:

\(_O)/ \((O_)/ (O_)/

The requirements for a Flomb to exist are:

It has: no more than 2 arms, 1 or 2 eyes, 1 mouth - mouths can be longer than one underscore - even infinitely long, the following Flomb has one mouth only: \(O______O(/ BUT this Flomb has 2 mouths \(O_O_), 2 or 3 ears

i.e. the following wouldn't be Flombs:
\\(O_O)/ has 3 arms (too many)
\(__)/ has 0 eyes
\(OO___O)/ has 3 eyes
\(((O_O)/ has 4 ears
\(O_O/ only has 1 ears

Write functions that do the following:

bool isFlomb(string) - returns true if the given string is a Flomb - it doesn't matter about the position of things, i.e. \\\___(()OO would still be a Flomb
void getStatistics(string) - prints the statistics of a given Flomb, i.e. number of eyes, number of ears etc
string generateFlomb() - that will generate a random Flomb, doesn't need to look 'right' just needs to make different Flombs that can all pass the isFlomb() test
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Daily Programming Challenge #2 23/02/12 Empty Re: Daily Programming Challenge #2 23/02/12

Post  Charlie Fri Feb 24, 2012 3:16 am

Not a great solution to be honest but it gets the job done Smile (Criticism is welcome Smile )

Also on pastebin: http://pastebin.com/ZK0p3dZY

Spoiler:
Charlie
Charlie

Posts : 16
Join date : 2012-02-23

Back to top Go down

Daily Programming Challenge #2 23/02/12 Empty Re: Daily Programming Challenge #2 23/02/12

Post  Alex Fri Feb 24, 2012 4:25 am

Nice,testing it atm (with my isFlomb() func) but having to edit your generate function, it's a char* but it's returning 0, think you forget to return a string Razz
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Daily Programming Challenge #2 23/02/12 Empty Re: Daily Programming Challenge #2 23/02/12

Post  Alex Fri Feb 24, 2012 4:27 am

Changed to

Code:
char * generateFlomb(void )
    {
            char *temp = new char[10];
         
            while(isFlomb(temp) == 0)
            {
                    arms = 0, ears = 0, eyes = 0, mouth = 0;
                    memset(temp, '\0', 10);
                    int a = 0, b = 0, c = 0;
                    a = rand() % 8;
         
                    for(int i = 0; i < a; i++)
                    {
                            b = rand() % 4;
                            c = rand() % 2;
         
                            switch(b)
                            {
                            case 0: //arm
                                    if(arms == 2)
                                    {
                                            // no more arms
                                    }
                                    else
                                    {
                                            if(c == 0)
                                                    temp[i] = '/';
                                            else
                                                    temp[i] = '\\';
                                            arms++;
                                    }
                                    break;
                            case 1: //ear
                                    if(ears == 3)
                                    {
                                            // no more ears
                                    }
                                    else
                                    {
                                            if(c == 0)
                                                    temp[i] = '(';
                                            else
                                                    temp[i] = ')';
                                            ears++;
                                    }
                                    break;
                            case 2: //eye
                                    if(eyes == 2)
                                    {
                                            // no more eyes
                                    }
                                    else
                                    {
                                            temp[i] = 'O';
                                            eyes++;
                                    }
                                    break;
                            case 3: //mouth
                                    if(mouth > 0)
                                    {
                                            if(temp[i-1] != '_')
                                            {
                                                    // no more mouths
                                            }
                                            else
                                            {
                                                    temp[i] = '_'; // longer mouth
                                            }
                                    }
                                    else
                                    {
                                            temp[i] = '_';
                                            mouth++;
                                    }
                                    break;
                            }
                    }
            }
   
            for(int i = 0; i < 10; i++)
            {
                    flomb[i] = temp[i];
            }
   
            return temp;
    }
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Daily Programming Challenge #2 23/02/12 Empty Re: Daily Programming Challenge #2 23/02/12

Post  Alex Fri Feb 24, 2012 4:38 am

It works Smile

  • Try using C++ strings instead of C style strings (or look into STL Vectors - though they are a bit overkill for this, but still they are very powerful) as C++ strings you don't have to worry about the size, so theoretically you can generate infinite size Flombs on the go, and not have a massive char Flomb[2348902357897937896389] array hogging memory which most likely will never be used
  • I (not sure but I've never seen it done before) don't think calling main() is good practice, you could easily stick in a while loop rather than calling main() - *may* break the function stack eventually as you are recursively calling main()
  • And as I mentioned above, returning 0 as a char pointer


Otherwise perfectly fine Smile
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Daily Programming Challenge #2 23/02/12 Empty Re: Daily Programming Challenge #2 23/02/12

Post  Charlie Fri Feb 24, 2012 4:49 am

Got confused with pointers and went for the easy way out Razz It still works but the function doesn't return anything useful. Thanks for the correction, I'll bear that in mind next time Smile
Charlie
Charlie

Posts : 16
Join date : 2012-02-23

Back to top Go down

Daily Programming Challenge #2 23/02/12 Empty Re: Daily Programming Challenge #2 23/02/12

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum