首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >合并两个排序线性图

合并两个排序线性图
EN

Code Review用户
提问于 2019-03-12 21:22:11
回答 1查看 67关注 0票数 0

我有一个函数的代码来计算,增加两个排序列表。列表中的每一个元素都是元组类型(xi,音译),它代表着坐标系统上的图形个体(x,y)点。

两个输入列表按x值和A的长度排序,B列表可能是不同的 (Ax0,Ay0),.,(Axi,Ayi),(Axi+1,Ayi+1),.(Axn,Ayn) (Bx0,By0),.,(Bxi,Byi),(Bxi+1,Byi+1),.(Bxk,Byk)

代码语言:javascript
运行
复制
example 1:
A:[(1, 1]]=> one point: 1, at x coordinate, 0 at y coordinate
B:[(0, 0]]=> one point: 0, at x coordinate, 0 at y coordinate
=> graph_addition(A, B) == [(0,0), (1,1)] 
a line with two point (0,0) and (1,1)

example 2:
A:[(3, 3), (5,5)]=> one line at (3,3), (5,5)
B:[(4, 4), (5, 5), (10, 8)]=> 2 segment line: 
ex: graph_addition(A, B) = [(3, 3), (4, 8.0), (5, 10)] 
3 segment line with 3 point [(3, 3), (4, 8.0), (5, 10), (10, 8)]
For A when x at 4, y should be 4 based on slope calculation

example 3:
A:[(3,3), (5,5), (6,3), (7,5)]
B:[(0,0), (2,2), (4,3), (5,3), (10,2)]
explanation
when x = 0 at B line, y=0, at A line y = 0 => (0, 0)
when x = 2 at B line, y=2, at A line y = 0 => (0, 0)
when x = 4 at B line, y=3, at A line y = (5-3 / 5-3 * 4) => (4, 7)
when x = 5 at B line, y=3, at A line y = 5 => (5, 8)
when x = 6 at B line, y= (2 - 3)/(10-5) * 6, at A line y = 3 => (6, 0)
when x = 7 at B line, y= (2 - 3)/(10-5) * 7, at A line y = 5 => (7, 1.5)
when x = 10 at B line, y=3, at A line y = (5-3 / 5-3 * 4) => (4, 7)
=> [(0, 0), (2, 2), (3, 5.5), (4, 7.0), (5, 8), (6, 5.8), (7, 7.6), (10, 2)]

有没有更好的方法不使用蛮力法来计算x,y值?

代码语言:javascript
运行
复制
def graph_addition(A, B):
     if not A or not B: return A or B
     res = []
     i = j = 0
     while i < len(A) and j < len(B):
         if A[i][0] < B[0][0]:
             x, y = A[i]
             i += 1
         elif B[j][0] < A[0][0]:
             x, y = B[j]
             j += 1
         elif A[i][0] < B[j][0]:
             x = A[i][0]
             y = (B[j][1] - B[j - 1][1]) / (B[j][0] - B[j - 1][0]) * (x - B[j - 1][0]) + B[j - 1][1] + A[i][1]
             i += 1
         elif A[i][0] > B[j][0]:
             x = B[j][0]
             y = (A[i][1] - A[i - 1][1]) / (A[i][0] - A[i - 1][0]) * (x - A[i - 1][0]) + A[i - 1][1] + B[j][1]
             j += 1
         else:
             x = A[i][0]
             y = A[i][1] + B[j][1]
             i += 1
             j += 1
         res.append((x, y))
     if A[i:]: res += A[i:]
     if B[j:]: res += B[j:]
     return res
EN

回答 1

Code Review用户

发布于 2019-03-13 15:32:40

  1. 使用yield而不是构建列表。没有必要有明确的清单。
  2. 在方法开始时,使用or输入和[]进行更干净的None检查。
  3. 将插值移到函数中。这样可以避免重复的代码。
  4. 将所有的案例都移动到插值函数中。
代码语言:javascript
运行
复制
def graph_value(L, i, x):
    if x == L[i][0]: return L[i][1]
    if i == 0: return 0
    m = (L[i][1] - L[i - 1][1]) / (L[i][0] - L[i - 1][0])
    return m * (x - L[i - 1][0]) + L[i - 1][1]

def graph_addition(A, B):
    A = A or []
    B = B or []
    i = j = 0
    while i < len(A) and j < len(B):
        x = min(A[i][0], B[j][0])
        y = graph_value(A, i, x) + graph_value(B, j, x)
        i += (x == A[i][0])
        j += (x == B[j][0])
        yield (x,y)
    yield from A[i:]
    yield from B[j:]

对读者的练习:编写一个包含两个任意迭代并执行相同功能的版本。

票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/215299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档