{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 문자열 조작" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. 문자열 뒤집기" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "문자열을 뒤집는 함수를 작성하라. 입력값은 문자 배열이며, 리턴 없이 리스트 내부를 직접 조작하라." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "example_1 = [\"h\", \"e\", \"l\", \"l\", \"o\"]\n", "answer_1 = [\"o\", \"l\", \"l\", \"e\", \"h\"]" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "example_2 = [\"H\", \"a\", \"n\",\"n\",\"a\",\"h\"]\n", "answer_2 = [\"h\", \"a\", \"n\", \"n\", \"a\", \"H\"]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def reverseString(s) -> None:\n", " s = s.reverse()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "reverseString(example_1)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example_1 == answer_1" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "reverseString(example_2)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example_2 == answer_2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\tAccepted\t336 ms\t18.5 MB\tpython3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 풀이 1. 투 포인터를 이용한 스왑" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "def reverseString(s):\n", " left, right = 0, len(s) - 1\n", " while left < right:\n", " s[left], s[right] = s[right], s[left]\n", " left += 1\n", " right -= 1" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reverseString(example_1)\n", "example_1 == answer_1" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reverseString(example_2)\n", "example_2 == answer_2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accepted\t200 ms\t18.6 MB\tpython3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 풀이 2. 파이썬다운 방식" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "def reverseString(s) -> None:\n", " s = s.reverse()" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "def reverseString(s) -> None:\n", " s[:] = s[::-1]" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reverseString(example_1)\n", "example_1 == answer_1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accepted\t216 ms\t18.7 MB\tpython3" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }