Python function to merge two sorted linear graphs












-1












$begingroup$


I have code for creating a function to calculator the addition of two sorted (lists) that each element represent x, y values.



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)]


Is there a better way for me to not using blueforce way to calculate x, y values?



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









share|improve this question











$endgroup$

















    -1












    $begingroup$


    I have code for creating a function to calculator the addition of two sorted (lists) that each element represent x, y values.



    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)]


    Is there a better way for me to not using blueforce way to calculate x, y values?



    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









    share|improve this question











    $endgroup$















      -1












      -1








      -1





      $begingroup$


      I have code for creating a function to calculator the addition of two sorted (lists) that each element represent x, y values.



      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)]


      Is there a better way for me to not using blueforce way to calculate x, y values?



      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









      share|improve this question











      $endgroup$




      I have code for creating a function to calculator the addition of two sorted (lists) that each element represent x, y values.



      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)]


      Is there a better way for me to not using blueforce way to calculate x, y values?



      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






      python python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 mins ago









      Jamal

      30.4k11121227




      30.4k11121227










      asked 5 hours ago









      A.LeeA.Lee

      492




      492






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215299%2fpython-function-to-merge-two-sorted-linear-graphs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215299%2fpython-function-to-merge-two-sorted-linear-graphs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin