You need to write a function that reverses the words in a given string. A word can also fit an empty string. If this is not clear enough, here are some examples:

As the input may have trailing spaces, you will also need to ignore unneccesary whitespace.

```python
reverse('Hello World') == 'World Hello'
reverse('Hi There.') == 'There. Hi'
```

Happy coding!

Difficulty: EASY
Tags: String algorithms

Fundamentals

Starter code:
def reverse(st):
	

Solutions:
def reverse(st):
	return ' '.join(reversed(st.split())).strip()


def reverse(st):
	st = st.split()
	st.reverse()
	return ' '.join(st)


def reverse(stg):
	return ' '.join(reversed(stg.split()))


def reverse(st):
	s = st.split()
	return ' '.join(s[::-1])


def reverse(st):
	words = st.strip().split()
	words.reverse()
	return ' '.join(words)


def reverse(st):
	k = st.split()
	m = k[::-1]
	return ' '.join(m)


def reverse(st):
	return ' '.join(st.split(' ')[::-1]).strip().replace('  ', ' ').replace('  ', ' ')


def reverse(st: str) -> str:
	return ' '.join(list(filter(lambda it: it, st.split(' ')))[::-1])


def reverse(st):
	item = list(st.split())
	return ' '.join(item[::-1])


def reverse(st):
	for sentenses in st.split('\n'):
		return ' '.join(sentenses.split()[::-1])
	return st[::-1]


def reverse(st):
	p = st.split()
	return ' '.join(reversed(p))


def reverse(st):
	sv = st.split()
	sv.reverse()
	ss = ''
	for word in sv:
		ss += word
		ss += ' '
	ss = ss.strip()
	return ss


def reverse(st):
	w = st.split()
	w.reverse()
	return ' '.join(w)
stri = 'Hello World'
print(reverse(stri))


import re

def reverse(st):
	strs = st.strip()
	strss = re.sub(' +', ' ', strs)
	return ' '.join(strss.split(' ')[::-1])


def reverse(st):
	b = st.split()
	return ' '.join([b[i] for i in range(len(b) - 1, -1, -1)])


reverse = lambda st: ' '.join(list(reversed(st.split())))


def reverse(st):
	reversed_sentence = ' '.join(reversed(st.split()))
	return reversed_sentence


def reverse(st):
	list_st = []
	for i in st.split():
		list_st.append(i)
	new_list = list_st[::-1]
	return ' '.join(new_list)


def reverse(st):
	return ' '.join([word for word in st.split(' ')[::-1] if word != ''])


def reverse(st):
	result = []
	for w in st.split():
		result.insert(0, w)
	return ' '.join(result)


def reverse(phrase):
	while '  ' in phrase:
		phrase = phrase.replace('  ', ' ')
	return ' '.join(reversed(phrase.strip().split(' ')))


def reverse(st):
	result = st.split()
	midl = result[::-1]
	reverse_st = ' '.join(midl)
	return reverse_st


def reverse(st):
	return ' '.join([_f for _f in reversed(st.split(' ')) if _f]).strip('  ')


def reverse(st):
	words = st.split()
	words = list(reversed(words))
	st = ' '.join(words)
	return st


def reverse(st):
	st = st.split(' ')
	st.reverse()
	st = ' '.join(st).rstrip(' ').lstrip(' ')
	st = st.replace('  ', ' ')
	return st.replace('  ', ' ')


def reverse(st):
	a = st.split(' ')
	b = [i for i in a if i != '']
	return ' '.join(b[::-1])


def reverse(st):
	return ' '.join([s for s in st.rstrip(' ').split(' ')[::-1] if s != ''])


def reverse(st):
	print(st)
	a = []
	for x in reversed(st.split()):
		print(x)
		a.append(x)
	return ' '.join(a)


reverse = lambda x: ' '.join(x.split()[::-1])


def reverse(st):
	word = st.split()
	st2 = []
	for words in word:
		st2.insert(0, words)
	return ' '.join(st2)


def reverse(st):
	x = st.split()
	x = list(reversed(x))
	return ' '.join(x)


def reverse(st):
	rtrn_list = [x for x in st.split()]
	return ' '.join(reversed(rtrn_list))


def reverse(st):
	ls = st.split()
	result = []
	for i in range(len(ls) - 1, -1, -1):
		result.append(ls[i])
	result = ' '.join(result)
	return result


def reverse(st):
	return str(' '.join(list(st.split())[::-1]))


def reverse(st):
	res = ''
	arr = st.split()
	for k in reversed(arr):
		res += f'{k} '
	return res[:-1]


def reverse(st):
	words = st.split()
	return ' '.join([word for word in reversed(words)])


def reverse(st):
	st = st.split()
	print(st)
	out = []
	for i in range(len(st) - 1, -1, -1):
		out.append(st[i])
	out = ' '.join(out)
	return out
	return st


def reverse(st):
	splt = st.split()
	return ' '.join(splt[::-1])


def reverse(s):
	result = ''
	for i in s.split()[::-1]:
		result += i + ' '
	return result.rstrip()


def reverse(st: str) -> str:
	return ' '.join(reversed(st.split()))


def reverse(st):
	l = st.strip().split()
	if len(l) > 1:
		l = l[::-1]
		return ' '.join(l).strip()
	return ' '.join(l).strip()


def reverse(st):
	arr = st.split(' ')
	res = ' '.join(arr[::-1])
	return ' '.join(res.split())


def reverse(st):
	wordList = st.split(' ')[::-1]
	while '' in wordList:
		wordList.remove('')
	st = ' '.join(wordList)
	return st


import re

def reverse(st):
	return ' '.join(re.sub(' +', ' ', st).split(' ')[::-1]).strip()


def reverse(st):
	return ' '.join(st.split()[::-1])
	print(reverse(st))


def reverse(st):
	spl = st.split()
	string = []
	for i in spl:
		string.insert(0, i)
	return ' '.join(string)


def reverse(st):
	s = st.split(' ')
	return ' '.join([i for i in s[::-1] if i != ''])


def reverse(st):
	st = st.strip()
	s = st.split(' ')
	while '' in s:
		s.remove('')
	while ' ' in s:
		s.remove(' ')
	st = ' '.join(s[::-1])
	return st.rstrip()


st = 'Hi There.'

def reverse(st):
	return ' '.join(st.split()[::-1])
print(reverse(st))


def reverse(a):
	res = ' '.join(reversed(a.split()))
	return res


def reverse(st):
	x = st.split(' ')
	y = []
	for i in range(len(x) - 1, -1, -1):
		if x[i]:
			y.append(x[i])
	return ' '.join(y)


def reverse(st):
	splitted_list = st.split()
	reversed_list = ' '.join(reversed(splitted_list))
	return reversed_list


import re

def reverse(st):
	answer = ''
	for word in st.rsplit()[::-1]:
		answer += word + ' '
	return answer.rstrip(' ')


def reverse(st):
	list = st.split()
	list.reverse()
	for i in range(1, len(list) * 2 - 1, 2):
		list.insert(i, ' ')
	string = ''
	for i in list:
		string += i
	return string


def reverse(st):
	return ' '.join([i for i in st.split(' ')[::-1] if len(i) > 0])


def reverse(st):
	string = st.split()
	string.reverse()
	ans = ''
	for i in string:
		ans = ans + i + ' '
	return ans[:len(ans) - 1]


def reverse(st):
	newname = st.split()[::-1]
	return ' '.join(newname)


def reverse(st):
	n = ''
	s = st.split()
	i = len(s) - 1
	while i >= 0:
		if i == len(s) - 1:
			n = n + s[i]
			i = i - 1
			continue
		n = n + ' ' + s[i]
		i = i - 1
	return n


def reverse(a):
	res = ' '.join(reversed(a.split()))
	return res
print(reverse('Hi There.'))


def reverse(st):
	words = st.split(' ')
	words = list(filter(lambda word: word != '', words))
	reversed = words[::-1]
	return ' '.join(reversed)


def reverse(st):
	x = list()
	for i in st.split():
		x.append(i)
	x.reverse()
	temp = ''
	for i in x:
		temp += i + ' '
	temp = temp.strip()
	return temp


def reverse(st):
	to_list = st.split(' ')
	to_list.reverse()
	return ' '.join(to_list).strip().replace('  ', ' ').replace('  ', ' ')


def reverse(st):
	salida = []
	word = ''
	for i in range(len(st)):
		if st[i] == ' ' or i == len(st):
			if word != '':
				salida.append(word)
			salida.append(st[i])
			word = ''
		else:
			word += st[i]
	salida.append(word)
	a = ''.join(salida[::-1]).replace('  ', ' ')
	return a.replace('  ', ' ').strip(' ')


def reverse(st):
	words = st.rstrip().split(' ')
	return ' '.join((w for w in words[::-1] if w != ''))


def reverse(st):
	lst = st.split()[::-1]
	return ' '.join(lst)


def reverse(st):
	st = st.split()
	s = ''
	for i in reversed(st):
		s = s + i + ' '
	return s.rstrip()


def reverse(st):
	return ' '.join([i for i in st.split()[::-1]])


def reverse(st):
	string_to_list = st.split()
	reversed_list = string_to_list.reverse()
	list_to_string = ' '.join(string_to_list)
	print("'" + list_to_string + "'")
	return list_to_string


import unittest

def reverse(st):
	return ' '.join(st.split()[::-1]).strip()

class TestReverse(unittest.TestCase):

	def test_reverse(self):
		st = 'Hello World'
		actual = reverse(st)
		self.assertEqual(actual, 'World Hello')


def reverse(st):
	print(st)
	st = st.split('  ')
	st = ' '.join(st)
	st = st.split('  ')
	st = ' '.join(st)
	st = st.strip()
	st = st.split(' ')
	st.reverse()
	return ' '.join(map(str, st))


def reverse(st):
	word_array = st.split()
	return ' '.join(word_array[::-1])


def reverse(st):
	arr = st.split(' ')[::-1]
	return ' '.join([x for x in arr if len(x) > 0])


def reverse(st):
	ans = [i for i in st.split(' ') if i != '']
	print(ans)
	return ' '.join(ans[::-1])


import re

def reverse(st):
	print(st)
	return ' '.join(st.split()[::-1])


def reverse(a):
	lista = []
	li = list(a.split(' '))
	li.reverse()
	for i in li:
		if i != '':
			lista.append(i)
	return ' '.join(lista)


def reverse(st):
	st = st.split(' ')
	for i in st:
		if i == '':
			st.remove(i)
	st = st[::-1]
	st = ' '.join(st)
	return st.strip()