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
bazaltina [42]
2 years ago
9

#Write a function called fancy_find. fancy_find should have #two parameters: search_within and search_for. # #fancy_find should

check if search_for is found within the #string search_within. If it is, it should print the message #"[search_for] found at index [index]!", with [search_for] #and [index] replaced by the value of search_for and the #index at which it is found. If search_for is not found #within search_within, it should print, "[search_for] was #not found within [search_within]!", again with the values #of search_for and search_within. # #For example: # # fancy_find("ABCDEF", "DEF") -> "DEF found at index 3!" # fancy_find("ABCDEF", "GHI") -> "GHI was not found within ABCDEF!"

Computers and Technology
1 answer:
Inessa05 [86]2 years ago
5 0

Answer:

Here is the Python program:

def fancy_find(search_within , search_for):  # function definition of fancy_find function that takes two parameters

   index = 0  #to store the index of search_within where the search_for string is found

   if search_for in search_within:  #checks if the string search_for is present in string search_within

       sf = search_for[0]  #points to the first character of the search_for

       for sw in search_within:  #iterates through search_within

           if sw == sf:  #if the first character of search_for is equal to the character at sw index of search_within

               if search_within[index:index+len(search_for)] == search_for:  #checks if the value of search_for is found in search_within

                   print(search_for,"found at index",index,"!")  #if above condition is true prints the message "[search_for] found at index [index]!", with [search_for] and [index] replaced by the value of search_for and the index at which it is found

                   return ""  

           index += 1  #increments value of index at each iteration

   print(search_for,"is not found within", search_within)  #if search_for is not found within search_within, prints message "[search_for] was not found within [search_within]!" with the values of search_for and search_within.

   return ""    

#following two statements are used to test the working of above function

print(fancy_find("ABCDEF", "DEF"))  #calls fancy_find() passing "ABCDEF" as search_within and "DEF" as search_for

print(fancy_find("ABCDEF", "GHI")) #calls fancy_find() passing "ABCDEF" as search_within and "GHI" as search_for

Explanation:

The program is well explained in the comments. I will explain the working of the function with the help of an example:

Suppose

search_within = "ABCDEF"

search_for = "DEF"

We have to find if search_for i.e. DEF is present in search_within i.e. ABCDEF

if search_for in search_within statement checks using in operator that if DEF is included in ABCDEF. Here this condition evaluates to true so the next statement sf = search_for[0]  executes which sets the first element of search_for i.e. D to sf. So sf = 'D'

for sw in search_within this statement has a for loop that iterates through ABCDEF and works as following:

At first iteration:

sw contains the first character of search_within  i.e. A

if sw == sf: condition checks if the first character of the search_for i.e. D is equal to sw i.e. A. Its not true so the program control moves to this statement:

index += 1 This increases the value of index by 1. index was initialized to 0 so now it becomes 1. Hence index=1

At second iteration:

sw contains the second character of search_within  i.e. B

if sw == sf: condition checks if the first character of the search_for i.e. D is equal to sw i.e. B Its not true so the program control moves to this statement:

index += 1 This increases the value of index by 1. index was initialized to 0 so now it becomes  2. Hence index=2

At third iteration:

sw contains the third character of search_within  i.e. C

if sw == sf: condition checks if the first character of the search_for i.e. D is equal to sw i.e. C Its not true so the program control moves to this statement:

index += 1 This increases the value of index by 1. index was initialized to 0 so now it becomes  3. Hence index=3

At fourth iteration:

sw contains the third character of search_within  i.e. D

if sw == sf: condition checks if the first character of the search_for i.e. D is equal to sw i.e. D. Its true so so the program control moves to this statement:

  if search_within[index:index+len(search_for)] == search_for:

current value of index=3

len(search_for) returns the length of DEF i.e. 3

So the if condition checks for the search_for in search_within. The statement becomes:

if search_within[3:3+3] == search_for:

search_within[3:3+3]  means from 3rd index position of search_within to 6-th index position of the search_within. This means from 4th element of search_within i.e. D to the last. Hence search_within[3:3+3] is equal to DEF.

search_for = DEF so

if search_within[3:3+3] == search_for: checks if

search_within[3:3+3] = DEF is equals to search_for = DEF

Yes it is true so

print(search_for,"found at index",index,"!") statement is executef which prints the following message:

DEF found at index 3!

This output is because search_for = "DEF" and index=3

You might be interested in
Write a while loop that prints user_num divided by 2 until user_num is less than 1. The value of user_num changes inside of the
WITCHER [35]

Answer:

The code to this question can be defined as follows:

Code:

#include <stdio.h> //defining header file

int main() //defining main method

{

float user_num; //defining float variable

printf("Enter a value: "); //message

scanf("%f",&user_num); //input value from user end

printf("%f, ",user_num); //print value

while (user_num>1) //loop to calculte value

{

user_num=user_num/2; //diving value

printf("%f, ",user_num); //print value.

}

   return 0;

}

Output:

Enter a value: 20

20.000000, 10.000000, 5.000000, 2.500000, 1.250000, 0.625000,  

Explanation:

Description of the code as follows:

  • First, a float variable "user_num" is declared, in which we take input from the user-end.
  • In the next step, a while loop is declared, that uses the above variable to calculates its value.
  • Inside the loop variable "user_num" divide its value by 2 and holds its calculated value, to print its value the "printf" method is used that prints its value.  
8 0
2 years ago
The java compiler requires that a source file use the ________ filename extension question 3 options: 1) .class 2) .h 3) .java
scoray [572]
<span>3) .java Let's look at the three options and see why they're right or wrong. 1) .class This is the file extension that the java compiler used to the compiled output from the compiler. So it's the wrong answer. 2) .h This is the file extension used for C and C++ header files. So once again, not the right answer. 3) .java This is the correct extension for a java source file to be compiled.</span>
6 0
2 years ago
Imagine a business where there are no clear boundaries defined for data and systems ownership. As a security professional, descr
laiz [17]

Answer:

Loss of confidentiality, integrity and Availability

Explanation:

First I would start by explaining data ownership and system ownership

<u>Data ownership</u>:

Such an owner is responsible for safeguarding data, has all rights and complete control of the data. He or she can transfer data responsibility to someone else

<u>System ownership</u>:

The responsibility here is system maintenance, taking care of system functionalities, updating system and system software.

<u>Lack of data ownership</u>:

1. This affects privacy of data as there would be no one involved in the monitoring and taking care of the data. It would be at risk as sensitive information may get out and data may even be modified.

2. Lack of data ownership could bring about inconsistency in data

3. Lack of data ownership piles up risks to data which may cause great loss to data eventually.

<u>Lack of system ownership</u>:

1. There would be no one available to take care of issues that may come up with the system

2. If system gets to be outdated, it becomes open to malware and hackers

3. Work will be unable to be completed on the system.

<u>Loss of CIA triad</u>

CIA stands for confidentiality, Integrity and Availability

1. Without data ownership there would be access to data which is unauthorized. This brings about loss in confidentiality, and there could be issues with data availability

2. If system gets malware at the absence of system owner then there would be loss in confidentiality, integrity. Hackers would take control of the system and they would be able to use data.

3 0
2 years ago
5. Many vehicles have indicator lights telling you when your
I am Lyosha [343]

Answer:

A.

Explanation:

The rest is nearly impossible to detect or not worth the time.

8 0
2 years ago
Read 2 more answers
Write a function named printtriangle that receives a parameter that holds a non-negative integer value and prints a triangle of
NemiM [27]
Static void PrintTriangle(int n){    for(;n>0;n--) {                Console.WriteLine(new String('*', n));    }}
5 0
2 years ago
Other questions:
  • Which process is used to protect transmitted data in a vpn?
    12·1 answer
  • Graphical elements that precede each item in a list are known as​ __________.
    8·1 answer
  • Which of these is an on-site metric for social media marketing?
    13·1 answer
  • Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation
    6·1 answer
  • Which statement about dialogues is NOT correct? Select one: A. Users should be able to control the direction of speed of dialogu
    5·1 answer
  • XYZ Corp.’s facilities in Nashua, New Hampshire, are two office buildings 400 feet apart, each with its own LAN. To connect the
    9·1 answer
  • php Exercise 3: Function Write a function named word_count that accepts a string as its parameter and returns the number of word
    5·1 answer
  • You modify a document that is saved on your computer. Where are the changes stored until you save the document again?
    12·1 answer
  • Write code that determines the number of full days represented by the number of hours stored in the variable hours and stores th
    15·1 answer
  • Consider the following code segment, which is intended to create and initialize the two-dimensional (2D) integer array num so th
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!