已知一个手串是由红色和蓝色珠子搭配构成的,并且每两个蓝色珠子之间夹着连续的2个红色珠子。
请编写一个函数,输入一个蓝色珠子的数量num,请求出红色珠子的个数。如果输入的数字小于2则返回0。
示例:
输入:1,输出:0。
输入:5,输出:8。
题目难度:简单
题目来源:CodeWars:Simple beads count
def solution(num: int) -> int:
# your code
assert solution(1) == 0
assert solution(3) == 4
assert solution(5) == 8
def solution(num: int) -> int:
# your code
if num < 2:
return 0
red = (num-1) * 2
return red
assert solution(1) == 0
assert solution(3) == 4
assert solution(5) == 8
1 个赞
def solution(num: int) -> int:
if num < 2:
return 0
return (num - 1) * 2
1 个赞
Kawi
4
def solution(num: int) -> int:
return 0 if num < 2 else (num-1)*2
def solution0110(num: int) -> int:
# 已知一个手串是由红色和蓝色珠子搭配构成的,并且每两个蓝色珠子之间夹着连续的2个红色珠子
# 请编写一个函数,输入一个蓝色珠子的数量num,请求出红色珠子的个数。如果输入的数字小于2则返回0
return (num-1)*2 if num >=2 else 0
def solution16(num:int):
if num<2:
return 0
else:
return (num-1)*2
assert solution16(1) == 0
assert solution16(3) == 4
assert solution16(5) == 8
def solution(num: int) -> int:
return (num - 1) * 2 if num > 1 else 0
Huis
8
def solution(num: int) -> int:
return 0 if num<2 else (num-1)*2
def solution(num: int) → int:
return (num-1)*2
def solution(num: int) -> int:
return 2*(num-1) if num >1 else 0
assert solution(1) == 0
assert solution(3) == 4
assert solution(5) == 8
def solution(num: int) -> int:
return 0 if num < 2 else (num - 1) * 2
assert solution(1) == 0
assert solution(3) == 4
assert solution(5) == 8
nobugs
12
def solution(num: int) -> int:
return (num - 1) * 2 if num > 0 else 0
assert solution(1) == 0
assert solution(3) == 4
assert solution(5) == 8
lekaixin
(ALe阿乐)
13
def solution(num: int) -> int:
return 0 if num<2 else (num-1)*2
assert solution(1) == 0
assert solution(3) == 4
assert solution(5) == 8