DefinetlyNotAI/AlgoPy

View on GitHub
jupyter/find.ipynb

Summary

Maintainability
Test Coverage
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Find:\n",
    "    class InArray:\n",
    "        @staticmethod\n",
    "        def __sort(List: list) -> list[int | float]:\n",
    "            \"\"\"\n",
    "            Sort a list of integers and floats.\n",
    "\n",
    "            :param List: The list to sort.\n",
    "            :return: A sorted list of integers and floats.\n",
    "            :raises Exception: If the input list is None.\n",
    "            \"\"\"\n",
    "            if List is None:\n",
    "                raise Exception(\"No input given.\")\n",
    "            converted_list = sorted(\n",
    "                float(item) for item in List if isinstance(item, (int, float))\n",
    "            )\n",
    "            return [int(item) if item.is_integer() else item for item in converted_list]\n",
    "\n",
    "        @classmethod\n",
    "        def largest_number(cls, List: list[int | float]) -> int | float:\n",
    "            \"\"\"\n",
    "            Find the largest number in a list of integers and floats.\n",
    "\n",
    "            :param List: The list to search.\n",
    "            :return: The largest number in the list.\n",
    "            :raises Exception: If the input list is None.\n",
    "            \"\"\"\n",
    "            if List is None:\n",
    "                raise Exception(\"No input given.\")\n",
    "            sorted_list = cls.__sort(List)\n",
    "            return sorted_list[-1] if sorted_list else None\n",
    "\n",
    "        @classmethod\n",
    "        def smallest_number(cls, List: list[int | float]) -> int | float:\n",
    "            \"\"\"\n",
    "            Find the smallest number in a list of integers and floats.\n",
    "\n",
    "            :param List: The list to search.\n",
    "            :return: The smallest number in the list.\n",
    "            :raises Exception: If the input list is None.\n",
    "            \"\"\"\n",
    "            if List is None:\n",
    "                raise Exception(\"No input given.\")\n",
    "            sorted_list = cls.__sort(List)\n",
    "            return sorted_list[0] if sorted_list else None\n",
    "\n",
    "        class Objects:\n",
    "            @staticmethod\n",
    "            def index_all(List: list, value_to_find: any) -> list[int]:\n",
    "                \"\"\"\n",
    "                Find all indices of a value in a list.\n",
    "\n",
    "                :param List: The list to search.\n",
    "                :param value_to_find: The value to find.\n",
    "                :return: A list of indices where the value is found.\n",
    "                :raises Exception: If the input list or value is None.\n",
    "                \"\"\"\n",
    "                if List is None or value_to_find is None:\n",
    "                    raise Exception(\"No input given.\")\n",
    "                return [\n",
    "                    index for index, value in enumerate(List) if value == value_to_find\n",
    "                ]\n",
    "\n",
    "            @staticmethod\n",
    "            def index(List: list, value_to_find: any) -> int | bool:\n",
    "                \"\"\"\n",
    "                Find the first index of a value in a list.\n",
    "\n",
    "                :param List: The list to search.\n",
    "                :param value_to_find: The value to find.\n",
    "                :return: The index of the value if found, otherwise False.\n",
    "                :raises Exception: If the input list or value is None.\n",
    "                \"\"\"\n",
    "                if List is None or value_to_find is None:\n",
    "                    raise Exception(\"No input given.\")\n",
    "                try:\n",
    "                    return List.index(value_to_find)\n",
    "                except ValueError:\n",
    "                    return False\n",
    "\n",
    "    class InSentence:\n",
    "        @staticmethod\n",
    "        def longest_word(text: str) -> str:\n",
    "            \"\"\"\n",
    "            Find the longest word in a sentence.\n",
    "\n",
    "            :param text: The sentence to search.\n",
    "            :return: The longest word in the sentence.\n",
    "            :raises Exception: If the input text is None.\n",
    "            \"\"\"\n",
    "            if text is None:\n",
    "                raise Exception(\"No input given.\")\n",
    "            words = text.split()\n",
    "            return max(words, key=len) if words else \"\"\n",
    "\n",
    "        @staticmethod\n",
    "        def shortest_word(text: str) -> str:\n",
    "            \"\"\"\n",
    "            Find the shortest word in a sentence.\n",
    "\n",
    "            :param text: The sentence to search.\n",
    "            :return: The shortest word in the sentence.\n",
    "            :raises Exception: If the input text is None.\n",
    "            \"\"\"\n",
    "            if text is None:\n",
    "                raise Exception(\"No input given.\")\n",
    "            words = text.split()\n",
    "            return min(words, key=len) if words else \"\"\n",
    "\n",
    "        class Word:\n",
    "            @staticmethod\n",
    "            def exists(text: str, word_to_find: str) -> bool:\n",
    "                \"\"\"\n",
    "                Check if a word exists in a sentence.\n",
    "\n",
    "                :param text: The sentence to search.\n",
    "                :param word_to_find: The word to find.\n",
    "                :return: True if the word exists, otherwise False.\n",
    "                :raises Exception: If the input text or word is None.\n",
    "                \"\"\"\n",
    "                if text is None or word_to_find is None:\n",
    "                    raise Exception(\"No input given.\")\n",
    "                return word_to_find in text\n",
    "\n",
    "            @staticmethod\n",
    "            def occurrences(text: str, word_to_count: str) -> int:\n",
    "                \"\"\"\n",
    "                Count the occurrences of a word in a sentence.\n",
    "\n",
    "                :param text: The sentence to search.\n",
    "                :param word_to_count: The word to count.\n",
    "                :return: The number of occurrences of the word.\n",
    "                :raises Exception: If the input text or word is None.\n",
    "                \"\"\"\n",
    "                if text is None or word_to_count is None:\n",
    "                    raise Exception(\"No input given.\")\n",
    "                return text.lower().split().count(word_to_count.lower())\n",
    "\n",
    "    class InWord:\n",
    "        @classmethod\n",
    "        def total_vowels(cls, Word: str) -> int:\n",
    "            \"\"\"\n",
    "            Count the total number of vowels in a word.\n",
    "\n",
    "            :param Word: The word to search.\n",
    "            :return: The total number of vowels in the word.\n",
    "            :raises Exception: If the input word is None.\n",
    "            \"\"\"\n",
    "            if Word is None:\n",
    "                raise Exception(\"No input given.\")\n",
    "            vowels = cls.__vowel_y(Word)\n",
    "            return sum(1 for char in Word if char in vowels)\n",
    "\n",
    "        @staticmethod\n",
    "        def __vowel_y(string: str, only_lowercase=False) -> str:\n",
    "            \"\"\"\n",
    "            Determine the set of vowels to use, including 'y' in certain cases.\n",
    "\n",
    "            :param string: The word to check.\n",
    "            :param only_lowercase: Whether to include only lowercase vowels.\n",
    "            :return: A string of vowels to use.\n",
    "            :raises Exception: If the input string is None.\n",
    "            \"\"\"\n",
    "            if string is None:\n",
    "                raise Exception(\"No input given.\")\n",
    "            if string in [\n",
    "                \"Cry\",\n",
    "                \"Dry\",\n",
    "                \"Gym\",\n",
    "                \"Hymn\",\n",
    "                \"Lynx\",\n",
    "                \"Myth\",\n",
    "                \"Pry\",\n",
    "                \"Rhythm\",\n",
    "                \"Shy\",\n",
    "                \"Spy\",\n",
    "                \"Spry\",\n",
    "                \"Sync\",\n",
    "                \"Try\",\n",
    "                \"Why\",\n",
    "                \"City\",\n",
    "                \"Party\",\n",
    "                \"Fly\",\n",
    "                \"Shy\",\n",
    "                \"Wary\",\n",
    "                \"Worthwhile\",\n",
    "                \"Type\",\n",
    "                \"Typical\",\n",
    "                \"Thyme\",\n",
    "                \"Cyst\",\n",
    "                \"Symbol\",\n",
    "                \"System\",\n",
    "                \"Lady\",\n",
    "                \"Pretty\",\n",
    "                \"Very\",\n",
    "                \"Deny\",\n",
    "                \"Daddy\",\n",
    "                \"Quickly\",\n",
    "            ]:\n",
    "                return \"aeiouy\" if only_lowercase else \"aeiouyAEIOUY\"\n",
    "            return \"aeiou\" if only_lowercase else \"aeiouAEIOU\"\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}