Python number line cluster exercise Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Python sequence cluster exerciseCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How do I get the number of elements in a list in Python?How to read a file line-by-line into a list?Does Python have a string 'contains' substring method?Catch multiple exceptions in one line (except block)

Recursive calls to a function - why is the address of the parameter passed to it lowering with each call?

Unix AIX passing variable and arguments to expect and spawn

"Destructive force" carried by a B-52?

Does using the Inspiration rules for character defects encourage My Guy Syndrome?

Are Flameskulls resistant to magical piercing damage?

What is the evidence that custom checks in Northern Ireland are going to result in violence?

Who can become a wight?

When speaking, how do you change your mind mid-sentence?

Putting Ant-Man on house arrest

Why did Israel vote against lifting the American embargo on Cuba?

Weaponising the Grasp-at-a-Distance spell

Providing direct feedback to a product salesperson

What's the difference between using dependency injection with a container and using a service locator?

Why aren't these two solutions equivalent? Combinatorics problem

How can I introduce the names of fantasy creatures to the reader?

tabularx column has extra padding at right?

Why aren't road bike wheels tiny?

How to produce a PS1 prompt in bash or ksh93 similar to tcsh

How to make an animal which can only breed for a certain number of generations?

Who's this lady in the war room?

Lights are flickering on and off after accidentally bumping into light switch

/bin/ls sorts differently than just ls

How to mute a string and play another at the same time

Is Vivien of the Wilds + Wilderness Reclamation a competitive combo?



Python number line cluster exercise



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Python sequence cluster exerciseCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How do I get the number of elements in a list in Python?How to read a file line-by-line into a list?Does Python have a string 'contains' substring method?Catch multiple exceptions in one line (except block)



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am working through an exercise in my textbook (Ex 4.7) and am implementing the code in Python to practice dynamic programming. I am having some trouble actually executing Algorithm 4.8. I understand what is going on until I get to 'Otherwise range s from 1 to t-1 and set s to minimize f(s). Why is the book using s in the for loop as well as setting it to the function f(s)? How should one go about implementing that line in Python?



[current code at bottom]



enter image description here



enter image description here



enter image description here



My current code is this so far:



x = [1,2,5,6,10]
k = 3
n = 5

r = [[0 for x in range(k)] for x in range(n)]
c = [[0 for x in range(k)] for x in range(n)]

def Union(lst1, lst2):
final_list = lst1 + lst2
return final_list

for j in range(k):
for t in range(n):
if j == 0:
r[t][j] = (x[t]-x[0])/2
c[t][j] = [(x[t]+x[0])/2]
else:
for s in range(t-1):
f = max(r[s][j-1], (x[t]-x[s+1])/2)
#set s to minimize f??
r[t][j] = f
w = []
w.append((x[t]+x[s+1])/2)
if c[s][j-1] == 0:
c[t][j] = w
else:
c[t][j] = Union(c[s][j - 1], w)

print(r)
print(c)


Any help is much appreciated!










share|improve this question






























    1















    I am working through an exercise in my textbook (Ex 4.7) and am implementing the code in Python to practice dynamic programming. I am having some trouble actually executing Algorithm 4.8. I understand what is going on until I get to 'Otherwise range s from 1 to t-1 and set s to minimize f(s). Why is the book using s in the for loop as well as setting it to the function f(s)? How should one go about implementing that line in Python?



    [current code at bottom]



    enter image description here



    enter image description here



    enter image description here



    My current code is this so far:



    x = [1,2,5,6,10]
    k = 3
    n = 5

    r = [[0 for x in range(k)] for x in range(n)]
    c = [[0 for x in range(k)] for x in range(n)]

    def Union(lst1, lst2):
    final_list = lst1 + lst2
    return final_list

    for j in range(k):
    for t in range(n):
    if j == 0:
    r[t][j] = (x[t]-x[0])/2
    c[t][j] = [(x[t]+x[0])/2]
    else:
    for s in range(t-1):
    f = max(r[s][j-1], (x[t]-x[s+1])/2)
    #set s to minimize f??
    r[t][j] = f
    w = []
    w.append((x[t]+x[s+1])/2)
    if c[s][j-1] == 0:
    c[t][j] = w
    else:
    c[t][j] = Union(c[s][j - 1], w)

    print(r)
    print(c)


    Any help is much appreciated!










    share|improve this question


























      1












      1








      1


      0






      I am working through an exercise in my textbook (Ex 4.7) and am implementing the code in Python to practice dynamic programming. I am having some trouble actually executing Algorithm 4.8. I understand what is going on until I get to 'Otherwise range s from 1 to t-1 and set s to minimize f(s). Why is the book using s in the for loop as well as setting it to the function f(s)? How should one go about implementing that line in Python?



      [current code at bottom]



      enter image description here



      enter image description here



      enter image description here



      My current code is this so far:



      x = [1,2,5,6,10]
      k = 3
      n = 5

      r = [[0 for x in range(k)] for x in range(n)]
      c = [[0 for x in range(k)] for x in range(n)]

      def Union(lst1, lst2):
      final_list = lst1 + lst2
      return final_list

      for j in range(k):
      for t in range(n):
      if j == 0:
      r[t][j] = (x[t]-x[0])/2
      c[t][j] = [(x[t]+x[0])/2]
      else:
      for s in range(t-1):
      f = max(r[s][j-1], (x[t]-x[s+1])/2)
      #set s to minimize f??
      r[t][j] = f
      w = []
      w.append((x[t]+x[s+1])/2)
      if c[s][j-1] == 0:
      c[t][j] = w
      else:
      c[t][j] = Union(c[s][j - 1], w)

      print(r)
      print(c)


      Any help is much appreciated!










      share|improve this question
















      I am working through an exercise in my textbook (Ex 4.7) and am implementing the code in Python to practice dynamic programming. I am having some trouble actually executing Algorithm 4.8. I understand what is going on until I get to 'Otherwise range s from 1 to t-1 and set s to minimize f(s). Why is the book using s in the for loop as well as setting it to the function f(s)? How should one go about implementing that line in Python?



      [current code at bottom]



      enter image description here



      enter image description here



      enter image description here



      My current code is this so far:



      x = [1,2,5,6,10]
      k = 3
      n = 5

      r = [[0 for x in range(k)] for x in range(n)]
      c = [[0 for x in range(k)] for x in range(n)]

      def Union(lst1, lst2):
      final_list = lst1 + lst2
      return final_list

      for j in range(k):
      for t in range(n):
      if j == 0:
      r[t][j] = (x[t]-x[0])/2
      c[t][j] = [(x[t]+x[0])/2]
      else:
      for s in range(t-1):
      f = max(r[s][j-1], (x[t]-x[s+1])/2)
      #set s to minimize f??
      r[t][j] = f
      w = []
      w.append((x[t]+x[s+1])/2)
      if c[s][j-1] == 0:
      c[t][j] = w
      else:
      c[t][j] = Union(c[s][j - 1], w)

      print(r)
      print(c)


      Any help is much appreciated!







      python algorithm cluster-analysis dynamic-programming






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 9 at 3:26

























      asked Mar 9 at 2:43







      user10322040





























          1 Answer
          1






          active

          oldest

          votes


















          1














          The algorithm is very good. My code is as follow.



          x = [1,2,5,6,10]
          k = 3
          n = 5

          r = [[[] for _ in range(k)] for _ in range(n)]
          c = [[[] for _ in range(k)] for _ in range(n)]


          def f(s, j_down, t):
          return max(r[s][j_down], (x[t]-x[s+1])/2.)

          def get_min_f_and_s(j_down, t):
          """ range s from 1 to t-1 and set s to minimize f(s)
          for example t=5 and j=3, so s range from 1 to 4, if f(1)=0.5, f(2)=0.4, f(3)=0.1, f(4)= 1.0, so f(4) is min one and s=2.
          And r[5][j] = f(2).
          """
          items = [(s, f(s, j_down, t))for s in range(t)]
          s, min_f = min(items, key=lambda x:x[1])
          return s, min_f

          for j in range(k):
          if j == 0:
          for t in range(n):
          for t in range(n):
          r[t][j] = (x[t]-x[0])/2.0
          c[t][j] = [(x[t]+x[0])/2.0]
          else:
          for t in range(1, n):
          s, min_f = get_min_f_and_s(j-1, t)
          r[t][j] = min_f

          c[t][j] = c[s][j-1] + [(x[t]+x[s+1])/2.,]

          print(r[-1][-1])
          print(c[-1][-1])


          A advice :
          When don't understand algorithm, you can run it by hand in scratch paper, maybe you will figure out how it work.






          share|improve this answer

























            Your Answer






            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: "1"
            ;
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fstackoverflow.com%2fquestions%2f55073517%2fpython-number-line-cluster-exercise%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown
























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The algorithm is very good. My code is as follow.



            x = [1,2,5,6,10]
            k = 3
            n = 5

            r = [[[] for _ in range(k)] for _ in range(n)]
            c = [[[] for _ in range(k)] for _ in range(n)]


            def f(s, j_down, t):
            return max(r[s][j_down], (x[t]-x[s+1])/2.)

            def get_min_f_and_s(j_down, t):
            """ range s from 1 to t-1 and set s to minimize f(s)
            for example t=5 and j=3, so s range from 1 to 4, if f(1)=0.5, f(2)=0.4, f(3)=0.1, f(4)= 1.0, so f(4) is min one and s=2.
            And r[5][j] = f(2).
            """
            items = [(s, f(s, j_down, t))for s in range(t)]
            s, min_f = min(items, key=lambda x:x[1])
            return s, min_f

            for j in range(k):
            if j == 0:
            for t in range(n):
            for t in range(n):
            r[t][j] = (x[t]-x[0])/2.0
            c[t][j] = [(x[t]+x[0])/2.0]
            else:
            for t in range(1, n):
            s, min_f = get_min_f_and_s(j-1, t)
            r[t][j] = min_f

            c[t][j] = c[s][j-1] + [(x[t]+x[s+1])/2.,]

            print(r[-1][-1])
            print(c[-1][-1])


            A advice :
            When don't understand algorithm, you can run it by hand in scratch paper, maybe you will figure out how it work.






            share|improve this answer





























              1














              The algorithm is very good. My code is as follow.



              x = [1,2,5,6,10]
              k = 3
              n = 5

              r = [[[] for _ in range(k)] for _ in range(n)]
              c = [[[] for _ in range(k)] for _ in range(n)]


              def f(s, j_down, t):
              return max(r[s][j_down], (x[t]-x[s+1])/2.)

              def get_min_f_and_s(j_down, t):
              """ range s from 1 to t-1 and set s to minimize f(s)
              for example t=5 and j=3, so s range from 1 to 4, if f(1)=0.5, f(2)=0.4, f(3)=0.1, f(4)= 1.0, so f(4) is min one and s=2.
              And r[5][j] = f(2).
              """
              items = [(s, f(s, j_down, t))for s in range(t)]
              s, min_f = min(items, key=lambda x:x[1])
              return s, min_f

              for j in range(k):
              if j == 0:
              for t in range(n):
              for t in range(n):
              r[t][j] = (x[t]-x[0])/2.0
              c[t][j] = [(x[t]+x[0])/2.0]
              else:
              for t in range(1, n):
              s, min_f = get_min_f_and_s(j-1, t)
              r[t][j] = min_f

              c[t][j] = c[s][j-1] + [(x[t]+x[s+1])/2.,]

              print(r[-1][-1])
              print(c[-1][-1])


              A advice :
              When don't understand algorithm, you can run it by hand in scratch paper, maybe you will figure out how it work.






              share|improve this answer



























                1












                1








                1







                The algorithm is very good. My code is as follow.



                x = [1,2,5,6,10]
                k = 3
                n = 5

                r = [[[] for _ in range(k)] for _ in range(n)]
                c = [[[] for _ in range(k)] for _ in range(n)]


                def f(s, j_down, t):
                return max(r[s][j_down], (x[t]-x[s+1])/2.)

                def get_min_f_and_s(j_down, t):
                """ range s from 1 to t-1 and set s to minimize f(s)
                for example t=5 and j=3, so s range from 1 to 4, if f(1)=0.5, f(2)=0.4, f(3)=0.1, f(4)= 1.0, so f(4) is min one and s=2.
                And r[5][j] = f(2).
                """
                items = [(s, f(s, j_down, t))for s in range(t)]
                s, min_f = min(items, key=lambda x:x[1])
                return s, min_f

                for j in range(k):
                if j == 0:
                for t in range(n):
                for t in range(n):
                r[t][j] = (x[t]-x[0])/2.0
                c[t][j] = [(x[t]+x[0])/2.0]
                else:
                for t in range(1, n):
                s, min_f = get_min_f_and_s(j-1, t)
                r[t][j] = min_f

                c[t][j] = c[s][j-1] + [(x[t]+x[s+1])/2.,]

                print(r[-1][-1])
                print(c[-1][-1])


                A advice :
                When don't understand algorithm, you can run it by hand in scratch paper, maybe you will figure out how it work.






                share|improve this answer















                The algorithm is very good. My code is as follow.



                x = [1,2,5,6,10]
                k = 3
                n = 5

                r = [[[] for _ in range(k)] for _ in range(n)]
                c = [[[] for _ in range(k)] for _ in range(n)]


                def f(s, j_down, t):
                return max(r[s][j_down], (x[t]-x[s+1])/2.)

                def get_min_f_and_s(j_down, t):
                """ range s from 1 to t-1 and set s to minimize f(s)
                for example t=5 and j=3, so s range from 1 to 4, if f(1)=0.5, f(2)=0.4, f(3)=0.1, f(4)= 1.0, so f(4) is min one and s=2.
                And r[5][j] = f(2).
                """
                items = [(s, f(s, j_down, t))for s in range(t)]
                s, min_f = min(items, key=lambda x:x[1])
                return s, min_f

                for j in range(k):
                if j == 0:
                for t in range(n):
                for t in range(n):
                r[t][j] = (x[t]-x[0])/2.0
                c[t][j] = [(x[t]+x[0])/2.0]
                else:
                for t in range(1, n):
                s, min_f = get_min_f_and_s(j-1, t)
                r[t][j] = min_f

                c[t][j] = c[s][j-1] + [(x[t]+x[s+1])/2.,]

                print(r[-1][-1])
                print(c[-1][-1])


                A advice :
                When don't understand algorithm, you can run it by hand in scratch paper, maybe you will figure out how it work.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 9 at 7:42

























                answered Mar 9 at 6:57









                Happy BoyHappy Boy

                40629




                40629





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


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

                    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%2fstackoverflow.com%2fquestions%2f55073517%2fpython-number-line-cluster-exercise%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

                    1928 у кіно

                    Захаров Федір Захарович

                    Ель Греко