# ff677c311e4ed43cda4a20b014ee33efab83e556034daa356aa252d7928af0b9 Elan 2.0.0-alpha4 valid

main
  variable fruit set to ["apple", "avocado", "banana", "blueberry", "cherry", "fig", "grape", "kiwi", "lemon", "lychee", "mango", "orange", "papaya", "peach", "pear", "pineapple", "plum", "raspberry", "strawberry", "watermelon"]
  variable done set to false
  while not done
    variable wanted set to input("What type of fruit do you want ('x' to exit)? ")
    if wanted.equals("x") then
      set done to true
    else
      variable result set to binarySearch(fruit, wanted)
      if result then
        call print($"\nWe can supply a {wanted}")
      else
        call print($"\nSorry, we cannot supply a {wanted}")
      end if
    end if
  end while
end main

function binarySearch(li as List<of String>, item as String) returns Boolean
  variable result set to false
  if li.length() > 0 then
    variable mid set to divAsInt(li.length(), 2)
    variable value set to li[mid]
    if item.equals(value) then
      set result to true
    elif item.isBefore(value) then
      set result to binarySearch(li.subList(0, mid), item)
    else
      set result to binarySearch(li.subList(mid + 1, li.length()), item)
    end if
  end if
  return result
end function

test test_binarySearch
  variable li1 set to ["lemon", "lime", "orange"]
  assert binarySearch(li1, "lemon") is true
  assert binarySearch(li1, "lime") is true
  assert binarySearch(li1, "orange") is true
  assert binarySearch(li1, "pear") is false
  variable li2 set to ["lemon", "orange"]
  assert binarySearch(li2, "lemon") is true
  assert binarySearch(li2, "orange") is true
  assert binarySearch(li2, "pear") is false
  variable li3 set to ["lemon"]
  assert binarySearch(li3, "lemon") is true
  assert binarySearch(li3, "lime") is false
  variable li4 set to new List<of String>()
  assert binarySearch(li4, "pear") is false
end test
