{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# <center>NumPy Sorting and Searching [8 exercises with solution]</center>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. Write a NumPy program to sort a given array of shape 2 along the first axis, last axis and on flattened array.\n", "\n", "Original array: <br> \n", "[[10 40]<br>\n", "[30 20]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Write a NumPy program to create a structured array from given student name, height, class and their data types. Now sort the array on height.\n", "\n", "Original array:\n", "[(b'James', 5, 48.5 ) (b'Nail', 6, 52.5 ) (b'Paul', 5, 42.1 )(b'Pit', 5, 40.11)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Write a NumPy program to create a structured array from given student name, height, class and their data types. Now sort by class, then height if class are equal. \n", "\n", "Original array:\n", "[(b'James', 5, 48.5 ) (b'Nail', 6, 52.5 ) (b'Paul', 5, 42.1 ) (b'Pit', 5, 40.11)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. Write a NumPy program to sort the student id with increasing height of the students from given students id and height. Print the integer indices that describes the sort order by multiple columns and the sorted data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5. Write a NumPy program to get the indices of the sorted elements of a given array.\n", "\n", "Original array:\n", "[1023 5202 6230 1671 1682 5241 4532]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6. Write a NumPy program to sort a given complex array using the real part first, then the imaginary part. Go to the editor\n", "\n", "Note: \"busday\" default of Monday through Friday being valid days.\n", "\n", "Original array:\n", "[(1+2j), (3-1j), (3-2j), (4-3j), (3+5j)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7. Write a NumPy program to partition a given array in a specified position and move all the smaller elements values to the left of the partition, and the remaining values to the right, in arbitrary order (based on random choice). \n", "\n", "Original array:\n", "[ 70 50 20 30 -11 60 50 40]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8. Write a NumPy program to sort the specified number of elements from beginning of a given array.\n", "\n", "Original array:\n", "[0.39536213 0.11779404 0.32612381 0.16327394 0.98837963 0.25510787 0.01398678 0.15188239 0.12057667 0.67278699]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Problem's Source: [https://www.w3resource.com/python-exercises/numpy/python-numpy-sorting-and-searching.php](https://www.w3resource.com/python-exercises/numpy/python-numpy-sorting-and-searching.php)" ] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }