Given a non-negative number "num", return True if num is within 2 of a multiple of 10.
时间: 2023-06-27 20:03:33 浏览: 50
你需要一个函数来实现这个功能,可以参考下面的代码实现:
```python
def within_two_of_ten(num):
return (num % 10 <= 2) or (num % 10 >= 8)
```
该函数首先通过 `num % 10` 取余数,得到 `num` 除以 10 的余数。如果余数小于等于 2 或者大于等于 8(即在 10 的前两个或后两个数之内),则返回 True,否则返回 False。这就是判断一个数是否离 10 的倍数最近的两个数之内的逻辑。
相关问题
Given a non-empty array of integers, return the result of multiplying the values together in order
Sure, I can help you with that! Here's a Python function that takes in an array of integers and returns the result of multiplying the values together in order:
```python
def multiply_values(arr):
result = 1
for num in arr:
result *= num
return result
```
You can call this function with an array of integers like this:
```python
my_array = [2, 3, 4, 5]
print(multiply_values(my_array)) # Output: 120 (which is the result of 2 * 3 * 4 * 5)
```
Let me know if you have any other questions!
Given an array of ints, return True if the array contains a 2 next to a 2 somewhere.
可以使用一个循环遍历整个数组,检查每个元素是否为 2,如果是,则检查下一个元素是否也为 2。如果两个元素都是 2,则返回 True,否则继续循环。如果循环结束后仍未找到相邻的 2,则返回 False。
以下是 Python 代码实现:
```python
def has22(nums):
for i in range(len(nums)-1):
if nums[i] == 2 and nums[i+1] == 2:
return True
return False
```
示例输入和输出:
```python
>>> has22([1, 2, 2])
True
>>> has22([1, 2, 1, 2])
False
>>> has22([2, 2, 1, 2])
True
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)