Page 1 of 2

Coding Challenge

Posted: April 7th, 2019, 4:10 am
by Alacaster
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.
2019.zip
(33.15 KiB) Downloaded 150 times

Re: Coding Challenge

Posted: April 7th, 2019, 10:04 pm
by albinopapa
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?

Re: Coding Challenge

Posted: April 7th, 2019, 10:23 pm
by Alacaster
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.

Re: Coding Challenge

Posted: April 8th, 2019, 4:24 am
by albinopapa
I'm most of the way there, got side tracked while waiting for your response.

Re: Coding Challenge

Posted: April 8th, 2019, 11:15 pm
by Alacaster
It should be relatively simple and satisfying. Mine was only 64 lines.

Re: Coding Challenge

Posted: April 9th, 2019, 10:57 pm
by Alacaster
hello?

Re: Coding Challenge

Posted: April 10th, 2019, 8:12 am
by albinopapa
Ok, this is what I came up with and is the best I can do to shrink it down as much as possible:
Spoiler:

Code: Select all

#include <iostream>
#include <iterator>
#include <optional>
int main()
{
	int choices[ 10 ]{ 1,2,3,4,5,6,7,8,9,10 };
	constexpr size_t max_count = std::size( choices ) - 1;

	auto find_choice = [ & ]( int count, int choice )
	{
		for( std::size_t i = 0; i < count; ++i )
			if( choices[ i ] == choice )
				return std::optional<std::size_t>( i );

		return std::optional<std::size_t>();
	};

	std::cout << "Welcome, you may choose a number!\n\n";
	for( int count = 10; count > 0;)
	{
		std::cout << "The available numbers are: ";
		for( int i = 0; i < count; ++i )
			std::cout << choices[ i ] << ' ';

		std::cout << "\nThe numbers that you've already chosen are: ";
		for( int i = count; i < std::size( choices ); ++i )
			std::cout << choices[ i ] << ' ';
		
		std::cout << "\n\nnumber: ";
		int choice = 0; 
		std::cin >> choice;

		if( auto idx = find_choice( count, choice ); idx )
		{
			for( std::size_t j = *idx; j < max_count; ++j )
				std::swap( choices[ j ], choices[ j + 1 ] );
			--count;
		}
		else
		{
			std::cout << "Sorry! You can't enter that number.";
		}

		std::cout << "\n\n";
	}
	
	std::cout << "congrats! you've entered all the numbers. good job!\n";
	system( "pause" );
	return 0;
}
File size = 1,259 bytes
variables + function parameters = 76 bytes
functions I wrote: main and find_choice: 2
function calls = 15
chars*5 + bytes + functions*6 - calls*4 = score
Did I do this right?
( ( 1259 * 5 ) + 76 + ( 2 * 2 ) - ( 15 * 2 ) ) * 2 = 5136;

or

( ( 1259 / 5 ) + 76 + ( 2 * 2 ) - ( 15 * 2 ) ) * 2 = 603.6

Re: Coding Challenge

Posted: April 11th, 2019, 3:33 am
by Alacaster
[*]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.)
Spoiler:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int repeat_checker(int integer, int * array, int exclusion, int size){

    int i = 0;

    while(array[i] != '\0' && i < size){
        if(i == exclusion){
            i++;
            continue;
        }else if(array[i] == integer){
            return 1;
            }
        i++;
    }

    return 0;

}

int main()
{
    printf("welcome, you may choose a number!\n\n");

    int i;

    int unavailable_numbers[10] = {'\0'};

    do{
        printf("the available numbers are: ");
        for(i = 1; i<=10; i++){
            if(repeat_checker(i, unavailable_numbers, -1, 10)){
                continue;
            }
            printf("%d ", i);
        }

        printf("\nthe numbers that you've already chosen are: ");
        for(i = 1; unavailable_numbers[i-1] != '\0' && i<=10; i++){
            printf("%d ", unavailable_numbers[i-1]);
        }

            repeat_error:

        printf("\n\nnumber: ");
        scanf(" %d", &unavailable_numbers[i-1]);

        if(unavailable_numbers[i-1] > 10 || unavailable_numbers[i-1] < 1
           || repeat_checker(unavailable_numbers[i-1], unavailable_numbers, i-1, 10)){
            unavailable_numbers[i-1] = '\0';
            printf("sorry! you can't enter that number\n");
            goto repeat_error;
        }

        putchar('\n');
        putchar('\n');

    }while(unavailable_numbers[9]=='\0');

    printf("congrats! you've entered all the numbers. good job!\n\n");
}
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.
  • 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
a/40 + b*2 - c*6 + d*4 - e*6 + f(exempt)*0 + g(exempt)*0 + h/2,000 + i/140,000 + j/30,000 + k/15,000,000

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.

Re: Coding Challenge

Posted: April 11th, 2019, 6:33 am
by albinopapa
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:

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;
}
It runs and works in Visual Studio setting it to C compilation instead of C++, but no guarantees on other compilers.

Re: Coding Challenge

Posted: April 12th, 2019, 1:59 am
by Alacaster
code blocks says

main.c|25|error: expected expression before 'optional_size_t'|