Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
C coding challenge
Goto page 1, 2  Next
 
Post new topic   Reply to topic Printable version
 View previous topic  Fibonacci Series into a Really Large N... Post :: Post C++ : Inheritance,Template and Threads...  View next topic  
Author Message
Bak
?ls -s
0 in


Age:18
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Tue Aug 04, 2009 11:35 pm    Post subject: C coding challenge Reply to topic Reply with quote

found these online, thought they were fun so I'll share. no cheating! I'll do one at a time; there's 5 total.

challenge #1: write strlen function without any types in the function body (types such as 'int' or 'char' or 'char*')

here's standard version for reference:
Code: Show/Hide
int strlen(char* s)
{
  int len = 0;

  for (int x = 0; s[x]; ++x)
    ++len;

  return len;
}

_________________
SubSpace Discretion: A Third Generation SubSpace Client
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Dr Brain
Flip-flopping like a wind surfer


Age:31
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Wed Aug 05, 2009 6:38 am    Post subject: Reply to topic Reply with quote

Took me a few minutes of trying to figure out how to get an extra hidden parameter into the declaration, but recursion is the answer.

Code: Show/Hide
int strlen(char *s)
{
   if (*s) return strlen(s+1) + 1;
   else return 0;
}


Note: I didn't compile or test it, but it gets the general idea across.
_________________
Hyperspace Owner

Smong> so long as 99% deaths feel lame it will always be hyperspace to me
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Bak
?ls -s
0 in


Age:18
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Wed Aug 05, 2009 9:12 am    Post subject: Reply to topic Reply with quote

That's right. Next one is a little easier, but try to do it without cheating (if you cheat don't post answer).

Challenge #2: what's the following code output and why:

Code: Show/Hide
printf("%c", 1["dr brain"]+1);
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Dr Brain
Flip-flopping like a wind surfer


Age:31
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Wed Aug 05, 2009 1:27 pm    Post subject: Reply to topic Reply with quote

s

The brackets don't care about order, so 1["dr brain"] is the same as "dr brain"[1], so that whole expression is 'r'+1 or 's'.
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Bak
?ls -s
0 in


Age:18
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Thu Aug 06, 2009 9:22 am    Post subject: Reply to topic Reply with quote

Challenge #3: Write a program that only prints "dr brain won #2" and has exit code 100 when the process terminates. Don't use any semicolons.
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Doc Flabby
Server Help Squatter


Joined: Feb 26 2006
Posts: 636
Offline

PostPosted: Thu Aug 06, 2009 9:45 am    Post subject: Reply to topic Reply with quote

pretty sure this isnt the right way to solve this one icon_smile.gif
Code: Show/Hide

int main()
{
   return printf("dr brain won #2 \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b\n")
}

_________________
Rediscover online gaming. Get Subspace | STF The future...prehaps
Back to top
View users profile Send private message Add User to Ignore List
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Thu Aug 06, 2009 10:22 am    Post subject: Reply to topic Reply with quote

That doesn't compile... return requires a ;

in VS anyway
_________________
DCME co-developer
17th Parallel Head Sysop
Subspace: The Future
Back to top
View users profile Send private message Add User to Ignore List
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Thu Aug 06, 2009 10:23 am    Post subject: Reply to topic Reply with quote

I'd do something like


if ( exit( printf("dr brain won #2")+85 ) )
{
}

But exit returns a void, so it doesn't compile either
Back to top
View users profile Send private message Add User to Ignore List
Dr Brain
Flip-flopping like a wind surfer


Age:31
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Thu Aug 06, 2009 11:04 am    Post subject: Reply to topic Reply with quote

Took a lot of head banging to come up with this:

Code: Show/Hide

int main()
{
    if (!pthread_create(malloc(sizeof(pthread_t)), NULL, exit, printf("dr brain won #2\n") + 100 - 16)) {while(1){}}
}


I'd wager there's a better solution than this.
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Thu Aug 06, 2009 11:10 am    Post subject: Reply to topic Reply with quote

+ 100 - 16 ? lol
Back to top
View users profile Send private message Add User to Ignore List
Dr Brain
Flip-flopping like a wind surfer


Age:31
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Thu Aug 06, 2009 11:44 am    Post subject: Reply to topic Reply with quote

I had to add a \n at the end because zsh freaks out if it's missing, so it was 16 instead of 15.
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Thu Aug 06, 2009 11:59 am    Post subject: Reply to topic Reply with quote

...

My point was, + 84 would be simpler tongue.gif
Though I can see the why... but still
Back to top
View users profile Send private message Add User to Ignore List
Hakaku
Server Help Squatter


Joined: Apr 07 2006
Posts: 299
Location: Canada
Offline

PostPosted: Thu Aug 06, 2009 2:44 pm    Post subject: Reply to topic Reply with quote

Samapico wrote:
I'd do something like


if ( exit( printf("dr brain won #2")+85 ) )
{
}

But exit returns a void, so it doesn't compile either

Couldn't you just put this in a while loop instead? As in (not sure if the 1 should go before or after)...

while (exit( printf("dr brain won #2\n")+84 ), 1)
{
}


edit: + \n


Last edited by Hakaku on Thu Aug 06, 2009 2:55 pm, edited 1 time in total
Back to top
View users profile Send private message Add User to Ignore List Send email
Dr Brain
Flip-flopping like a wind surfer


Age:31
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Thu Aug 06, 2009 2:55 pm    Post subject: Reply to topic Reply with quote

Yes, the comma operator was the missing piece of the puzzle. I'd completely forgotten that it existed. Kudos for a more elegant solution.
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Initrd.gz
Seasoned Helper


Joined: Sep 18 2008
Posts: 134
Location: Over there --->
Offline

PostPosted: Thu Aug 06, 2009 9:40 pm    Post subject: Reply to topic Reply with quote

Cuz just maybe we don't want to use semicolons in C biggrin.gif
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Bak
?ls -s
0 in


Age:18
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Thu Aug 06, 2009 11:21 pm    Post subject: Reply to topic Reply with quote

yeah comma operator was the trick, although pthread version was pretty original, i'll post next one in a little bit

if (printf("dr brain won #2"), exit(100), 0) {}
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Bak
?ls -s
0 in


Age:18
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Thu Aug 06, 2009 11:38 pm    Post subject: Reply to topic Reply with quote

Challenge #4: What's the following code print and why:

Code: Show/Hide
#include <stdio.h>

int i(int i, int ii)
{
   return ii == i;
}

int main()
{
   int (*ii)(int, int) = i;
   int i = 0;

   if (i++ == ++i)
      printf("%i", i + 1);
   else
      printf("%i", i);

   if (ii(i++, ++i))
      printf("%i", i + 1);
   else
      printf("%i", i);

   if (ii(++i, i))
      printf("%i", i + 1);
   else
      printf("%i", i);

   return 0;
}
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Thu Aug 06, 2009 11:49 pm    Post subject: Reply to topic Reply with quote

Never seen that comma operator... well... except in a for statement...

So basically, the value returned by the whole expression is the result of the part after the last comma?


And holy shit, that's a lot of i's
Back to top
View users profile Send private message Add User to Ignore List
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Fri Aug 07, 2009 12:00 am    Post subject: Reply to topic Reply with quote

hmm... ii would be a pointer to the function...

i++ == ++i
would be false, and increments i twice...

> 2

you call ii with (2, 3), which returns false, i becomes 4

> 4

you call ii with ... I guess it depends in which order arguments are evaluated. ......... hmmmmm hold on a second. I think they're evaluated in reverse order... which means my past reasoning doesn't work.


The first if block is fine...
>2
the next one...
i = 2
second argument evaluated first, gives 3, then first argument, gives 3 also; i becomes 4 after
so... it returns true
> 5

now i = 4
you call ii with (5, 4) -> false
> 5

So...
output:
> 255
Back to top
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:18
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Fri Aug 07, 2009 1:41 am    Post subject: Reply to topic Reply with quote

Quote:
So basically, the value returned by the whole expression is the result of the part after the last comma?
yeah

It gets even more confusing in C++ where you can do operator overloading. Consider the following code:

Code: Show/Hide
#include <stdio.h>

struct EqualInt
{
   int i;

   EqualInt operator++() { ++i; return *this; } // preincrement
   EqualInt operator++(int) { EqualInt copy = *this; ++i; return copy; } // post increment
};

bool operator==(const EqualInt &left, const EqualInt &right)
{
   return left.i == right.i;
}

int main()
{
   EqualInt a;

   int i = 0;

   if (i++ == ++i)
      printf("true\n");
   else
      printf("false\n");

   if (a++ == ++a)
      printf("true\n");
   else
      printf("false\n");

   return 0;
}


the int's get evaluated from left to right, but the a's comparison is actually a function call to operator==, so they get evaluated from right to left and the code ends up printing false then true. This is even the case if you move the operator== into the struct, so it actually ends up constructing the parameter before it constructs the the instance it will be calling.

It's impossible to know if (i++ == ++i) is true or false unless you know the types.
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Samapico
No, these DO NOT look like penises, ok?


Age:31
Gender:Gender:Male
Joined: May 08 2003
Posts: 1252
Location: Montreal, Canada
Offline

PostPosted: Fri Aug 07, 2009 2:30 pm    Post subject: Reply to topic Reply with quote

So did I get it or not? tongue.gif
Back to top
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:18
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Fri Aug 07, 2009 4:21 pm    Post subject: Reply to topic Reply with quote

Yes, you could also compile and check icon_wink.gif

Anyways, Challenge #5 (last one):

Here is a swap function:
Code: Show/Hide
void swap(int* a, int* b)
{
   int temp = *a;
   *a = *b;
   *b = temp;
}


Now rewrite the body of the function to have the same functionality but only using one semicolon, no commas, and not containing "if" anywhere in the body.
Back to top
View users profile Send private message Add User to Ignore List AIM Address
JoWie
Server Help Squatter


Gender:Gender:Male
Joined: Feb 25 2004
Posts: 215
Offline

PostPosted: Fri Aug 07, 2009 5:28 pm    Post subject: Reply to topic Reply with quote

Code: Show/Hide

void swap(int* a, int* b)
{
   *a ^= (*b ^= (*a ^= *b));
}



assuming a != b



or (safe but lame version)

Code: Show/Hide

void swap(int* a, int* b)
{
   #define lol ;
   int temp = *a lol
   *a = *b lol
   *b = temp lol
}
Back to top
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:18
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Fri Aug 07, 2009 6:41 pm    Post subject: Reply to topic Reply with quote

can you do it without assuming a != b?

lol@lame version too
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Doc Flabby
Server Help Squatter


Joined: Feb 26 2006
Posts: 636
Offline

PostPosted: Fri Aug 07, 2009 7:02 pm    Post subject: Reply to topic Reply with quote

Code: Show/Hide

void swap(int* a, int* b)
{
       a==b ?   b = a :  *a ^= (*b ^= (*a ^= *b));
}



^^
Back to top
View users profile Send private message Add User to Ignore List
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Non-Subspace Related Coding All times are GMT - 5 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 84 page(s) served in previous 5 minutes.

phpBB Created this page in 0.132814 seconds : 50 queries executed (46.1%): GZIP compression disabled