{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 人工智能与机器学习-实验1\n",
    "\n",
    "## Part.I Python语言练习\n",
    "\n",
    "|学号       |姓名    |\n",
    "|----------|--------|\n",
    "|***REMOVED***|***REMOVED***|\n",
    "|2020113874|何一涛|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 名人名言\n",
    "\n",
    "找一句你钦佩的名人说的名言，将名人的姓名存储在变量`famous_person`中，再将他说的名言存储在变量`message`中，然后如下显示出该名人说了该名言（包括引号）：\n",
    "\n",
    "`xxx once said, \"......\"`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Linus Torvalds once said, \"So nvidia, fuck you.\"\n"
     ]
    }
   ],
   "source": [
    "famous_person=\"Linus Torvalds\"\n",
    "message=\"So nvidia, fuck you.\"\n",
    "print(\"%s once said, \\\"%s\\\"\" %(famous_person, message))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 朋友\n",
    "\n",
    "将一些朋友的姓名存储在一个列表中，并将其命名为`names`。\n",
    "\n",
    "1. 依次显示出列表中所有朋友的姓名；\n",
    "\n",
    "2. 使用`sort()`将列表中的姓名按字典顺序排序后，再显示出排好序的所有朋友的命名；\n",
    "\n",
    "3. 将`\"Albert Einstein\"`添加到你的朋友姓名列表中；\n",
    "\n",
    "4. 显示列表的长度；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Linus Torvalds', 'Richard Stallman', 'Greg Kroah-Hartman']\n"
     ]
    }
   ],
   "source": [
    "names=[\"Linus Torvalds\", \"Richard Stallman\", \"Greg Kroah-Hartman\"]\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Greg Kroah-Hartman', 'Linus Torvalds', 'Richard Stallman']\n"
     ]
    }
   ],
   "source": [
    "names.sort()\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Greg Kroah-Hartman', 'Linus Torvalds', 'Richard Stallman', 'Albert Einstein']\n"
     ]
    }
   ],
   "source": [
    "names.append(\"Albert Einstein\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "print(len(names))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 3的倍数\n",
    "\n",
    "创建一个列表，其中包含3~30内能被3 整除的数字；再使用一个`for`循环将这个列表中的数字都打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 6 9 12 15 18 21 24 27 "
     ]
    }
   ],
   "source": [
    "list=[]\n",
    "for i in range(3,30):\n",
    "    if i%3==0:\n",
    "        list.append(i)\n",
    "for i in list:\n",
    "    print(i,end=\" \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. 登录信息\n",
    "\n",
    "创建一个至少包含5 个用户名的列表，且其中一个用户名为`'admin'`。想象你要编写代码，在每位用户登录网站后都打印一条问候消息。遍历用户名列表，并向每位用户打印一条问候消息。如果用户名为`'admin'`，就打印一条特殊的问候消息，如`“Hello admin, would you like to see a status report?”`。否则，打印一条普通的问候消息，如`“Hello Eric, thank you for logging in again”`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello admin, would you like to see a status report?\n",
      "Hello user1, thank you for logging in again.\n",
      "Hello user2, thank you for logging in again.\n",
      "Hello user3, thank you for logging in again.\n",
      "Hello user4, thank you for logging in again.\n"
     ]
    }
   ],
   "source": [
    "user=[\"admin\", \"user1\", \"user2\", \"user3\", \"user4\"]\n",
    "for i in user:  \n",
    "    if i==\"admin\":\n",
    "        print(\"Hello admin, would you like to see a status report?\")\n",
    "    else:\n",
    "        print(\"Hello %s, thank you for logging in again.\" %i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. 升序降序\n",
    "\n",
    "给你一个整数组成的列表`L`，按照下列条件输出：\n",
    "* 若`L`是升序排列的,则输出\"UP\";\n",
    "* 若`L`是降序排列的,则输出\"DOWN\";\n",
    "* 若`L`无序，则输出\"WRONG\"。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "UP\n",
      "DOWN\n",
      "WRONG\n"
     ]
    }
   ],
   "source": [
    "up=[1,5,7,9,10]\n",
    "down=[11,9,7,5,3]\n",
    "wrong=[2,4,6,8,5]\n",
    "\n",
    "def order(list:list)->str:\n",
    "    if list==sorted(list):\n",
    "        return \"UP\"\n",
    "    elif list==sorted(list, reverse=True):\n",
    "        return \"DOWN\"\n",
    "    else:\n",
    "        return \"WRONG\"\n",
    "\n",
    "print(order(up))\n",
    "print(order(down))\n",
    "print(order(wrong))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. IP地址\n",
    "\n",
    "互联网上的每台计算机都有一个IP，合法的IP格式为：`A.B.C.D`。其中`A`、`B`、`C`、`D`均为[0, 255]中的整数。为了简单起见，我们规定这四个整数中不允许有前导零存在，如001。\n",
    "\n",
    "现在给你一个字符串`s`（`s`不含空白符），请你判断`s`是不是合法IP，若是，输出`Yes`，否则输出`No`。如：s=“202.115.32.24”，则输出Yes；s=“a.11.11.11”, 则输出No。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yes; s=\"202.115.32.24\"\n",
      "No\n",
      "No\n"
     ]
    }
   ],
   "source": [
    "def isIP(s:str)->bool:\n",
    "    if len(s.split(\".\"))==4:\n",
    "        for i in s.split(\".\"):\n",
    "            if i.isdigit() and 0<=int(i)<=255:\n",
    "                if(int(i)!=0 and i[0]==\"0\"):\n",
    "                    return False\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "s=[\"202.115.32.24\",\"002.115.32.24\",\"256.115.32.24\"]\n",
    "for i in s:\n",
    "    if(isIP(i)):\n",
    "        print(\"Yes; s=\\\"%s\\\"\" %i)\n",
    "    else:\n",
    "        print(\"No\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7. 三明治\n",
    "\n",
    "创建一个名为`sandwich_orders`的列表，在其中包含各种三明治的名字；\n",
    "\n",
    "再创建一个名为`finished_sandwiches`的空列表。\n",
    "\n",
    "遍历列表`sandwich_orders`，对于其中的每种三明治，都打印一条消息，如I made your tuna sandwich，并将其移到列表`finished_sandwiches`。\n",
    "\n",
    "所有三明治都制作好后，打印一条消息，将这些三明治列出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I made your chicken sandwich.\n",
      "I made your beef sandwich.\n",
      "I made your tuna sandwich.\n",
      "All sandwiches are made.\n",
      "sandwich_orders: []\n",
      "finished_sandwiches: ['chicken', 'beef', 'tuna']\n"
     ]
    }
   ],
   "source": [
    "sandwich_orders=[\"tuna\", \"beef\", \"chicken\"]\n",
    "finished_sandwiches=[]\n",
    "while sandwich_orders:\n",
    "    buf = sandwich_orders.pop()\n",
    "    print(\"I made your %s sandwich.\" %buf)\n",
    "    finished_sandwiches.append(buf)\n",
    "print(\"All sandwiches are made.\")\n",
    "print(\"sandwich_orders: %s\" %sandwich_orders)\n",
    "print(\"finished_sandwiches: %s\" %finished_sandwiches)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8. 城市\n",
    "\n",
    "创建一个名为`cities`的字典，其中将三个城市名用作键；对于每座城市，都创建一个字典，并在其中包含该城市所属的国家、人口约数以及一个有关该城市的事实。在表示每座城市的字典中，应包含`country`、`population`和`fact`等键。将每座城市的名字以及有关它们的信息都打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "City: Beijing\n",
      "country: China\n",
      "population: 21500000\n",
      "fact: Capital of China\n",
      "City: Tokyo\n",
      "country: Japan\n",
      "population: 13500000\n",
      "fact: Capital of Japan\n",
      "City: New York\n",
      "country: USA\n",
      "population: 53500000\n",
      "fact: Biggest city of USA\n"
     ]
    }
   ],
   "source": [
    "cities={\"Beijing\":{\"country\":\"China\", \"population\":21500000, \"fact\":\"Capital of China\"}, \\\n",
    "    \"Tokyo\":{\"country\":\"Japan\", \"population\":13500000, \"fact\":\"Capital of Japan\"},  \\\n",
    "    \"New York\":{\"country\":\"USA\", \"population\":53500000, \"fact\":\"Biggest city of USA\"}  }\n",
    "for city, info in cities.items():\n",
    "    print(\"City: %s\" %city)\n",
    "    for k, v in info.items():\n",
    "        print(\"%s: %s\" %(k, v))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 9. 时间\n",
    "\n",
    "给你一个时间`t`，`t`是一个字典，共有六个字符串键`(year, month, day, hour, minute, second)`，每个值为数字组成的字符串，如`t = {‘year’:‘2013’, ‘month’:‘9’, ‘day’:‘30’, ‘hour’:‘16’, ‘minute’:‘45’, ‘second’:‘2‘}`。\n",
    "\n",
    "请将其按照以下格式输出：`XXXX-XX-XX XX:XX:XX`。如上例应该输出：`2013-09-30 16:45:02`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2013-09-30 16:45:02\n"
     ]
    }
   ],
   "source": [
    "t = {\"year\":\"2013\", \"month\":\"9\", \"day\":\"30\", \"hour\":\"16\", \"minute\":\"45\", \"second\":\"2\"}\n",
    "print(\"%s-%02d-%02d %02d:%02d:%02d\" %(t[\"year\"], int(t[\"month\"]), int(t[\"day\"]), int(t[\"hour\"]), int(t[\"minute\"]), int(t[\"second\"])))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10. 最喜欢的书\n",
    "\n",
    "编写一个名为`favorite_book()`的函数，其中包含一个名为`title`的形参。这个函数打印一条消息，如One of my favorite books is Alice in Wonderland。调用这个函数，并将一本图书的名称作为实参传递给它。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "One of my favorite books is Alice in Wonderland.\n"
     ]
    }
   ],
   "source": [
    "def favorite_book(title:str)->None:\n",
    "    print(\"One of my favorite books is %s.\" %title)\n",
    "\n",
    "favorite_book(\"Alice in Wonderland\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.7"
  },
  "vscode": {
   "interpreter": {
    "hash": "cdd56286faa2ee04874a677dc80784b0c94990d70e3798725c60e428b9bb159e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
