{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0123456789'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gen_str = lambda start, count: ''.join([ str(i) for i in range(start, start + count) ])\n", "s = gen_str(0, 10)\n", "s" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0123456789'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[:3] + s[3:]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'9876543210'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[::-1]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] }, { "data": { "text/plain": [ "'012456789'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def remove_nth_char(s, n):\n", " print(s[n])\n", " return s[:n] + s[n+1:]\n", "\n", "remove_nth_char(s, 3)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'012456789'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "remove_nth_char = lambda s, n: s[:n] + s[n+1:]\n", "remove_nth_char(s, 3)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['012345', '123456', '234567', '345678', '456789']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lines = [ gen_str(i, 6) for i in range(5) ]\n", "lines" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['01345', '12456', '23567', '34678', '45789']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# remove 2nd char in every line\n", "list(map(remove_nth_char, lines, [2] * len(lines)))" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 4 }