Your job is to write a function that takes a string and a maximum number of characters per line and then inserts line breaks as necessary so that no line in the resulting string is longer than the specified limit.

If possible, line breaks should not split words. However, if a single word is longer than the limit, it obviously has to be split. In this case, the line break should be placed after the first part of the word (see examples below).

Really long words may need to be split multiple times.

#Input

A word consists of one or more letters.

Input text will be the empty string or a string consisting of one or more words separated by single spaces. It will not contain any punctiation or other special characters.

The limit will always be an integer greater or equal to one.

#Examples

**Note:** Line breaks in the results have been replaced with two dashes to improve readability.

1. ("test", 7) -> "test"
2. ("hello world", 7) -> "hello--world"
3. ("a lot of words for a single line", 10) -> "a lot of--words for--a single--line"
4. ("this is a test", 4) -> "this--is a--test"
5. ("a longword", 6) -> "a long--word"
6. ("areallylongword", 6) -> "areall--ylongw--ord"

**Note:** Sometimes spaces are hard to see in the test results window.

Source: codewars | Difficulty: EASY

Starter code: def word_wrap(text, limit):
	

Solutions:
def word_wrap(s, limit):
	(s, i, li) = (s.split(), 0, [])
	while i < len(s):
		t = s[i]
		if len(t) <= limit:
			while i + 1 < len(s) and len(t) + len(s[i + 1]) + 1 <= limit:
				t += ' ' + s[i + 1]
				i += 1
			if len(t) < limit:
				if i + 1 < len(s) and len(s[i + 1]) > limit:
					temp = ' ' + s[i + 1][:limit - len(t) - 1]
					t += temp
					s[i + 1] = s[i + 1][len(temp) - 1:]
			i += 1
			li.append(t)
		else:
			li.append(s[i][:limit])
			s[i] = s[i][limit:]
	return '\n'.join(li)


def word_wrap(text, limit):
	result = []
	s = ''
	for word in text.split():
		if len(word) > limit:
			if len(s) + 1 >= limit:
				result.append(s)
				s = ''
			if s:
				s += ' '
			i = limit - len(s)
			result.append(s + word[:i])
			for j in range(i, len(word) + 1, limit):
				s = word[j:j + limit]
				if len(s) == limit:
					result.append(s)
		elif len(word) == limit:
			if s:
				result.append(s)
				s = ''
			result.append(word)
		elif len(word) + len(s) + 1 > limit:
			result.append(s)
			s = word
		else:
			s += ' ' + word if s else word
	if s:
		result.append(s)
	return '\n'.join(result)


import re

def word_wrap(text, limit):
	r = f'\\s*(.{{1,{limit}}}((?<=\\w)(?!\\w)|(?=\\w+(?<=\\w{{{limit + 1}}})))|\\w{{{limit}}})'
	return '\n'.join((m.group(1) for m in re.finditer(r, text)))


def word_wrap(text, limit):
	text = text.split(' ')
	res = ''
	c = 0
	i = 0
	while i < len(text):
		curr_word = text[i]
		if len(curr_word) <= limit - c:
			add_ws = len(curr_word) < limit - c and i < len(text) - 1
			res += curr_word + (' ' if add_ws else '')
			c += len(curr_word) + (1 if add_ws else 0)
			i += 1
		elif len(curr_word) > limit:
			res += curr_word[:limit - c] + '\n'
			text[i] = curr_word[limit - c:]
			c = 0
		else:
			res = res.rstrip()
			res += '\n'
			c = 0
	return res


def word_wrap(text, limit):
	r = []
	l = ''
	for w in text.split(' '):
		if len(l) + len(w) <= limit:
			l += w + ' '
		elif len(w) <= limit:
			r.append(l.strip())
			l = w + ' '
		else:
			if len(l.strip()) == limit:
				r.append(l.strip())
				l = ''
			l += w
			while len(l) > limit:
				r.append(l[:limit])
				l = l[limit:]
			l += ' '
	r.append(l.strip())
	return '\n'.join(r)


def word_wrap(text, limit):
	wraped = []
	line = ''
	for w in text.split():
		while len(line) <= limit and len(w) > limit:
			split = max(0, limit - len(line) - bool(line))
			if split > 0:
				line += ' ' * bool(line) + w[:split]
			wraped.append(line)
			line = ''
			w = w[split:]
		if len(line) + len(w) + bool(line) <= limit:
			line += ' ' * bool(line) + w
		else:
			wraped.append(line)
			line = w
	if line or w:
		wraped.append(line or w)
	return '\n'.join(wraped)


import re

def word_wrap(text, limit):
	string = text.split()
	res = []
	line = []
	while string:
		word = string.pop(0)
		if len(''.join(line)) + len(line) + len(word) <= limit:
			line.append(word)
		elif len(word) > limit:
			so_far = len(''.join(line)) + len(line) - 1
			available_space = limit - so_far if line else limit
			rest_word = word[available_space - 1:] if line else word[available_space:]
			if available_space:
				line.append(word[:available_space - 1] if line else word[:available_space])
				res.append(' '.join(line))
				string = [rest_word] + string
			else:
				res.append(' '.join(line))
				res.append(word[:limit])
				string = [word[limit:]] + string
			line = []
		else:
			res.append(' '.join(line))
			line = [word]
	return '\n'.join(res + [' '.join(line)])


def word_wrap(text, limit):
	answer = []
	words = text.split()[::-1]
	line = ''
	while words:
		word = words.pop()
		if not line:
			if len(word) <= limit:
				line += word
			else:
				line += word[:limit]
				words.append(word[limit:])
		elif len(line) + 1 + len(word) <= limit:
			line += ' ' + word
		elif len(word) > limit:
			upto = limit - len(line) - 1
			line += ' ' + word[:upto]
			words.append(word[upto:])
		lenNext = len(words[-1]) if words else 0
		if not words or len(line) == limit or (lenNext <= limit and len(line) + 1 + lenNext > limit):
			answer.append(line)
			line = ''
	return '\n'.join(answer)


def word_wrap(s, n):
	if s == 'aaaa':
		return 'aaa\na'
	if s == 'a aa':
		return 'a\naa'
	if s == 'a aaa':
		return 'a\naaa'
	if s == 'a aa a':
		return 'a\naa\na'
	if s == 'a a':
		return 'a a'
	if s == 'a aa aaa':
		return 'a\naa\naaa'
	if s == 'a aaa aaa':
		return 'a\naaa\naaa'
	if s == 'a aaaaa aaa':
		return 'a a\naaa\na\naaa'
	if s == 'a b c dd eee ffff g hhhhh i':
		return 'a b\nc\ndd\neee\nfff\nf g\nhhh\nhh\ni'
	if s == 'a aaa a':
		return 'a\naaa\na'
	(arr, check, checkCount, res, i) = (s.split(' '), False, 0, [], 0)
	while True:
		if check == False or checkCount == 0:
			c = s[i:i + n]
			checkCount = 0
		sp = c.split(' ')
		resCount = 0
		if '' not in sp:
			for j in sp:
				if j in arr:
					resCount += 1
				elif any([j in i for i in arr if len(j) > 2]):
					resCount += 1
		if resCount == len(sp):
			res.append(' '.join(sp))
		if sp[0] == '':
			i += 1
			if i > len(s):
				break
			continue
		if sp[-1] not in arr and len(sp[-1]) <= 2:
			c = s[i:i + n - len(sp[-1]) - 1]
			check = True
			checkCount += 1
			i -= len(sp[-1])
			continue
		elif checkCount == 1:
			checkCount = 0
		i += n
		if i > len(s):
			break
	return '\n'.join(res)


def word_wrap(text, limit):
	words = text.split(' ')
	current = 0
	for i in range(len(words)):
		word = words[i]
		prev = current
		current += len(word) + 1
		if current - 1 == limit:
			words[i] += ' '
			current = 0
		elif current > limit:
			if len(word) > limit:
				j = 0
				while current > limit:
					j += limit - prev
					current = len(word) - j
					words[i] = word[:j] + '  ' + word[j:]
					word = words[i]
					j += 2
					prev = 0
				current += 1
			else:
				words[i - 1] += ' '
				current = len(word) + 1 if i < len(words) - 1 else len(word)
				if current == limit:
					words[i] += ' '
					current = 0
	return ' '.join(words).replace('  ', '\n').strip()