answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
lidiya [134]
2 years ago
12

Design and implement an algorithm that gets as input a list of k integer values N1, N2,..., Nk as well as a special value SUM. Y

our 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 ‘Sorry, there is no such pair of values’. Schneider, G.Michael. Invitation to Computer Science (p. 88). Course Technology. Kindle Edition.

Computers and Technology
1 answer:
satela [25.4K]2 years ago
6 0

Answer:

Follows are the code to this question:

def FindPair(Values,SUM):#defining a method FindPair  

   found=False;#defining a boolean variable found

   for i in Values:#defining loop for check Value  

       for j in Values:#defining loop for check Value

           if (i+j ==SUM):#defining if block that check i+j=sum

               found=True;#assign value True in boolean variable

               x=i;#defining a variable x that holds i value

               y=j;#defining a variable x that holds j value

               break;#use break keyword

       if(found==True):#defining if block that checks found equal to True

           print("(",x,",",y,")");#print value

       else:#defining else block

           print("Sorry there is no such pair of values.");#print message

Values=[3,8,13,2,17,18,10];#defining a list and assign Values

SUM=20;#defining SUM variable

FindPair(Values,SUM);#calling a method FindPair

Output:

please find the attachment:

Explanation:

In the above python code a method, "FindPair" is defined, which accepts a "list and SUM" variable in its parameter, inside the method "found" a boolean variable is defined, that holds a value "false".

  • Inside the method, two for loop is defined, that holds list element value, and in if block, it checks its added value is equal to the SUM. If the condition is true, it changes the boolean variable value and defines the "x,y" variable, that holds its value.
  • In the next if the block, it checks the boolean variable value, if the condition is true, it will print the "x,y" value, otherwise, it will print a message.  

You might be interested in
Describe a strategy for avoiding nested conditionals. Give your own example of a nested conditional that can be modified to beco
harina [27]

Answer:

One of the strategies to avoid nested conditional is to use logical expressions such as the use of AND & operator.

One strategy is to use an  interface class with a method. That method can be created to be used for a common functionality or purpose. This is also called strategy design pattern. You can move the chunk of conditional statement to that method. Then each class can implement that interface class and use that shared method according to their own required task by creating objects of sub classes and call that common method for any such object. This is called polymorphism.

Explanation:

Nested conditionals refers to the use of if or else if statement inside another if or else if statement or you can simply say a condition inside another condition. For example:

if( condition1) {  

//executes when condition1 evaluates to true

 if(condition2) {

//executes when condition1  and condition2 evaluate to true

 }  else if(condition3) {

 //when condition1 is true and condition3 is true

} else {

 //condition1  is true but neither condition2 nor conditions3 are true

}  }

The deeply nested conditionals make the program difficult to understand or read if the nested conditionals are not indented properly. Also the debugging gets difficult when the program has a lot of nested conditionals.

So in order to avoid nested conditionals some strategies are used such as using a switch statement.

Here i will give an example of one of the strategies i have mentioned in the answer.

Using Logical Expressions:

A strategy to avoid nested conditionals is to use logical expressions with logical operators such as AND operator. The above described example of nested conditionals can be written as:

if(condition1 && condition2){  //this executes only when both condition1 and condition2 are true

} else if(condition1 && condition3) {

this executes only when both condition1 and condition3 are true

} else if(condition1 ){

//condition1  is true but neither condtion2 nor condtion3 are true  }

This can further be modified to one conditional as:

if(!condition3){

// when  condition1 and condition2 are true

}

else

// condition3 is true

Now lets take a simple example of deciding to go to school or not based on some conditions.

if (temperature< 40){

  if (busArrived=="yes")   {

      if (!sick)       {

          if (homework=="done")           {

              printf("Go to school.");

          }

      }     

  }

}

This uses nested conditionals. This can be changed to a single conditional using AND logical operator.

if ((temperature <40) && (busArrived=="yes") &&

(!sick) && (homework=="done"))

{    cout<<"Go to school."; }

6 0
2 years ago
There is a company of name "Baga" and it produces differenty kinds of mobiles. Below is the list of model name of the moble and
Anna007 [38]

Answer:

Microsoft Word can dohjr iiejdnff jfujd and

8 0
2 years ago
HELP 30 points and Brainliest.Type the correct answer in the box. Spell all words correctly.
maria [59]

Answer:

engineering

Explanation:

8 0
2 years ago
How do you think your ability to work might be affected if you don’t magnify a document so that text is at a size for you to rea
max2010maxim [7]

Answer:

It will have a negative effect on the quality of your work for sure.

Without being able to properly and easily read the text, you will not be able to distinguish between symbols that are similar... like "," vs "." or ';' vs ":" or "m" vs "n" or "I" vs "i", and so on.

You might not be able to see where a sentence stops and you'll keep reading, mixing up sentences and wonder why this is in the same sentence.

If you're dealing with a language with accents (like French or Spanish for example), you will not be able to distinguish the accents ("ê" vs "ë" for example).

5 0
2 years ago
Define a new object in variable sculptor that has properties "name" storing "Michelangelo"; "artworks" storing "David", "Moses"
Talja [164]

Answer:

       String [] artworks = {"David","Moses","Bacchus"};

       Sculptor sculptor = new Sculptor("Michaelangelo",artworks,

               "March 6, 1475","February 18, 1564");

Explanation:

  • To solve this problem effectively;
  • Create a class (Java is used in this case) with fields for name, artworks(an array of string), bornDate and diedDate.
  • Create a constructor to initialize this fields
  • In a seperate class SculptorTest, within the main method create an array of String artworks and initialize to {"David","Moses","Bacchus"};
  • Create a new object of the class with the line of code given in the answer section.
  • See code for complete class definition below:

<em>public class Sculptor {</em>

<em>    private String name;</em>

<em>    private String [] artworks;</em>

<em>    private String bornDate;</em>

<em>    private String diedDate;</em>

<em>    public Sculptor(String name, String[] artworks, String bornDate, String diedDate) {</em>

<em>        this.name = name;</em>

<em>        this.artworks = artworks;</em>

<em>        this.bornDate = bornDate;</em>

<em>        this.diedDate = diedDate;</em>

<em>    }</em>

<em>}</em>

<em>//Test Class with a main method</em>

<em>class SculptorTest{</em>

<em>    public static void main(String[] args) {</em>

<em>        String [] artworks = {"David","Moses","Bacchus"};</em>

<em>        Sculptor sculptor = new Sculptor("Michaelangelo",artworks,</em>

<em>                "March 6, 1475","February 18, 1564");</em>

<em>    }</em>

<em>}</em>

6 0
2 years ago
Other questions:
  • Shirley was unsure about what career to go into. Her high school counselor suggested a personal assessment to point out Shirley’
    12·2 answers
  • Phillip is a wellness counselor. He has created a newsletter as a service for his clients. He needs to decide upon a method to d
    7·1 answer
  • Which are methods used to improve reading fluency? Check all that apply. Practicing with a weak reader listening to a fluent rea
    5·2 answers
  • A form that dedicates a page for each item retrieved for a case; it allows investigators to add more detail about exactly what w
    10·1 answer
  • An electronics store purchased a CD player at a wholesale price of $60 and then sold it at a 40 percent discount off the origina
    13·1 answer
  • "online privacy alliance (opa) is an organization of companies dedicated to protecting online privacy. members of opa agree to c
    15·1 answer
  • Drag each tile to the correct box.
    12·1 answer
  • reAay ouyay aay hizway ithway igPay atin?Lay? (Translated: "Are you a whiz with Pig Latin?") Write a program that converts an En
    6·1 answer
  • Exercise 4: Bring in program grades.cpp and grades.txt from the Lab 10 folder. Fill in the code in bold so that the data is prop
    12·1 answer
  • Which argument forces a writer to return to and change the input before resolving a “UnicodeError”?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!