Best Python code snippet using hypothesis
0166_fraction-to-recurring-decimal.py
Source:0166_fraction-to-recurring-decimal.py
...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__':...
166.py
Source:166.py
...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)'...
166_fractionToRecurringDecimal.py
Source:166_fractionToRecurringDecimal.py
...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...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!