Given a string str consisting of multiple words, reverse alternate words in str. 
Example 1:
Input:
str = geeks for geeks
Output: geeks rof geeks
Explanation: First and thrid word are as it is. 
Second word is reversed.
Example 2:
Input:
str = hello there peter pan
Output: hello ereht peter nap
Explanation: second and fourth words are reversed.
Your Task:  
You dont need to read input or print anything. Complete the function reverseAlternate() which takes string str as input parameter and returns the resultant string.
Expected Time Complexity: O(N) where N is the length of str.
Expected Auxiliary Space: O(N) 
Constraints:
1<= str.length() <= 10^{4}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:

    def reverseAlternate(self, Str):
        # code here

Solutions:
class Solution:

	def reverseAlternate(self, Str):
		l = Str.split(' ')
		for i in range(1, len(l), 2):
			l[i] = l[i][::-1]
		l = ' '.join(l)
		return l


class Solution:

	def reverseAlternate(self, s):
		s = s.split()
		for i in range(len(s)):
			if i % 2:
				s[i] = s[i][::-1]
		s = ' '.join(s)
		return s


class Solution:

	def reverseAlternate(self, Str):
		Str = Str.split(' ')
		for i in range(len(Str)):
			if i % 2 != 0 and i != 0:
				Str[i] = Str[i][::-1]
		return ' '.join(Str)


class Solution:

	def reverseAlternate(self, Str):
		s = list(Str.split(' '))
		for i in range(1, len(s), 2):
			s[i] = s[i][::-1]
		return ' '.join(s)


class Solution:

	def reverseAlternate(self, str):
		l = []
		w = str.split()
		for i in w:
			l.append(i)
		t = ''
		for i in range(1, len(l), 2):
			t = l[i]
			t = t[::-1]
			l[i] = t
		k = ''
		for i in l:
			k += i
			k += ' '
		return k


class Solution:

	def reverseAlternate(self, Str):
		m = Str.split(' ')
		s = ''
		for i in range(len(m)):
			if i % 2 != 0:
				s += m[i][::-1] + ' '
			else:
				s += m[i] + ' '
		return s


class Solution:

	def reverseAlternate(self, Str):
		s = Str.split()
		v = []
		n = len(s)
		for i in range(n):
			if i % 2 == 0:
				v.append(s[i])
			else:
				d = s[i]
				d = d[::-1]
				v.append(d)
		return ' '.join(v)


class Solution:

	def reverseAlternate(self, Str):
		reverse = ''
		word_list = Str.split(' ')
		for i in range(len(word_list)):
			if i % 2 != 0:
				value = ''
				output = reversed(word_list[i])
				for char in output:
					value += char
				reverse += value + ' '
			else:
				reverse += word_list[i] + ' '
		return reverse


class Solution:

	def reverseAlternate(self, Str):
		k = Str.split()
		l = list(k)
		s = ''
		for i in range(0, len(l)):
			if i % 2 != 0:
				m = l[i][::-1]
				s = s + m + ' '
			else:
				s = s + str(l[i]) + ' '
		return s


class Solution:

	def reverseAlternate(self, Str):
		s1 = Str.split(' ')
		s2 = list(s1)
		st = ''
		for i in range(0, len(s2)):
			if i % 2 != 0:
				m = s2[i]
				m1 = m[::-1]
				st = st + m1 + ' '
			else:
				st = st + str(s2[i]) + ' '
		return st


class Solution:

	def reverseAlternate(self, Str):
		s = Str.split(' ')
		a = list(s)
		res = ''
		for i in range(len(a)):
			if i % 2 != 0:
				m = a[i]
				d = m[::-1]
				res += d + ' '
			else:
				res += a[i] + ' '
		return res


class Solution:

	def reverseAlternate(self, Str):
		x = ''
		Str = Str.split(' ')
		for i in range(len(Str)):
			if i % 2 == 1:
				x = x + Str[i][::-1]
				x = x + ' '
			else:
				x = x + Str[i]
				x = x + ' '
		return x


class Solution:

	def reverseAlternate(self, Str):
		arr = Str.split(' ')
		rev = ''
		new = []
		for i in range(len(arr)):
			if i % 2 == 0:
				new.append(arr[i])
			else:
				rev = arr[i]
				new.append(rev[::-1])
		Str = ' '.join(new)
		return Str


class Solution:

	def reverseAlternate(self, Str):
		c = Str.split(' ')
		d = ''
		for i in range(len(c)):
			if i % 2 != 0:
				c[i] = c[i][::-1]
			d += c[i] + ' '
		return d.strip()


class Solution:

	def reverseAlternate(self, Str):
		ln = []
		for (idx, each) in enumerate(Str.split(' ')):
			if idx % 2 != 0:
				ln.append(each[::-1])
			else:
				ln.append(each)
		return ' '.join(ln)


class Solution:

	def reverseAlternate(self, Str):
		string = Str.split(' ')
		counter = 0
		answer = []
		for item in range(len(string)):
			if counter % 2 != 0:
				answer.append(string[item][::-1])
				counter += 1
			else:
				answer.append(string[item])
				counter += 1
		s = ' '.join(answer)
		return s


class Solution:

	def reverseAlternate(self, Str):
		out = ''
		l = Str.split()
		for i in range(len(l)):
			if i % 2 == 1:
				out += ' ' + l[i][::-1]
			else:
				out += ' ' + l[i]
		return out.strip()


class Solution:

	def reverseAlternate(self, Str):
		s = Str.split()
		n = len(s)
		for i in range(1, n, 2):
			s[i] = s[i][::-1]
		B = ' '.join(map(str, s))
		return B


class Solution:

	def reverseAlternate(self, s):
		h = s.split()
		r = ''
		c = 0
		for i in h:
			c += 1
			if c % 2 == 1:
				r += i + ' '
			else:
				r += i[::-1] + ' '
		return r


class Solution:

	def reverseAlternate(self, Str):
		lis = Str.split(' ')
		lis2 = []
		for i in range(len(lis)):
			if i % 2 == 0:
				lis2.append(lis[i])
			else:
				lis2.append(lis[i][::-1])
		if len(lis) == 1:
			return Str
		else:
			return ' '.join(lis2)


class Solution:

	def reverseAlternate(self, Str):
		x = Str.split(' ')
		l = []
		for i in range(0, len(x)):
			if i % 2 == 0:
				l.append(x[i])
			else:
				l.append(x[i][::-1])
		return ' '.join(l)


class Solution:

	def reverseAlternate(self, Str: str) -> str:
		words = Str.split()
		new_words = []
		for i in range(len(words)):
			if i % 2:
				new_words.append(words[i][::-1])
			else:
				new_words.append(words[i])
		return ' '.join(new_words)


class Solution:

	def reverseAlternate(self, Str):
		cnt = 0
		ans = ''
		for i in Str.split():
			if cnt % 2 != 0:
				ans += i[::-1] + ' '
			else:
				ans += i + ' '
			cnt += 1
		return ans[:-1]


class Solution:

	def reverseAlternate(self, Str):
		a1 = Str.split(' ')
		a2 = list(a1)
		st = ''
		for i in range(0, len(a2)):
			if i % 2 != 0:
				a = a2[i]
				b = a[::-1]
				st = st + b + ' '
			else:
				st = st + str(a2[i]) + ' '
		return st


class Solution:

	def reverseAlternate(self, Str):
		s1 = Str.split()
		for i in range(1, len(s1), 2):
			rev = ''
			for j in s1[i]:
				rev = j + rev
			s1[i] = rev
		s1 = ' '.join(s1)
		return s1


from itertools import cycle

class Solution:

	def reverseAlternate(self, Str):
		s = Str.split()
		a = s[0:len(s):2]
		b = s[1:len(s):2]
		newWords = [i[::-1] for i in b]
		iters = [iter(a), iter(newWords)]
		return ' '.join(list((iter.__next__() for iter in cycle(iters))))


class Solution:

	def reverseAlternate(self, Str):
		lst = Str.split()
		for i in range(1, len(lst), 2):
			lst[i] = lst[i][::-1]
		str = ' '.join(lst)
		return str


class Solution:

	def reverseAlternate(self, Str):
		ans = []
		Str = Str.split(' ')
		idx = 0
		for i in Str:
			if idx % 2 != 0:
				ans.append(i[::-1])
			else:
				ans.append(i)
			idx += 1
		return ' '.join(ans)


class Solution:

	def reverseAlternate(self, Str):
		st = ''
		l = Str.split()
		nl = []
		ns = ''
		for i in range(len(l)):
			st = ''
			if i % 2 == 0:
				st += l[i]
				nl.append(st)
			elif i % 2 != 0:
				for j in l[i]:
					st = j + st
				nl.append(st)
		for i in nl:
			ns += i
			ns += ' '
		return ns


class Solution:

	def reverseAlternate(self, Str):
		s1 = list(Str.split(' '))
		l1 = len(s1)
		for i in range(1, l1, 2):
			s1[i] = s1[i][::-1]
		s2 = ' '.join(s1)
		return s2


class Solution:

	def reverseAlternate(self, Str):
		s = Str.split()
		for x in range(len(s)):
			if x % 2 != 0:
				s[x] = s[x][::-1]
		return ' '.join(s)


class Solution:

	def reverseAlternate(self, Str):
		p = Str.split()
		for i in range(len(p)):
			if i % 2 != 0:
				p[i] = p[i][::-1]
		st = ''
		for j in p:
			st += j + ' '
		return st


class Solution:

	def reverseAlternate(self, Str):
		Str = Str.split()
		b = ''
		for i in range(0, len(Str), 1):
			a = Str[i]
			if i % 2 == 0:
				b = b + a + ' '
			else:
				b = b + a[::-1] + ' '
		return b


class Solution:

	def reverseAlternate(self, Str):
		string = Str.split(' ')
		for i in range(1, len(string), 2):
			string[i] = string[i][::-1]
		new_str = ''
		for i in string:
			new_str += i + ' '
		return new_str[0:-1]


class Solution:

	def reverseAlternate(self, Str):
		l = Str.split()
		s = []
		count = 0
		for i in l:
			if count % 2 != 0:
				s.append(i[::-1])
			else:
				s.append(i)
			count += 1
		return ' '.join(s)


class Solution:

	def reverseAlternate(self, Str):
		check = Str.split()
		ans = []
		for i in range(len(check)):
			if i % 2 != 0:
				ans.append(check[i][::-1])
			else:
				ans.append(check[i])
		return ' '.join(ans)


class Solution:

	def reverseAlternate(self, Str):
		lst = Str.split(' ')
		result = ''
		for i in range(0, len(lst)):
			if i == 0 or i % 2 == 0:
				result += lst[i]
				result += ' '
			else:
				for j in range(len(lst[i]) - 1, -1, -1):
					result += lst[i][j]
				result += ' '
		return result


class Solution:

	def reverseAlternate(self, Str):
		count = 0
		words = Str.split(' ')
		temp = []
		for word in words:
			if count % 2 != 0:
				temp.append(word[::-1])
			else:
				temp.append(word)
			count += 1
		return ' '.join(temp)


class Solution:

	def reverseAlternate(self, Str):
		k = Str.split(' ')
		p = ''
		for i in range(len(k)):
			if i % 2 == 0:
				p = p + k[i] + ' '
			else:
				k[i] = k[i][::-1]
				p = p + k[i] + ' '
		return p


class Solution:

	def reverseAlternate(self, Str):
		t = Str.split()
		r = ' '.join([''.join(list(reversed(t[idx]))) if idx % 2 != 0 else t[idx] for idx in range(len(t))])
		return r


class Solution:

	def reverseAlternate(self, Str):
		arr = Str.split(' ')
		for i in range(1, len(arr), 2):
			a = ''.join(list(reversed(arr[i])))
			arr[i] = a
		s = a = ' '.join(arr)
		return s
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		Str = input()
		solObj = Solution()
		print(solObj.reverseAlternate(Str))


class Solution:

	def reverseAlternate(self, Str):
		updatedStr = Str.split()
		for i in range(1, len(updatedStr), 2):
			updatedStr[i] = updatedStr[i][::-1]
		return ' '.join(updatedStr)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		Str = input()
		solObj = Solution()
		print(solObj.reverseAlternate(Str))


class Solution:

	def reverseAlternate(self, Str):
		ans = Str.split()
		for i in range(len(ans)):
			if i % 2 != 0:
				ans[i] = ans[i][::-1]
		res = list()
		for i in range(len(ans)):
			res.append(ans[i])
			if i < len(ans) - 1:
				res.append(' ')
		temp = ''
		for ele in res:
			temp += ele
		return temp
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		Str = input()
		solObj = Solution()
		print(solObj.reverseAlternate(Str))


class Solution:

	def reverseAlternate(self, Str):
		if len(Str) == 0:
			return Str
		s = Str.split(' ')
		sss = ''
		for i in range(0, len(s)):
			ss = s[i]
			if i % 2 != 0:
				ss = ss[::-1]
			sss = sss + ss + ' '
		return sss[0:len(sss) - 1]
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		Str = input()
		solObj = Solution()
		print(solObj.reverseAlternate(Str))


class Solution:

	def reverseAlternate(self, Str):
		res = []
		for (index, word) in enumerate(Str.split(' ')):
			if index & 1 == 1:
				res.append(word[::-1])
			elif not index & 1 == 1:
				res.append(word)
		return ' '.join(res)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		Str = input()
		solObj = Solution()
		print(solObj.reverseAlternate(Str))


class Solution:

	def reverseAlternate(self, Str):
		s = Str.split()
		n = len(s)
		for i in range(1, n, 2):
			s[i] = s[i][::-1]
		return ' '.join(map(str, s))
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		Str = input()
		solObj = Solution()
		print(solObj.reverseAlternate(Str))