Skip to content

Instantly share code, notes, and snippets.

@mcouthon
Last active December 25, 2025 18:24
Show Gist options
  • Select an option

  • Save mcouthon/18b864d3d3f652a0f54a5b3932b346bc to your computer and use it in GitHub Desktop.

Select an option

Save mcouthon/18b864d3d3f652a0f54a5b3932b346bc to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "TV165YKUejpp"
},
"outputs": [],
"source": [
"\n",
"##########################################################\n",
"#\n",
"# Binomial.py - for Stat_Formulas, Stat_Formulas - by Alex Brodsky.\n",
"# Python 3.8.7\n",
"#\n",
"# 21/12/25 19:00 Beginning\n",
"#\n",
"# Versions:\n",
"# V 1.00 21/12/25 19:00 Beginning\n",
"#\n",
"# Calculate 2 Formulas. Binomial Distribution.\n",
"#\n",
"# © Copyright (c) 2025 Alex A. Brodsky\n",
"#\n",
"# Command line arguments: Default:\n",
"# INPUT Parameters:\n",
"# 1st: n <must>\n",
"# 2nd: p <must>\n",
"#\n",
"##########################################################\n",
"\n",
"import os\n",
"import re\n",
"import sys\n",
"\n",
"import math\n",
"\n",
"h = \"-\"\n",
"\n",
"pattern = re.compile(\n",
" r'^[+-]?(\\d+([.,]\\d+)?|[.,]\\d+)([eE][+-]?\\d+)?$'\n",
")\n",
"\n",
"# -----------------------------------------\n",
"def C(n, m):\n",
"# -----------------------------------------\n",
" \"\"\"Биномиальный коэффициент\"\"\"\n",
" return math.comb(n, m)\n",
"\n",
"# -----------------------------------------\n",
"def Calc_S1(n, p):\n",
"# -----------------------------------------\n",
" s = 0.0\n",
" if 0 < p < 1:\n",
" for m in range(1, n + 1):\n",
" s += (1 / m) * C(n, m) * (p ** m) * ((1 - p) ** (n - m))\n",
" return s\n",
"\n",
"# -----------------------------------------\n",
"def Calc_S2(n, p):\n",
"# -----------------------------------------\n",
" s = 0.0\n",
" if 0 < p < 1:\n",
" for m in range(1, n + 1):\n",
" s += (1 / (m ** 2)) * C(n, m) * (p ** m) * ((1 - p) ** (n - m))\n",
" return s\n",
"\n"
]
},
{
"cell_type": "code",
"source": [
"\n",
"##########################################################\n",
"#\n",
"# Binom_Distr_1.py - by Alex Brodsky.\n",
"# Python 3.8.7\n",
"#\n",
"# 18/12/25 09:47 Beginning\n",
"#\n",
"# Versions:\n",
"# V 1.00 18/12/25 09:47 Beginning\n",
"#\n",
"# The Stat Formulas Jana`s Project\n",
"# Calculate 2 Formulas. Binomial Distribution.\n",
"# The One meaning\n",
"#\n",
"# © Copyright (c) 2025 Alex A. Brodsky\n",
"#\n",
"# Command line arguments: Default:\n",
"# INPUT Parameters:\n",
"# 1st: n <must> 10\n",
"# 2nd: p <must> 0.3\n",
"#\n",
"# OUTPUT:\n",
"#\n",
"#\n",
"#\n",
"##########################################################\n",
"\n",
"\n",
"#from Binomial import *\n",
"\n",
"\n",
"# -----------------------------------------\n",
"# Main\n",
"# -----------------------------------------\n",
"\n",
"is_Debug = True\n",
"is_Debug = False\n",
"\n",
"\n",
"# ----- пример использования -----\n",
"\n",
"try:\n",
" n = 10\n",
" p = 0.3\n",
"\n",
"# n = sys.argv[1]\n",
" n = input( \"Пожалуйста, введите n: \")\n",
" if not n.isdecimal():\n",
" raise ValueError(\"n должно быть натуральным числом\")\n",
"\n",
"# p = sys.argv[2]\n",
" p = input( \"Пожалуйста, введите p: \")\n",
" if not pattern.match(p):\n",
" raise ValueError(\"p должно быть десятичной дробью\")\n",
"\n",
" n = int( n)\n",
" if n <= 1:\n",
" raise ValueError(\"n должно быть натуральным числом > 1\")\n",
"\n",
" p = float( p)\n",
" if not (0 <= p <= 1):\n",
" raise ValueError(\"p должно быть в диапазоне [0, 1]\")\n",
"\n",
"except ValueError:\n",
"# print ( f\"{e = }\")\n",
" print ( \"exception: \", end=\"\")\n",
" raise\n",
" sys.exit(1)\n",
"\n",
"print ( f\"{n = }\")\n",
"print ( f\"{p = }\")\n",
"\n",
"print(\"S1 =\", Calc_S1(n, p))\n",
"print(\"S2 =\", Calc_S2(n, p))\n",
"\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 382
},
"id": "5gSEiaGBem0J",
"outputId": "bffee21d-f31b-4ce6-bd37-bc1f78299f49"
},
"execution_count": null,
"outputs": [
{
"output_type": "error",
"ename": "KeyboardInterrupt",
"evalue": "Interrupted by user",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipython-input-4253914098.py\u001b[0m in \u001b[0;36m<cell line: 0>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 46\u001b[0m \u001b[0;31m# n = sys.argv[1]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 47\u001b[0;31m \u001b[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0;34m\"Пожалуйста, введите n: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 48\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misdecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"n должно быть натуральным числом\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 1175\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1176\u001b[0m )\n\u001b[0;32m-> 1177\u001b[0;31m return self._input_request(\n\u001b[0m\u001b[1;32m 1178\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprompt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1179\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"shell\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 1217\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1218\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1219\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Interrupted by user\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1220\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1221\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Invalid Message:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment