[英]determining the period of a number when dividing
So I need to find period of a number when dividing, for example 1/7 = 0.142857142857142857... and print it like 0.(142857) so in brackets I need to print a period of that number.所以我需要在除法时找到一个数字的句点,例如 1/7 = 0.142857142857142857... 并像 0.(142857) 一样打印它,所以在括号中我需要打印一个该数字的句点。
I'm using Set for this example我在这个例子中使用 Set
class Set:
def __init__(self):
self.content= []
def __str__(self):
self.content.sort()
return '' + str(self.content)
def length(self):
return len(self.content)
def insert(self,x):
if x not in self.content:
self.content.append(x)
def erase(self,x):
for i in range(self.length()-1):
if self.content[i]==x:
del self.content[i]
def find(self,x):
if x in self.content:
return True
else:
return False
def items(self):
return self.content
Here I want to convert that eval input to a string because I want to find point while dividing using expression.index('.') and finding that period.在这里,我想将该 eval 输入转换为字符串,因为我想在使用 expression.index('.') 进行除法并找到该句点时找到点。 After that I'm putting that period in 'k' and print the number to the point + period of that number.
之后,我将该句点放在“k”中并将数字打印到该数字的点+句点。
if __name__=='__main__':
set1=Set()
expression=eval(input('enter an expression: '))
expression=str(expression)
point=expression.index('.')
for i in range(point+1,len(expression)):
set1.insert(expression[i])
k=''
for x in set1.items():
k+=x
print(f'{expression[:point+1]}({k})')
Problem here is that for example 7/9 = 0.77777777778 program will print 0.(78) like 78 is a period, but only 7 is a period here not 0.78787878787878...这里的问题是,例如 7/9 = 0.77777777778 程序将打印 0.(78) 就像 78 是一个句点,但这里只有 7 是一个句点而不是 0.78787878787878...
The problem is that eval()
returns a floating point representation of the decimal number.问题是
eval()
返回十进制数的浮点表示。 This representation is inexact as you see from your question.正如您从问题中看到的那样,这种表示是不准确的。 Instead, you need to come up with an algorithm to calculate the digits yourself.
相反,您需要想出一个算法来自己计算数字。 I suggest that you parse the integers from the entered fraction and perform the division yourself.
我建议您从输入的分数中解析整数并自己执行除法。
There are a few ways to do this.有几种方法可以做到这一点。 I am using the string manipulation to figure out if the expression is repeating.
我正在使用字符串操作来确定表达式是否重复。
no decimals
no decimals
位The code is as shown below:代码如下所示:
def find_period(e):
x = e.find('.')
if x == -1: return 'no decimals'
period = ''
dec_exp = e[x+1:]
for i in range(len(dec_exp)//2,0,-1):
if dec_exp[i:].find(dec_exp[:i]) == 0:
period = dec_exp[:i]
return f'0.({period})' if period else 'no decimals'
expression = eval(input('enter an expression: '))
result = str(expression)
print (find_period(result))
The output for some of the expressions are:一些表达式的 output 是:
enter an expression: 1/7
0.(142857)
enter an expression: 3/7
0.(428571)
enter an expression: 7/9
0.(7)
enter an expression: 3/14
no decimals
enter an expression: 22 + 5
no decimals
enter an expression: 8/2
no decimals
enter an expression: 6 * 2
no decimals
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.