此函数如何计算? [英] How does this function calculate?

查看:43
本文介绍了此函数如何计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究CodeWars katas,我遇到了一个很酷的解决方案,有人提出来了.我的问题是我不知道它是如何工作的.我了解其中的一些内容,就像它通常所做的一样,但不了解具体细节.它会回来吗?它是如何进行计算的?有人可以向我解释一下,因为我真的学到了如何做.如果您知道其他任何资源,我也可以阅读或观看,这将是有帮助的.在Swift文档中我没有看到类似的东西.

I've been working through CodeWars katas and I came across a pretty cool solution that someone came up with. The problem I have is I don't understand how it works. I understand some of it like what it is generally doing but not detail specifics. Is it returning itself? How is it doing the calculation? Can someone explain this to me because I really what to learn how to do this. And if you know of any other resources I can read or watch that would be helpful. I didn't see anything like this in the Swift documentation.

    func findDigit(_ num: Int, _ nth: Int) -> Int {
           let positive = abs(num)

           guard nth > 0 else { return -1 }
           guard positive > 0 else { return 0 }
           guard nth > 1 else { return positive % 10 }

           return findDigit(positive / 10, nth - 1) }        

对于上下文:

说明:

函数findDigit将两个数字num和nth作为输入.它输出num的第n个数字(从右到左计数).

The function findDigit takes two numbers as input, num and nth. It outputs the nth digit of num (counting from right to left).

注意

如果num为负数,则忽略其符号并将其视为正值.如果nth不为正,则返回-1.请记住,42 =00042.这意味着findDigit(42,5)将返回0.

If num is negative, ignore its sign and treat it as a positive value. If nth is not positive, return -1. Keep in mind that 42 = 00042. This means that findDigit(42, 5) would return 0.

示例

findDigit(5673,4)返回5

findDigit(5673, 4) returns 5

findDigit(129,2)返回2

findDigit(129, 2) returns 2

findDigit(-2825,3)返回8

findDigit(-2825, 3) returns 8

findDigit(-456,4)返回0

findDigit(-456, 4) returns 0

findDigit(0,20)返回0

findDigit(0, 20) returns 0

findDigit(65,0)返回-1

findDigit(65, 0) returns -1

findDigit(24,-8)返回-1

findDigit(24, -8) returns -1

非常感谢您的帮助.谢谢.

Greatly appreciate any help. Thanks.

推荐答案

这是一个简单的递归函数.递归意味着它反复调用自己,直到满足结束递归的条件为止.如果从不满足条件,那么您将获得无限递归,这不是一件好事:)

This is a simple recursive function. Recursive means that it calls itself over and over until a condition is satisfied that ends the recursion. If the condition is never satisfied, you'll end up with an infinite recursion which is not a good thing :)

您已经了解了该功能的用途,下面是其内部工作方式的详细信息:

As you already understand the purpose of the function, here are the details of how it works internally:

// Saves the absolute value (removes the negative sign) of num
let positive = abs(num)

// Returns -1 if num is 0 or negative
guard nth > 0 else { return -1 } 

// Returns 0 if the absolute value of num is 0 (can't be negative)
guard positive > 0 else { return 0 } // Could be guard positive == 0

// nth is a counter that is decremented with every recursion.
// positive % 10 returns the remainder of positive / 10
// For example 23 % 10 = 3
// In this line it always returns a number from 0 - 9 IF nth <= 0
guard nth > 1 else { return positive % 10 }

// If none of the above conditions are true, calls itself using
// the current absolute value divided by 10, decreasing nth.
// nth serves to target a different digit in the original number
return findDigit(positive / 10, nth - 1) 

让我们逐步介绍一个示例:

Let's run through an example step by step:

findDigit(3454, 3)
num = 3454, positive = 3454, nth = 3
-> return findDigit(3454 / 10, 3 - 1)

num = 345, positive = 345, nth = 2 // 345, not 345.4: integer type
-> return findDigit(345 / 10, 2 - 1)

num = 35, positive = 35, nth = 1
-> return 35 % 10
-> return 5

这篇关于此函数如何计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆