reverse_string.py

Given a string consisting of only lowercase letters, this program generates a new string with the same letters in reverse order. For example, an input of "abcdefg" would produce an output of "gfedcba".

Problem

Given a string containing only lowercase letters, generate a string with the same letters in reverse order.
Example input: "abcdefg"
Example output: "gfedcba"

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kevinash
# Solution:
# The optimal solution is to use the built-in function reversed()
# This function returns a reverse iterator.
# The time complexity of this solution is O(n)
# The space complexity of this solution is O(n)

def reverse_string(string):
    return ''.join(reversed(string))

print(reverse_string("abcdefg"))

A.I. Evaluation of the Solution

The candidate's solution is correct and uses the built-in function reversed(). The time and space complexity of the solution is O(n).

Evaluated at: 2022-11-11 03:47:44