fork download
  1. #include <stdio.h>
  2.  
  3.  
  4.  
  5. int main(void) {
  6. printf("{7, 5, 4, 3, 1}: %d\r\n", f(5, (int[]) {7, 5, 4, 3, 1}));
  7. printf("{42, 41}: %d\r\n", f(2, (int[]) {42, 41}));
  8. printf("{5}: %d\r\n", f(1, (int[]) {5}));
  9. printf("{27, 19, 19, 10, 3}: %d\r\n", f(5, (int[]) {27, 19, 19, 10, 3}));
  10. printf("{6, 4, 2, 2, 2}: %d\r\n", f(5, (int[]) {6, 4, 2, 2, 2}));
  11. printf("{9, 9, 9, 9}: %d\r\n", f(4, (int[]) {9, 9, 9, 9}));
  12. printf("{1, 2, 3, 2}: %d\r\n", f(4, (int[]) {1, 2, 3, 2}));
  13. printf("{10, 9, 8, 7, 12}: %d\r\n", f(5, (int[]) {10, 9, 8, 7, 12}));
  14. printf("{4, 6, 4, 4, 2}: %d\r\n", f(5, (int[]) {4, 6, 4, 4, 2}));
  15. return 0;
  16. }
  17.  
  18. f(int n,int*l){return n<2?3:((l[0]>l[1])*2|l[0]>=l[1])&f(n-1,l+1);}
  19.  
  20. int f_semiungolfed(int n, int* l) {
  21. return (n < 2) ? 3 : ((l[0] > l[1]) * 2 | l[0] >= l[1]) & f(n - 1, l + 1);
  22. }
  23.  
  24. int f_ungolfed(int n, int* l) {
  25. int case1 = 0, case2 = 0, recursion = 0;
  26. if (n < 2) { // Analogous to the ternary conditional I used - n < 2 means we have a single-element/empty list
  27. return 3; // Handles the vacuous-truth scenario for single lists
  28. } else {
  29. case1 = l[0] > l[1]; // The first case - are the two numbers in the list strictly decreasing? (case1 is 1 if so)
  30. case2 = l[0] >= l[1]; // The second case - are the two numbers strictly non-increasing (case2 is 1 if so)
  31. recursion = f_ungolfed(n - 1, l + 1); // Recursively call ourselves on the "rest" of the list (that is, everything other than the first element). Consider that comparison is transitive, and that we already have a vacuous-truth scenario covered.
  32. case1 *= 2; // Shift case1's value over to the left by one bit by multiplying by 2. If case1 was 1 (0b01), it's now 2 (0b10) - otherwise it's still 0 (0b00)
  33. return (case1 | case2) & recursion;
  34. // The bitwise OR operator (|) will combine any 1-bits from case1's value (either 0b10 or 0b00) or case2's value (either 0b01 or 0b00) into either 3, 2, 1, or 0 (0b11, 0b10, 0b01, or 0b00 respectively).
  35. // The bitwise AND operator (&) will combine only matching 1-bits from (case1|case2) and the return value of the recursive call - if recursion = 0b11 and case1|case2 = 0b01, then the return value will be 0b01.
  36. }
  37. }
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
{7, 5, 4, 3, 1}: 3
{42, 41}: 3
{5}: 3
{27, 19, 19, 10, 3}: 1
{6, 4, 2, 2, 2}: 1
{9, 9, 9, 9}: 1
{1, 2, 3, 2}: 0
{10, 9, 8, 7, 12}: 0
{4, 6, 4, 4, 2}: 0