Given two int values, a and b, return True if either one is 6. Or if their sum or difference is 6. Note: the function abs(num) computes the absolute value of a number.
时间: 2023-12-28 22:03:22 浏览: 24
以下是Python的代码实现:
```python
def love6(a, b):
if a == 6 or b == 6 or a + b == 6 or abs(a - b) == 6:
return True
else:
return False
```
首先判断a或b是否为6,如果是,则返回True。如果不是,再判断a和b的和或差是否为6的绝对值,如果是,则返回True,否则返回False。
相关问题
Given three ints, a b c, return True if one of b or c is "close" (differing from a by at most 1), while the other is "far", differing from both other values by 2 or more. Note: abs(num) computes the absolute value of a number.
Sure, here's a Python function that solves the problem:
```
def close_far(a, b, c):
if abs(a-b) <= 1:
if abs(a-c) >= 2 and abs(b-c) >= 2:
return True
elif abs(a-c) <= 1:
if abs(a-b) >= 2 and abs(c-b) >= 2:
return True
return False
```
You can call this function with three integers `a`, `b`, and `c`, and it will return `True` if one of `b` or `c` is "close" to `a` while the other is "far". Otherwise, it will return `False`.
For example:
```
>>> close_far(1, 2, 10)
True
>>> close_far(1, 2, 3)
False
>>> close_far(4, 1, 3)
True
```
JavaScript:ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits. If the function is passed a valid PIN string, return true, else return false
Certainly! Here's a JavaScript function that checks if a PIN string is valid according to the given conditions:
```javascript
function validatePIN(pin) {
if (pin.length === 4 || pin.length === 6) {
if (/^\d+$/.test(pin)) {
return true;
}
}
return false;
}
```
You can use this function by passing a PIN string to it. It will return `true` if the PIN is valid (contains exactly 4 or 6 digits only), and `false` otherwise.