小编T.B*_*.BJ的帖子

Go中数字(字符串类型)的高效反向索引

背景

TLDR(和简化版):给定一个 string s,其中s是任何正整数,反转顺序并对每个数字乘以其新索引 (+1) 求和。

例如,从“98765”返回的值将是:(1x5) + (2x6) + (3x7) + (4x8) + (5*9)= 115

我当前的工作解决方案可以在这里找到:Go Playground。我想知道是否有更好的方法来做到这一点,无论是可读性还是效率。例如,我决定使用count变量而不是使用iand ,len因为它看起来更清晰。我也不太熟悉int/string转换,但我假设strconv需要使用。

func reverseStringSum(s string) int {
    total := 0
    count := 1
    for i := len(s) - 1; i >= 0; i-- {
        char := string([]rune(s)[i])
        num, _ := strconv.Atoi(char)
        total += count * num
        count++
    }
    return total
}
Run Code Online (Sandbox Code Playgroud)

string integer go

2
推荐指数
1
解决办法
174
查看次数

Python中的割线方法解决f(x)= 0

在给定2个初始猜测,x0和x1?的情况下,如何使用Python中的割线方法求解方程f(x)= 0.

def secant(f,x0,x1,tol):
Run Code Online (Sandbox Code Playgroud)

我需要用它来找到二次方的解和x的更高因子,例如,对于给定的间隔,x ^ 3 -4x ^ 2 + 1 = 0.这只是在黑暗中拍摄,所以任何有用网站的链接也将受到赞赏!

python math methods

-4
推荐指数
1
解决办法
4951
查看次数

标签 统计

go ×1

integer ×1

math ×1

methods ×1

python ×1

string ×1