Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Вольнов Фёдор ИП214 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 237 additions & 0 deletions 03ip214/Pr02.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "82c6045f-466b-435d-827b-c69c7d1b99d2",
"metadata": {},
"source": [
"# Практическая работа №2"
]
},
{
"cell_type": "markdown",
"id": "1475d839-a1ea-4bd4-8631-909bb21d45f5",
"metadata": {},
"source": [
"## Задание 3.0"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4d46b466-d726-4ea4-b954-6a84d7a73600",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1 \n"
]
}
],
"source": [
"n = 27\n",
"\n",
"while True:\n",
" print(n, end=' ')\n",
" if n == 1:\n",
" break\n",
" if n % 2:\n",
" n = 3 * n + 1\n",
" else:\n",
" n = n // 2\n",
"print()"
]
},
{
"cell_type": "markdown",
"id": "200135c2-cad7-434b-8c51-a47b54b696ab",
"metadata": {},
"source": [
"## Задание 3.1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2ef34f4a-2182-407b-a859-e588496b0a4d",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"try:\n",
" n = int(sys.argv[1])\n",
" if n < 1:\n",
" raise ValueError\n",
"except (IndexError, ValueError):\n",
" print('Введите число больше 0 в командную строку')\n",
" sys.exit(1)\n",
"\n",
"while True:\n",
" print(n, end=' ')\n",
" if n == 1:\n",
" break\n",
" if n % 2:\n",
" n = 3 * n + 1\n",
" else:\n",
" n = n // 2\n",
"print()"
]
},
{
"cell_type": "markdown",
"id": "7bd26cf2-d748-4a12-9fc1-7e8a03d84f3c",
"metadata": {},
"source": [
"## Задание 3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15339645-baae-4f02-bd91-69d29c16c483",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import math\n",
"\n",
"haversin = lambda alpha: math.sin(alpha/2)**2\n",
"\n",
"def gc_distance(loc1, loc2, R=6378.1):\n",
" (phi1, lambda1), (phi2, lambda2) = loc1, loc2\n",
" d = 2 * R * math.asin(math.sqrt(haversin(phi2-phi1) + math.cos(phi1)*math.cos(phi2)*haversin(lambda2-lambda1)))\n",
" return d\n",
"\n",
"loc1, loc2 = sys.argv[1:]\n",
"phi1, lambda1 = [math.radians(float(x)) for x in loc1.split(',')]\n",
"phi2, lambda2 = [math.radians(float(x)) for x in loc2.split(',')]\n",
"\n",
"d = gc_distance((phi1, lambda1), (phi2, lambda2))\n",
"print('{} km'.format(int(d)))"
]
},
{
"cell_type": "markdown",
"id": "35f05b60-d423-4e39-8e80-4bea34315cdc",
"metadata": {},
"source": [
"## Задание 3.3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb152e4e-d13c-42a2-ab61-7dc16071f860",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import math\n",
"\n",
"nfigs = 20\n",
"HOME = os.getenv('HOME')\n",
"pathdir = os.path.join(HOME, 'test')\n",
"if not os.path.exists(pathdir):\n",
" os.mkdir(pathdir)\n",
"\n",
"canvas_height, canvas_width = 500, 500\n",
"cx, cy = canvas_height / 2, canvas_width / 2\n",
"r1, r2 = 200, 20\n",
"\n",
"alphas = [2*math.pi / nfigs * n for n in range(nfigs)]\n",
"\n",
"for n, alpha in enumerate(alphas):\n",
" filename = os.path.join(pathdir, 'fig{:02d}.svg'.format(n))\n",
" with open(filename, 'w') as fo:\n",
" # SVG preamble\n",
" print(\"\"\"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
" <svg xmlns=\"http://www.w3.org/2000/svg\"\n",
" xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n",
" width=\"{}\" height=\"{}\" style=\"background: {}\">\"\"\".format(\n",
" canvas_width, canvas_height, '#ffffff'), file=fo)\n",
" print('<circle cx=\"{}\" cy=\"{}\" r=\"{}\" style=\"stroke: black;'\n",
" ' stroke-width: 2px; fill: none;\"/>'.format(cx, cy, r1),file=fo)\n",
" cx2, cy2 = cx+(r1-r2) * math.cos(alpha), cy+(r1-r2) * math.sin(alpha)\n",
" print('<circle cx=\"{}\" cy=\"{}\" r=\"{}\" style=\"stroke: red; fill: red;\"/>'\n",
" .format(cx2, cy2, r2), file=fo)\n",
" print('</svg>', file=fo)"
]
},
{
"cell_type": "markdown",
"id": "35946f8f-1adf-46e3-919a-331730a929e7",
"metadata": {},
"source": [
"## Задание 3.4"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2153350-e452-405c-92c6-f0909e61e178",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun',\n",
" 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']\n",
"\n",
"try:\n",
" dir_name = sys.argv[1]\n",
"except IndexError:\n",
" print('No directory name provided. Usage:')\n",
" print('{} <dir_name>'.format(sys.argv[0]))\n",
" sys.exit(1)\n",
"\n",
"if not os.path.exists(dir_name):\n",
" print('Directory {} does not exist'.format(dir_name))\n",
" sys.exit(1)\n",
"\n",
"for filename in os.listdir(dir_name):\n",
" try:\n",
" d, month, y = int(filename[5:7]), filename[8:11], int(filename[12:14])\n",
" except (IndexError, ValueError):\n",
" print('Skipping file', filename)\n",
" continue\n",
" \n",
" try:\n",
" m = months.index(month.lower())+1\n",
" except ValueError:\n",
" print('Skipping file {} (unrecognised month)'.format(filename))\n",
" continue\n",
" \n",
" newname = 'data-20{:02d}-{:02d}-{:02d}.txt'.format(y, m, d)\n",
" newpath = os.path.join(dir_name, newname)\n",
" oldpath = os.path.join(dir_name, filename)\n",
" print(oldpath, '->', newpath)\n",
" os.rename(oldpath, newpath)"
]
}
],
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading