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 #9

Open
wants to merge 1 commit 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
315 changes: 315 additions & 0 deletions practices/pr2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "97f2c1f9-7408-4883-941a-28cf1fbda1d6",
"metadata": {},
"source": [
"# Задача 3.0. "
]
},
{
"cell_type": "markdown",
"id": "b1f5fba4-0929-494a-8449-8f52968565d1",
"metadata": {},
"source": [
"## а)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2ba071cb-e1d8-4b78-a743-58947eb0f742",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Последовательность чисел-градин для n=27: [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": [
"def hailstone_sequence(n):\n",
" sequence = [n]\n",
" while n != 1:\n",
" if n % 2 == 0:\n",
" n = n // 2\n",
" else:\n",
" n = 3 * n + 1\n",
" sequence.append(n)\n",
" return sequence\n",
"\n",
"# Пример для числа 27\n",
"sequence = hailstone_sequence(27)\n",
"print(\"Последовательность чисел-градин для n=27:\", sequence)"
]
},
{
"cell_type": "markdown",
"id": "ef3122d4-4b99-4579-ace0-73235a48bea0",
"metadata": {},
"source": [
"## б)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "87b768d7-fac6-4a8d-a109-7072e36480e3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Время останова процесса для n=27: 111\n"
]
}
],
"source": [
"def hailstone_stopping_time(n):\n",
" count = 0\n",
" while n != 1:\n",
" count += 1\n",
" if n % 2 == 0:\n",
" n = n // 2\n",
" else:\n",
" n = 3 * n + 1\n",
" return count\n",
"\n",
"# Пример для числа 27\n",
"stopping_time = hailstone_stopping_time(27)\n",
"print(\"Время останова процесса для n=27:\", stopping_time)"
]
},
{
"cell_type": "markdown",
"id": "ce1c8d0f-fd4a-4da7-8534-754c39f02389",
"metadata": {},
"source": [
"## Задача 3.1."
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "7a3f45c6-3e92-4de2-9af5-2086cdc0bfc7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ошибка: Укажите одно положительное целое число в командной строке.\n"
]
}
],
"source": [
"import sys\n",
"\n",
"def hailstone_sequence(n):\n",
" sequence = [n]\n",
" while n != 1:\n",
" if n % 2 == 0:\n",
" n = n // 2\n",
" else:\n",
" n = 3 * n + 1\n",
" sequence.append(n)\n",
" return sequence\n",
"\n",
"if __name__ == \"__main__\":\n",
" if len(sys.argv) != 2:\n",
" print(\"Ошибка: Укажите одно положительное целое число в командной строке.\")\n",
" else:\n",
" try:\n",
" n = int(sys.argv[1])\n",
" if n > 0:\n",
" sequence = hailstone_sequence(n)\n",
" print(\"Последовательность чисел-градин для n=\", n, \":\", sequence)\n",
" else:\n",
" print(\"Ошибка: число должно быть положительным.\")\n",
" except ValueError:\n",
" print(\"Ошибка: введите корректное целое число.\")"
]
},
{
"cell_type": "markdown",
"id": "df6f40e6-f7e3-4472-af17-f2247758f796",
"metadata": {},
"source": [
"## Задача 3.2."
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "a6c5628c-b9b6-49f0-ae9f-b67fce4566ae",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: Please provide valid latitude and longitude values in the format 'lat,long'.\n"
]
}
],
"source": [
"import sys\n",
"import math\n",
"\n",
"# Calculate the shortest distance between two points on a sphere provided on\n",
"# the command line as (longitude, latitude pairs). For example, Paris and Rome:\n",
"# 48.9,2.4 41.9,12.5\n",
"\n",
"haversin = lambda alpha: math.sin(alpha / 2) ** 2\n",
"\n",
"def gc_distance(loc1, loc2, R=6378.1):\n",
" \"\"\"\n",
" Return the great-circle distance between two points on a sphere of radius\n",
" R, provided as (latitude, longitude) pairs, loc=(phi, lambda) in radians.\n",
" \"\"\"\n",
" (phi1, lambda1), (phi2, lambda2) = loc1, loc2\n",
" d = 2 * R * math.asin(math.sqrt(\n",
" haversin(phi2 - phi1) + math.cos(phi1) * math.cos(phi2) * haversin(lambda2 - lambda1)\n",
" ))\n",
" return d\n",
"\n",
"if len(sys.argv) != 3:\n",
" print(\"Usage: python script.py 'latitude1,longitude1' 'latitude2,longitude2'\")\n",
"else:\n",
" try:\n",
" loc1, loc2 = sys.argv[1], sys.argv[2]\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(f\"{int(d)} km\")\n",
" except ValueError:\n",
" print(\"Error: Please provide valid latitude and longitude values in the format 'lat,long'.\")\n"
]
},
{
"cell_type": "markdown",
"id": "837e21ff-97cd-42f4-a04a-bb04f8da9bf9",
"metadata": {},
"source": [
"## Задача 3.3. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb198419-dca7-4a07-99c8-33f4821e6f51",
"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": "f5dbb291-9e4e-445a-958f-02073cd6aa7f",
"metadata": {},
"source": [
"## Задача 3.4."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "596c38c7-eb9e-40fc-9386-e5956ae644be",
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading