How to use fraction_to_decimal method in hypothesis

Best Python code snippet using hypothesis

0166_fraction-to-recurring-decimal.py

Source:0166_fraction-to-recurring-decimal.py Github

copy

Full Screen

...22 - 有小数点的话,是否存在循环23"""24import unittest25class OfficialSolution:26 def fraction_to_decimal(self, numerator: int, denominator: int) -> str:27 # 特殊情况。28 if numerator == 0:29 return '0'30 # 存储正负号、整数部分、小数点、小数部分,用于最后返回拼接后的字符串。31 res = []32 # 若为 fasle,说明结果是负数;否则,结果为正数。33 if (numerator > 0) ^ (denominator > 0):34 res.append('-')35 # 取正数。36 numerator = abs(numerator)37 denominator = abs(denominator)38 # 添加整数部分。39 res.append(str(numerator // denominator))40 # 若余数为 0,说明没有小数点和小数,直接返回结果。41 remainder = numerator % denominator42 if remainder == 0:43 return ''.join(res)44 # 添加小数点。45 res.append('.')46 # 记录余数及位置,用于遇到循环时插入括号。47 d = {}48 # 余数不为零,说明可以继续除。49 while remainder != 0:50 # 存在已经出现过的余数,说明存在循环。51 if remainder in d:52 # 在之前出现的位置插入括号。53 res.insert(d[remainder], '(')54 res.append(')')55 break56 # 记录余数及位置。57 d[remainder] = len(res)58 # 余数加 0,继续除。59 remainder *= 1060 # 添加小数。61 res.append(str(remainder // denominator))62 # 更新余数。63 remainder = remainder % denominator64 return ''.join(res)65class TestOfficialSolution(unittest.TestCase):66 def setUp(self) -> None:67 self.s = OfficialSolution()68 def test_fraction_to_decimal(self) -> None:69 self.assertEqual(70 self.s.fraction_to_decimal(1, 2),71 '0.5',72 )73 self.assertEqual(74 self.s.fraction_to_decimal(2, 1),75 '2',76 )77 self.assertEqual(78 self.s.fraction_to_decimal(2, 3),79 '0.(6)',80 )81 self.assertEqual(82 self.s.fraction_to_decimal(-2147483648, -1),83 '2147483648',84 )85 self.assertEqual(86 self.s.fraction_to_decimal(-50, 8),87 '-6.25',88 )89if __name__ == '__main__':...

Full Screen

Full Screen

166.py

Source:166.py Github

copy

Full Screen

...24Python versions). When we find a repetition of both the previous remainder and the number of tens, we stop dividing as25we have entered a cycle and thus found the repeating part of the decimal.26Then we just have to iterate over the division results, appending digits and brackets to the result str as necessary.27"""28def fraction_to_decimal(numerator, denominator):29 if not numerator:30 return '0'31 result = '-' if (numerator < 0) != (denominator < 0) else ''32 numerator, denominator = abs(numerator), abs(denominator)33 quotient, remainder = divmod(numerator, denominator)34 result += str(quotient)35 if not remainder:36 return result37 result += '.'38 divisions = {}39 repeating = None40 while remainder:41 tens = -142 while remainder < denominator:43 remainder *= 1044 tens += 145 quotient, new_remainder = divmod(remainder, denominator)46 if (remainder, tens) in divisions:47 repeating = (remainder, tens)48 break49 divisions[remainder, tens] = quotient50 remainder = new_remainder51 for (remainder, tens), quotient in divisions.items():52 if (remainder, tens) == repeating:53 result += '('54 result += ('0' * tens) + str(quotient)55 if repeating:56 result += ')'57 return result58assert fraction_to_decimal(1, 2) == '0.5'59assert fraction_to_decimal(2, 1) == '2'60assert fraction_to_decimal(2, 3) == '0.(6)'61assert fraction_to_decimal(4, 333) == '0.(012)'62assert fraction_to_decimal(1, 5) == '0.2'63assert fraction_to_decimal(1, 7) == '0.(142857)'64assert fraction_to_decimal(10, 7) == '1.(428571)'65assert fraction_to_decimal(1, 90) == '0.01(1)'66assert fraction_to_decimal(1, 99) == '0.(01)'67assert fraction_to_decimal(-50, 8) == '-6.25'68assert fraction_to_decimal(0, -5) == '0'69assert fraction_to_decimal(1, 29) == '0.(0344827586206896551724137931)'...

Full Screen

Full Screen

166_fractionToRecurringDecimal.py

Source:166_fractionToRecurringDecimal.py Github

copy

Full Screen

...7 Given numerator = 1, denominator = 2, return "0.5".8 Given numerator = 2, denominator = 1, return "2".9 Given numerator = 2, denominator = 3, return "0.(6)".10"""11def fraction_to_decimal(numerator, denominator):12 n = numerator13 d = denominator14 sign = '-' if n * d < 0 else ''15 n = abs(n)16 d = abs(d)17 res = []18 res.append(sign)19 res.append(str(n / d))20 r = n % d # remainder21 if not r:22 return ''.join(res)23 24 res.append('.')25 seen = {}26 while not r in seen:27 seen[r] = len(res)28 res.append(str(10 * r / d))29 r = 10 * r % d30 idx = seen[r]31 res.insert(idx, '(')32 res.append(')')33 return ''.join(res).replace('(0)', '')34#print fraction_to_decimal(1, 2)35#print fraction_to_decimal(2, 3)36print fraction_to_decimal(1, 19)37def fractionToDecimal(self, numerator, denominator):38 n = numerator39 d = denominator40 if n % d == 0:41 return str(n//d)42 # Deal with negatives43 if (abs(n)/n) * (abs(d)/d) < 0:44 res = '-'45 n = abs(n)46 d = abs(d)47 else:48 res = ''49 # Integer part50 res = res + str(n//d) + '.'51 n = n % d52 # Start point of the "list"53 frem = n54 srem = n55 firstTime = True56 while frem != 0 and not (firstTime == False and frem == srem):57 firstTime = False58 srem = (srem * 10) % d59 frem = (frem * 10) % d60 if frem:61 frem = (frem * 10) % d62 # The fast pointer encounters a remainder of 0, so no cycle in the "list"63 if frem == 0:64 res += str((n * 10) // d)65 rem = (n * 10) % d66 while rem:67 res += str((rem * 10) // d)68 rem = (rem * 10) % d69 return res70 else:71 # Find the start point of the cycle, meanwhile, generate the non recurring part72 srem = n73 while frem != srem:74 res += str((srem * 10) // d)75 srem = (srem * 10) % d76 frem = (frem * 10) % d77 res += '('78 # Generate the recurring part79 firstTime = True80 while not (firstTime == False and srem == frem):81 firstTime = False82 res += str((srem * 10) // d)83 srem = (srem * 10) % d84 res += ')'85 return res86#def fraction_to_decimal(numerator, denominator):87# sign = '-' if numerator * denominator < 0 else ''88# numerator = abs(numerator)89# denominator = abs(denominator)90# if numerator == 0:91# return '0'92# elif numerator == denominator:93# return '1'94# elif numerator > denominator:95# return sign + str(numerator / denominator) + fraction_to_decimal(numerator % denominator)96# else:97# d = 298# while d <= min(int(numerator ** 0.5), int(denominator ** 0.5)):99# if numerator % d == denominator % d == 0:100# numerator /= d101# denominator /= d102# if is_prime(denominator):103# pass104# else:105# return str(numerator / denominator)[1:]106#def is_prime(n):107# if n==2 or n==3: return True108# if n%2==0 or n<2: return False109# for i in range(3,int(n**0.5)+1,2): # only odd numbers...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful