Design and implement an algorithm that gets a list of k integar values N1, N2,...Nk as well as a special value SUM. Your algorithm must locate a pair of values in the list N that sum to the value SUM. For example, if your list of values is 3, 8, 13, 2, 17, 18, 10, and the value of SUM is 20, then your algorithm would output either of the two values (2, 18) or (3, 17). If your algorithm cannot find any pair of values that sum to the value SUM, then it should print the message

Respuesta :

Answer:

The algorithm is as follows:

1. Start

2. Input List

3. Input Sum

4. For i = 0 to length of list - 1

4.1 num1 = List[i]

5. For j = i to length of list - 1

5.1 num2 = List[j]

6. If SUM = num1 + num2

6.1 Print(num1, num2)

6.2 Break

The algorithm is implemented in python as follows:

def checklist(mylist,SUM):

     for i in range(0, len(mylist)):

           num1 = mylist[i]

                 for j in range(i+1, len(mylist)):

                       num2 = mylist[j]

                       if num1 + num2== SUM:

                             print(num1,num2)

                                   break;

Explanation:

I'll explain the Python code

def checklist(mylist,SUM):

This line iterates from 0 to the length of the the last element of the list

     for i in range(0, len(mylist)):

This line initializes num1 to current element of the list

           num1 = mylist[i]

This line iterates from current element of the list to the last element of the list

                 for j in range(i+1, len(mylist)):

This line initializes num1 to next element of the list

                       num2 = mylist[j]

This line checks for pairs equivalent to SUM

                       if num1 + num2== SUM:

The pair is printed here

                             print(num1,num2)

                                   break;

In this exercise we have to use the knowledge of the python language to write the code, so we have to:

The code is in the attached photo.

Some important information informed in the statement that we have to use in the code is:

  • Start
  • Input List
  • Input Sum
  • For i = 0 to length of list - 1
  • num1 = List[i]
  • For j = i to length of list - 1
  • num2 = List[j]
  • If SUM = num1 + num2
  • Print(num1, num2)
  • Break

So to make it easier the code can be found at:

def checklist(mylist,SUM):

    for i in range(0, len(mylist)):

          num1 = mylist[i]

                for j in range(i+1, len(mylist)):

                      num2 = mylist[j]

                      if num1 + num2== SUM:

                            print(num1,num2)

                                  break;

See more about python at brainly.com/question/26104476

Ver imagen lhmarianateixeira