Friedrich Ewald My Personal Website

Posts


  • Nearest binary search

    To find the exact element or the next element that is greater than the target, use the following code. This algorithm returns an invalid index if the element that is searched for is greater than the greatest element in the array. This needs to be manually checked with s(items, target) == len(items).

    def s(items, target):
      l = 0
      r = len(items)
      m = (l + r) // 2
    
      while l < r:
        if items[m] == target:
          return m
        elif items[m] <= target:
          l = m + 1
        elif items[m] > target:
          r = m
        m = (l + r) // 2
      return m
        
    
    l = [1,2,3,5,6,7,8,20,30,40,50]
    targets = [0, 3, 4, 11, 12, 20, 30, 38]
    for target in targets:
      idx = s(l, target)
      print(f"[{target:02}] Index: {idx} => {l[idx]}")
    If you want to find the element which is exactly the element or less than the element, change the return value to return m - 1 instead. In the smallest case this will return -1 which means that the element searched for is smaller than the smallest on in the list. The resulting code looks like this:
    def s(items, target):
      l = 0
      r = len(items)
      m = (l+r) // 2
    
      while l < r:
        if items[m] == target:
          return m
        elif items[m] <= target:
          l = m + 1
        elif items[m] > target:
          r = m
        m = (l+r) // 2
      return m - 1

  • Predictable hash function

    The hash method in python produces different results for the same object on each different Python run. This is meant as a security feature to prevent predictable hash values. For testing, this can be disabled by setting the environment variable PYTHONHASHSEED to 0. The hash value for a given key will then always be the same.

    PYTHONHASHSEED=0 python script.py

  • OpenAI API price reduction

    OpenAI just reduced their prices, starting September 1 2022 on midnight UTC. For the DaVinci model, the price is cut by around two thirds.

    Model Before On Sept 1
    Davinci $0.06 / 1k tokens $0.02 / 1k tokens
    Curie $0.006 / 1k tokens $0.002 / 1k tokens
    Babbage $0.0012 / 1k tokens $0.0005 / 1k tokens
    Ada $0.0008 / 1k tokens $0.0004 / 1k tokens
    Davinci Embeddings $0.6 / 1k tokens $0.2 / 1k tokens
    Curie Embeddings $0.06 / 1k tokens $0.02 / 1k tokens
    Babbage Embeddings $0.012 / 1k tokens $0.005 / 1k tokens
    Ada Embeddings $0.008 / 1k tokens $0.004 / 1k tokens
    This is a significant reduction. I am curious if that means that Dall-E 2 will follow this example in the near future.

  • Secure Sidekiq with BasicAuth

    I am using Sidekiq for background processing for my website Every Podcast. One of the background jobs is loading new episodes from known feeds. Because this relies on external resources, many things that are out of my control can go wrong. To isolate the individual fetch and potential errors, I automatically create a job for every single podcast update. This obviously leads to a lot of jobs. Sidekiq comes with a UI that allows me to monitor the job queue, find dead jobs and see the throughput.

    Continue reading

  • Visualizations by Aaron Koblin

    Beautiful visualizations and projects by Aaron Koblin. Through this I discovered the flight patterns, generated with the processing framework which is now on my list to try out. I meant to post this some time ago and recently found it in my “drafts” folder.

Page: 15 of 28