|
64 | 64 | "assert sys.version_info >= (3, 5)" |
65 | 65 | ], |
66 | 66 | "id": "DYxtfmnM4caC", |
67 | | - "execution_count": 1, |
| 67 | + "execution_count": null, |
68 | 68 | "outputs": [] |
69 | 69 | }, |
70 | 70 | { |
|
1402 | 1402 | "\n", |
1403 | 1403 | "The definition of the mode is the *most commonly occurring value* or feature across our data.\n", |
1404 | 1404 | "\n", |
1405 | | - "**Hint**: think about how you would solve this problem without code. You will probably need to keep track of the number of times each unique element appears in `data`. How might you use a *histogram* to help you achieve this?\n", |
1406 | | - "\n", |
1407 | | - "[TODO] add diagram of a histogram" |
| 1405 | + "**Hint**: think about how you would solve this problem without code. You will probably need to keep track of the number of times each unique element appears in `data`. How might you use a *histogram* to help you achieve this?" |
1408 | 1406 | ], |
1409 | 1407 | "id": "dietary-skating" |
1410 | 1408 | }, |
|
1537 | 1535 | "source": [ |
1538 | 1536 | "### Solution 2: Using a `Counter`\n", |
1539 | 1537 | "\n", |
| 1538 | + "Using the `dict` type is to compute histograms in Python - so often in fact, that the creators of the language decided to also create the `Counter` type.\n", |
1540 | 1539 | "\n", |
1541 | | - "TODO: add explanation\n", |
| 1540 | + "Simply put, `Counter` is just a child of the `dict` type with some extra properties. These properties simplify several operations that we typically perform on histograms.\n", |
1542 | 1541 | "\n", |
1543 | | - "[Python documentation](https://docs.python.org/3/library/collections.html#collections.Counter.most_common)\n" |
| 1542 | + "For example, let's see how we can find the mode of `dataset` by instantiating a `Counter` object, and then using the `most_commmon()` method:\n" |
1544 | 1543 | ], |
1545 | 1544 | "id": "28BJ_7PZbhEy" |
1546 | 1545 | }, |
|
1552 | 1551 | "source": [ |
1553 | 1552 | "from collections import Counter\n", |
1554 | 1553 | "\n", |
1555 | | - "\n", |
1556 | 1554 | "def compute_mode_2(dataset):\n", |
1557 | 1555 | " # A: compute the histogram\n", |
1558 | 1556 | " histogram = Counter(dataset)\n", |
|
1561 | 1559 | " return mode_value" |
1562 | 1560 | ], |
1563 | 1561 | "id": "V5AGm-fgdOmE", |
1564 | | - "execution_count": null, |
| 1562 | + "execution_count": 1, |
1565 | 1563 | "outputs": [] |
1566 | 1564 | }, |
| 1565 | + { |
| 1566 | + "cell_type": "markdown", |
| 1567 | + "metadata": { |
| 1568 | + "id": "_b6Q_jTh2iHI" |
| 1569 | + }, |
| 1570 | + "source": [ |
| 1571 | + "For more on the properties of the `Counter`, please visit the [Python documentation](https://docs.python.org/3/library/collections.html#collections.Counter)." |
| 1572 | + ], |
| 1573 | + "id": "_b6Q_jTh2iHI" |
| 1574 | + }, |
1567 | 1575 | { |
1568 | 1576 | "cell_type": "markdown", |
1569 | 1577 | "metadata": { |
|
1621 | 1629 | }, |
1622 | 1630 | "source": [ |
1623 | 1631 | "### Example Input\n", |
| 1632 | + "To wrap your head around technical questions like this, it often helps to just ignore all the verbiage - and create an analogy that helps you understand it clearly.\n", |
1624 | 1633 | "\n", |
1625 | | - "TODO: add scoreboard analogy?\n", |
| 1634 | + "For example, consider the values in `nums` like the scores of different players on an old, arcade video game machine. And let `k` represent the top highest scores in this game. Now, how would you go about building a scoreboard for this machine (such as shown below) with the top `k` scores listed at the top?\n", |
1626 | 1635 | "\n", |
1627 | | - "Example:\n", |
| 1636 | + "Use this example input to guide your thinking:\n", |
1628 | 1637 | "\n", |
1629 | 1638 | "```\n", |
1630 | 1639 | "Inputs:\n", |
|
2335 | 2344 | "\n", |
2336 | 2345 | "You are given a list of numbers, `nums`, and another integer, `target`. How would you implement Python code to find all the pairs of numbers found in `nums` that sum to `target`?\n", |
2337 | 2346 | "\n", |
| 2347 | + "Please return a 2D list of the pairs you find.\n", |
| 2348 | + "\n", |
2338 | 2349 | "You may assume that:\n", |
2339 | 2350 | "\n", |
2340 | | - "1. `nums` contains at least 2 integers, is unsorted, and contains no duplicate elements.\n", |
2341 | | - "2. You may return a 2D list of the pairs you find.\n", |
| 2351 | + "1. `nums` contains at least 2 integers.\n", |
| 2352 | + "2. `nums` is unsorted.\n", |
2342 | 2353 | "3. The ordering of the elements in each pair does not matter." |
2343 | 2354 | ], |
2344 | 2355 | "id": "alternate-trust" |
|
2363 | 2374 | "nums = [3, 5, 2, -4, 8, 11]" |
2364 | 2375 | ], |
2365 | 2376 | "id": "7jAsV3CgVmeQ", |
2366 | | - "execution_count": 2, |
| 2377 | + "execution_count": null, |
2367 | 2378 | "outputs": [] |
2368 | 2379 | }, |
2369 | 2380 | { |
|
2400 | 2411 | " return pairs" |
2401 | 2412 | ], |
2402 | 2413 | "id": "guided-kuwait", |
2403 | | - "execution_count": 4, |
| 2414 | + "execution_count": null, |
2404 | 2415 | "outputs": [ |
2405 | 2416 | { |
2406 | 2417 | "output_type": "stream", |
|
2434 | 2445 | "print(find_sum_pairs_1(target, nums))" |
2435 | 2446 | ], |
2436 | 2447 | "id": "VwYRUIPtVwrp", |
2437 | | - "execution_count": 5, |
| 2448 | + "execution_count": null, |
2438 | 2449 | "outputs": [ |
2439 | 2450 | { |
2440 | 2451 | "output_type": "stream", |
|
2476 | 2487 | " return pairs" |
2477 | 2488 | ], |
2478 | 2489 | "id": "TLb7wYxS5Ujg", |
2479 | | - "execution_count": 10, |
| 2490 | + "execution_count": null, |
2480 | 2491 | "outputs": [] |
2481 | 2492 | }, |
2482 | 2493 | { |
|
2502 | 2513 | "print(find_sum_pairs_2(target, nums))" |
2503 | 2514 | ], |
2504 | 2515 | "id": "A7fGWqGBV4FQ", |
2505 | | - "execution_count": 11, |
| 2516 | + "execution_count": null, |
2506 | 2517 | "outputs": [ |
2507 | 2518 | { |
2508 | 2519 | "output_type": "stream", |
|
2567 | 2578 | "end = [4, 5, 7, 10]" |
2568 | 2579 | ], |
2569 | 2580 | "id": "gFas01LmjvpS", |
2570 | | - "execution_count": 12, |
| 2581 | + "execution_count": null, |
2571 | 2582 | "outputs": [] |
2572 | 2583 | }, |
2573 | 2584 | { |
|
2603 | 2614 | " return max(num_patients)" |
2604 | 2615 | ], |
2605 | 2616 | "id": "first-hacker", |
2606 | | - "execution_count": 15, |
| 2617 | + "execution_count": null, |
2607 | 2618 | "outputs": [] |
2608 | 2619 | }, |
2609 | 2620 | { |
|
2629 | 2640 | "print(find_max_num_patients_1(start, end))" |
2630 | 2641 | ], |
2631 | 2642 | "id": "Iv7Sw8PTj3kq", |
2632 | | - "execution_count": 16, |
| 2643 | + "execution_count": null, |
2633 | 2644 | "outputs": [ |
2634 | 2645 | { |
2635 | 2646 | "output_type": "stream", |
|
2676 | 2687 | " return max_patient_count" |
2677 | 2688 | ], |
2678 | 2689 | "id": "vtujB3xpTzAW", |
2679 | | - "execution_count": 17, |
| 2690 | + "execution_count": null, |
2680 | 2691 | "outputs": [] |
2681 | 2692 | }, |
2682 | 2693 | { |
|
2702 | 2713 | "print(find_max_num_patients_2(start, end))" |
2703 | 2714 | ], |
2704 | 2715 | "id": "T3a3surzkKyh", |
2705 | | - "execution_count": 18, |
| 2716 | + "execution_count": null, |
2706 | 2717 | "outputs": [ |
2707 | 2718 | { |
2708 | 2719 | "output_type": "stream", |
|
2755 | 2766 | " return max_patient_count\n" |
2756 | 2767 | ], |
2757 | 2768 | "id": "CSstcxCQewSl", |
2758 | | - "execution_count": 19, |
| 2769 | + "execution_count": null, |
2759 | 2770 | "outputs": [] |
2760 | 2771 | }, |
2761 | 2772 | { |
|
2781 | 2792 | "print(find_max_num_patients_3(start, end))" |
2782 | 2793 | ], |
2783 | 2794 | "id": "DyRTM1k_kZ_s", |
2784 | | - "execution_count": 20, |
| 2795 | + "execution_count": null, |
2785 | 2796 | "outputs": [ |
2786 | 2797 | { |
2787 | 2798 | "output_type": "stream", |
|
2802 | 2813 | "\n", |
2803 | 2814 | "You are given a list of numbers, `nums`, and are asked to divide it into a certain number of partitions, `n_parts`.\n", |
2804 | 2815 | "\n", |
2805 | | - "How would you implement Python code to perform this task?\n", |
2806 | | - "\n", |
2807 | | - "TODO: should the reader assume we have to return a `list` data type?" |
| 2816 | + "How would you implement Python code to perform this task?" |
2808 | 2817 | ], |
2809 | 2818 | "id": "nonprofit-sigma" |
2810 | 2819 | }, |
|
2816 | 2825 | "source": [ |
2817 | 2826 | "### Example Input\n", |
2818 | 2827 | "\n", |
2819 | | - "You may assume `n_parts` is a nonnegative integer. For simplicity, don't worry about which elements go in which partition." |
| 2828 | + "You may assume `n_parts` is a positive integer. For simplicity, don't worry about which elements go in which partition." |
2820 | 2829 | ], |
2821 | 2830 | "id": "lqoRQjGclmiH" |
2822 | 2831 | }, |
|
2830 | 2839 | "n_parts = 2" |
2831 | 2840 | ], |
2832 | 2841 | "id": "lXWGM8i_l6Mc", |
2833 | | - "execution_count": 21, |
| 2842 | + "execution_count": null, |
2834 | 2843 | "outputs": [] |
2835 | 2844 | }, |
2836 | 2845 | { |
|
2859 | 2868 | " ]" |
2860 | 2869 | ], |
2861 | 2870 | "id": "cathedral-maple", |
2862 | | - "execution_count": 22, |
| 2871 | + "execution_count": null, |
2863 | 2872 | "outputs": [] |
2864 | 2873 | }, |
2865 | 2874 | { |
|
2885 | 2894 | "print(partition_1(nums, n_parts))" |
2886 | 2895 | ], |
2887 | 2896 | "id": "C6tY9jwQmGHG", |
2888 | | - "execution_count": 23, |
| 2897 | + "execution_count": null, |
2889 | 2898 | "outputs": [ |
2890 | 2899 | { |
2891 | 2900 | "output_type": "stream", |
|
2923 | 2932 | " return [list(part) for part in reshaped_array]" |
2924 | 2933 | ], |
2925 | 2934 | "id": "cpqBscYECoYx", |
2926 | | - "execution_count": 24, |
| 2935 | + "execution_count": null, |
2927 | 2936 | "outputs": [] |
2928 | 2937 | }, |
2929 | 2938 | { |
|
2959 | 2968 | "print(partition_1(nums, n_parts))" |
2960 | 2969 | ], |
2961 | 2970 | "id": "FBeyCa7EmLpW", |
2962 | | - "execution_count": 25, |
| 2971 | + "execution_count": null, |
2963 | 2972 | "outputs": [ |
2964 | 2973 | { |
2965 | 2974 | "output_type": "stream", |
|
3018 | 3027 | "threshold = 0.5" |
3019 | 3028 | ], |
3020 | 3029 | "id": "religious-cooking", |
3021 | | - "execution_count": 26, |
| 3030 | + "execution_count": null, |
3022 | 3031 | "outputs": [] |
3023 | 3032 | }, |
3024 | 3033 | { |
|
3044 | 3053 | " print((element, index_row, index_element)) " |
3045 | 3054 | ], |
3046 | 3055 | "id": "distributed-emission", |
3047 | | - "execution_count": 27, |
| 3056 | + "execution_count": null, |
3048 | 3057 | "outputs": [] |
3049 | 3058 | }, |
3050 | 3059 | { |
|
3060 | 3069 | "index_cosine_matrix(matrix, threshold)" |
3061 | 3070 | ], |
3062 | 3071 | "id": "hired-nickel", |
3063 | | - "execution_count": 28, |
| 3072 | + "execution_count": null, |
3064 | 3073 | "outputs": [ |
3065 | 3074 | { |
3066 | 3075 | "output_type": "stream", |
|
0 commit comments