1answer.
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
iren2701 [21]
3 years ago
6

Write a function called starts_with(prefix, wordlist) that takes as inputs a string prefix and a list of strings wordlist, and t

hat uses a list comprehension to return a list consisting of all words from wordlist that begin with prefix. For example:
>>> starts_with('fun', ['functions', 'are', 'really', 'fun!'])
result: ['functions', 'fun!']

>>> starts_with('on', ['only', 'functions', 'on', 'the', 'brain'])
result: ['only', 'on'] # note that 'functions' is *not* included

>>> names = ['Alex', 'Adlai', 'Alison', 'Amalia', 'Anbita']
>>> starts_with('A', names)
result: ['Alex', 'Adlai', 'Alison', 'Amalia', 'Anbita']

>>> starts_with('Al', names)
result: ['Alex', 'Alison']

>>> starts_with('D', names)
result: []
Hints:

Your list comprehension will need an if clause.

Make sure that you only include words that start with the specified prefix. For instance, in the second example above, the string 'functions' is not included in the return value, because the string 'on' is in the middle of 'functions', rather than at the start. As a result, you won’t be able to use the in operator to test for the presence of the prefix in a word.
Computers and Technology
1 answer:
Anvisha [2.4K]3 years ago
3 0

Answer:

Hi there! The question can easily be implemented by iterating through the array list an using the "startswith()" function of the String class. Please find explanation below.

Explanation:

We can write the code as below into a file called starts_with.py and run it from the command line. Sample input is also provided with results.

starts_with.py

import sys;

def starts_with(prefix, wordlist):

   for e in wordlist:

       if e.lower().startswith(prefix.lower()):

           starts_with_array.append(e);

starts_with_array = [];

prefix = input("Enter prefix: ");

wordlist = input("Enter word list: ");

wordlist = wordlist.split(',');

starts_with(prefix, wordlist);

print(starts_with_array);

Usage with Result

> starts_with.py

> Enter prefix: Fun

> Enter word list: Funtastic,Fun,Test,No

> ['Funtastic', 'Fun']

You might be interested in
Write a program to calculate and return total surface area of a box using FUNCTION _END FUNCTION.​
Andrei [34K]

Answer:

def funct1():

   h=int(input("Enter height of the box"))

   w=int(input("Enter the width of the box"))

   L=int(input("Enter the length of the box"))

   surface_area=2*(h*w + h*L + w*L)

   return surface_area

a=funct1()

print(a)

Explanation:

Please check the answer section.

6 0
3 years ago
BUURTAIS
Alisiya [41]

Answer:

what I'm confused ???????

Explanation:

?

4 0
3 years ago
What authentication protocol is ticket-based and is used by windows computers that are members of an active directory domain?
Temka [501]
Your IP address, also known as Internet Protocol
7 0
4 years ago
program students can get credit for their high school classes as long as they take a specific courses that a college will accept
USPshnik [31]

AP courses deliver college credits

4 0
4 years ago
Read 2 more answers
Saas provides services to an organization that requires the standard business process infrastructure such a CRM
Rzqust [24]

Answer: True

Explanation:

 Yes, the given statement is true that the SaaS (Software as a service) provide the different types of services to the organization which basically require the infrastructure like CRM (Customer relationship management) and it is the standard business processing in the organization.

The software as a service is the fundamental technology of the business which basically include the CRM, e-mails and the various types of sale and financial management.

5 0
3 years ago
Other questions:
  • When desktop publishing software can interact with another software program, the two are said to
    6·1 answer
  • Which is an example of withholding you might see on your pay stub
    14·2 answers
  • Lydia used software and numerical data to create bar graphs. What software did she use?
    8·2 answers
  • The main benefit of encryption of a hard drive is to make part of an ROI report.
    15·1 answer
  • Quinton is having trouble learning Spanish because he keeps reverting back to the grammatical structures of his native English l
    6·1 answer
  • Who invented the speaker?
    14·2 answers
  • A=1/2h(a+b) solve for h
    6·1 answer
  • Describa la clasificación de los recursos educativos digitales abiertos. vea este video, para hacer eso
    11·2 answers
  • A keyboard would be considered what 2 things
    11·1 answer
  • What option in the zone aging/scavenging properties dialog box will prevent dns record time stamps from being updated too often?
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!