<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kernel</title>
    <description>The latest articles on DEV Community by Kernel (@kernel_dev).</description>
    <link>https://dev.to/kernel_dev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4172663%2Fc51cad43-0071-4769-a267-33f8b7cb8457.jpg</url>
      <title>DEV Community: Kernel</title>
      <link>https://dev.to/kernel_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kernel_dev"/>
    <language>en</language>
    <item>
      <title>Solved exercise: recursive search in a rotated list in Python</title>
      <dc:creator>Kernel</dc:creator>
      <pubDate>Wed, 25 Nov 2026 11:06:23 +0000</pubDate>
      <link>https://dev.to/kernel_dev/solved-exercise-recursive-search-in-a-rotated-list-in-python-2mbo</link>
      <guid>https://dev.to/kernel_dev/solved-exercise-recursive-search-in-a-rotated-list-in-python-2mbo</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://www.larebelion.com/2026/11/solved-exercise-recursive-search-in.html" rel="noopener noreferrer"&gt;larebelion.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Exam-level Python exercise on recursion and binary search in a sorted, rotated list, with hints, a tested solution and a full cost analysis.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Updated on &lt;time&gt;November 16, 2026&lt;/time&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.larebelion.com/p/exercises.html" rel="noopener noreferrer"&gt;Exam Code&lt;/a&gt; &lt;span&gt;Python&lt;/span&gt; &lt;span&gt;Algorithms&lt;/span&gt; &lt;span&gt;Exam level&lt;/span&gt; &lt;span&gt;Tested code&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Flarebelion-img%2Fmedia%2Fd1673e3d80ed09546a9bc7069f111210cf1c4781512a9eebbce58485ea32947f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Flarebelion-img%2Fmedia%2Fd1673e3d80ed09546a9bc7069f111210cf1c4781512a9eebbce58485ea32947f.png" alt="Exam Code: Solved exercise: recursive search in a rotated list in Python" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Programming II · Midterm Exam&lt;/span&gt;&lt;span&gt;Question 2 · 2.5 points · 30 min&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;A server stores the timestamps of its events in a &lt;em&gt;circular buffer&lt;/em&gt;. When the buffer is dumped into a Python list, the result is a sequence of &lt;strong&gt;distinct&lt;/strong&gt; integers. The sequence was sorted in increasing order but has been &lt;strong&gt;rotated&lt;/strong&gt;: an initial segment was moved to the end. For example, the increasing list &lt;code&gt;[2, 5, 9, 14]&lt;/code&gt; rotated by two positions becomes &lt;code&gt;[9, 14, 2, 5]&lt;/code&gt;. The rotation may be zero, in which case the list is already sorted.&lt;/p&gt;
&lt;p&gt;Write a program that first computes the &lt;strong&gt;pivot&lt;/strong&gt;, which is the index that holds the minimum value of the list. Then, for each query, it must report the index in the original list where the requested value appears, or &lt;code&gt;-1&lt;/code&gt; if it does not appear.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mandatory constraints&lt;/strong&gt;: the pivot and every search must be computed by &lt;strong&gt;recursive&lt;/strong&gt; functions that run in &lt;em&gt;O(log n)&lt;/em&gt;. You may not scan the list linearly, and you may not use &lt;code&gt;list.index&lt;/code&gt;, the &lt;code&gt;in&lt;/code&gt; operator on the list, &lt;code&gt;min&lt;/code&gt;, &lt;code&gt;sorted&lt;/code&gt; or the &lt;code&gt;bisect&lt;/code&gt; module. A linear solution scores at most 0.75 points, even if its output is correct.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Input&lt;/strong&gt; (standard input): the first line contains &lt;code&gt;n&lt;/code&gt; (0 ≤ n ≤ 100000). The second line contains the &lt;code&gt;n&lt;/code&gt; integers of the list separated by spaces, and it is empty when &lt;code&gt;n&lt;/code&gt; is 0. The third line contains &lt;code&gt;q&lt;/code&gt;, the number of queries. It is followed by &lt;code&gt;q&lt;/code&gt; lines with one integer each.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt; (standard output): a first line &lt;code&gt;PIVOT k&lt;/code&gt; with the index of the minimum, or &lt;code&gt;PIVOT -1&lt;/code&gt; if the list is empty. Then one line per query, in the same order, with the queried value, a space and its index (or &lt;code&gt;-1&lt;/code&gt;). Finally, a line &lt;code&gt;FOUND c&lt;/code&gt; with the number of queries that were present in the list.&lt;/p&gt;
&lt;p&gt;Pay attention to the edge cases: an empty list, a list with a single element, a list that is not rotated, and a list whose minimum is in the last position.&lt;/p&gt;
&lt;p&gt;Sample input&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;8&lt;br&gt;
14 18 23 31 2 5 9 11&lt;br&gt;
6&lt;br&gt;
5&lt;br&gt;
31&lt;br&gt;
14&lt;br&gt;
11&lt;br&gt;
20&lt;br&gt;
2&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Expected output&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;PIVOT 4&lt;br&gt;
5 5&lt;br&gt;
31 3&lt;br&gt;
14 0&lt;br&gt;
11 7&lt;br&gt;
20 -1&lt;br&gt;
2 4&lt;br&gt;
FOUND 5&lt;/code&gt;&lt;/pre&gt;
&lt;dl&gt;
&lt;dt&gt;Recursive function that finds the pivot in logarithmic time, with a correct base case and guaranteed termination&lt;/dt&gt;
&lt;dd&gt;1.0&lt;/dd&gt;
&lt;dt&gt;Recursive binary search in the correct block, returning the index in the original list or -1&lt;/dt&gt;
&lt;dd&gt;0.75&lt;/dd&gt;
&lt;dt&gt;Input and output in the exact format, including the empty list and the one-element list&lt;/dt&gt;
&lt;dd&gt;0.5&lt;/dd&gt;
&lt;dt&gt;Justified time and space cost, including the depth of the call stack&lt;/dt&gt;
&lt;dd&gt;0.25&lt;/dd&gt;
&lt;/dl&gt;


&lt;h2&gt;Hints&lt;/h2&gt;Hint 1 · What does comparing the two ends of a range tell you?&lt;p&gt;Take a range &lt;code&gt;v[lo..hi]&lt;/code&gt;. If &lt;code&gt;v[lo] &amp;lt;= v[hi]&lt;/code&gt;, the range does not contain the rotation. It is sorted and its minimum is &lt;code&gt;v[lo]&lt;/code&gt;. That is your base case. If the condition fails, the drop from a large value to a small value lies inside the range.&lt;/p&gt;Hint 2 · Which half contains the drop?&lt;p&gt;Look at the middle element &lt;code&gt;v[mid]&lt;/code&gt; and compare it with &lt;code&gt;v[hi]&lt;/code&gt;, not with &lt;code&gt;v[lo]&lt;/code&gt;. If &lt;code&gt;v[mid] &amp;gt; v[hi]&lt;/code&gt;, the minimum lies strictly to the right of &lt;code&gt;mid&lt;/code&gt;. Otherwise the minimum lies in &lt;code&gt;[lo, mid]&lt;/code&gt;, and &lt;code&gt;mid&lt;/code&gt; may itself be the minimum. Check which of the two recursive calls keeps &lt;code&gt;mid&lt;/code&gt;.&lt;/p&gt;Hint 3 · I have the pivot. What next?&lt;p&gt;With the index &lt;code&gt;k&lt;/code&gt; of the minimum, the list splits into two sorted blocks, &lt;code&gt;v[0..k-1]&lt;/code&gt; and &lt;code&gt;v[k..n-1]&lt;/code&gt;. Every value in the first block is larger than every value in the second. A single range check is enough to decide which block to search. After that, an ordinary recursive binary search that uses the indices of the original list answers the query.&lt;/p&gt;
&lt;br&gt;
&lt;h2&gt;Solution&lt;/h2&gt;Explained solution&lt;p&gt;The idea is to split the problem into two independent logarithmic searches. The first one finds the pivot. The second one is a classic binary search, restricted to the sorted block where the value can possibly be. Both functions are recursive, and each call halves the range it works on.&lt;/p&gt;
&lt;p&gt;If you want to review plain binary search and its cost analysis first, see the &lt;a href="https://www.larebelion.com/2026/11/solved-exercise-sorting-and-binary.html" rel="noopener noreferrer"&gt;solved exercise on sorting and binary search with cost analysis in Python&lt;/a&gt;. This exercise builds on that one.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import sys

&lt;h1&gt;
  
  
  Index of the minimum of v[lo..hi]. v is increasing and rotated, no duplicates.
&lt;/h1&gt;

&lt;p&gt;def find_pivot(v, lo, hi):&lt;br&gt;
    if v[lo] &amp;lt;= v[hi]:&lt;br&gt;
        return lo&lt;br&gt;
    mid = (lo + hi) // 2&lt;br&gt;
    if v[mid] &amp;gt; v[hi]:&lt;br&gt;
        return find_pivot(v, mid + 1, hi)&lt;br&gt;
    return find_pivot(v, lo, mid)&lt;/p&gt;

&lt;h1&gt;
  
  
  Recursive binary search in the sorted range v[lo..hi].
&lt;/h1&gt;

&lt;p&gt;def binary_search(v, x, lo, hi):&lt;br&gt;
    if lo &amp;gt; hi:&lt;br&gt;
        return -1&lt;br&gt;
    mid = (lo + hi) // 2&lt;br&gt;
    if v[mid] == x:&lt;br&gt;
        return mid&lt;br&gt;
    if v[mid] &amp;lt; x:&lt;br&gt;
        return binary_search(v, x, mid + 1, hi)&lt;br&gt;
    return binary_search(v, x, lo, mid - 1)&lt;/p&gt;

&lt;h1&gt;
  
  
  Decide which sorted block may contain x and search only there.
&lt;/h1&gt;

&lt;p&gt;def locate(v, x, k):&lt;br&gt;
    if not v:&lt;br&gt;
        return -1&lt;br&gt;
    if k &amp;gt; 0 and v[0] &amp;lt;= x &amp;lt;= v[k - 1]:&lt;br&gt;
        return binary_search(v, x, 0, k - 1)&lt;br&gt;
    return binary_search(v, x, k, len(v) - 1)&lt;/p&gt;

&lt;p&gt;def main():&lt;br&gt;
    data = sys.stdin.read().split()&lt;br&gt;
    n = int(data[0])&lt;br&gt;
    v = [int(tok) for tok in data[1:1 + n]]&lt;br&gt;
    q = int(data[1 + n])&lt;br&gt;
    queries = [int(tok) for tok in data[2 + n:2 + n + q]]&lt;br&gt;
    k = find_pivot(v, 0, n - 1) if n &amp;gt; 0 else -1&lt;br&gt;
    out = ['PIVOT ' + str(k)]&lt;br&gt;
    found = 0&lt;br&gt;
    for x in queries:&lt;br&gt;
        i = locate(v, x, k)&lt;br&gt;
        if i != -1:&lt;br&gt;
            found += 1&lt;br&gt;
        out.append(f'{x} {i}')&lt;br&gt;
    out.append(f'FOUND {found}')&lt;br&gt;
    print('\n'.join(out))&lt;/p&gt;

&lt;/code&gt;&lt;p&gt;&lt;code&gt;if &lt;strong&gt;name&lt;/strong&gt; == '&lt;strong&gt;main&lt;/strong&gt;':&lt;br&gt;&lt;br&gt;
    main()&lt;/code&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Reading the input.&lt;/strong&gt; &lt;code&gt;main&lt;/code&gt; reads all of the input with &lt;code&gt;sys.stdin.read().split()&lt;/code&gt;. The empty line in the &lt;code&gt;n = 0&lt;/code&gt; case causes no trouble, because &lt;code&gt;split()&lt;/code&gt; discards whitespace and line breaks. The &lt;code&gt;n&lt;/code&gt; tokens after the first one form the list, the next token is &lt;code&gt;q&lt;/code&gt;, and the remaining tokens are the queries.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The pivot.&lt;/strong&gt; &lt;code&gt;find_pivot(v, lo, hi)&lt;/code&gt; has a precise base case: if &lt;code&gt;v[lo] &amp;lt;= v[hi]&lt;/code&gt;, the range is sorted and its minimum is at &lt;code&gt;lo&lt;/code&gt;. The values are distinct, so equality happens only when &lt;code&gt;lo == hi&lt;/code&gt;. If the base case does not apply, the function computes &lt;code&gt;mid&lt;/code&gt;. When &lt;code&gt;v[mid] &amp;gt; v[hi]&lt;/code&gt;, the drop lies between &lt;code&gt;mid&lt;/code&gt; and &lt;code&gt;hi&lt;/code&gt;, and the function recurses on &lt;code&gt;[mid + 1, hi]&lt;/code&gt;. Otherwise the range &lt;code&gt;[mid, hi]&lt;/code&gt; is sorted, so the minimum is in &lt;code&gt;[lo, mid]&lt;/code&gt;. That call keeps &lt;code&gt;mid&lt;/code&gt; because it may be the minimum.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Termination.&lt;/strong&gt; In the recursive case &lt;code&gt;lo &amp;lt; hi&lt;/code&gt; always holds, because &lt;code&gt;v[lo] &amp;gt; v[hi]&lt;/code&gt; is impossible when &lt;code&gt;lo == hi&lt;/code&gt;. Therefore &lt;code&gt;mid &amp;lt; hi&lt;/code&gt;, and both calls work on a strictly smaller range. In the example, &lt;code&gt;find_pivot&lt;/code&gt; starts with &lt;code&gt;[0, 7]&lt;/code&gt;. Since 14 &amp;gt; 11 and &lt;code&gt;v[3] = 31 &amp;gt; 11&lt;/code&gt;, it moves to &lt;code&gt;[4, 7]&lt;/code&gt;. There &lt;code&gt;v[4] = 2 &amp;lt;= 11&lt;/code&gt;, so it returns 4 after only two calls.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Choosing the block.&lt;/strong&gt; &lt;code&gt;locate&lt;/code&gt; relies on the fact that the whole left block &lt;code&gt;v[0..k-1]&lt;/code&gt; is greater than the whole right block &lt;code&gt;v[k..n-1]&lt;/code&gt;. If &lt;code&gt;x&lt;/code&gt; lies between &lt;code&gt;v[0]&lt;/code&gt; and &lt;code&gt;v[k-1]&lt;/code&gt;, it can only be on the left. Otherwise it can only be on the right. When &lt;code&gt;k == 0&lt;/code&gt; there is no left block and the whole list is searched, which is what happens in the &lt;em&gt;No rotation&lt;/em&gt; test.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The search.&lt;/strong&gt; &lt;code&gt;binary_search&lt;/code&gt; is the usual recursive binary search, but with absolute indices. The index it returns is therefore already an index of the original list, and no conversion is needed. In the example, query 20 falls within the range of the left block (14 to 31) and is not there, so the program prints &lt;code&gt;20 -1&lt;/code&gt; without ever looking at the right block.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Edge cases.&lt;/strong&gt; With an empty list, &lt;code&gt;find_pivot&lt;/code&gt; is never called, because &lt;code&gt;v[0]&lt;/code&gt; does not exist. The program prints &lt;code&gt;PIVOT -1&lt;/code&gt; and &lt;code&gt;locate&lt;/code&gt; returns -1 for every query. With one element, the base case holds on the first call. The &lt;em&gt;Minimum at the end&lt;/em&gt; test takes the &lt;code&gt;mid + 1&lt;/code&gt; branch twice in a row before reaching &lt;code&gt;[5, 5]&lt;/code&gt;. The &lt;em&gt;Pivot at index 1&lt;/em&gt; test exercises the branch that keeps &lt;code&gt;mid&lt;/code&gt;. The output lines are collected in &lt;code&gt;out&lt;/code&gt; and written with a single &lt;code&gt;print&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://gitlab.com/larebelion/codigo-de-examen/-/tree/main/python/busqueda-recursiva-en-lista-rotada-en-python/en" rel="noopener noreferrer"&gt;Code and tests on GitLab&lt;/a&gt;&lt;/p&gt;


&lt;p&gt;Step-by-step trace · generated by running the code&lt;/p&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;func&lt;/th&gt;
&lt;th&gt;x&lt;/th&gt;
&lt;th&gt;lo&lt;/th&gt;
&lt;th&gt;hi&lt;/th&gt;
&lt;th&gt;mid&lt;/th&gt;
&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;pivot&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;pivot&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;search&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h2&gt;Test cases&lt;/h2&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;
&lt;th&gt;Case&lt;/th&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;Expected output&lt;/th&gt;
&lt;th&gt;Actual output&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Statement example&lt;/td&gt;
&lt;td&gt;&lt;code&gt;8&lt;br&gt;
14 18 23 31 2 5 9 11&lt;br&gt;
6&lt;br&gt;
5&lt;br&gt;
31&lt;br&gt;
14&lt;br&gt;
11&lt;br&gt;
20&lt;br&gt;
2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PIVOT 4&lt;br&gt;
5 5&lt;br&gt;
31 3&lt;br&gt;
14 0&lt;br&gt;
11 7&lt;br&gt;
20 -1&lt;br&gt;
2 4&lt;br&gt;
FOUND 5&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PIVOT 4&lt;br&gt;
5 5&lt;br&gt;
31 3&lt;br&gt;
14 0&lt;br&gt;
11 7&lt;br&gt;
20 -1&lt;br&gt;
2 4&lt;br&gt;
FOUND 5&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;OK&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No rotation&lt;/td&gt;
&lt;td&gt;&lt;code&gt;5&lt;br&gt;
1 3 5 7 9&lt;br&gt;
4&lt;br&gt;
7&lt;br&gt;
4&lt;br&gt;
1&lt;br&gt;
10&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PIVOT 0&lt;br&gt;
7 3&lt;br&gt;
4 -1&lt;br&gt;
1 0&lt;br&gt;
10 -1&lt;br&gt;
FOUND 2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PIVOT 0&lt;br&gt;
7 3&lt;br&gt;
4 -1&lt;br&gt;
1 0&lt;br&gt;
10 -1&lt;br&gt;
FOUND 2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;OK&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Empty list&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;0

&lt;/code&gt;&lt;p&gt;&lt;code&gt;2&lt;br&gt;&lt;br&gt;
3&lt;br&gt;&lt;br&gt;
-1&lt;/code&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PIVOT -1&lt;br&gt;&lt;br&gt;
3 -1&lt;br&gt;&lt;br&gt;
-1 -1&lt;br&gt;&lt;br&gt;
FOUND 0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PIVOT -1&lt;br&gt;&lt;br&gt;
3 -1&lt;br&gt;&lt;br&gt;
-1 -1&lt;br&gt;&lt;br&gt;
FOUND 0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;OK&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Single element&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1&lt;br&gt;&lt;br&gt;
42&lt;br&gt;&lt;br&gt;
2&lt;br&gt;&lt;br&gt;
42&lt;br&gt;&lt;br&gt;
7&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PIVOT 0&lt;br&gt;&lt;br&gt;
42 0&lt;br&gt;&lt;br&gt;
7 -1&lt;br&gt;&lt;br&gt;
FOUND 1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PIVOT 0&lt;br&gt;&lt;br&gt;
42 0&lt;br&gt;&lt;br&gt;
7 -1&lt;br&gt;&lt;br&gt;
FOUND 1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;OK&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Minimum at the end, negatives&lt;/td&gt;
&lt;td&gt;&lt;code&gt;6&lt;br&gt;&lt;br&gt;
-3 0 4 8 12 -10&lt;br&gt;&lt;br&gt;
4&lt;br&gt;&lt;br&gt;
-10&lt;br&gt;&lt;br&gt;
12&lt;br&gt;&lt;br&gt;
-3&lt;br&gt;&lt;br&gt;
5&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PIVOT 5&lt;br&gt;&lt;br&gt;
-10 5&lt;br&gt;&lt;br&gt;
12 4&lt;br&gt;&lt;br&gt;
-3 0&lt;br&gt;&lt;br&gt;
5 -1&lt;br&gt;&lt;br&gt;
FOUND 3&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PIVOT 5&lt;br&gt;&lt;br&gt;
-10 5&lt;br&gt;&lt;br&gt;
12 4&lt;br&gt;&lt;br&gt;
-3 0&lt;br&gt;&lt;br&gt;
5 -1&lt;br&gt;&lt;br&gt;
FOUND 3&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;OK&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pivot at index 1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;4&lt;br&gt;&lt;br&gt;
9 1 3 6&lt;br&gt;&lt;br&gt;
3&lt;br&gt;&lt;br&gt;
9&lt;br&gt;&lt;br&gt;
6&lt;br&gt;&lt;br&gt;
0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PIVOT 1&lt;br&gt;&lt;br&gt;
9 0&lt;br&gt;&lt;br&gt;
6 3&lt;br&gt;&lt;br&gt;
0 -1&lt;br&gt;&lt;br&gt;
FOUND 2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PIVOT 1&lt;br&gt;&lt;br&gt;
9 0&lt;br&gt;&lt;br&gt;
6 3&lt;br&gt;&lt;br&gt;
0 -1&lt;br&gt;&lt;br&gt;
FOUND 2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;OK&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;Actual outputs: code compiled with Python 3.13.16 and run in an isolated container on 16 November 2026.&lt;/p&gt;
&lt;br&gt;&lt;br&gt;
&lt;h2&gt;Complexity&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Time.&lt;/strong&gt; Each call to &lt;code&gt;find_pivot&lt;/code&gt; or &lt;code&gt;binary_search&lt;/code&gt; does constant work and leaves a range of at most half the size. Each of the two functions therefore costs &lt;em&gt;O(log n)&lt;/em&gt;. The pivot is computed once and each query costs &lt;em&gt;O(log n)&lt;/em&gt;, so the total is &lt;em&gt;O(n + q log n)&lt;/em&gt;. The &lt;em&gt;n&lt;/em&gt; term comes only from reading and converting the input, which cannot be avoided. The algorithmic part is &lt;em&gt;O(log n + q log n)&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Memory.&lt;/strong&gt; The list takes &lt;em&gt;O(n)&lt;/em&gt;, and the queries together with the collected output take &lt;em&gt;O(q)&lt;/em&gt;. Recursion adds a stack of depth &lt;em&gt;O(log n)&lt;/em&gt;. With n = 100000 that is about 17 nested calls, far below Python's default recursion limit of 1000. You therefore do not need &lt;code&gt;sys.setrecursionlimit&lt;/code&gt;. No function copies the list: the calls pass the indices &lt;code&gt;lo&lt;/code&gt; and &lt;code&gt;hi&lt;/code&gt;, not sublists.&lt;/p&gt;
&lt;p&gt;If each call passed a slice such as &lt;code&gt;v[mid + 1:]&lt;/code&gt;, every level would copy half the list, and the cost would grow to &lt;em&gt;O(n)&lt;/em&gt; per search. The returned index would also be relative to the slice and would need correcting.&lt;/p&gt;
&lt;br&gt;&lt;br&gt;
&lt;h2&gt;Common mistakes&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Recursing on &lt;code&gt;[lo, mid - 1]&lt;/code&gt; when &lt;code&gt;v[mid] &amp;lt;= v[hi]&lt;/code&gt;. This discards the minimum itself, and the test with the pivot at index 1 catches it.&lt;/li&gt;
&lt;li&gt;Comparing &lt;code&gt;v[mid]&lt;/code&gt; with &lt;code&gt;v[lo]&lt;/code&gt; instead of &lt;code&gt;v[hi]&lt;/code&gt; without handling the unrotated list separately. On a sorted list this picks the wrong half.&lt;/li&gt;
&lt;li&gt;Passing sublists such as &lt;code&gt;v[:mid]&lt;/code&gt; to the recursive calls. This breaks the logarithmic cost and returns indices relative to the sublist rather than to the original list.&lt;/li&gt;
&lt;li&gt;Calling &lt;code&gt;find_pivot(v, 0, -1)&lt;/code&gt; on an empty list. &lt;code&gt;v[0]&lt;/code&gt; raises &lt;code&gt;IndexError&lt;/code&gt; and the program prints nothing.&lt;/li&gt;
&lt;li&gt;Forgetting the &lt;code&gt;k == 0&lt;/code&gt; case in &lt;code&gt;locate&lt;/code&gt;. In the condition &lt;code&gt;v[0] &amp;lt;= x &amp;lt;= v[k - 1]&lt;/code&gt;, &lt;code&gt;v[-1]&lt;/code&gt; is then the last element, so the program searches an empty or wrong range.&lt;/li&gt;
&lt;li&gt;Reading line by line with &lt;code&gt;input().split()&lt;/code&gt; and assuming the second line always holds numbers. When n = 0, the empty line shifts the reading unless it is handled explicitly.&lt;/li&gt;
&lt;/ul&gt;
&lt;br&gt;&lt;br&gt;
&lt;h2&gt;Variants&lt;/h2&gt;
&lt;h3&gt;Turn the recursive functions into iterative ones&lt;/h3&gt;
&lt;p&gt;Both functions are &lt;em&gt;tail-recursive&lt;/em&gt;: the recursive call is the last action, and its result is returned unchanged. They therefore convert mechanically into a &lt;code&gt;while&lt;/code&gt; loop that updates &lt;code&gt;lo&lt;/code&gt; and &lt;code&gt;hi&lt;/code&gt;. In &lt;code&gt;find_pivot&lt;/code&gt;, the loop condition is the negation of the base case. The time cost stays the same, and the extra memory drops from &lt;em&gt;O(log n)&lt;/em&gt; to &lt;em&gt;O(1)&lt;/em&gt;. Python does not eliminate tail calls, so this version is the one to prefer in production code.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import sys

&lt;p&gt;def find_pivot(v):&lt;br&gt;
    lo, hi = 0, len(v) - 1&lt;br&gt;
    while v[lo] &amp;gt; v[hi]:&lt;br&gt;
        mid = (lo + hi) // 2&lt;br&gt;
        if v[mid] &amp;gt; v[hi]:&lt;br&gt;
            lo = mid + 1&lt;br&gt;
        else:&lt;br&gt;
            hi = mid&lt;br&gt;
    return lo&lt;/p&gt;

&lt;p&gt;def binary_search(v, x, lo, hi):&lt;br&gt;
    while lo &amp;lt;= hi:&lt;br&gt;
        mid = (lo + hi) // 2&lt;br&gt;
        if v[mid] == x:&lt;br&gt;
            return mid&lt;br&gt;
        if v[mid] &amp;lt; x:&lt;br&gt;
            lo = mid + 1&lt;br&gt;
        else:&lt;br&gt;
            hi = mid - 1&lt;br&gt;
    return -1&lt;/p&gt;

&lt;p&gt;def locate(v, x, k):&lt;br&gt;
    if not v:&lt;br&gt;
        return -1&lt;br&gt;
    if k &amp;gt; 0 and v[0] &amp;lt;= x &amp;lt;= v[k - 1]:&lt;br&gt;
        return binary_search(v, x, 0, k - 1)&lt;br&gt;
    return binary_search(v, x, k, len(v) - 1)&lt;/p&gt;

&lt;p&gt;def main():&lt;br&gt;
    data = sys.stdin.read().split()&lt;br&gt;
    n = int(data[0])&lt;br&gt;
    v = [int(tok) for tok in data[1:1 + n]]&lt;br&gt;
    q = int(data[1 + n])&lt;br&gt;
    queries = [int(tok) for tok in data[2 + n:2 + n + q]]&lt;br&gt;
    k = find_pivot(v) if n &amp;gt; 0 else -1&lt;br&gt;
    out = ['PIVOT ' + str(k)]&lt;br&gt;
    found = 0&lt;br&gt;
    for x in queries:&lt;br&gt;
        i = locate(v, x, k)&lt;br&gt;
        if i != -1:&lt;br&gt;
            found += 1&lt;br&gt;
        out.append(f'{x} {i}')&lt;br&gt;
    out.append(f'FOUND {found}')&lt;br&gt;
    print('\n'.join(out))&lt;/p&gt;

&lt;/code&gt;&lt;p&gt;&lt;code&gt;if &lt;strong&gt;name&lt;/strong&gt; == '&lt;strong&gt;main&lt;/strong&gt;':&lt;br&gt;&lt;br&gt;
    main()&lt;/code&gt;&lt;/p&gt;&lt;/pre&gt;
&lt;h3&gt;A single recursive search without computing the pivot&lt;/h3&gt;
&lt;p&gt;You can also search directly. In every call, at least one of the halves &lt;code&gt;[lo, mid]&lt;/code&gt; or &lt;code&gt;[mid, hi]&lt;/code&gt; is sorted, and comparing &lt;code&gt;v[lo]&lt;/code&gt; with &lt;code&gt;v[mid]&lt;/code&gt; tells you which one. If &lt;code&gt;x&lt;/code&gt; falls within the range of the sorted half, recurse into it. Otherwise recurse into the other half. This is more compact, but it has more branches where a &lt;code&gt;&amp;lt;&lt;/code&gt; can be confused with a &lt;code&gt;&amp;lt;=&lt;/code&gt;. The statement also asks you to print the pivot, so you would have to compute it anyway.&lt;/p&gt;


&lt;p&gt;What the professor will ask&lt;/p&gt;
&lt;p&gt;“Suppose the list may now contain repeated values, for example 2 2 2 1 2. Does your find_pivot still run in logarithmic time?”&lt;/p&gt;
&lt;p&gt;No. With duplicates you can have v[mid] == v[hi] and also v[lo] == v[hi], and those comparisons do not reveal which half holds the minimum. In 2 2 2 1 2 the minimum is to the right of mid, and in 2 1 2 2 2 it is to the left, with exactly the same values at both ends and in the middle. The base case v[lo] &amp;lt;= v[hi] also stops being valid: in 2 2 2 1 2 it holds and the function would return lo, which is not the minimum. The usual fix is to drop one end with hi - 1 on a tie. That keeps the answer correct but degrades the worst case to O(n), because a list where all values but one are equal forces you to inspect almost all of them. That is why the statement requires distinct values.&lt;/p&gt;


&lt;h2&gt;On the syllabus&lt;/h2&gt;
&lt;p&gt;Searching is a core topic in algorithms courses on both coasts of the US. The syllabus of &lt;a href="https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-spring-2020/pages/syllabus/" rel="noopener noreferrer"&gt;MIT 6.006 Introduction to Algorithms&lt;/a&gt; lists sorting, searching data structures and algorithmic paradigms among its contents. The course page of &lt;a href="https://web.stanford.edu/class/cs161/" rel="noopener noreferrer"&gt;Stanford CS161 Design and Analysis of Algorithms&lt;/a&gt; mentions its focus on algorithms for sorting and searching. &lt;a href="https://www.cs.cmu.edu/~15210/" rel="noopener noreferrer"&gt;Carnegie Mellon 15-210&lt;/a&gt; stresses defining a problem cleanly and analysing its cost in terms of work, which is what the complexity section of this exercise asks for.&lt;/p&gt;
&lt;p&gt;Outside the US, recursion has its own unit in the syllabus of &lt;a href="https://www.fciencias.unam.mx/sites/default/files/temario/1222.pdf" rel="noopener noreferrer"&gt;Estructuras de Datos at UNAM's Facultad de Ciencias&lt;/a&gt;, just before lists and binary search trees.&lt;/p&gt;


&lt;p&gt;Opinion · Missing context&lt;/p&gt;
&lt;p&gt;Missing context, from where I sit: this question is less about binary search than about boundaries. Most students know the textbook search by heart. What separates the marks is whether they kept mid or dropped it, and whether they tried the empty list before handing in.&lt;/p&gt;
&lt;p&gt;My advice is blunt. Before writing a line, list the four edge cases on paper and trace each recursive call by hand on two or three elements. That ten-minute habit is worth more than any memorised template.&lt;/p&gt;


&lt;h2&gt;Related articles from LaRebelión&lt;/h2&gt;
&lt;a href="https://www.larebelion.com/2026/11/solved-exercise-sorting-and-binary.html" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Flarebelion-img%2Fmedia%2F3d3c3c77d87996647f38b13a9013bd3e701cc52bb4841176cdd466a6622e579a.png" alt="Solved exercise: sorting and binary search with cost analysis in Python" width="800" height="420"&gt;&lt;span&gt;Solved exercise: sorting and binary search with cost analysis in Python&lt;/span&gt;&lt;/a&gt;&lt;a href="https://www.larebelion.com/2026/11/solved-exercise-streaks-and-maximum-in.html" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Flarebelion-img%2Fmedia%2F109c919bf23fef7a25b6db20d767c39f7cc07cf56c99ab242742d1d9a00d8cb9.png" alt="Solved exercise: streaks and maximum in a list in Python" width="800" height="420"&gt;&lt;span&gt;Solved exercise: streaks and maximum in a list in Python&lt;/span&gt;&lt;/a&gt;&lt;a href="https://www.larebelion.com/2026/11/solved-exercise-stack-and-recursive.html" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Flarebelion-img%2Fmedia%2F121efb1e347ad8b0c4e842d12851468f68741b9ab47beaa438e84076ef68baf1.png" alt="Solved exercise: stack and recursive expression evaluator in Python" width="800" height="420"&gt;&lt;span&gt;Solved exercise: stack and recursive expression evaluator in Python&lt;/span&gt;&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.larebelion.com/p/exercises.html" rel="noopener noreferrer"&gt;Programming exercises&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.larebelion.com/2026/11/solved-exercise-threads-that-split.html" rel="noopener noreferrer"&gt;Solved exercise: threads that split a calculation in Python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.larebelion.com/2026/11/solved-exercise-dictionaries-sets-and.html" rel="noopener noreferrer"&gt;Solved exercise: dictionaries, sets and tuples in Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Article generated with AI.larebelion&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Flarebelion-img%2Fmedia%2Feditors%2Fbyline-ae73d4a13364.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstorage.googleapis.com%2Flarebelion-img%2Fmedia%2Feditors%2Fbyline-ae73d4a13364.svg" alt="Byline" width="120" height="120"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://www.larebelion.com/p/newsroom.html#byline" rel="author noopener noreferrer"&gt;Byline&lt;/a&gt; · &lt;span&gt;Chief editor · English edition · London&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;“A logarithmic search that breaks on one element is not fast. It is a bug that shows up quickly.”&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>A model router for Claude Code, benchmarked: 82% cheaper, 2 tests lost</title>
      <dc:creator>Kernel</dc:creator>
      <pubDate>Sun, 11 Oct 2026 17:36:48 +0000</pubDate>
      <link>https://dev.to/kernel_dev/a-model-router-for-claude-code-benchmarked-82-cheaper-2-tests-lost-50m7</link>
      <guid>https://dev.to/kernel_dev/a-model-router-for-claude-code-benchmarked-82-cheaper-2-tests-lost-50m7</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://www.larebelion.com/2026/10/why-you-should-stop-using-one-model-for.html" rel="noopener noreferrer"&gt;larebelion.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most of us pick one model when a Claude Code session starts and let it do everything: plan a migration, write a test, summarise a file, touch up a README. Every one of those requests is billed at the price of that model, and on simple tasks the price rarely buys a better result.&lt;/p&gt;

&lt;p&gt;To put numbers on it, we built &lt;a href="https://gitlab.com/larebelion/rebel-router" rel="noopener noreferrer"&gt;Rebel Router&lt;/a&gt; (open source, MIT), a local proxy that picks a model per request, and ran it through a small benchmark of our own. This post covers how it works, how to set it up, and what it measured, including what it lost.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it is
&lt;/h2&gt;

&lt;p&gt;Rebel Router runs on your machine (&lt;code&gt;127.0.0.1:8787&lt;/code&gt;) between your coding agent and the model providers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Claude Code (Anthropic Messages) ─┐
                                  ├─► Rebel Router ─► Anthropic · OpenAI · xAI · Gemini · DeepSeek · Ollama
OpenCode (OpenAI Chat)  ──────────┘        │
                                           └─► SQLite ledger ─► dashboard · CSV · rebel spend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It has three parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Broker:&lt;/strong&gt; a catalogue of models with price, speed, context and a quality score per task class, which ranks the top 3 for each request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Router:&lt;/strong&gt; the classifier plus the proxy, translating between the Anthropic and OpenAI formats (streaming and tool calls included) when the chosen provider needs it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FinOps:&lt;/strong&gt; daily and per-project budgets, and a ledger of every request with its cost.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Providers: Anthropic, OpenAI, xAI (Grok), Google Gemini, DeepSeek and local Ollama models. Bring your own keys, or a single &lt;a href="https://opencode.ai/docs/zen" rel="noopener noreferrer"&gt;OpenCode Zen&lt;/a&gt; key that reaches GPT, Grok, DeepSeek, GLM, Qwen and Kimi through one endpoint.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it decides
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Classify.&lt;/strong&gt; Every request lands in one of six classes (&lt;code&gt;orchestration&lt;/code&gt;, &lt;code&gt;complex_code&lt;/code&gt;, &lt;code&gt;bulk_code&lt;/code&gt;, &lt;code&gt;unit_tests&lt;/code&gt;, &lt;code&gt;qa_automation&lt;/code&gt;, &lt;code&gt;documentation&lt;/code&gt;) with a complexity score from 0 to 1.

&lt;ul&gt;
&lt;li&gt;With a &lt;code&gt;VENICE_API_KEY&lt;/code&gt;, it asks Venice's JEV decision model (in beta). Any error, odd answer or response slower than 800 ms falls back to the rules, and results are cached per instruction so an agent's tool loop doesn't ask again every turn.&lt;/li&gt;
&lt;li&gt;Without it, local rules look at keywords (English and Spanish), the tools used in recent turns (&lt;code&gt;Task&lt;/code&gt;, &lt;code&gt;TodoWrite&lt;/code&gt;, &lt;code&gt;Edit&lt;/code&gt;…), file types (&lt;code&gt;.spec.ts&lt;/code&gt;, &lt;code&gt;.md&lt;/code&gt;…), context size and Claude Code's request type. Compaction and auxiliary calls go to the cheapest documentation model.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rank.&lt;/strong&gt; The Broker drops models below a quality floor for the class (raised by complexity or an &lt;code&gt;opus&lt;/code&gt; request, lowered by &lt;code&gt;haiku&lt;/code&gt;), then weighs quality, price (input:output blended 3:1) and speed with per-class weights. The top 3 become the chosen model plus two fallbacks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Budget.&lt;/strong&gt; At 80 % of a budget it prefers cheaper models; at 100 % it downgrades to the cheapest acceptable model or answers HTTP 402 (&lt;code&gt;onLimit: block&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Forward.&lt;/strong&gt; Claude-to-Claude requests pass through untouched apart from &lt;code&gt;model&lt;/code&gt; (headers such as &lt;code&gt;anthropic-beta&lt;/code&gt; and the SSE stream are relayed as-is). Everything else is translated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Record.&lt;/strong&gt; One ledger row per request: timestamp, project, class, provider, model, input/output/cache tokens, cost, latency, which classifier decided and why.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One thing to know: no Claude Code hook can change the session model. The UI keeps showing the model Claude Code asked for; the router's status line shows the one that actually answered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://gitlab.com/larebelion/rebel-router.git &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;rebel-router
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm &lt;span class="nb"&gt;link&lt;/span&gt;                 &lt;span class="c"&gt;# puts `rebel` on your PATH&lt;/span&gt;
&lt;span class="nb"&gt;cp&lt;/span&gt; .env.example .env                    &lt;span class="c"&gt;# fill in the providers you use&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; ./.env&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt; +a&lt;span class="p"&gt;;&lt;/span&gt; rebel start   &lt;span class="c"&gt;# router on http://127.0.0.1:8787&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Providers without a key are skipped; Ollama models are used only if they're pulled locally.&lt;/p&gt;

&lt;h3&gt;
  
  
  Claude Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rebel init claude &lt;span class="nt"&gt;--global&lt;/span&gt;     &lt;span class="c"&gt;# ~/.claude/settings.json: every project, CLI, claude -p, IDE extensions, Agent SDK&lt;/span&gt;
rebel init claude              &lt;span class="c"&gt;# or just this project (.claude/settings.local.json)&lt;/span&gt;
rebel init claude &lt;span class="nt"&gt;--uninstall&lt;/span&gt;  &lt;span class="c"&gt;# remove it again (add --global for the user-level install)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It backs up your settings and sets:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ANTHROPIC_BASE_URL=http://127.0.0.1:8787&lt;/code&gt;, so the main session, subagents and background calls all go through the router;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ANTHROPIC_CUSTOM_HEADERS=x-rebel-project: &amp;lt;name&amp;gt;&lt;/code&gt; for per-project budgets and rules;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CLAUDE_CODE_GATEWAY_HINT_HEADERS=1&lt;/code&gt;, so the router can tell a &lt;code&gt;main&lt;/code&gt; call from a &lt;code&gt;subagent&lt;/code&gt; or &lt;code&gt;compaction&lt;/code&gt; one;&lt;/li&gt;
&lt;li&gt;optional hooks that tag the task, a status line with the routed model and today's spend, and the &lt;code&gt;/rebel-spend&lt;/code&gt; and &lt;code&gt;/rebel-top&lt;/code&gt; slash commands.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Claude desktop app is the exception: it takes its gateway from Developer → Configure Third-Party Inference, not from settings.&lt;/p&gt;

&lt;h3&gt;
  
  
  OpenCode
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rebel init opencode   &lt;span class="c"&gt;# writes opencode.json (provider "rebel") and a small plugin&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then select &lt;code&gt;rebel/auto&lt;/code&gt;, or pin a class with &lt;code&gt;rebel/auto:unit_tests&lt;/code&gt;, &lt;code&gt;rebel/auto:documentation&lt;/code&gt; and so on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pin a class from your prompt
&lt;/h3&gt;

&lt;p&gt;Write &lt;code&gt;#rebel:tests&lt;/code&gt;, &lt;code&gt;#rebel:docs&lt;/code&gt;, &lt;code&gt;#rebel:qa&lt;/code&gt;, &lt;code&gt;#rebel:bulk&lt;/code&gt;, &lt;code&gt;#rebel:complex&lt;/code&gt; or &lt;code&gt;#rebel:plan&lt;/code&gt; anywhere in a prompt. The router uses that class for the session until your next prompt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuration
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;rebel.yaml&lt;/code&gt; in the project or in &lt;code&gt;~/.rebel-router/&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;routing&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;excludeProviders&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;xai&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;classModels&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;documentation&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;deepseek/deepseek-flash&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;aliases&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;opus&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;passthrough&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;haiku&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;hint&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;# keep Opus requests on Opus; route Haiku freely&lt;/span&gt;
&lt;span class="na"&gt;budgets&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;daily&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
  &lt;span class="na"&gt;projects&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;shop&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;3&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
  &lt;span class="na"&gt;onLimit&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;downgrade&lt;/span&gt;
&lt;span class="na"&gt;projects&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;secret-client&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;localOnly&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;true&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;            &lt;span class="c1"&gt;# this project never leaves the machine&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Watching the spend
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rebel status&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;router status, today's spend, budget state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rebel top --task tests&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;top 3 for a class, with reasons&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rebel spend --today --by model&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;spend by model, class, project or day (&lt;code&gt;--csv&lt;/code&gt;, &lt;code&gt;--json&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rebel export --days 30&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;CSV of the ledger&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rebel dashboard&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;local dashboard on :8788&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rebel classify "&amp;lt;prompt&amp;gt;"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;see how a prompt would be classified&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The benchmark
&lt;/h2&gt;

&lt;p&gt;12 real tasks, two per class, each scored deterministically: hidden tests, mutation testing, deliberately buggy servers and rubrics. No model grades another. Run on 11 October 2026 with 10 models, one attempt per task, 1.78 USD in total. &lt;a href="https://gitlab.com/larebelion/rebel-router/-/blob/main/bench/README.md" rel="noopener noreferrer"&gt;Tasks&lt;/a&gt; and &lt;a href="https://gitlab.com/larebelion/rebel-router/-/raw/main/bench/results/latest.json" rel="noopener noreferrer"&gt;raw results&lt;/a&gt; are public.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;Cost per task (USD)&lt;/th&gt;
&lt;th&gt;Pass rate&lt;/th&gt;
&lt;th&gt;Tasks run&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;gpt-6-luna&lt;/td&gt;
&lt;td&gt;0.0010&lt;/td&gt;
&lt;td&gt;92 %&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;gemini-3.1-flash-lite&lt;/td&gt;
&lt;td&gt;0.0012&lt;/td&gt;
&lt;td&gt;67 %&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;claude-haiku-5-5&lt;/td&gt;
&lt;td&gt;0.0023&lt;/td&gt;
&lt;td&gt;75 %&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;gemini-3.5-flash-lite&lt;/td&gt;
&lt;td&gt;0.0028&lt;/td&gt;
&lt;td&gt;83 %&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;deepseek-flash&lt;/td&gt;
&lt;td&gt;0.0051&lt;/td&gt;
&lt;td&gt;88 %&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;deepseek-v4-pro&lt;/td&gt;
&lt;td&gt;0.0097&lt;/td&gt;
&lt;td&gt;100 %&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;qwen3.8-max&lt;/td&gt;
&lt;td&gt;0.0137&lt;/td&gt;
&lt;td&gt;100 %&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;glm-5.3&lt;/td&gt;
&lt;td&gt;0.0198&lt;/td&gt;
&lt;td&gt;67 %&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;claude-sonnet-5-5&lt;/td&gt;
&lt;td&gt;0.0306&lt;/td&gt;
&lt;td&gt;83 %&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;claude-opus-5-5&lt;/td&gt;
&lt;td&gt;0.0799&lt;/td&gt;
&lt;td&gt;92 %&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Rows with fewer than 12 tasks are partial runs; don't read much into a 100 % on one or two tasks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Router vs. Opus for everything
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cost:&lt;/strong&gt; 0.17 USD with the router choosing, against 0.96 USD with Opus 5.5 on every task: &lt;strong&gt;82 % less&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Quality:&lt;/strong&gt; 9 of 12 passed (75 %) against 11 of 12 (92 %). The three misses were two unit-test tasks and one QA task the router sent to Claude Haiku 5.5, whose answers ran past the 8,000-token limit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Latency:&lt;/strong&gt; mean time per task dropped from 31.7 s to 19.5 s.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Classifier:&lt;/strong&gt; the rule classifier (no JEV) put 9 of 12 tasks in the right class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;GPT-6 Luna passed the same 11 of 12 as Opus at roughly 1/80 of the cost on this set. That doesn't make it an Opus replacement; it says many everyday tasks don't need the most expensive model.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;12 tasks, one attempt each. Per-class numbers are indicative, not a leaderboard.&lt;/li&gt;
&lt;li&gt;The savings cost two tasks Opus solved, both in testing and QA. Keep Opus wherever you ask for Opus (&lt;code&gt;aliases.opus: passthrough&lt;/code&gt;) or pin a model per class with &lt;code&gt;classModels&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Not covered: Grok, Gemini Pro, Ollama, and GPT models other than Luna. JEV was not measured; the benchmark used the rules.&lt;/li&gt;
&lt;li&gt;Anthropic doesn't support routing Claude Code to non-Claude models. It works, but Claude-only features are dropped in translation.&lt;/li&gt;
&lt;li&gt;JEV and Venice's Decisions API are in beta, so their request and response shape may change.&lt;/li&gt;
&lt;li&gt;Cost is list price: reported tokens times the official price in the catalogue.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  More
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Code, docs and the benchmark: &lt;a href="https://gitlab.com/larebelion/rebel-router" rel="noopener noreferrer"&gt;gitlab.com/larebelion/rebel-router&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.larebelion.com/2026/10/build-your-own-ai-model-router-with.html" rel="noopener noreferrer"&gt;Step-by-step lab&lt;/a&gt;: install, budgets, OpenCode, and checking which model answered and what it cost.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.larebelion.com/p/model-recommender.html" rel="noopener noreferrer"&gt;Model recommender&lt;/a&gt;: the same data, updated daily. Pick a task class and your limits, get the three models that fit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The number I'd watch is the 75 %. A router saving 82 % is only worth it if the tests it skips aren't the ones that would have warned you, so watch the ledger for the first few weeks before trusting it with your test suite.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>opensource</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
