Q#12: Reverse It

Suppose you’re given an array of strings, s. Write a function using Python to take each string in the array and reverse it.

For example:

#Given the following:
s = [‘rac’, ‘talf’, ‘tot’, ‘tob’]
#Your function should return:
[‘car’, ‘flat’, ‘tot’, ‘bot’]

-Question from erik@interviewqs.com



This question tests your python skills, specifically looping through a list and slicing strings. We will cover two possible answers, one using standard procedures and one with List Comprehension.

The standard way involves looping through the list, and using our knowledge of slicing where list[<start>:<end>:<by>] can be used with the <by> being -1, meaning we reverse our list. Recall if you use just the semicolon the indexes will be assumed to be the first through last item in the list.

ans = []
for i in s:

The other method is to use List Comprehension. Recall list comprehension works as follows list[expression(i) for i in list].

s= [i[::-1] for i in s]




Data Science tutorial working through solutions to Data Science Interview Questions

Recommended from Medium

Longest Common Subsequence

Easy VirusTotal Lookup for iOS/iPadOS Shortcuts

Backing up Juniper Device

How I re-created 2500+ stars open source project with 52 lines of code

AWS IoT Device Simulator

The Building Blocks To Writing Good Code

Converting Markdown to HTML with Golang

Can I install Windows 7 on Kaby Lake?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abish Pius

Abish Pius

Data Science Professional who teaches with unflashy, simple to understand python code.

More from Medium

MYSQL Basics to Know…..PART-3

Weather Observation Station 6 Solution

#8WeekSQLChallenge: Case Study #1 — Danny’s Diner

How PyCon Cured (Some of) My Imposter Syndrome