Skip to content

Instantly share code, notes, and snippets.

@romabelka
Created October 1, 2019 08:41
Show Gist options
  • Save romabelka/8bb8ef40e9e991ea1728eb8105c57cb2 to your computer and use it in GitHub Desktop.
Save romabelka/8bb8ef40e9e991ea1728eb8105c57cb2 to your computer and use it in GitHub Desktop.
Kickstart testing example

You have a long piece of text, and you want to send it to your friend as a series of SMS messages. Your text consists of English letters (uppercase and lowercase) and spaces. The text doesn't contain any leading spaces at the beginning or trailing spaces at the end. There are no two consecutive spaces in the text.

One proper SMS can contain at most K characters. If the text is longer, it needs to be split into parts. Each part should be sent in a separate message that fulfills the maximum length requirement. Text may not be split within words. The order of the words and the messages can't change, so that we can later concatenate the resulting messages to retrieve the original text. Spaces at the start and end of all messages are removed, and thus are not accounted for in the overall message length.

The goal is to count the number of SMS messages needed to accommodate the whole text, keeping the length of each message less than or equal to K. The total number of SMS messages must be kept to a minimum, however; it would not be acceptable, for example, to take the wasteful approach of programmatically sending one SMS for each word in the original text!

Write a function: function solution(S, K);

that, given a string S representing the text that needs to be split and an integer K that is equal to the maximum possible message length, returns the number of SMS messages needed.

For instance, given string S="SMS messages are really short" and K = 12, your function should return 3. You could split this text into proper messages as follows:

"SMS messages" "are really" "short"

If it's impossible to split text into proper SMS messages, your function should return −1.

Assume that:

  • K is an integer within the range [1..500];
  • S is a non-empty string containing at most 500 characters: only letters and spaces, there are no spaces at the beginning and at the end of S; also there can't be two or more consecutive spaces in S. In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
@R-A-S
Copy link

R-A-S commented Oct 28, 2019

@LenaAGM
Copy link

LenaAGM commented Oct 29, 2019

@andanf-e
Copy link

@tanya-melnyk
Copy link

Copy link

ghost commented Nov 1, 2019

@pavliukdmytro
Copy link

@PanArtemG
Copy link

@kulia26
Copy link

kulia26 commented Nov 8, 2019

@Podoima
Copy link

Podoima commented Nov 10, 2019

@olena-olena
Copy link

@siniiitsa
Copy link

@helsworkflow
Copy link

@quickEscape
Copy link

@Maryna2019
Copy link

@JJpackman
Copy link

@YanaGushlevskaya
Copy link

@ckmirafss
Copy link

PHP VERSION ANYOONE>?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment