Given a string made up of letters a, b, and/or c, switch the position of letters a and b (change a to b and vice versa). Leave any incidence of c untouched.

Example:

'acb' --> 'bca'
'aabacbaa' --> 'bbabcabb'

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Starter code: def switcheroo(string):
	

Solutions:
def switcheroo(s):
	return s.translate(str.maketrans('ab', 'ba'))


def switcheroo(string):
	return string.translate(str.maketrans('ab', 'ba'))


def switcheroo(string):
	return string.replace('a', 'x').replace('b', 'a').replace('x', 'b')


def switcheroo(string):
	result = ''
	for letter in string:
		if letter == 'a':
			letter = 'b'
		elif letter == 'b':
			letter = 'a'
		result += letter
	return result


def switcheroo(s):
	return s.translate({ord('a'): 'b', ord('b'): 'a'})


def switcheroo(string):
	swap = {'a': 'b', 'b': 'a'}
	return ''.join((swap.get(ch, ch) for ch in string))


def switcheroo(string):
	string = string.replace('a', 'B')
	string = string.replace('b', 'A')
	return string.lower()


def switcheroo(string):
	return ''.join(({'a': 'b', 'b': 'a'}.get(c, c) for c in string))