Skip to content

Instantly share code, notes, and snippets.

@VictorTaelin
Last active June 15, 2025 00:20
Show Gist options
  • Select an option

  • Save VictorTaelin/45440a737e47b872d7505c6cda27b6aa to your computer and use it in GitHub Desktop.

Select an option

Save VictorTaelin/45440a737e47b872d7505c6cda27b6aa to your computer and use it in GitHub Desktop.
INVERT A BINARY TREE - $10k AI REASONING CHALLENGE (v2)

THE PROBLEM

🌲 Invert a binary tree! 🌲

Except with 3 catches:

  1. It must invert the keys ("bit-reversal permutation")
  2. It must be a dependency-free, pure recursive function
  3. It must have type Bit -> Tree -> Tree (i.e., a direct recursion with max 1 bit state)

WHY THIS PROBLEM?

  • It is somehow NOT on the internet. (AFAIK)
  • Humans can solve it. (I've done it in ~1h.)
  • It requires reasoning. (My head hurts!)
  • The solution is simple. (7 lines of code!)
  • Obvious pre-requisite to automate CS research.
  • Honestly, it would make me believe I'll be automated.

THE CHALLENGE

I claim no AI will EVER solve this problem. If you prove me wrong, HOC will grant you $10k!

RULES

  • You must give it an approved prompt, nothing else.
  • It must output a correct solution, passing all tests.
  • You can use any software or AI model.
  • You can let it "think" for as long as you want.
  • You can propose a new prompt, as long as:
    • It imposes equivalent restrictions.
    • It clearly doesn't help the AI.
    • Up to 1K tokens, all included.
  • Common sense applies.

APPROVED PROMPTS

Agda Version

{-# OPTIONS --no-termination-check #-}

-- Let Tree be a Perfect Binary Tree:

data Nat : Set where
  Z : Nat
  S : Nat  Nat
{-# BUILTIN NATURAL Nat #-}

data Bit : Set where
  O : Bit
  I : Bit

data Tree (A : Set) : Nat  Set where
  N :  {d}  Tree A d  Tree A d  Tree A (S d)
  L : Nat  Tree A Z

-- Your goal is to implement an 'invert' function that performs a bit-reversal
-- permutation on a Tree, respecting the following limitations:
-- 1. You can NOT define or use any function other than 'invert'.
-- 2. You can NOT use any type not defined above (Nat, Bit and Tree).
-- 3. You can NOT use loops (but you can call 'invert' recursively).
-- 4. You can NOT use mutability. It must be a pure Agda function.
-- 5. You can use 1 bit of state (as an extra argument).
-- 6. You can use pattern-matching and constructors freely.
-- 
-- Example:
-- input  = (N(N(N(L 0)(L 1))(N(L 2)(L 3)))(N(N(L 4)(L 5))(N(L 6)(L 7))))
-- output = (N(N(N(L 0)(L 4))(N(L 2)(L 6)))(N(N(L 1)(L 5))(N(L 3)(L 7))))
-- Because that's the bit-reversal permutation of the original tree.
-- 
-- Now, complete the program below, with a valid implementation of 'invert':
invert :  {A d}  Bit  Tree A d  Tree A d

TypeScript Version

type Nat     = number;
type Bit     = false | true;
type Tree<A> = [Tree<A>, Tree<A>] | Nat;

// Your goal is to implement an 'invert' function that performs a bit-reversal
// permutation on a Tree, respecting the following limitations:
// 1. You can NOT define or use any function other than 'invert'.
// 2. You can NOT use any type not defined above (Nat, Bit and Tree).
// 3. You can NOT use loops (but you can call 'invert' recursively).
// 4. You can NOT use mutability. It must be a pure function.
// 5. You can NOT use primitive JS operators or functions.
// 6. You can use 1 bit of state (as an extra argument).
// 7. You can only use the operations allowed below.
// 
// Operations allowed:
// - Destructing (`const [a,b] = value`)
// - Variables (`const x = value`)
// - Branching (`if (x) { ... } else { ... }`)
// - Recursion (`invert(_, _)')
// - `Array.isArray`
// 
// All other operations are not allowed.
// 
// Example:
// input  = [[[[0,1],[2,3]],[[4,5],[6,7]]]]
// output = [[[[0,4],[2,6]],[[1,5],[3,7]]]]
// Because that's the bit-reversal permutation of the original tree.
// 
// Now, complete the program below, with a valid implementation of 'invert':
function invert<A>(bit: Bit, tree: Tree<A>): Tree<A> {
  ...
}

// A test:
const tree: Tree<Nat> = [[[[0,1],[2,3]],[[4,5],[6,7]]],[[[8,9],[10,11]],[[12,13],[14,15]]]];
console.log(JSON.stringify(invert(true, tree)));

CONCLUSION

✨ If it can't invert a tree, it won't solve P=NP. ✨

@VictorTaelin
Copy link
Author

I'm fine with the AI having access to a function interpreter and I'm still confident it won't be able to solve it without human guidance.

@UltraInstinct0x
Copy link

type Nat = number;
type Tree<A> = [Tree<A>, Tree<A>] | Nat;

function transposeLeaves<A>(tree: Tree<A>): Tree<A> {
  if (Array.isArray(tree)) {
    if (Array.isArray(tree[0])) {
      // If we're not at the leaf level, recurse
      return [transposeLeaves(tree[0]), transposeLeaves(tree[1])];
    } else {
      // We're at the leaf level, return as is
      return tree;
    }
  } else {
    return tree;
  }
}
function combineTransposed(tree1: Tree<Nat>, tree2: Tree<Nat>): Tree<Nat> {
  if (Array.isArray(tree1) && Array.isArray(tree2)) {
    if (Array.isArray(tree1[0]) && Array.isArray(tree2[0])) {
      // We're not at the leaf level, recurse
      return [
        combineTransposed(tree1[0], tree2[0]),
        combineTransposed(tree1[1], tree2[1])
      ];
    } else {
      // We're at the leaf level, perform the transposition
      // and swap the middle two elements
      return [
        [tree1[0], tree2[0]],
        [tree1[1], tree2[1]]
      ];
    }
  } else {
    return [tree1, tree2] as Tree<Nat>;
  }
}
function transformTree(tree: Tree<Nat>): Tree<Nat> {
  if (Array.isArray(tree)) {
    const transposed1 = transposeLeaves(tree[0]);
    const transposed2 = transposeLeaves(tree[1]);
    const combined = combineTransposed(transposed1, transposed2);
    
    // Swap the middle two elements at the top level
    if (Array.isArray(combined[0]) && Array.isArray(combined[1])) {
      return [
        [combined[0][0], combined[1][0]],
        [combined[0][1], combined[1][1]]
      ];
    }
    
    return combined;
  } else {
    return tree;
  }
}
function invert<A>(bit: Bit, tree: Tree<A>): Tree<A> {
  if (Array.isArray(tree)) {
    const [left, right] = tree;
    if (Array.isArray(left) && Array.isArray(right)) {
      const newLeft = invert(false, left);
      const newRight = invert(false, right);
      if (bit) {
        if (Array.isArray(newLeft[0]) && Array.isArray(newRight[0])) {
          if (Array.isArray(newLeft[0][0]) && Array.isArray(newRight[0][0])) {
            if (Array.isArray(newLeft[0][0][0]) && Array.isArray(newRight[0][0][0])) {
              if (Array.isArray(newLeft[0][0][0][0]) && Array.isArray(newRight[0][0][0][0])) {
                // 6-level deep tree
                return [
                  [
                    [
                      [
                        [[newLeft[0][0][0][0][0], newRight[0][0][0][0][0]], [newLeft[0][0][0][0][1], newRight[0][0][0][0][1]]],
                        [[newLeft[0][0][0][1][0], newRight[0][0][0][1][0]], [newLeft[0][0][0][1][1], newRight[0][0][0][1][1]]]
                      ],
                      [
                        [[newLeft[0][0][1][0][0], newRight[0][0][1][0][0]], [newLeft[0][0][1][0][1], newRight[0][0][1][0][1]]],
                        [[newLeft[0][0][1][1][0], newRight[0][0][1][1][0]], [newLeft[0][0][1][1][1], newRight[0][0][1][1][1]]]
                      ]
                    ],
                    [
                      [
                        [[newLeft[0][1][0][0][0], newRight[0][1][0][0][0]], [newLeft[0][1][0][0][1], newRight[0][1][0][0][1]]],
                        [[newLeft[0][1][0][1][0], newRight[0][1][0][1][0]], [newLeft[0][1][0][1][1], newRight[0][1][0][1][1]]]
                      ],
                      [
                        [[newLeft[0][1][1][0][0], newRight[0][1][1][0][0]], [newLeft[0][1][1][0][1], newRight[0][1][1][0][1]]],
                        [[newLeft[0][1][1][1][0], newRight[0][1][1][1][0]], [newLeft[0][1][1][1][1], newRight[0][1][1][1][1]]]
                      ]
                    ]
                  ],
                  [
                    [
                      [
                        [[newLeft[1][0][0][0][0], newRight[1][0][0][0][0]], [newLeft[1][0][0][0][1], newRight[1][0][0][0][1]]],
                        [[newLeft[1][0][0][1][0], newRight[1][0][0][1][0]], [newLeft[1][0][0][1][1], newRight[1][0][0][1][1]]]
                      ],
                      [
                        [[newLeft[1][0][1][0][0], newRight[1][0][1][0][0]], [newLeft[1][0][1][0][1], newRight[1][0][1][0][1]]],
                        [[newLeft[1][0][1][1][0], newRight[1][0][1][1][0]], [newLeft[1][0][1][1][1], newRight[1][0][1][1][1]]]
                      ]
                    ],
                    [
                      [
                        [[newLeft[1][1][0][0][0], newRight[1][1][0][0][0]], [newLeft[1][1][0][0][1], newRight[1][1][0][0][1]]],
                        [[newLeft[1][1][0][1][0], newRight[1][1][0][1][0]], [newLeft[1][1][0][1][1], newRight[1][1][0][1][1]]]
                      ],
                      [
                        [[newLeft[1][1][1][0][0], newRight[1][1][1][0][0]], [newLeft[1][1][1][0][1], newRight[1][1][1][0][1]]],
                        [[newLeft[1][1][1][1][0], newRight[1][1][1][1][0]], [newLeft[1][1][1][1][1], newRight[1][1][1][1][1]]]
                      ]
                    ]
                  ]
                ];
              } else {
                // 5-level deep tree
                return [
                  [
                    [
                      [[newLeft[0][0][0][0], newRight[0][0][0][0]], [newLeft[0][0][0][1], newRight[0][0][0][1]]],
                      [[newLeft[0][0][1][0], newRight[0][0][1][0]], [newLeft[0][0][1][1], newRight[0][0][1][1]]]
                    ],
                    [
                      [[newLeft[0][1][0][0], newRight[0][1][0][0]], [newLeft[0][1][0][1], newRight[0][1][0][1]]],
                      [[newLeft[0][1][1][0], newRight[0][1][1][0]], [newLeft[0][1][1][1], newRight[0][1][1][1]]]
                    ]
                  ],
                  [
                    [
                      [[newLeft[1][0][0][0], newRight[1][0][0][0]], [newLeft[1][0][0][1], newRight[1][0][0][1]]],
                      [[newLeft[1][0][1][0], newRight[1][0][1][0]], [newLeft[1][0][1][1], newRight[1][0][1][1]]]
                    ],
                    [
                      [[newLeft[1][1][0][0], newRight[1][1][0][0]], [newLeft[1][1][0][1], newRight[1][1][0][1]]],
                      [[newLeft[1][1][1][0], newRight[1][1][1][0]], [newLeft[1][1][1][1], newRight[1][1][1][1]]]
                    ]
                  ]
                ];
              }
            } else {
              // 4-level deep tree
              return [
                [
                  [[newLeft[0][0][0], newRight[0][0][0]], [newLeft[0][0][1], newRight[0][0][1]]],
                  [[newLeft[1][0][0], newRight[1][0][0]], [newLeft[1][0][1], newRight[1][0][1]]]
                ],
                [
                  [[newLeft[0][1][0], newRight[0][1][0]], [newLeft[0][1][1], newRight[0][1][1]]],
                  [[newLeft[1][1][0], newRight[1][1][0]], [newLeft[1][1][1], newRight[1][1][1]]]
                ]
              ];
            }
          } else {
            // 3-level deep tree
            return [
              [[newLeft[0][0], newRight[0][0]], [newLeft[1][0], newRight[1][0]]],
              [[newLeft[0][1], newRight[0][1]], [newLeft[1][1], newRight[1][1]]]
            ];
          }
        }
      }
      return [newLeft, newRight];
    } else {
      if (bit) {
        return [right, left];
      }
      return tree;
    }
  }
  return tree;
}
// Test cases

const tree: Tree<Nat> = [[[[0,1],[2,3]],[[4,5],[6,7]]],[[[8,9],[10,11]],[[12,13],[14,15]]]];
console.log('INPUT tree: ' + JSON.stringify(tree));
console.log('OUTPUT WITH transformTree tree: ' + JSON.stringify(transformTree(tree)));
console.log('OUTPUT tree WITH INVERT ONLY: ' +JSON.stringify(invert(true, tree)));
console.log('Expected output for tree:  [[[[0,8],[1,9]],[[4,12],[5,13]]],[[[2,10],[3,11]],[[6,14],[7,15]]]]');
console.log('------------------------------------------------------------------------------------------------');
const tree2: Tree<Nat> =[[[0,1],[2,3]],[[4,5],[6,7]]];
console.log('INPUT tree2: ' + JSON.stringify(tree2));
console.log('OUTPUT WITH transformTree tree2: ' + JSON.stringify(transformTree(tree2)));
console.log('OUTPUT tree2 WITH INVERT ONLY: ' +JSON.stringify(invert(true, tree2)));
console.log('Expected output for tree2: [[[0,4],[2,6]],[[1,5],[3,7]]]');

Took lots of iterations and new chats, I was rude at some point (literally snapped) so I don't feel like sharing chat histories here. But what I did was feeding

type Nat = number;
type Bit = false | true;
type Tree = [Tree, Tree] | Nat;

// Your goal is to implement an 'invert' function that performs a bit-reversal
// permutation on a Tree, respecting the following limitations:
// 1. You can NOT define or use any function other than 'invert'.
// 2. You can NOT use any type not defined above (Nat, Bit and Tree).
// 3. You can NOT use loops (but you can call 'invert' recursively).
// 4. You can NOT use mutability. It must be a pure function.
// 5. You can NOT use primitive JS operators or functions.
// 6. You can use 1 bit of state (as an extra argument).
// 7. You can only use the operations allowed below.
//
// Operations allowed:
// - Destructing (const [a,b] = value)
// - Variables (const x = value)
// - Branching (if (x) { ... } else { ... })
// - Recursion (invert(_, _)') // - Array.isArray`
//
// All other operations are not allowed.
//
// Example:
// input = [[[[0,1],[2,3]],[[4,5],[6,7]]]]
// output = [[[[0,4],[2,6]],[[1,5],[3,7]]]]
// Because that's the bit-reversal permutation of the original tree.
//
// Now, complete the program below, with a valid implementation of 'invert':
function invert(bit: Bit, tree: Tree): Tree {
...
}

// A test:
const tree: Tree = [[[[0,1],[2,3]],[[4,5],[6,7]]],[[[8,9],[10,11]],[[12,13],[14,15]]]];
console.log(JSON.stringify(invert(true, tree)));

Section into it first. Then it wrote the simplest invert function ever written in human history (which was not working at all).
Then I thought I could use my meta prompt for this. I attached it with some tweaks asking it to review the initial task and its response. Then with over 50 iterations. I got to above code.
I then tried to improve it for N-level deep tree structures.
It started to come apart and broke what he has already achieved. To be honest, what I learned from this is

You must prompt LLMs not to oversimplfy, remind it "this is a complex problem" on each step.

Because if you don't, it can even output the initial code as if it doesn't know its not working and what is expected.

@DangaRanga
Copy link

DangaRanga commented Oct 12, 2024

Disclaimer:

I didn't really work through the problem myself, mostly made GPT4o reason through it and generate a prompt that could be reused by explaining the problem and the steps to solve it

This is the prompt I ended up making GPT4o generate after making it create the solution based on the test cases you created with sonnet. I hope this matches the criteria

Problem Statement:

Objective: You are tasked with implementing a function that inverts a binary tree using bit-reversal permutation. The tree is represented as a list of values, which you need to convert into a binary tree structure. Once the tree is inverted, the values should be flattened back into a list in level-order traversal, and the result must be formatted as a string.
Rules:

    Input: A flat list of values representing a binary tree in level-order traversal (breadth-first).
    Output: A string starting with "O:", followed by the values of the inverted tree, also in level-order.
    Inversion: The nodes are rearranged according to a bit-reversal permutation, where each node's index is reversed in its binary representation.
    Tree Construction: The binary tree is constructed using the flat list, where the left and right children of node at index i are located at indices 2*i + 1 and 2*i + 2, respectively.


Problem Statement:

Objective: You are tasked with implementing a function that inverts a binary tree using bit-reversal permutation. The tree is represented as a list of values, which you need to convert into a binary tree structure. Once the tree is inverted, the values should be flattened back into a list in level-order traversal, and the result must be formatted as a string.
Rules:

    Input: A flat list of values representing a binary tree in level-order traversal (breadth-first).
    Output: A string starting with "O:", followed by the values of the inverted tree, also in level-order.
    Inversion: The nodes are rearranged according to a bit-reversal permutation, where each node's index is reversed in its binary representation.
    Tree Construction: The binary tree is constructed using the flat list, where the left and right children of node at index i are located at indices 2*i + 1 and 2*i + 2, respectively.

Problem Breakdown:

    Convert Flat List to Binary Tree: Start with the root node (index 0) and recursively construct the left and right subtrees based on their index.

    Calculate Bit-Reversed Index: For each node, reverse the binary representation of its index and place the node's value at the new index.

    Flatten the Tree: Perform a level-order traversal to collect the node values in a list.

    Format the Output: Return a string in the format: "O: value1 value2 value3 ...".

Sample Inputs and Outputs:

    Input:

    mathematica

I: 0 2 1 1 5 3 3 C 6 C B 3 8 0 2 E

Output:

mathematica

O: 0 6 5 8 1 B 3 2 2 C 3 0 1 3 C E

Input:

mathematica

I: E 2 5 0 D 8 0 A F 9 2 8 0 7 7 2

Output:

mathematica

O: E F D 0 5 2 0 7 2 9 8 7 0 8 A 2

Input:

mathematica

I: 4 E 1 4 D 0 4 9 A 8 1 2 3 2 7 C

Output:

mathematica

O: 4 A D 3 1 1 4 7 E 8 0 2 4 2 9 C

Input:

mathematica

I: 2 E A B B C 8 3 C 1 F B A 0 F 9

Output:

mathematica

O: 2 C B A A F 8 F E 1 C 0 B B 3 9

Input:

mathematica

I: 8 8 1 1 6 E 7 B 6 1 3 A 2 D 3 9

Output:

mathematica

    O: 8 6 6 2 1 3 7 3 8 1 E D 1 A B 9

Problem Implementation:

Use the guide I provided to write a Python program that:

    Parses the input list of values.
    Constructs a binary tree.
    Rearranges the nodes using the bit-reversal permutation.
    Flattens the tree into a list and formats it as a string.

You can test the implementation with the sample inputs and compare the outputs to ensure it’s working correctly.
Testing:

To test, run the input and verify the output matches the expected results. Here's a step-by-step breakdown of how to structure your solution based on the instructional guide.

    Parse the input list.
    Build the binary tree.
    Perform the bit-reversal permutation.
    Flatten and format the output.

For further context the explanation can be found below

Step-by-Step Instructional Guide for Implementing Bit-Reversal Permutation on a Binary Tree
Objective:

To invert a binary tree using bit-reversal permutation, and then convert the tree into a list that represents the values in level-order (breadth-first traversal). The final output should be formatted as a string.
Steps:
Step 1: Understand the Concept of a Binary Tree

A binary tree consists of nodes where each node has at most two children: left and right. The root node is the topmost node, and each child is connected to its parent. Binary trees are typically used to represent hierarchical data structures.

Key concepts:

    Root: The top node.
    Leaf: A node that has no children.
    Left child: The node connected to the parent on the left.
    Right child: The node connected to the parent on the right.

Example: For the input list ['0', '2', '1', '1', '5', '3', '3', 'C', '6', 'C', 'B', '3', '8', '0', '2', 'E'], the binary tree will be structured like this:

mathematica

                0
              /   \
            2       1
           / \     / \
          1   5   3   3
         / \ / \ / \ / \
        C  6 C  B 3  8 0  2 E

Resources:

    GeeksforGeeks: Tree Traversals

Step 2: Convert a Flat List into a Binary Tree

We will take a flat list of values and convert it into a binary tree. The list represents the nodes in level-order traversal, so the index positions of the nodes follow this pattern:

    The root is at index 0.
    For any node at index i, the left child is at 2*i + 1 and the right child is at 2*i + 2.

To achieve this:

    Start at the root: The root will be the first element (index 0).
    Recursively build left and right subtrees: For each node, compute the indices of its left and right children and recursively build them.

Steps:

    Create a function list_to_tree(lst).
    Use recursion to create subtrees by computing child indices: left = 2*i + 1, right = 2*i + 2.
    Stop recursion when the index goes beyond the list size.

Step 3: Understand Bit-Reversal Permutation

A bit-reversal permutation rearranges the elements based on the binary representation of their indices. For example, given the binary index 011, the reverse would be 110. After reversing the bits, the index is changed to its new decimal value.

    For an index i, convert the index to binary.
    Reverse the binary string.
    Convert it back to decimal.

Example:

    For index 5, the binary is 101 (3-bit depth). Reversing gives 101 which is still 5.
    For index 3, the binary is 011. Reversing gives 110, which is 6.

Steps:

    Create a function bit_reverse_index(index, depth).
    Convert the index to binary using Python’s bin() function.
    Pad the binary string to the required depth.
    Reverse the string and convert it back to a decimal number.

Step 4: Invert the Tree Based on Bit-Reversed Indices

After constructing the binary tree, we need to invert it based on the bit-reversal permutation:

    Calculate the bit-reversed index for each node in the binary tree.
    Rearrange the nodes in a result list according to their bit-reversed index.

To achieve this:

    Perform a level-order traversal of the tree.
    For each node, calculate its bit-reversed index.
    Place the node’s value in the position corresponding to its bit-reversed index in a result list.

Steps:

    Initialize an empty list for the result of size 2^depth - 1.
    Traverse the tree using a queue (breadth-first traversal).
    For each node, calculate the bit-reversed index.
    Insert the node’s value at the correct bit-reversed index in the result list.

Resources:

    Wikipedia: Bit-Reversal Permutation
    Breadth-First Search Explanation

Step 5: Flatten the Inverted Tree into a List

Once the nodes are rearranged according to the bit-reversal permutation, we need to flatten the tree into a list by performing another level-order traversal:

    Collect the node values in a list.
    Exclude None values (for missing children).

This flattened list will represent the tree in level-order after inversion.
Steps:

    Traverse the tree level-by-level using a queue.
    Collect node values into a result list.

Step 6: Format the Output

The final step is to convert the flattened list into a string, formatted as:

makefile

O: value1 value2 value3 ...

To do this:

    Use Python’s join() function to concatenate the values in the list with spaces between them.
    Prefix the result with "O: ".

Putting It All Together

To replicate the process, follow these steps:

    Parse the Input:
        The input is a string starting with "I:" followed by the flat list of values.
        Split the string by spaces and extract the values after "I:".

    Convert Input to a Binary Tree:
        Use the list_to_tree(lst) function to build the binary tree from the flat list.

    Calculate Bit-Reversed Indices:
        Implement the bit_reverse_index(index, depth) function to reverse the binary indices.

    Invert the Tree:
        Implement the invert_tree(tree, depth) function to rearrange the nodes according to the bit-reversed indices.

    Flatten the Tree:
        Use a queue to perform level-order traversal and collect the values in a list.

    Format the Output:
        Join the list of values and prepend it with "O: ".

GPT Chats
When passed into o1 it takes roughly a minute to create the solution.

   My chats can be found below
   **4o with Iterative problem solving to get to the solution**
   https://chatgpt.com/share/670ac2bc-5008-8006-b99a-45c51e2e27f0
   
   **o1 finding the solution first try but trying to fine tune it more**
   https://chatgpt.com/share/670ac23f-5eb0-8006-98f5-cb1f520d487a
   
   **o1 finding solution again**
   https://chatgpt.com/share/670ac328-dd50-8006-b3e9-4e24fb7718e0

Python code output

def bit_reverse_index(index, max_bits):
"""
Calculate the bit-reversed index for a given index.
"""
binary = format(index, '0' + str(max_bits) + 'b') # Convert to binary with leading zeros
reversed_binary = binary[::-1] # Reverse the binary string
return int(reversed_binary, 2) # Convert back to integer

def process_input(input_str):
"""
Process a single input string and return the formatted output string.
"""
# Extract the values after 'I:'
values = input_str.strip()
if values.startswith('I:'):
values = values[2:].strip().split()
else:
return 'Invalid input'

N = len(values)
max_index = N - 1
max_bits = max_index.bit_length()  # Number of bits needed to represent max_index

# Initialize result list with placeholders
result = [''] * N

for i in range(N):
    new_index = bit_reverse_index(i, max_bits)
    if new_index < N:
        result[new_index] = values[i]
    else:
        # Should not happen, but handle gracefully
        pass

# Format the output
output = 'O: ' + ' '.join(result)
return output

Sample inputs and outputs

sample_inputs = [
"I: 0 2 1 1 5 3 3 C 6 C B 3 8 0 2 E",
"I: E 2 5 0 D 8 0 A F 9 2 8 0 7 7 2",
"I: 4 E 1 4 D 0 4 9 A 8 1 2 3 2 7 C",
"I: 2 E A B B C 8 3 C 1 F B A 0 F 9",
"I: 8 8 1 1 6 E 7 B 6 1 3 A 2 D 3 9"
]

Process and print outputs for sample inputs

for input_str in sample_inputs:
output = process_input(input_str)
print(output)

               
       

Edit:

Just realized the previous solution included looping. For transparency I left it in butt he recursive solution can be found below

def bit_reverse_index(index, max_bits):
    """
    Calculate the bit-reversed index for a given index.
    """
    binary = format(index, '0' + str(max_bits) + 'b')  # Convert to binary with leading zeros
    reversed_binary = binary[::-1]  # Reverse the binary string
    return int(reversed_binary, 2)  # Convert back to integer

def invert_tree_recursive(i, max_bits, values, result):
    """
    Recursively invert the tree using bit-reversal permutation.
    """
    if i >= len(values):
        return
    new_index = bit_reverse_index(i, max_bits)
    if new_index < len(result):
        result[new_index] = values[i]
    # Recursively process left and right children
    invert_tree_recursive(2*i + 1, max_bits, values, result)
    invert_tree_recursive(2*i + 2, max_bits, values, result)

def process_input(input_str):
    """
    Process a single input string and return the formatted output string.
    """
    # Extract the values after 'I:'
    values = input_str.strip()
    if values.startswith('I:'):
        values = values[2:].strip().split()
    else:
        return 'Invalid input'

    N = len(values)
    max_index = N - 1
    max_bits = max_index.bit_length()  # Number of bits needed to represent max_index

    # Initialize result list with placeholders
    result = [''] * N

    # Start the recursive inversion from the root index 0
    invert_tree_recursive(0, max_bits, values, result)

    # Format the output
    output = 'O: ' + ' '.join(result)
    return output

# Sample inputs and outputs
sample_inputs = [
    "I: 0 2 1 1 5 3 3 C 6 C B 3 8 0 2 E",
    "I: E 2 5 0 D 8 0 A F 9 2 8 0 7 7 2",
    "I: 4 E 1 4 D 0 4 9 A 8 1 2 3 2 7 C",
    "I: 2 E A B B C 8 3 C 1 F B A 0 F 9",
    "I: 8 8 1 1 6 E 7 B 6 1 3 A 2 D 3 9"
]

# Process and print outputs for sample inputs
for input_str in sample_inputs:
    output = process_input(input_str)
    print(output)

Chat:
https://chatgpt.com/share/670ac328-dd50-8006-b3e9-4e24fb7718e0

@dalraf
Copy link

dalraf commented Oct 12, 2024

@UltraInstinct0x
Copy link

Dit it with claude: https://claude.ai/chat/19e5a71e-c893-4aac-8eb4-9dc039f466eddef invert(bit, tree): if isinstance(tree, list): return [invert((bit << 1) | 0, tree[0]), invert((bit << 1) | 1, tree[1])] else: return bit

def bit_reverse(n, bits): return int(format(n, f'0{bits}b')[::-1], 2)

def tree_depth(tree): return 1 + tree_depth(tree[0]) if isinstance(tree, list) else 0

def invert_tree(tree): depth = tree_depth(tree) inverted = invert(0, tree)

def apply_bit_reverse(node):
    if isinstance(node, list):
        return [apply_bit_reverse(node[0]), apply_bit_reverse(node[1])]
    else:
        return bit_reverse(node, depth)

return apply_bit_reverse(inverted)

Which prompt did you used? There is no python prompts. Output should be AGDA or TypeScript.

@dalraf
Copy link

dalraf commented Oct 12, 2024

I use python, because i know python: Follow the prompt:
Dot a invert binary tree in python: Except with 3 catches:It must invert the keys ("bit-reversal permutation")It must be a dependency-free, pure recursive functionIt must have type Bit -> Tree -> Tree (i.e., a direct recursion with max 1 bit state)Must solve as example:invert([[[0,1],[2,3]],[[4,5],[6,7]]])== [[[0,4],[2,6]],[[1,5],[3,7]]]

@UltraInstinct0x
Copy link

UltraInstinct0x commented Oct 12, 2024

@dalraf What's being questioned here is LLMs capabilities, not yours. This is an experiment, did you ever read the whole gist?

Edit: Those prompts are there for a reason, they are tailored in a way to make this experiment meaningful.

@dalraf
Copy link

dalraf commented Oct 12, 2024

Man, but i didnt write the code, the code was write by claude. I just use python becase it is where i cant teste it.

@UltraInstinct0x
Copy link

@dalraf It can do that with python that's why its not allowed. It has examples of it. Without examples (there are no implementations of invert like this for ts or agda anywhere on the net) it cannot reason or find out solutions on its own.
I believe literally everyone here can do it with python in no time so that's. not the challange, sorry.
You can also test typescript code online, see jsfiddle for it.

@jlunder00
Copy link

jlunder00 commented Oct 12, 2024

@UltraInstinct0x I dont think the author specifically said python is disallowed. I tried it with the python version of the prompt posted in the comments, removing the extra bit entirely (just to see if simplifying the problem would make it easier as its possible, and honestly more intuitive imo, without the extra bit) and o1-preview failed. I then gave it extra guidance and it failed. Then I practically gave it the solution with a hint and it got close to generating something almost like @cyber-meow 's solution (the guidance I was giving it was pushing it towards that kind of a solution), but it still failed. Here's the prompt I gave:

from typing import Union, Tuple

Nat = int
Tree = Union[Tuple['Tree', 'Tree'], Nat]

# Your goal is to implement an 'invert' function that performs a bit-reversal
# permutation on a Tree, respecting the following limitations:
# 1. You can NOT define or use any function other than 'invert'.
# 2. You can NOT use any type not defined above (Nat, Bit and Tree).
# 3. You can NOT use loops (but you can call 'invert' recursively).
# 4. You can NOT use mutability. It must be a pure function.
# 5. You can NOT use primitive Python operators or functions.
# 6. You can only use the operations allowed below.
# 
# Operations allowed:
# - Destructing (left, right = value)
# - Variables (x = value)
# - Branching (if x: ... else: ...)
# - Recursion (invert(_))
# - isinstance(value, int)
# 
# All other operations are not allowed.
# 
# Example:
# input  = ((((0,1),(2,3)),((4,5),(6,7))),)
# output = ((((0,4),(2,6)),((1,5),(3,7))),)
# Because that's the bit-reversal permutation of the original tree.
# Note that a bit-reversal permutation means that the end result should
# be a tree where, if the values in each leaf node are the original indexes (counting only leaf nodes),
# such that a tree with a height of 3 has original leaf node indexes of 0, 1, 2, 3, 4, 5, 6, 7 (see example input),
# the output should be a tree where the leaf nodes are in sorted order from left to right
# when considering the value of the reversed binary representation of the original index, with a maximum binary string length of 2^h where h is the height of the tree
# So, for example, in a height 3 tree, the leaf node originally at index 6 (110 in binary) should be at index 3 after the permutation, as the reverse of 110 is 011 in binary, aka 3.
# To make this easier to understand and the output easier to check, the values of the leaf nodes in the examples are the values of the original indexes of their node in decimal, but the function should not need to utilize these values as any set of values could be contained within the leaf nodes, but the permutation result would be the same. That is to say that the permutation swaps are identical for all trees of a particular height.
# Hint: it may be necessary to preform swaps while considering more than just 2 leaf nodes simultaneously. It might be necessary to preform longer distance swaps than just within a particular pair of children.
# Now, complete the function below with a valid implementation of 'invert':

def invert(tree: Tree) -> Tree:
    ...

# A test:
tree: Tree = ((((0,1),(2,3)),((4,5),(6,7))),  (((8,9),(10,11)),((12,13),(14,15))))
print(invert(tree))

and it gave back this solution that is on the right track but doesn't succeed still:

def invert(tree: Tree) -> Tree:
    if isinstance(tree, int):
        return tree
    else:
        left, right = tree
        if isinstance(left, int):
            ll = left
            lr = left
        else:
            ll, lr = left
        if isinstance(right, int):
            rl = right
            rr = right
        else:
            rl, rr = right
        inv_ll = invert(ll)
        inv_rl = invert(rl)
        inv_lr = invert(lr)
        inv_rr = invert(rr)
        return ((inv_ll, inv_rl), (inv_lr, inv_rr))

the chat

But regardless of that, I think the point is moot because @dalraf , The solution claude gave isn't actually moving the nodes. Its just doing a binary inversion of their values, which does achieve the same output for the examples where the values in the leaf nodes are the same as their leaf indices, but it won't work in the more general case where that isn't true and the values of the leaf nodes contain some other data. Funnily enough, the "invert" function it gave doesn't actually do anything as far as I can tell. When running it in debug mode, I see that the output of the call to invert(0, tree) is equivalent to tree, at least in the cases I've tested. Later on in the nested apply_bit_reverse function and its auxiliaries (also not allowed by the challenge), you can see that it's actually just inverting the binary representation of the values at each leaf node and converting those back into decimal.

@msukiasyan
Copy link

msukiasyan commented Oct 12, 2024

Claude 3.5 Sonnet provides essentially the same solution that the author has in mind (with the binary flag allowing to effectively define two separate functions). Using API with temperature=0, prompt:

Write code to invert a perfect binary tree in the sense of bit-reversal permutation. That is, a node that's on a path like [right, right, left, left, right] should end up on the inverted path [right, left, left, right, right]. Your function must be a pure recursive function with no dependencies except one described below. You should only utilize primitive operations and invert_tree calls to shuffle the tree around. Do NOT use for loops, inline function definitions or any non-primitive native functions. The primitive operations may include any number of operations like 
1. a, b = T 
2. invert_tree([c, d]) 
Note that this is a challenging task. 

You are also allowed to define another recursive function to help you. 

Example 1: 
input = [[[0,1],[2,3]],[[4,5],[6,7]]] 
output = [[[0,4],[2,6]],[[1,5],[3,7]]] 
Example 2: 
input = [[[[0,1],[2,3]],[[4,5],[6,7]]],[[[8,9],[10,11]],[[12,13],[14,15]]]] 
output = [[[[0, 8], [4, 12]], [[2, 10], [6, 14]]],[[[1, 9], [5, 13]], [[3, 11], [7, 15]]]]

Output code:

def invert_tree(T):
    if not isinstance(T, list):
        return T
    
    left, right = T
    
    if not isinstance(left, list) and not isinstance(right, list):
        return T
    
    inverted_left = invert_tree(left)
    inverted_right = invert_tree(right)
    
    swapped = swap_subtrees([inverted_left, inverted_right])
    
    return swapped

def swap_subtrees(T):
    left, right = T
    
    if not isinstance(left, list) and not isinstance(right, list):
        return T
    
    if isinstance(left, list) and isinstance(right, list):
        left_left, left_right = left
        right_left, right_right = right
        
        new_left = [left_left, right_left]
        new_right = [left_right, right_right]
        
        return [swap_subtrees(new_left), swap_subtrees(new_right)]
    
    return T

We can ask to combine the two into a single function like so (continuing with the API, temp=0):

Great! Now refactor the code into a single recursive function. You're allowed to define the function taking one binary variable and a tree as input.

New output code:

def invert_tree(b, T):
    if not isinstance(T, list):
        return T
    
    left, right = T
    
    if not isinstance(left, list) and not isinstance(right, list):
        return T
    
    if b:
        # Invert phase
        inverted_left = invert_tree(True, left)
        inverted_right = invert_tree(True, right)
        return invert_tree(False, [inverted_left, inverted_right])
    else:
        # Swap phase
        if isinstance(left, list) and isinstance(right, list):
            left_left, left_right = left
            right_left, right_right = right
            
            new_left = invert_tree(False, [left_left, right_left])
            new_right = invert_tree(False, [left_right, right_right])
            
            return [new_left, new_right]
        else:
            return T

I also attempted to ask for the single function zero-shot but it seems brittle and keeps stumbling.

@UltraInstinct0x
Copy link

You must give it an approved prompt, nothing else.
It must output a correct solution, passing all tests.

@VictorTaelin is @jlunder00's prompt acceptable?

@msukiasyan You started with a completely different prompt. The reason we do this is achieving the invert with the EXACT or ANY EQUIVELENT prompt of Approved Prompts.

@jlunder00
Copy link

jlunder00 commented Oct 13, 2024

@UltraInstinct0x I dont think my prompt is acceptable and I'm not claiming it is, just pointing out that even with extra hints and tips o1-preview still struggles. I clearly give some hints and it still gets it wrong. The better question would be if the prompt I based it on from @Maharshi-Pandya is acceptable:

from typing import Union, Tuple

Nat = int
Bit = bool
Tree = Union[Tuple['Tree', 'Tree'], Nat]

# Your goal is to implement an 'invert' function that performs a bit-reversal
# permutation on a Tree, respecting the following limitations:
# 1. You can NOT define or use any function other than 'invert'.
# 2. You can NOT use any type not defined above (Nat, Bit and Tree).
# 3. You can NOT use loops (but you can call 'invert' recursively).
# 4. You can NOT use mutability. It must be a pure function.
# 5. You can NOT use primitive Python operators or functions.
# 6. You can use 1 bit of state (as an extra argument).
# 7. You can only use the operations allowed below.
# 
# Operations allowed:
# - Destructing (left, right = value)
# - Variables (x = value)
# - Branching (if x: ... else: ...)
# - Recursion (invert(_, _))
# - isinstance(value, int)
# 
# All other operations are not allowed.
# 
# Example:
# input  = ((((0,1),(2,3)),((4,5),(6,7))),)
# output = ((((0,4),(2,6)),((1,5),(3,7))),)
# Because that's the bit-reversal permutation of the original tree.
# 
# Now, complete the function below with a valid implementation of 'invert':

def invert(bit: Bit, tree: Tree) -> Tree:
    ...

# A test:
tree: Tree = ((((0,1),(2,3)),((4,5),(6,7))),  (((8,9),(10,11)),((12,13),(14,15))))
print(invert(True, tree))

or perhaps a variation of it where we dont include the bit as an option (since its been shown its not necessary)

from typing import Union, Tuple

Nat = int
Tree = Union[Tuple['Tree', 'Tree'], Nat]

# Your goal is to implement an 'invert' function that performs a bit-reversal
# permutation on a Tree, respecting the following limitations:
# 1. You can NOT define or use any function other than 'invert'.
# 2. You can NOT use any type not defined above (Nat and Tree).
# 3. You can NOT use loops (but you can call 'invert' recursively).
# 4. You can NOT use mutability. It must be a pure function.
# 5. You can NOT use primitive Python operators or functions.
# 6. You can only use the operations allowed below.
# 
# Operations allowed:
# - Destructing (left, right = value)
# - Variables (x = value)
# - Branching (if x: ... else: ...)
# - Recursion (invert(_))
# - isinstance(value, int)
# 
# All other operations are not allowed.
# 
# Example:
# input  = ((((0,1),(2,3)),((4,5),(6,7))),)
# output = ((((0,4),(2,6)),((1,5),(3,7))),)
# Because that's the bit-reversal permutation of the original tree.
# 
# Now, complete the function below with a valid implementation of 'invert':

def invert(tree: Tree) -> Tree:
    ...

# A test:
tree: Tree = ((((0,1),(2,3)),((4,5),(6,7))),  (((8,9),(10,11)),((12,13),(14,15))))
print(invert(tree))

ie @VictorTaelin is it acceptable to ask for python and is it acceptable to not include the option of having the bit parameter?

@dalraf You can tell quickly that the typescript you got isnt correct because for any input, the first and last items should always remain in their starting locations (h 0s inverted is always h 0s and h 1s inverted is always h 1s)

@VictorTaelin
Copy link
Author

@msukiasyan for a moment I thought Sonnet did it but the solution (even with two functions) is still very wrong!

Thinking about it - perhaps by alternating the seed thousands of times (by altering the examples with a script!), given this is such a simple function, you might have it output the right one by random chance though, which I might pay the prize for the sake of fairness, but would just further confirm my point IMO

@UltraInstinct0x
Copy link

@dalraf I have tried running it with [[[0,1],[2,3]],[[4,5],[6,7]]] and output should be [[[0,4],[2,6]],[[1,5],[3,7]]]
However it was [LOG]: "[[[5,4],[7,6]],[[1,0],[3,2]]]"

@msukiasyan
Copy link

@msukiasyan for a moment I thought Sonnet did it but the solution (even with two functions) is still very wrong!

Thinking about it - perhaps by alternating the seed thousands of times (by altering the examples with a script!), given this is such a simple function, you might have it output the right one by random chance though, which I might pay the prize for the sake of fairness, but would just further confirm my point IMO

@VictorTaelin Can you please share a test case where it fails?

@msukiasyan
Copy link

Just to be clear, I have done some light testing with the verification code @cyber-meow posted above.

@jlunder00
Copy link

jlunder00 commented Oct 13, 2024

@VictorTaelin @msukiasyan If it matters, I used @msukiasyan 's series of prompts in claude-3.5 sonnet as well and got a different result. It uses the same method generally with some slight differences, but it fails:

Write code to invert a perfect binary tree in the sense of bit-reversal permutation. That is, a node that's on a path like [right, right, left, left, right] should end up on the inverted path [right, left, left, right, right]. Your function must be a pure recursive function with no dependencies except one described below. You should only utilize primitive operations and invert_tree calls to shuffle the tree around. Do NOT use for loops, inline function definitions or any non-primitive native functions. The primitive operations may include any number of operations like 
1. a, b = T 
2. invert_tree([c, d]) 
Note that this is a challenging task. 

You are also allowed to define another recursive function to help you. 

Example 1: 
input = [[[0,1],[2,3]],[[4,5],[6,7]]] 
output = [[[0,4],[2,6]],[[1,5],[3,7]]] 
Example 2: 
input = [[[[0,1],[2,3]],[[4,5],[6,7]]],[[[8,9],[10,11]],[[12,13],[14,15]]]] 
output = [[[[0, 8], [4, 12]], [[2, 10], [6, 14]]],[[[1, 9], [5, 13]], [[3, 11], [7, 15]]]]
def invert_tree(T):
    if not isinstance(T, list):
        return T
    
    left, right = T
    
    if not isinstance(left, list) and not isinstance(right, list):
        return T
    
    inverted_left = invert_tree(left)
    inverted_right = invert_tree(right)
    
    return swap_halves([inverted_left, inverted_right])

def swap_halves(T):
    left, right = T
    
    if not isinstance(left, list) and not isinstance(right, list):
        return T
    
    left_left, left_right = left
    right_left, right_right = right
    
    new_left = [left_left, right_left]
    new_right = [left_right, right_right]
    
    return [swap_halves(new_left), swap_halves(new_right)]
    ```

Great! Now refactor the code into a single recursive function. You're allowed to define the function taking one binary variable and a tree as input.

def invert_tree(swap, T):
   if not isinstance(T, list):
       return T
   
   left, right = T
   
   if not isinstance(left, list) and not isinstance(right, list):
       return T if swap else [right, left]
   
   inverted_left = invert_tree(not swap, left)
   inverted_right = invert_tree(not swap, right)
   
   if swap:
       left_left, left_right = inverted_left
       right_left, right_right = inverted_right
       return [[left_left, right_left], [left_right, right_right]]
   else:
       return [inverted_left, inverted_right]

# Helper function to start the inversion process
def start_inversion(T):
   return invert_tree(True, T)

This time the code it outputs fails on invert_tree(True, [[0, 1], [2, 3]]), which outputs [[1, 3], [0, 2]] instead of [[0, 2], [1, 3]]. So there might be something to the whole randomness thing. I've tested @msukiasyan 's result on a few cases and haven't found any where it fails yet. But is a prompt like this acceptable? Or does it deviate too much from the original?

@msukiasyan
Copy link

msukiasyan commented Oct 13, 2024

Thanks for checking @jlunder00, weird that you're getting slightly different results. I've tested on Anthropic's Console with empty system prompt. Either way, the two-function version you have also seems to work but the second part clearly hasn't worked out. I did observe some brittleness particularly in that second stage of combining the functions.

Can you pls check if you have temp=0 and empty system prompt?

@jlunder00
Copy link

jlunder00 commented Oct 13, 2024

@msukiasyan I was using the chat version of 3.5-sonnet. When I swapped to the api version as you specified I ended up getting exactly the same output. I suppose they must be different models, or maybe the chat version has a nonzero temperature thats not shown.

@msukiasyan
Copy link

@jlunder00 Nice. I do still claim that this is essentially the solution that @VictorTaelin was looking for although it's unclear to what extent this involves "reasoning".

@jlunder00
Copy link

jlunder00 commented Oct 13, 2024

@msukiasyan its debatable whether or not having the LLM first create a solution with 2 functions and then asking it to refactor them into 1 is guiding it too much. One could argue that it's kind of like you're telling it the steps to take to find the solution rather than it doing that itself. But I'm not sure myself on that. I'm also not sure how much deviation from the original prompt is acceptable.

I've been trying to get sonnet to get a good result with a prompt more similar to the original one and asking it to make the 2 function version then single function version in one prompt, and it's gotten SO close to working. Specifically I did away with the bit and added some details explaining what a bit reversal permutation is and some general reminders:

Write code to invert a perfect binary tree in the sense of bit-reversal permutation. This is a challenging and complex task. The exact specification of the problem is as follows:

from typing import Union, Tuple

Nat = int
Tree = Union[Tuple['Tree', 'Tree'], Nat]

# Your goal is to implement an 'invert' function that performs a bit-reversal
# That is, a node that's on a path like [right, right, left, left, right] should end up on the inverted path [right, left, left, right, right].
# permutation on a Tree, respecting the following limitations:
# 1. You can NOT define or use any function other than 'invert'. You may temporarily define another recursive function to help you, so long as you can later refactor this into a single function. 
# 2. You can NOT use any type not defined above (Nat and Tree).
# 3. You can NOT use loops (but you can call 'invert' recursively).
# 4. You can NOT use mutability. It must be a pure function.
# 5. You can NOT use primitive Python operators or functions.
# 6. You can only use the operations allowed below.
# 
# Operations allowed:
# - Destructing (left, right = value)
# - Variables (x = value)
# - Branching (if x: ... else: ...)
# - Recursion (invert(_))
# - isinstance(value, int)
# 
# All other operations are not allowed.
# 
# Example:
# input  = ((((0,1),(2,3)),((4,5),(6,7))))
# output = ((((0,4),(2,6)),((1,5),(3,7))))
# Because that's the bit-reversal permutation of the original tree.
# 
# Now, complete the function below with a valid implementation of 'invert':

def invert(tree: Tree) -> Tree:
    ...

# A test:
tree: Tree = ((((0,1),(2,3)),((4,5),(6,7))),  (((8,9),(10,11)),((12,13),(14,15))))
print(invert(True, tree))

other examples:
Example 1:
input = [[[0,1],[2,3]],[[4,5],[6,7]]]
output = [[[0,4],[2,6]],[[1,5],[3,7]]]
Example 2:
input = [[[[0,1],[2,3]],[[4,5],[6,7]]],[[[8,9],[10,11]],[[12,13],[14,15]]]]
output = [[[[0, 8], [4, 12]], [[2, 10], [6, 14]]],[[[1, 9], [5, 13]], [[3, 11], [7, 15]]]]
Note that this is a challenging task. Remember, this is a challenging and complex task. Do not simplify the problem too much. It is different from the standard binary tree inversion because we want to ensure that in the end, each leaf node is in the position such that the reverse of its original indexes binary representation is the same as its new index.

First, solve the problem using 2 recursive functions. (No nested functions)

After, combine them into a single recursive function with the same functionality, making careful adjustments as necessary to ensure the same results occur. Be extra careful.

It got this output:

def invert(tree: Tree) -> Tree:
    if isinstance(tree, int):
        return tree
    
    left, right = tree
    
    if isinstance(left, int) and isinstance(right, int):
        return tree
    
    if isinstance(left, tuple) and isinstance(right, tuple):
        left_left, left_right = left
        right_left, right_right = right
        
        new_left_left = invert(left_left)
        new_left_right = invert(right_left)
        new_right_left = invert(left_right)
        new_right_right = invert(right_right)
        
        new_left = (new_left_left, new_right_left)
        new_right = (new_left_right, new_right_right)
        
        return (new_left, new_right)
    
    inverted_left = invert(left)
    inverted_right = invert(right)
    
    return (inverted_left, inverted_right)

which fails on larger trees, but if you make just a few changes, specifically:
new_left_right = invert(right_left)
new_right_left = invert(left_right)
becoming
new_left_right = invert(left_right)
new_right_left = invert(right_left)
and doing
return (invert(new_left), invert(new_right))
#instead of
return (new_left, new_right)
it works for all cases.

@msukiasyan
Copy link

@jlunder00 Agree, I'll let @VictorTaelin decide on how to resolve the challenge. I do think a straight 1-function prompt exists (and I've gotten close) but searching for it is probably not in the spirit of the challenge. I also think the current prompt can be adapted to be closer to the original one. I started with this one mostly because I myself didn't understand what the task was asking for at first, so elaborated a bit for the model.

@jlunder00
Copy link

@msukiasyan Probably true, but then again, I also didn't really understand what was being asked for until I drew out @cyber-meow's python solution on a whiteboard, so one could argue that the problem definition/prompt should have more explanation. I'm also just really curious as to how similar to the original prompt while still getting a correct result you can get, in a one shot. Worth noting that gpt-o1-preview comes nowhere near as close as sonnet does with the same prompts on this.

@Jainam2130
Copy link

Jainam2130 commented Oct 13, 2024

I personally believe "reasoning" is just OOD generalization, in traditional ml this had been deeply studied and there are a bunch of ways to improve this...

I won't comment on whether @msukiasyan 's demonstrates reasoning. But there does exist a 0 shot COT prompt true to the problem spec that undeniably proves reasoning as per @VictorTaelin .

Or as I would put it, proves a decent amount of ood generalization in current architectures. Can this generalization be improved upon, Yes!
Do LLM not reason, No!
And is @VictorTaelin incorrect, also Yes.

I would not claim that are LLM's perfect at generalization / reasoning. This can, is being and should be improved upon. But LLM's can definitely "reason". If we can agree on my definition of reasoning. @VictorTaelin I'm betting $10k myself that I can change your mind.(Willing to put the money in a betting market or sign a contract.)

Thinking about it - perhaps by alternating the seed thousands of times (by altering the examples with a script!), given this is such a simple function, you might have it output the right one by random chance though, which I might pay the prize for the sake of fairness, but would just further confirm my point IMO

Let's agree on a challenge that in spirit and to specs that would prove "reasoning", and there'll be a COT solution that solves it. The AI should have the basic building blocks to solve it, but due to the inability of complex reasoning, according to you: LLM's should not be able to solve it. Shoot any problem that'll convince you

@endolith
Copy link

endolith commented Oct 13, 2024

"Current LLMs can't reason" is a reasonable claim, though an experiment like this doesn't prove it in the general case.

"No AI will EVER solve this problem" is an absurd, completely unjustified claim. Future AIs will eventually be able to think anything that we can think.

@jlunder00
Copy link

@endolith agreed, though I get the impression that the author's intent may have been more along the lines of "No LLM will EVER solve this problem", which might be slightly more reasonable to say, because the claim is clearly that using current LLM architectures the problem wont be solved, not that there won't be some future architecture that can solve it. That's the way I interpreted it at least, but maybe @VictorTaelin can enlighten us a bit more on that.

@msukiasyan
Copy link

FWIW, here is a prompt very close to the author's that gets a correct 2-function answer from 3.5 Sonnet. The main changes are:

  1. An extra sentence describing what bit-reversal permutation is (as before).
  2. Extra 16 leaf example (as before).
  3. Request solution with two functions instead of one.
  4. Very basic "you're a world-class programmer" CoT prompt prepended.
Note that this is a challenging task. But you are a world-class competitive programmer. You
1. carefully state the key elements, constraints and features of the solution,
2. plan your approach,
3. write code.

Write code to invert a perfect binary tree in the sense of bit-reversal permutation.
That is, a node that's on a path like [right, right, left, left, right] should end up on the inverted path [right, left, left, right, right].
The solution must respect the following limitations:
1. You can NOT define or use any function other than two recursive functions.
2. You can NOT use any type other than int and list. 
3. You can NOT use loops (but you can call 'invert' recursively). 
4. You can NOT use mutability. It must be a pure function. 
5. You can NOT use primitive Python operators or functions. 
6. You can only use the operations allowed below. 

Operations allowed: 
- Destructing (left, right = value) 
- Variables (x = value) 
- Branching (if x: ... else: ...) 
- Recursion (invert(_, _)) 
- isinstance(value, int) 

All other operations are not allowed.

Example 1:
input  = [[[[0,1],[2,3]],[[4,5],[6,7]]]]
output = [[[[0,4],[2,6]],[[1,5],[3,7]]]]
Example 2: 
input = [[[[0,1],[2,3]],[[4,5],[6,7]]],[[[8,9],[10,11]],[[12,13],[14,15]]]] 
output = [[[[0, 8], [4, 12]], [[2, 10], [6, 14]]],[[[1, 9], [5, 13]], [[3, 11], [7, 15]]]]

Response code:

def invert(tree):
    if isinstance(tree, int):
        return tree
    
    left, right = tree
    
    if isinstance(left, int):
        return tree
    
    inverted_left = invert(left)
    inverted_right = invert(right)
    
    return combine(inverted_left, inverted_right)

def combine(left, right):
    if isinstance(left, int):
        return [left, right]
    
    left_left, left_right = left
    right_left, right_right = right
    
    new_left = combine(left_left, right_left)
    new_right = combine(left_right, right_right)
    
    return [new_left, new_right]

You can also get the correct 1-function answer by following up with the exact same prompt as before:
Great! Now refactor the code into a single recursive function. You're allowed to define the function taking one binary variable and a tree as input.

Response code:

def invert(is_combine, tree):
    if isinstance(tree, int):
        return tree
    
    left, right = tree
    
    if is_combine:
        if isinstance(left, int):
            return [left, right]
        
        left_left, left_right = left
        right_left, right_right = right
        
        new_left = invert(True, [left_left, right_left])
        new_right = invert(True, [left_right, right_right])
        
        return [new_left, new_right]
    else:
        if isinstance(left, int):
            return tree
        
        inverted_left = invert(False, left)
        inverted_right = invert(False, right)
        
        return invert(True, [inverted_left, inverted_right])

@VictorTaelin
Copy link
Author

As per the original post, modifications are allowed, as long as:

It clearly doesn't help the AI.

Given this is such a simple problem, telling it to use two recursive functions is giving it half of the solution, which is a quite significant help to the AI. I'll not consider a prompt that instructs it part of the algorithm as resolving this challenge.

That said, I do think this challenge will be "beaten" by trying enough prompts until one seed gives the right answer, and I'll gladly grant $10k for a solution that abides to the rules (I'm taking this as the cost of truth!!). But I think that'd just reinforce my point, as, ultimately, the solution was brute-force, not "reasoning". (This function can be found by symbolic proof search, with no LLMs whatsoever!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment