{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.1 first_last6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given an array of ints, return True if 6 appears as either the first or last element in the array. The array will be length 1 or more.\n", "\n", "\n", "first_last6([1, 2, 6]) → True\n", "first_last6([6, 1, 2, 3]) → True\n", "first_last6([13, 6, 1, 2, 3]) → False" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def first_last6(nums):\n", " \n", " return 6 == nums[0] or 6 == nums[-1]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_last6([1, 2, 6]) # True" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_last6([6, 1, 2, 3]) # True" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_last6([13, 6, 1, 2, 3]) # False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.2 same_first_last" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Given an array of ints, return True if the array is length 1 or more, and the first element and the last element are equal.\n", "\n", "\n", "same_first_last([1, 2, 3]) → False\n", "same_first_last([1, 2, 3, 1]) → True\n", "same_first_last([1, 2, 1]) → True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def same_first_last(nums):\n", " \n", " return len(nums) > 0 and nums[0] == nums[-1]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "same_first_last([1, 2, 3]) # False" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "same_first_last([1, 2, 3, 1]) # True" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "same_first_last([1, 2, 1]) # True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.3 make_pi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}.\n", "\n", "\n", "make_pi() → [3, 1, 4]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "def make_pi():\n", " \n", " return [3,1,4]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 1, 4]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_pi()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.4 common_end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Given 2 arrays of ints, a and b, return True if they have the same first element or they have the same last element. Both arrays will be length 1 or more.\n", "\n", "\n", "common_end([1, 2, 3], [7, 3]) → True\n", "common_end([1, 2, 3], [7, 3, 2]) → False\n", "common_end([1, 2, 3], [1, 3]) → True" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def common_end(a, b):\n", " first_same = a[0] == b[0]\n", " last_same = a[-1] == b[-1]\n", " \n", " return first_same or last_same" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "common_end([1, 2, 3], [7, 3]) # True" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "common_end([1, 2, 3], [7, 3, 2]) # False" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "common_end([1, 2, 3], [1, 3]) # True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.5 sum_3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Given an array of ints length 3, return the sum of all the elements.\n", "\n", "\n", "sum3([1, 2, 3]) → 6\n", "sum3([5, 11, 2]) → 18\n", "sum3([7, 0, 0]) → 7" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "def sum3(nums):\n", " sum = 0\n", " \n", " for num in nums:\n", " sum += num\n", " \n", " return sum" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum3([1, 2, 3]) # 6" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum3([5, 11, 2]) # 18" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum3([7, 0, 0]) # 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.6 rotate_left_3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Given an array of ints length 3, return an array with the elements \"rotated left\" so {1, 2, 3} yields {2, 3, 1}.\n", "\n", "\n", "rotate_left3([1, 2, 3]) → [2, 3, 1]\n", "rotate_left3([5, 11, 9]) → [11, 9, 5]\n", "rotate_left3([7, 0, 0]) → [0, 0, 7]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "def rotate_left3(nums):\n", " \n", " return [nums[1], nums[2], nums[0]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 1]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rotate_left3([1, 2, 3]) # [2, 3, 1]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[11, 9, 5]" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rotate_left3([5, 11, 9]) # [11, 9, 5]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 0, 7]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rotate_left3([7, 0, 0]) # [0, 0, 7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.7 rotate_left_3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}.\n", "\n", "\n", "reverse3([1, 2, 3]) → [3, 2, 1]\n", "reverse3([5, 11, 9]) → [9, 11, 5]\n", "reverse3([7, 0, 0]) → [0, 0, 7]" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "def reverse3(nums):\n", " \n", " return nums[::-1]" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 1]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reverse3([1, 2, 3]) # [3, 2, 1]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[9, 11, 5]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reverse3([5, 11, 9]) # [9, 11, 5]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 0, 7]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reverse3([7, 0, 0]) # [0, 0, 7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.8 max_end_3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Given an array of ints length 3, figure out which is larger, the first or last element in the array, and set all the other elements to be that value. Return the changed array.\n", "\n", "\n", "max_end3([1, 2, 3]) → [3, 3, 3]\n", "max_end3([11, 5, 9]) → [11, 11, 11]\n", "max_end3([2, 11, 3]) → [3, 3, 3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "def max_end3(nums):\n", " max_val = max(nums[0], nums[-1])\n", " res = []\n", " \n", " for i in range(3):\n", " res.append(max_val)\n", " \n", " return res" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 3, 3]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max_end3([1, 2, 3]) # [3, 3, 3]" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[11, 11, 11]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max_end3([11, 5, 9]) # [11, 11, 11]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 3, 3]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max_end3([2, 11, 3]) # [3, 3, 3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.9 sum_2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given an array of ints, return the sum of the first 2 elements in the array. If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0.\n", "\n", "sum2([1, 2, 3]) → 3\n", "sum2([1, 1]) → 2\n", "sum2([1, 1, 1, 1]) → 2" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "def sum2(nums):\n", " sum = 0\n", " nums_end = len(nums)\n", "\n", " if nums_end == 0:\n", " return sum\n", " elif nums_end < 2:\n", " sum += nums[0]\n", " else:\n", " sum += nums[0] + nums[1]\n", " \n", " return sum" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum2([1, 2, 3]) # 3" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum2([1, 1]) # 2" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum2([1, 1, 1, 1]) # 2" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n" ] } ], "source": [ "for i in range(2):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.10 middle_way" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements.\n", "\n", "\n", "middle_way([1, 2, 3], [4, 5, 6]) → [2, 5]\n", "middle_way([7, 7, 7], [3, 8, 0]) → [7, 8]\n", "middle_way([5, 2, 9], [1, 4, 5]) → [2, 4]" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "def middle_way(a, b):\n", " res = []\n", " res.append(a[1])\n", " res.append(b[1])\n", " \n", " return res" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 5]" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "middle_way([1, 2, 3], [4, 5, 6]) # [2, 5]" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[7, 8]" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "middle_way([7, 7, 7], [3, 8, 0]) # [7, 8]" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 4]" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "middle_way([5, 2, 9], [1, 4, 5]) # [2, 4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.11 make_ends" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more.\n", "\n", "\n", "make_ends([1, 2, 3]) → [1, 3]\n", "make_ends([1, 2, 3, 4]) → [1, 4]\n", "make_ends([7, 4, 6, 2]) → [7, 2]" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "def make_ends(nums):\n", " first = nums[0]\n", " last = nums[-1]\n", " \n", " res = []\n", " res.append(first)\n", " res.append(last)\n", " \n", " return res" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3]" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_ends([1, 2, 3]) # [1, 3]" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4]" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_ends([1, 2, 3, 4]) # [1, 4]" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[7, 2]" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "make_ends([7, 4, 6, 2]) # [7, 2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...\n", "\n", "...\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.12 has_23" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given an int array length 2, return True if it contains a 2 or a 3.\n", "\n", "\n", "has23([2, 5]) → True\n", "has23([4, 3]) → True\n", "has23([4, 5]) → False" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "def has23(nums):\n", " \n", " return 2 in nums or 3 in nums" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "has23([2, 5]) # True" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "has23([4, 3]) # True" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "has23([4, 5]) # False" ] }, { "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": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.12.2" } }, "nbformat": 4, "nbformat_minor": 2 }