Coding Challenge
Coding Challenge
Take a brain break from whatever you're doing and relax.
Just a little thing.
make a program that does exactly what the program I've uploaded does. This is a competition.
Score is number of characters over five, plus the sizeof() all variables initialized, plus function defs times 3 minus function calls times 2 all times 2.
chars*5 + bytes + functions*6 - calls*4 = score.
have fun, 12 points added for every functionality missed, the program should be able to handle inputs up to 4 bytes long. It's like golf.
Just a little thing.
make a program that does exactly what the program I've uploaded does. This is a competition.
Score is number of characters over five, plus the sizeof() all variables initialized, plus function defs times 3 minus function calls times 2 all times 2.
chars*5 + bytes + functions*6 - calls*4 = score.
have fun, 12 points added for every functionality missed, the program should be able to handle inputs up to 4 bytes long. It's like golf.
You can't be betrayed if you don't have any friends.
Why live? Cause why not.
Why live? Cause why not.
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Coding Challenge
So the list of numbers to choose doesn't have to be 1-10?
If the program should handle inputs up to four bytes long, which range are we talking? ( -INT_MAX - 1 to +INT_MAX ) or ( 0 to UINT_MAX ).
Where is the starting list of numbers coming from?
If the program should handle inputs up to four bytes long, which range are we talking? ( -INT_MAX - 1 to +INT_MAX ) or ( 0 to UINT_MAX ).
Where is the starting list of numbers coming from?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
Re: Coding Challenge
your program should do exactly what this one does. As far as what it prints based on input. I'll make some exceptions, but use int for user input. The starting list is arbitrary, don't make the program (uhhh... easily changeable what's that word?). as short as possible is the goal. It should do exactly what my program does.
You can't be betrayed if you don't have any friends.
Why live? Cause why not.
Why live? Cause why not.
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Coding Challenge
I'm most of the way there, got side tracked while waiting for your response.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
Re: Coding Challenge
It should be relatively simple and satisfying. Mine was only 64 lines.
You can't be betrayed if you don't have any friends.
Why live? Cause why not.
Why live? Cause why not.
Re: Coding Challenge
hello?
You can't be betrayed if you don't have any friends.
Why live? Cause why not.
Why live? Cause why not.
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Coding Challenge
Ok, this is what I came up with and is the best I can do to shrink it down as much as possible:
File size = 1,259 bytes
variables + function parameters = 76 bytes
functions I wrote: main and find_choice: 2
function calls = 15
( ( 1259 * 5 ) + 76 + ( 2 * 2 ) - ( 15 * 2 ) ) * 2 = 5136;
or
( ( 1259 / 5 ) + 76 + ( 2 * 2 ) - ( 15 * 2 ) ) * 2 = 603.6
Spoiler:
variables + function parameters = 76 bytes
functions I wrote: main and find_choice: 2
function calls = 15
Did I do this right?chars*5 + bytes + functions*6 - calls*4 = score
( ( 1259 * 5 ) + 76 + ( 2 * 2 ) - ( 15 * 2 ) ) * 2 = 5136;
or
( ( 1259 / 5 ) + 76 + ( 2 * 2 ) - ( 15 * 2 ) ) * 2 = 603.6
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
Re: Coding Challenge
[*]oh no, yeah I wrote it wrong. it's chars / 5.
so for my code, which is in c. (btw I'm really kidding myself here, I have no idea what I'm doing, I'm in high school, I'm so non-committal I need to get my act together. You're a million leagues above me.)
Your code is better in a lot of ways, such as being easily modified and it not requiring several iterations of the same printf() or whatever to get the print order right.
so, I should probably change the scoring because I've decided I don't like it but that would be unfair besides one thing. instead of char/5 it'll be chars/40. You can change your own score. and that could be characters without white space.
( 992 / 5 ) + 15 + ( 2 )*6 - 2*4
992chars / 40 + 15bytes + 2functions*6 - 2*4 = uhh. Actually, now I have a better understanding of this. I'm going to redesign the whole scoring system. I haven't been coding for a while because of school, so I've forgotten what I would need to judge a program. I think I have a better definition.
This could be used to judge any program but the coefficients and instructions on how to run the program to measure cycles would differ on different programs. There needs to be specific user, run time, instructions to judge the program.
Run the program and input the expected inputs. 1-10 and let the program close for memory used and cpu cycles. If you don't have tools with your IDE you can use a process explorer from the internet. These numbers could change but it's much better than before. There are so many inputs for system side statistics; run time statistics, that when balanced 1 to 1 for each code statistic; precompiled statistic, they out weigh the statistics on your code so I'd maybe want them to be worth less.
to clarify: run time statistics (f -> k), precompiled statistic (a -> e)
My score
a = 992, b = 9, c = 2, d = 2, e = 1, h = 33,792, i = 2,727,936, j = 630,784, k = 309,422,241
score = 23 + 18 - 12 + 8 - 6 + 17 + 19 + 21 + 21 = 109
You don't have to do this if it's too stupid.
Also, is someone going to pick up where chili left off, someone should keep making tutorials. Maybe with a new format because this forum exists, just answer questions in video form with elaboration and lot's of interesting follow up questions for the viewer to investigate.
so for my code, which is in c. (btw I'm really kidding myself here, I have no idea what I'm doing, I'm in high school, I'm so non-committal I need to get my act together. You're a million leagues above me.)
Spoiler:
so, I should probably change the scoring because I've decided I don't like it but that would be unfair besides one thing. instead of char/5 it'll be chars/40. You can change your own score. and that could be characters without white space.
( 992 / 5 ) + 15 + ( 2 )*6 - 2*4
992chars / 40 + 15bytes + 2functions*6 - 2*4 = uhh. Actually, now I have a better understanding of this. I'm going to redesign the whole scoring system. I haven't been coding for a while because of school, so I've forgotten what I would need to judge a program. I think I have a better definition.
- a = characters without whitespace
- b = calls to library functions
- c = calls to custom functions (including recursive calls)
- d = library functions used
- e = custom functions defined, not including main
- f = I/O reads, will elaborate
- g = I/O writes, will elaborate
- h = virtual size on disc in bytes
- i = peak working set in bytes
- j = peak private bytes
- k = cpu cycles, will elaborate
This could be used to judge any program but the coefficients and instructions on how to run the program to measure cycles would differ on different programs. There needs to be specific user, run time, instructions to judge the program.
Run the program and input the expected inputs. 1-10 and let the program close for memory used and cpu cycles. If you don't have tools with your IDE you can use a process explorer from the internet. These numbers could change but it's much better than before. There are so many inputs for system side statistics; run time statistics, that when balanced 1 to 1 for each code statistic; precompiled statistic, they out weigh the statistics on your code so I'd maybe want them to be worth less.
to clarify: run time statistics (f -> k), precompiled statistic (a -> e)
My score
a = 992, b = 9, c = 2, d = 2, e = 1, h = 33,792, i = 2,727,936, j = 630,784, k = 309,422,241
score = 23 + 18 - 12 + 8 - 6 + 17 + 19 + 21 + 21 = 109
You don't have to do this if it's too stupid.
Also, is someone going to pick up where chili left off, someone should keep making tutorials. Maybe with a new format because this forum exists, just answer questions in video form with elaboration and lot's of interesting follow up questions for the viewer to investigate.
You can't be betrayed if you don't have any friends.
Why live? Cause why not.
Why live? Cause why not.
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Coding Challenge
Yeah, I know you are a C user and I started with all C code, but decided to use C++ I/O as it looked nicer to me. Also, I like lambdas as they allow automatic capture depending on what you put between the [ and ], [&] means capture any variables used inside the lambda by reference and [=] means capture them by value.
Another reason I chose C++ was because C has no nullable value aside from 0, nor does it have a bool ( until C99's _Bool ). So returning a value like -1 or whatever from a function to indicate that the result wasn't found is something that I don't really like. With C++ they introduced std::optional which is either returns true/false and if true, holds a meaningful value.
I suppose you could make something similar in C:
It runs and works in Visual Studio setting it to C compilation instead of C++, but no guarantees on other compilers.
Another reason I chose C++ was because C has no nullable value aside from 0, nor does it have a bool ( until C99's _Bool ). So returning a value like -1 or whatever from a function to indicate that the result wasn't found is something that I don't really like. With C++ they introduced std::optional which is either returns true/false and if true, holds a meaningful value.
I suppose you could make something similar in C:
Code: Select all
#include <stdio.h>
#include <stdint.h>
// Here because Visual Studio doesn't like scanf
#pragma warning(disable:4996)
#define declare_optional(type) typedef struct _optional_##type { int has_value : 1; uintptr_t value; }optional_##type
typedef struct _nullopt {
const int has_value;
const uintptr_t value;
}nullopt;
declare_optional( nullopt );
static const optional_nullopt null_opt = { 0, 0 };
declare_optional( size_t );
optional_size_t find_choice( size_t count, int choice, int* choices )
{
for( size_t i = 0; i < count; ++i )
{
if( choices[ i ] == choice )
{
return optional_size_t{ 1, ( uintptr_t )i };
}
}
return *( optional_size_t* )&null_opt;
}
// Calculates number of elements in a static array
#define array_size(arr) sizeof(arr) / sizeof(arr[0])
int main()
{
int choices[ 10 ] = { 1,2,3,4,5,6,7,8,9,10 };
const size_t max_count = array_size( choices ) - 1;
printf( "Welcome, you may choose a number!\n\n" );
for( size_t count = 10; count > 0;)
{
printf( "The available numbers are: " );
for( size_t i = 0; i < count; ++i )
printf( "%d ", choices[ i ] );
printf( "\nThe numbers that you've already chosen are: " );
for( size_t i = count; i < array_size( choices ); ++i )
printf( "%d ", choices[ i ] );
printf( "\n\nnumber: " );
int choice = 0;
scanf( "%d", &choice );
optional_size_t idx = find_choice( count, choice, choices );
if( idx.has_value )
{
for( size_t j = ( size_t )idx.value; j < max_count; ++j )
{
int temp = choices[ j ];
choices[ j ] = choices[ j + 1 ];
choices[ j + 1 ] = temp;
}
--count;
}
else
{
printf( "Sorry! You can't enter that number." );
}
printf( "\n\n" );
}
printf( "congrats! you've entered all the numbers. good job!\n" );
return 0;
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
Re: Coding Challenge
code blocks says
main.c|25|error: expected expression before 'optional_size_t'|
main.c|25|error: expected expression before 'optional_size_t'|
You can't be betrayed if you don't have any friends.
Why live? Cause why not.
Why live? Cause why not.