From 6ebc4fb6d10ea426fe72d32bcb57a9838829ea2b Mon Sep 17 00:00:00 2001 From: Katrina Owen Date: Tue, 3 Oct 2023 01:54:33 -0700 Subject: [PATCH] Tweak code example in binary-search approach (#2713) --- .../binary-search/.approaches/loop-with-switch/content.md | 6 +++--- .../binary-search/.approaches/loop-with-switch/snippet.txt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/exercises/practice/binary-search/.approaches/loop-with-switch/content.md b/exercises/practice/binary-search/.approaches/loop-with-switch/content.md index 62d0ee622..e028e013c 100644 --- a/exercises/practice/binary-search/.approaches/loop-with-switch/content.md +++ b/exercises/practice/binary-search/.approaches/loop-with-switch/content.md @@ -8,13 +8,13 @@ package binarysearch func SearchInts(list []int, key int) int { for left, right := 0, len(list); left != right; { mid := (left + right) / 2 - + switch { case list[mid] == key: return mid case key < list[mid]: right = mid - default: + case key > list[mid]: left = mid + 1 } } @@ -37,7 +37,7 @@ A [`switch` with no condition][switch-no-condition] is used to check the value o - If the value being searched for is less than the element at the index of the middle value, then `right` is set to the middle value so that the next iteration will look at lower numbers. -- Otherwise, the value being searched for must be greater than the element at the index of the middle value, so `left` is set to the middle value +- Otherwise, the value being searched is greater than the element at the index of the middle value, so `left` is set to the middle value plus one so that the next iteration will look for higher numbers. If `left` and `right` are changed during the iterations so that they equal other, then the value being searched for is not in the slice of `int`s. diff --git a/exercises/practice/binary-search/.approaches/loop-with-switch/snippet.txt b/exercises/practice/binary-search/.approaches/loop-with-switch/snippet.txt index 24878606f..e7d9623cb 100644 --- a/exercises/practice/binary-search/.approaches/loop-with-switch/snippet.txt +++ b/exercises/practice/binary-search/.approaches/loop-with-switch/snippet.txt @@ -3,6 +3,6 @@ case list[mid] == key: return mid case key < list[mid]: right = mid -default: +case key > list[mid]: left = mid + 1 }