给定一个浮点数,请编写一个函数,将其按照0.5为跨度进行舍入操作。如果小数位刚好是5结尾,那么则上浮。
示例:
输入:4.2,输出:4。因为在4和4.5之间,距离4较近。
输入:4.3,输出:4.5。
输入:4.75,输出:5。
题目难度:中等
题目来源:codewars: Round by 0.5 steps
def solution(num: float):
# your code
assert solution(4.2) == 4
assert solution(4.3) == 4.5
assert solution(4.6) == 4.5
assert solution(4.75) == 5
Mooii
2
def solution(num: float):
s = divmod(num,0.5)
num_float = s[1]
if num_float >= 0.25:
return (s[0]+1)*0.5
else:
return s[0]*0.5
2 个赞
nobugs
3
def solution(num: float):
return int(num) + (0 if (num - int(num)) < 0.25 else 0.5 if 0.75 > (num - int(num)) > 0.25 else 1)
assert solution(4.2) == 4
assert solution(4.3) == 4.5
assert solution(4.6) == 4.5
assert solution(4.75) == 5
1 个赞
def solution(num: float):
if (num-int(num)) < 0.25:
return (int(num))
elif (num-int(num)) >=0.25 and (num-int(num)) <0.75:
return (int(num)+0.5)
else:
return (int(num)+1)
if __name__ == '__main__':
assert solution(4.2) == 4
assert solution(4.3) == 4.5
assert solution(4.6) == 4.5
assert solution(4.75) == 5
lekaixin
(ALe阿乐)
5
def solution(num: float):
if 0 < num - int(num) < 0.25:
return int(num)
elif 0.25 <= num - int(num) < 0.75:
return int(num) + 0.5
elif 0.75 <= num - int(num) < 1:
return int(num) + 1
assert solution(4.2) == 4
assert solution(4.3) == 4.5
assert solution(4.6) == 4.5
assert solution(4.75) == 5
def solution(num: float):
# if 0 < num - int(num) < 0.25:
# return int(num)
# elif 0.25 <= num - int(num) < 0.75:
# return int(num) + 0.5
# elif 0.75 <= num - int(num) < 1:
# return int(num) + 1
return int(num) if 0<num-int(num)<0.25 else int(num)+0.5 if 0.25 <= num - int(num) < 0.75 else int(num)+1
assert solution(4.2) == 4
assert solution(4.3) == 4.5
assert solution(4.6) == 4.5
assert solution(4.75) == 5
def solution(num: float):
tmp = num
arr=str(tmp).split('.')
tmp1 = arr[0]
tmp1_target1 = float(tmp1 + '.25')
tmp1_target2 = float(tmp1 + '.5')
tmp1_target3 = float(tmp1 + '.75')
if num >= tmp1_target3:
return int(tmp1) + 1
elif tmp1_target3 > num >= tmp1_target1:
return tmp1_target2
else:
return int(tmp1)
assert solution(4.2) == 4
assert solution(4.3) == 4.5
assert solution(4.6) == 4.5
assert solution(4.75) == 5
def solution(num: float):
num_int = int(num)
if num >= num_int + 0.75:
return num_int + 1
elif num >= num_int + 0.25:
return num_int + 0.5
else:
return num_int
assert solution(4.2) == 4
assert solution(4.3) == 4.5
assert solution(4.6) == 4.5
assert solution(4.75) == 5
Huis
8
def solution(num: float):
dor = 0.5 if num%1%0.5/0.5 >= 0.5 else 0
return num//1 + num%1//0.5*0.5 + dor
def solution(num: float):
if num - int(num) < 0.25:
return int(num)
elif num - int(num) >=0.25 and num - int(num) <0.75:
return int(num)+0.5
else:
return int(num)+1
assert solution(4.2) == 4
assert solution(4.3) == 4.5
assert solution(4.6) == 4.5
assert solution(4.75) == 5
import math
def solution(num):
result = {}
key_list = []
min_num = math.floor(num)
max_num = min_num + 1
middle_num = (min_num + max_num)/2
result[min_num] = abs(min_num-num)
result[max_num] = abs(max_num-num)
result[middle_num] = abs(middle_num-num)
# print(result)
min_num_value = min(abs(min_num - num), abs(max_num - num), abs(middle_num - num))
for key, value in result.items():
if min_num_value == value:
key_list.append(key)
if num>middle_num:
return max(key_list)
else:
return min(key_list)
assert solution(4.2) == 4
assert solution(4.3) == 4.5
assert solution(4.6) == 4.5
assert solution(4.75) == 5
assert solution(4.1333) == 4
def solution(num: float):
nums = str(num).split('.')
num1 = int(nums[0])
num2 = nums[1]
if int(num2) % 5 == 0:
num1 += 0.5
if -3 < int(num2[0]) - 5 < 3:
num1 += 0.5
elif int(num2[0]) - 5 > 2:
num1 += 1
return num1
Adebugger
(Adebugger)
14
def solution(num: float):
integer = int(num)
remain = num - int(num)
if remain < .25:
pass
elif remain < .75:
integer += .5
else:
integer += 1
# your code
return integer
assert solution(4.2) == 4
assert solution(4.3) == 4.5
assert solution(4.6) == 4.5
assert solution(4.75) == 5
def solution(num: float):
return 0.5 * (num // 0.5+1) if num % 0.5 >= 0.25 else 0.5 * (num // 0.5)
1 个赞
记录
def solution(num: float):
num = round(num, 1)
a = num % 1
if(a<0.25):
num = int(num)
elif(a>=0.25 and a<0.75):
num = int(num) + 0.5
else:
num = int(num) + 1
return num
assert solution(4.2) == 4
assert solution(4.3) == 4.5
assert solution(4.6) == 4.5
assert solution(4.75) == 5