美文网首页
python马丁challenge14.Mediants

python马丁challenge14.Mediants

作者: 33jubi | 来源:发表于2019-04-25 09:37 被阅读0次

Let two distinct reduced positive fractions F1 = p1
q1 and F2 = p2
q2 be given, with the denominator
set to 1 in case the fraction is 0. The mediant of F1 and F2 is defined as p1+p2
q1+q2 ; it is also in reduced
form, and sits between F1 and F2. Let a reduced fraction F = p
q in (0, 1) be given. It can be shown
that starting with 01
and 11
, one can compute a finite number of mediants and eventually generate
F. More precisely, there exists n 2 N and a sequence of pairs of fractions (Fi 1, Fi 2)i�n such that:
• F0
1 = 01
and F0
2 = 11
;
1
• for all i < n, either Fi+1
1 or Fi+1
2 is the mediant of Fi 1 and Fi 2, with Fi+1
2 equal to Fi 2 or Fi+1
1
equal to Fi 1, respectively, depending on whether that mediant is strictly smaller or strictly
greater than F, respectively.
• F is the mediant of Fn 1 and Fn 2 .
Write a Python program mediants.py that defines a function mediants_to() that given as arguments
two strictly positive integers p and q with p < q and gcd(p, q) = 1, computes the sequence
(Fi 1, Fi 2)i�n previously defined. The function returns None but displays that sequence, one pair per
line, with the mediant of the pair in-between, and for all pairs except the last one, indicating with
the * character whether p
q is between the first member of the pair and the mediant, or between the
mediant and the second member of the pair. The numerators and denominators of all fractions are
aligned and displayed in a field of width equal to the maximum of the number of digits in p and
the number of digits in q. Five spaces, or two spaces, the * character and two spaces, precede and
follow the display of all mediants. Using the doctest module to test mediants_to(), the following
behaviour would then be observed:

思路:
除了耐心读懂题目规则之外
考察占位的表示
f'{a:b}' a占b个字符位a向右对齐
f'{a:<b}' a占b个字符位a向左对齐

#马丁解法
def mediants_to(p, q):
    f_start_left=0, 1
    f_start_right=1, 1
    f_end=min(p,q),max(p,q)
    width=max(len(str(p)),len(str(q)))
    mediant=f_start_left[0]+f_start_right[0],f_start_left[1]+f_start_right[1]
    while mediant!=f_end:
        right=''
        left=''
        if mediant[0]*f_end[1]<f_end[0]*mediant[1]:
            left=' '
            right='*'
            #f_start_left=mediant
        else:
            left='*'
            right=' '
            #f_start_right=mediant
        print(f'{f_start_left[0]:{width}}/{f_start_left[1]:<{width}}  {left}  {mediant[0]:{width}}/{mediant[1]:<{width}}  {right}  {f_start_right[0]:{width}}/{f_start_right[1]:<{width}}')
        
        if mediant[0]*f_end[1]<f_end[0]*mediant[1]:
            f_start_left=mediant
        else:
            f_start_right=mediant
        mediant=f_start_left[0]+f_start_right[0],f_start_left[1]+f_start_right[1]   
    if mediant[0]*f_end[1]<f_end[0]*mediant[1]:
        left=' '
        right=' '
    else:
        left=' '
        right=' '
    print(f'{f_start_left[0]:{width}}/{f_start_left[1]:<{width}}  {left}  {mediant[0]:{width}}/{mediant[1]:<{width}}  {right}  {f_start_right[0]:{width}}/{f_start_right[1]:<{width}}') 
    
    

相关文章

网友评论

      本文标题:python马丁challenge14.Mediants

      本文链接:https://www.haomeiwen.com/subject/umgogqtx.html