Python function to merge two sorted linear graphTime optimization of counting shifts in insertion sortMerge...

Placing subfig vertically

Aliens englobed the Solar System: will we notice?

Do f-stop and exposure time perfectly cancel?

How much attack damage does the AC boost from a shield prevent on average?

How to pass a string to a command that expects a file?

Make a transparent 448*448 image

Should I tell my boss the work he did was worthless

Are the terms "stab" and "staccato" synonyms?

Replacing Windows 7 security updates with anti-virus?

Can Mathematica be used to create an Artistic 3D extrusion from a 2D image and wrap a line pattern around it?

Reverse string, can I make it faster?

What is the likely impact of grounding an entire aircraft series?

infinitive telling the purpose

How to clip a background including nodes according to an arbitrary shape?

Low budget alien movie about the Earth being cooked

How can The Temple of Elementary Evil reliably protect itself against kinetic bombardment?

Single word request: Harming the benefactor

Can a bounded number sequence be strictly ascending?

Why don't MCU characters ever seem to have language issues?

Why is there a voltage between the mains ground and my radiator?

Good for you! in Russian

Solving "Resistance between two nodes on a grid" problem in Mathematica

How do I deal with a powergamer in a game full of beginners in a school club?

How to edit the properties of a page programmatically?



Python function to merge two sorted linear graph


Time optimization of counting shifts in insertion sortMerge two list and discarding duplicatesSimple Graph in PythonPython Merge Sort ImplementationLinear Regression Model with Python 3Python Merge sort count inversionsMerge n sorted iteratorsMerge Sort efficiency in PythonUnivariate linear regression from scratch in PythonNatural merge: mergesort that uses already sorted subarrays













0












$begingroup$


I have one code about
create a function to calculator 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)]


there is better way for me to not using blueforce way to calculate x, y value?



    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









$endgroup$

















    0












    $begingroup$


    I have one code about
    create a function to calculator 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)]


    there is better way for me to not using blueforce way to calculate x, y value?



        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









    $endgroup$















      0












      0








      0





      $begingroup$


      I have one code about
      create a function to calculator 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)]


      there is better way for me to not using blueforce way to calculate x, y value?



          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









      $endgroup$




      I have one code about
      create a function to calculator 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)]


      there is better way for me to not using blueforce way to calculate x, y value?



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





      share












      share










      share



      share










      asked 2 mins ago









      A.LeeA.Lee

      512




      512






















          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-graph%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-graph%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

          is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

          How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...