• Custom User Avatar

    kindly do not post solutions like this, as it goes to the main comment thread where nobody wants to see it

  • Custom User Avatar

    #include <stddef.h>
    #include <stdlib.h>

    int *twin_sum_solutions(int *input, size_t input_len, size_t *output_len) {
    if (input_len < 2) {
    *output_len = 0;
    return NULL; // Return NULL if there are no pairs
    }

    int *res = malloc(input_len * sizeof(int));  // Allocate memory for the result
    if (!res) {
        *output_len = 0;
        return NULL;  // Return NULL if memory allocation fails
    }
    
    *output_len = 0;  // Initialize the result counter
    
    for (size_t i = 0; i < input_len - 1; i++) {
        if (input[i] == input[i + 1]) {  // Check if it's a matching pair
            res[*output_len] = input[i] * 2;  // Store the "twin sum"
            (*output_len)++;
            i++;  // Skip the next element as it's already processed
        }
    }
    
    // Resize the memory to the correct size of results
    int *final_result = realloc(res, *output_len * sizeof(int));
    
    // If realloc fails and there are results, free the memory
    if (!final_result && *output_len > 0) {
        free(res);
        *output_len = 0;
        return NULL;
    }
    
    return final_result;  // Return the result (or NULL if no results)
    

    }

  • Custom User Avatar

    This is marked as resolved, but still seems to be a problem for the sample test cases.

  • Custom User Avatar

    What you've asked is a good question. I don't have an answer to that. I chose to solve the question as proposed before your answer because it requires a bit more from the coder, in this case, me.

  • Custom User Avatar

    Fair play, that is more elegant. As a question, why allow negative prices or servings?

  • Custom User Avatar

    The lambda function is being bound to the variable test_password, effectively making it a named function. ethan_ is code golfing for fun, which is when you try to complete a challenge with as few characters as possible. Maintainability isn't the goal.

  • Custom User Avatar

    hey man, id say rather just throw it inside a function instead of lambda so it can be used more than just that line and easily modified in the future

  • Custom User Avatar

    I applied minimal changes to remove the warnings that appear in stderr, as well as updating the description for Rust to use the correct terms field and struct. Code style could also be improved but I didn't want to editorialize.

  • Custom User Avatar

    This is the nature of Freestyle Sparring. We take turns remixing and refactoring each others code. If you want to make the code shorter, just fork it!🍴

  • Custom User Avatar

    bro im trying to make the code short

  • Custom User Avatar

    I appreciate your counterplay - Good job!

  • Custom User Avatar

    using string as a parameter name is bad practice because it overwrites the string module

  • Custom User Avatar

    Ah, we've gone full circle now.

  • Custom User Avatar

    Interesting, thanks, we'll have to look at that with a benchmark.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Loading more items...