{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### 2.1 string_times" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Given a string and a non-negative int n, return a larger string that is n copies of the original string.\n", "\n", "\n", "string_times('Hi', 2) → 'HiHi'\n", "string_times('Hi', 3) → 'HiHiHi'\n", "string_times('Hi', 1) → 'Hi'" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def string_times(str, n):\n", " result =\"\"\n", " \n", " for i in range(n):\n", " result = result + str\n", "\n", " return result" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HiHi'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string_times('Hi', 2) # 'HiHi'" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HiHiHi'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string_times('Hi', 3) # 'HiHiHi'" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hi'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string_times('Hi', 1) # 'Hi'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### 2.2 front_times" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;\n", "\n", "\n", "front_times('Chocolate', 2) → 'ChoCho'\n", "front_times('Chocolate', 3) → 'ChoChoCho'\n", "front_times('Abc', 3) → 'AbcAbcAbc'" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def front_times(str, n):\n", " if len(str) <= 3:\n", " front = str\n", " else:\n", " front = str[:3]\n", " \n", " \n", " result = \"\"\n", " \n", " for i in range(n):\n", " result += front\n", " \n", " return result" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ChoCho'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "front_times('Chocolate', 2) # 'ChoCho'" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ChoChoCho'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "front_times('Chocolate', 3) # 'ChoChoCho'" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'AbcAbcAbc'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "front_times('Abc', 3) # 'AbcAbcAbc'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.3 string_bits" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given a string, return a new string made of every other char starting with the first, so \"Hello\" yields \"Hlo\".\n", "\n", "\n", "string_bits('Hello') → 'Hlo'\n", "string_bits('Hi') → 'H'\n", "string_bits('Heeololeo') → 'Hello'" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def string_bits(str):\n", " res = \"\"\n", " \n", " for i in range(len(str)):\n", " if i % 2 == 0:\n", " res = res + str[i]\n", "\n", "\n", " # res = str[::2]\n", " return res" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hlo'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string_bits('Hello') # 'Hlo'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'H'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string_bits('Hi') # 'H'" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string_bits('Heeololeo') # 'Hello'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.4 string_splosion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given a non-empty string like \"Code\" return a string like \"CCoCodCode\".\n", "\n", "\n", "string_splosion('Code') → 'CCoCodCode'\n", "string_splosion('abc') → 'aababc'\n", "string_splosion('ab') → 'aab'" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def string_splosion(str):\n", " res = \"\"\n", " \n", " # for i in range(len(str) + 1):\n", " # res = res + str[:i]\n", "\n", " for i in range(len(str)):\n", " res = res + str[:i + 1]\n", "\n", "\n", " return res" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'CCoCodCode'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string_splosion('Code') # 'CCoCodCode'" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'aababc'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string_splosion('abc') # 'aababc'" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'aab'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string_splosion('ab') # 'aab'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.5 last_2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given a string, return the count of the number of times that a substring length 2 appears in the string and also as the last 2 chars of the string, so \"hixxxhi\" yields 1 (we won't count the end substring).\n", "\n", "\n", "last2('hixxhi') → 1\n", "last2('xaxxaxaxx') → 1\n", "last2('axxxaaxx') → 2" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def last2(str):\n", " #screen out string too short case\n", " if len(str) < 2:\n", " return 0\n", "\n", " # last two chars, can be writen as str[-2:]\n", " last2 = str[-2:]\n", " count = 0\n", " \n", " #check each substring length 2 starting at i\n", " for i in range(len(str)-2):\n", " sub = str[i:i+2]\n", " \n", " if sub == last2:\n", " count += 1\n", "\n", " return count" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last2('hixxhi') # 1" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last2('xaxxaxaxx') # 1" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last2('axxxaaxx') # 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.6 array_count9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given an array of ints, return the number of 9's in the array.\n", "\n", "\n", "array_count9([1, 2, 9]) → 1\n", "array_count9([1, 9, 9]) → 2\n", "array_count9([1, 9, 9, 3, 9]) → 3" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def array_count9(nums):\n", " \n", " count = 0\n", " \n", " # for i in range(len(nums)):\n", " for num in nums:\n", " if num == 9:\n", " count += 1\n", " \n", " \n", " return count" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_count9([1, 2, 9]) # 1" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_count9([1, 9, 9]) # 2" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_count9([1, 9, 9, 3, 9]) # 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.7 array_front_9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given an array of ints, return True if one of the first 4 elements in the array is a 9. The array length may be less than 4.\n", "\n", "\n", "array_front9([1, 2, 9, 3, 4]) → True\n", "array_front9([1, 2, 3, 4, 9]) → False\n", "array_front9([1, 2, 3, 4, 5]) → False" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "def array_front9(nums):\n", "\n", " sub = nums[:4]\n", " \n", " return 9 in sub" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_front9([1, 2, 9, 3, 4]) # True" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_front9([1, 2, 3, 4, 9]) # False" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_front9([1, 2, 3, 4, 5]) # False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.8 array_123" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given an array of ints, return True if the sequence of numbers 1, 2, 3 appears in the array somewhere.\n", "\n", "\n", "array123([1, 1, 2, 3, 1]) → True\n", "array123([1, 1, 2, 4, 1]) → False\n", "array123([1, 1, 2, 1, 2, 3]) → True" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "def array123(nums):\n", " \n", " sequence = [1,2,3]\n", " seqIdx = 0\n", " \n", " for num in nums:\n", " if seqIdx == len(sequence):\n", " break\n", " if sequence[seqIdx] == num:\n", " seqIdx += 1\n", " \n", " return seqIdx == len(sequence)\n", " \n", " \n", " # for i in range(len(nums)-2):\n", " # if nums[i]==1 and nums[i+1]==2 and nums[i+2]==3:\n", " # return True\n", " # return False" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array123([1, 1, 2, 3, 1]) # True" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array123([1, 1, 2, 4, 1]) # False" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array123([1, 1, 2, 1, 2, 3]) # True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.9 string_match" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. So \"xxcaazz\" and \"xxbaaz\" yields 3, since the \"xx\", \"aa\", and \"az\" substrings appear in the same place in both strings.\n", "\n", "\n", "string_match('xxcaazz', 'xxbaaz') → 3\n", "string_match('abc', 'abc') → 2\n", "string_match('abc', 'axc') → 0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def string_match(a, b):\n", " count = 0\n", " shorter = min(len(a), len(b))\n", " \n", " \n", " for i in range(shorter-1):\n", " sub_a = a[i:i+2]\n", " sub_b = b[i:i+2]\n", " \n", " if sub_a == sub_b:\n", " count += 1\n", " \n", " return count" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "string_match('xxcaazz', 'xxbaaz') # 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "string_match('abc', 'abc') # 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "string_match('abc', 'axc') # 0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.12.2" } }, "nbformat": 4, "nbformat_minor": 2 }