Участник:Alvant/TaskImplementRand10UsingRand7

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/implement-rand10-using-rand7

# The rand7() API is already defined for you.
#
# def rand7() -> int:
# """returns a random integer in the range 1 to 7"""
 
class Solution:
    def rand10(self) -> int:
        number = (rand7() - 1) / 2 * 3 + 1
        return_without_round = False
 
        if number - int(number) < 1e-7:
            return_without_round = True
 
        what_to_do_next_indicator_value = rand7()
        neutral_value = 4
 
        while what_to_do_next_indicator_value == neutral_value:
            what_to_do_next_indicator_value = rand7()
 
        if return_without_round and what_to_do_next_indicator_value < neutral_value:
            return int(number)
        elif return_without_round and what_to_do_next_indicator_value > neutral_value:
            return self.rand10()
 
        round_to_floor = what_to_do_next_indicator_value < neutral_value
 
        if round_to_floor:
            return int(number)
        else:
            return int(number) + 1