Daily Programming Challenge #2 23/02/12
2 posters
RUSES :: Programming :: Challenges
Page 1 of 1
Daily Programming Challenge #2 23/02/12
(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
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- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Daily Programming Challenge #2 23/02/12
Not a great solution to be honest but it gets the job done (Criticism is welcome )
Also on pastebin: http://pastebin.com/ZK0p3dZY
Also on pastebin: http://pastebin.com/ZK0p3dZY
- Spoiler:
- Code:
#include <iostream>
#include <cstring>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
bool isFlomb(char *);
void getStatistics(char *);
char * generateFlomb(void );
char flomb[20];
int eyes, arms, ears, mouth;
int main()
{
srand(time(NULL));
cout << "Press 1 to enter your own Flomb, 2 to generate a Flomb or 3 to get statistics" << endl;
cout << "Option: ";
char option = _getch();
cout << endl << endl;
if(option == '1') // user inputs flomb
{
cout << "Enter flomb: ";
cin.getline(flomb, 20);
if(isFlomb(flomb) == 1)
{
cout << endl << "Your Flomb is valid!" << endl << endl;
}
else
{
cout << endl << "Your Flomb is not valid." << endl << endl;
}
}
else if(option == '2') // generate flomb
{
generateFlomb();
cout << "Generated Flomb: " << flomb << endl << endl;
}
else if(option == '3') // get statistics
{
getStatistics(flomb);
}
else // invalid input
{
cout << "Please try again." << endl;
}
main();
return 0;
}
bool isFlomb(char * input)
{
int length = strlen(input);
arms = 0, ears = 0, eyes = 0, mouth = 0;
for(int i = 0; i < length; i++)
{
switch(input[i])
{
case '\\':
case '/':
arms++;
break;
case '_':
if(mouth > 0)
{
if(input[i-1] != '_')
{
mouth++; // more than one mouth
}
else
{
// just a long mouth
}
}
else
{
mouth++; // first mouth
}
break;
case 'O':
case 'o':
eyes++;
break;
case '(':
case ')':
ears++;
}
}
if(arms > 2)
{
return 0;
}
else if(eyes > 2 || eyes < 1)
{
return 0;
}
else if(ears > 3 || ears < 2)
{
return 0;
}
else if(mouth > 1 || mouth < 1)
{
return 0;
}
else
{
return 1;
}
}
void getStatistics(char * input)
{
int length = strlen(input);
arms = 0, ears = 0, eyes = 0, mouth = 0;
for(int i = 0; i < length; i++)
{
switch(input[i])
{
case '\\':
case '/':
arms++;
break;
case '_':
if(mouth > 0)
{
if(input[i-1] != '_')
{
mouth++; // more than one mouth
}
else
{
// just a long mouth
}
}
else
{
mouth++; // first mouth
}
break;
case 'O':
case 'o':
eyes++;
break;
case '(':
case ')':
ears++;
}
}
cout << "Flomb has " << arms << " arms, " << ears << " ears, " << eyes << " eyes and " << mouth << " mouths" << endl << endl;
}
char * generateFlomb(void )
{
char temp[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 0;
}
Charlie- Posts : 16
Join date : 2012-02-23
Re: Daily Programming Challenge #2 23/02/12
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
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Daily Programming Challenge #2 23/02/12
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- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Daily Programming Challenge #2 23/02/12
It works
Otherwise perfectly fine
- 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
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Daily Programming Challenge #2 23/02/12
Got confused with pointers and went for the easy way out It still works but the function doesn't return anything useful. Thanks for the correction, I'll bear that in mind next time
Charlie- Posts : 16
Join date : 2012-02-23
Similar topics
» Daily Programming Challenge #1 22/02/12
» Weekend Programming Challenge #3 24/02
» Programming Challenge #4 - Code Obfuscation
» Challenge Rules
» Challenge Points
» Weekend Programming Challenge #3 24/02
» Programming Challenge #4 - Code Obfuscation
» Challenge Rules
» Challenge Points
RUSES :: Programming :: Challenges
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum