有没有办法让布尔函数的返回文本不在终端中显示?

huangapple go评论81阅读模式
英文:

Is there anyway to make the return text of a boolean function not show in the terminal

问题

我正在为学校做一个项目,需要使用二分查找来判断一个特定的数字是否存在于一串数字中。我对编程还很陌生,正在尝试找到一种方法,让"true"不在终端中显示出来。有没有办法做到这一点,还是我必须删除布尔函数?我知道不能从代码中删除"return true",因为这会导致它停止工作,但我希望输出只是我打印的代码,而不是"true"。

func BinarySearch(target int, input []int) bool {

	first := 0
	last := len(input) - 1

	for first <= last{

		median := (first + last) / 2

		if input[median] < target {
			first = median + 1
		}else{
			last = median - 1
		}

	}

	if first == len(input) || input[first] != target {
		fmt.Println("The searched integer", target, "was not found")
	} else {
        fmt.Println("The searched integer", target, "was found")
    }

    return true

}

我已经输入了我想要打印的文本,但不知道该如何处理返回值。

英文:

Im doing a project for school where I have to use binary search to figure out if a specific number is present in a string of numbers. I am new to coding and am trying to figure out a way to have "true" not show up in the terminal. Is there a way to do this or do I have to remove the boolean function. I know I cannot remove the "return true" from the code because it stops it from working but I want the output to just be the code I am printing not the "true"

func BinarySearch(target int, input []int) bool {

	first := 0
	last := len(input) - 1

	for first &lt;= last{

		median := (first + last) / 2

		if input[median] &lt; target {
			first = median + 1
		}else{
			last = median - 1
		}

	}

	if first == len(input) || input[first] != target {
		fmt.Println(&quot;The searched integer&quot;, target, &quot;was not found&quot;)
	} else {
        fmt.Println(&quot;The searched integer&quot;, target, &quot;was found&quot;)
    }

    return true

}

I have input the text I want to print but do not know what to do about the return

答案1

得分: 1

你可以通过在函数参数后面删除bool关键字来实现这一点,因为基本上你是在告诉函数必须返回一个布尔值,而你将要将其用作void。在将输入传递给函数之前,不要忘记对其进行预排序。祝你好运。

func BinarySearch(target int, input []int) {

    first := 0
    last := len(input) - 1

    for first <= last{

        median := (first + last) / 2

        if input[median] < target {
            first = median + 1
        }else{
            last = median - 1
        }

    }

    if first == len(input) || input[first] != target {
        fmt.Println("要查找的整数", target, "未找到")
    } else {
        fmt.Println("要查找的整数", target, "已找到")
    }
}
英文:

You can just do that by removing the bool after the params of the function, because basically you were telling that the function must return a boolean, while you were going to use it as a void. Dont forget also to pre-sort your input prior to passing it to your function. Best of luck

func BinarySearch(target int, input []int) {

    first := 0
    last := len(input) - 1

    for first &lt;= last{

        median := (first + last) / 2

        if input[median] &lt; target {
            first = median + 1
        }else{
            last = median - 1
        }

    }

    if first == len(input) || input[first] != target {
        fmt.Println(&quot;The searched integer&quot;, target, &quot;was not found&quot;)
    } else {
        fmt.Println(&quot;The searched integer&quot;, target, &quot;was found&quot;)
    }
}

答案2

得分: 0

我可能会这样做:

package main

import (
	"fmt"
)

func main() {
	orderedList := []int{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25}

	for i := 0; i < 27; i++ {
		var slug string
		if found := BinarySearch(i, orderedList); !found {
			slug = "不"
		}
		fmt.Printf("%d 在列表中%s存在\n", i, slug)
	}
}

func BinarySearch(target int, values []int) (found bool) {
	lo := 0
	hi := len(values)

	for hi > lo {
		mid := lo + (hi-lo)/2

		if values[mid] < target {
			lo = mid + 1
		} else {
			hi = mid
		}

	}

	found = lo < len(values) && target == values[lo]
	return found
}

这是一个使用二分查找算法的示例代码,它在一个有序列表中查找给定的目标值。代码中使用了一个名为BinarySearch的函数来执行二分查找操作,并在main函数中进行了测试。

英文:

I would probably do something like this:

https://goplay.tools/snippet/vBgmo4ASUh5

package main

import (
	&quot;fmt&quot;
)

func main() {
	orderedList := []int{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25}

	for i := 0; i &lt; 27; i++ {
		var slug string
		if found := BinarySearch(i, orderedList); !found {
			slug = &quot;not &quot;
		}
		fmt.Printf(&quot;%d is %s in the list\n&quot;, i, slug)
	}
}

func BinarySearch(target int, values []int) (found bool) {
	lo := 0
	hi := len(values)

	for hi &gt; lo {
		mid := lo + (hi-lo)/2

		if values[mid] &lt; target {
			lo = mid + 1
		} else {
			hi = mid
		}

	}

	found = lo &lt; len(values) &amp;&amp; target == values[lo]
	return found
}

huangapple
  • 本文由 发表于 2022年12月7日 19:17:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/74715761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定