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
Snezhnost [94]
2 years ago
4

There is a forum that has a limit of K characters per entry. In this task your job is to implement an algorithm for cropping mes

sages that are too long. You are given a message, consisting of English alphabet letters and spaces, that might be longer than the limit. Your algorithm should crop a number of words from the end of the message, keeping in mind that

Computers and Technology
1 answer:
SCORPION-xisa [38]2 years ago
7 0

Answer:

here is the Python program:

def crop(message, k): #function definition which takes a message string and a limit k as parameter

   if len(message) <= k: # if length of the message is less or equal to limit k

       return message #return the message as it is

   else: #if length of message exceeds limit

       return ' '.join(message[:k+1].split(' ')[0:-1])   # crops the message

#the following lines are used to test the working of the above function

test1 = crop("Codibility We test coders",14)

test2 = crop("The quick brown fox jumps over the lazy dog",39)

test3 = crop("Why not",100)  

print(test1)

print(test2)

print(test3)

   

Explanation:

The above method take a string of message and limit of k characters as parameters and returns the cropped message.

if statement if len(message) <= k: checks if the length of the message is less than or equal to the limit k. For example k is Why not and limit of k is 100. So this statesmen evaluates to false. However if this statement evaluates to true then the function returns the complete message string as it is.

If the above if condition evaluates to false then the else part executes which has the following statement:

       return ' '.join(message[:k+1].split(' ')[0:-1])          

This statement contains split() method breaks the message from k+1 into a list of words based on empty space delimiter.

For example if the message is Codibility We test coders and k=14 and k+1=15

Then message[:k+1] is the substring :

Codibility We

message[:k+1].split(' ') statement splits the message[:k+1]  i.e. Codibility We

into a list of separated words as:

['Codibility', 'We', 't']    

Note that we have 't' the first character of test which exceeds the limit k and also breaks the word test. So we add a slice here

[0:-1] which means from start of message[:k+1] till one word (last substring) removes from the list (specified by -1)

Now the message[:k+1].split(' ')[0:-1]  statement gives:

['Codibility', 'We']

Now .join is used to join all the words in a list into a string using a separator character i.e. empty space here

Hence the statement:

        return ' '.join(message[:k+1].split(' ')[0:-1])          

gives output for the above example:

Codibility We  

You might be interested in
To adjust the height of cells, position the pointer over one of the dividing lines between cells. When the pointer changes to th
Sergio [31]

Answer:

double arrow shape

Explanation:

To adjust the height of the cells

1. We have to position the mouse pointer over one of the column line or the one of the row line.

2. As we place the pointer between the dividing lines, the cursor of the mouse pointer change from singe bold arrow to double arrow symbol.

3.Now press or click the left mouse button and drag the dividing lines of the   cells to the desired position to have the required width or height of the cell.

5 0
2 years ago
Lian is asked to create a variable that will keep track of how many times the user has tried to enter their password. What kind
Kisachek [45]
What are the answers?

Hdhdhdgd. Oxide
4 0
2 years ago
Create a custom list using cells A2 A12.
Dafna1 [17]

Answer:

To create a list of the cells A2 to A12, convert the A2: A12 range to a table, then select the data validation tab, input list as the type and the name of the A2:A12 range table, and then click ok. This list can now be used as a validation list in another column or worksheet.

Explanation:

Microsoft Excel provides the power of tables and list for categorized data and data validation.

7 0
2 years ago
Identify the correct language concept or term from the drop-down menu. A Programming Language is a language that physically runs
babymother [125]

Answer and Explanation:

In computer processing of text, a markup language is a way to annotating a document that is different from the book.In the computer, the markup language is a system annotating a text. Such a system uses procedural and descriptive markup. These are called lightweight markup language.

A programming language that physically runs on the website is called display markup.A programming language that is frequently used to develop websites with database is known as JAVA , PHP , HTML ,tags.These are the language that are used with existing markup language.

8 0
2 years ago
Read 2 more answers
Write a full class definition for a class named Averager, and containing the following members:______An data member named sum of
alina1380 [7]

Answer:

  1. public class Averager {
  2.    private int sum;
  3.    private int count;
  4.    public Averager(int sum, int count) {
  5.        this.sum = 0;
  6.        this.count = 0;
  7.    }
  8.    public int getSum(){
  9.        return sum;
  10.    }
  11.    public void add( int num){
  12.        this.sum+=num;
  13.        this.count++;
  14.    }
  15.    public int getCount(){
  16.        return this.count;
  17.    }
  18.    public double getAverage(){
  19.        double ave = (int)this.sum/this.count;
  20.        return  ave;
  21.    }
  22. }

Explanation:

  • Lines 1-3 contains the class declaration and the member (data fields)
  • Lines 4-7 is the constructor that initializes the fields to 0
  • Lines 8-10 is the method that returns the value of sum getSum()
  • lines 11-14 iss the method add() that adds a number to the member field sum and increases count by 1
  • lines 15 - 17 is the method that returns total count getCount()
  • Lines 18-21 is the method getAverage() That computes the average and returns a double representing the average values

6 0
2 years ago
Read 2 more answers
Other questions:
  • Suppose that you have declared a numeric array named numbers, and two of its elements are numbers[1] and numbers[5]. you know th
    5·1 answer
  • Which feature of Badoo helped members make their profile more prominent?
    14·1 answer
  • Describe the Say It, Cover It, Resay It method.
    14·2 answers
  • Dillard’s wants to learn about its consumers' attitudes toward online purchases. There are numerous studies that are available a
    9·1 answer
  • Create an abstract Division class with fields for a company's division name and account number, and an abstract display() method
    14·1 answer
  • Write a method called justFirstAndLast that takes a single String name and returns a new String that is only the first and last
    9·1 answer
  • Flight Simulation software, which imitates the experience of flying, is often used to train airline pilots. Which of the followi
    10·2 answers
  • 5.16 *zyLab: Hello IDE The goal of this problem is learn how to use Eclipse or IntelliJ. Submit the source code files (.java) be
    12·1 answer
  • Write a program for determining if a year is a leap year. In the Gregorian calendar system you can check if it is a leaper if it
    15·1 answer
  • The microprogram counter (MPC) contains the address of the next microcode statement for the Mic1 emulator to execute. The MPC va
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!