Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

python - 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.

I'm using Set for this example

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. After that I'm putting that period in 'k' and print the number to the point + period of that number.

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are a few ways to do this. I am using the string manipulation to figure out if the expression is repeating.

  • Step 1: Find out the decimal place in the result. Grab all the values after the decimal place to process
  • Step 2: If there is no decimal place, the response is no decimals
  • Step 3: Iterate through the first half of the result string to find the pattern
  • Step 3a: If the left half can be found in the right half, then keep track of the left half. It may be the recurring period.
  • Step 3b: If the left half is NOT found in the right half, shift the left half by 1 to the left, and find that portion of the string towards the right. If found, keep track, if not found repeat Step 3b
  • Step 4: Once you have iterated through the full string and reached the 0th position, you will have the smallest sized string that's repeated. That's your period string
  • Step 5: After processing the string from mid point to 0th position, if you don't find anything, then there is no repeated period. Return the response based on whether you found the repeated period string or not

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...