{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 01. 编写自定义函数fun1，输入参数为两个数字a和b，返回值为a和b的平方和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2fc8bc47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n"
     ]
    }
   ],
   "source": [
    "def fun1(a, b):\n",
    "    return a ** 2 + b ** 2\n",
    "\n",
    "print(fun1(3, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28caaee0",
   "metadata": {},
   "source": [
    "## 02. 编写自定义函数fun2，输入参数为三个数字x、y和z，返回值为一个元组，该元组是x、y和z按从小到大顺序排列的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b51997dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 5, 8)\n"
     ]
    }
   ],
   "source": [
    "def fun2(x, y, z):\n",
    "    return tuple(sorted((x, y, z)))\n",
    "\n",
    "print(fun2(5, 2, 8))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21d08168",
   "metadata": {},
   "source": [
    "## 03. 编写自定义函数fun3，函数参数为多个数字，返回值为所有参数的和，要求使用不定长参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dc38e37a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "source": [
    "def fun3(*args):\n",
    "    return sum(args)\n",
    "\n",
    "print(fun3(1, 2, 3, 4, 5))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "521a43be",
   "metadata": {},
   "source": [
    "## 04. 编写自定义函数fun4，输入参数为字符串s，返回值为字符串中英文字母、空格、数字和其它字符的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b14b04c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母: 10, 空格: 2, 数字: 3, 其它字符: 1\n"
     ]
    }
   ],
   "source": [
    "def fun4(s):\n",
    "    letters = 0\n",
    "    spaces = 0\n",
    "    digits = 0\n",
    "    others = 0\n",
    "    for ch in s:\n",
    "        if ch.isalpha():\n",
    "            letters += 1\n",
    "        elif ch == ' ':\n",
    "            spaces += 1\n",
    "        elif ch.isdigit():\n",
    "            digits += 1\n",
    "        else:\n",
    "            others += 1\n",
    "    return letters, spaces, digits, others\n",
    "\n",
    "s = 'Hello World! 123'\n",
    "letters, spaces, digits, others = fun4(s)\n",
    "print(f'英文字母: {letters}, 空格: {spaces}, 数字: {digits}, 其它字符: {others}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9aa7e8cd",
   "metadata": {},
   "source": [
    "## 05. 字典排序：通过sorted函数和匿名函数对字典键值对按值排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a91a4de1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('王五', 79), ('张三', 85), ('李四', 92)]\n"
     ]
    }
   ],
   "source": [
    "dic1 = {'张三': 85, '李四': 92, '王五': 79}\n",
    "\n",
    "result = sorted(dic1.items(), key=lambda x: x[1])\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "314ef7fe",
   "metadata": {},
   "source": [
    "## 06. 编写自定义函数fun5，输入参数为正整数N，返回值为1+2+...+N的结果，要求使用递归函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f7f3b97e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "def fun5(N):\n",
    "    if N == 1:\n",
    "        return 1\n",
    "    return N + fun5(N - 1)\n",
    "\n",
    "print(fun5(100))"
   ]
  }
 ],
 "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.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
