656 lines
17 KiB
Plaintext
656 lines
17 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"\"\"\n",
|
||
|
"Sprawdź czy tekst 'aAaAaA' znajduje się w tablicy passwords.\n",
|
||
|
"W zależności czy znajduje się czy też nie, wyświetl na ekranie odpowiedni komunikat.\n",
|
||
|
"\"\"\"\n",
|
||
|
"\n",
|
||
|
"passwords = ['aaAaa', 'aAAAaa', 'aaaaaaA', 'aaaAAAAA', 'aaAAAaa', 'aAaAaA', 'aAaAaAA']"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"napisaAaAaAznajduje sie w tablicy passwords\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"if 'aAaAaA' in passwords:\n",
|
||
|
" print('napis ' + 'aAaAaA' + ' znajduje sie w tablicy passwords')\n",
|
||
|
"else:\n",
|
||
|
" print(' napis' + 'aAaAaA' + ' nie znajduje sie w tablicy passwords')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"\"\"\n",
|
||
|
"Niech x oznacza liczbę uzyskanych punktów. Standardowa skala ocen jest następująca:\n",
|
||
|
"* x >= 90 -- 5.0\n",
|
||
|
"* 90 > x >= 80 -- 4.5\n",
|
||
|
"* 80 > x >= 70 -- 4.0\n",
|
||
|
"* 70 > x >= 60 -- 3.5\n",
|
||
|
"* 60 > x >= 50 -- 3.0\n",
|
||
|
"* x < 50 -- 2.0\n",
|
||
|
"\n",
|
||
|
"Zmienna `points` zawiera liczbę uzyskanych punktów przez studenta.\n",
|
||
|
"Napisz instrukcję warunką, która wyświetli ocenę studenta w zależności od liczby punktów.\n",
|
||
|
"\"\"\"\n",
|
||
|
"\n",
|
||
|
"points = 85\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Twoja ocena to: 4.5\n",
|
||
|
"Twoja ocena to: 4.5\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"def print_grade(grade):\n",
|
||
|
" print('Twoja ocena to: ',grade)\n",
|
||
|
" \n",
|
||
|
" \n",
|
||
|
"# te rozwiązanie korzysta z ciekawej możliwości zawarcia dwóch warunków a<x<b w jednej lini\n",
|
||
|
"if points >= 90:\n",
|
||
|
" print_grade(5.0)\n",
|
||
|
"elif 90 > points >= 80:\n",
|
||
|
" print_grade(4.5)\n",
|
||
|
"elif 80 > points >= 70:\n",
|
||
|
" print_grade(4.0)\n",
|
||
|
"elif 70 > points >= 60:\n",
|
||
|
" print_grade(3.5)\n",
|
||
|
"elif 60 > points >= 50:\n",
|
||
|
" print_grade(3.0)\n",
|
||
|
"else:\n",
|
||
|
" print_grade(2.0)\n",
|
||
|
" \n",
|
||
|
"# albo można tak:\n",
|
||
|
"if points >= 90:\n",
|
||
|
" print_grade(5.0)\n",
|
||
|
"elif 90 > points:\n",
|
||
|
" print_grade(4.5)\n",
|
||
|
"elif 80 > points:\n",
|
||
|
" print_grade(4.0)\n",
|
||
|
"elif 70 > points:\n",
|
||
|
" print_grade(3.5)\n",
|
||
|
"elif 60 > points:\n",
|
||
|
" print_grade(3.0)\n",
|
||
|
"else:\n",
|
||
|
" print_grade(2.0)\n",
|
||
|
" "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"230181"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 9,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"\"\"\"\n",
|
||
|
"Oblicz sumę liczb od 1 do 678.\n",
|
||
|
"\"\"\"\n",
|
||
|
"# te dodanie 1 jest ważne, bo python nie włącza końca przedziału\n",
|
||
|
"sum(list(range(1,678+1)))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 11,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\n",
|
||
|
"\"\"\"\n",
|
||
|
"Słownik `oceny` zawiera oceny kilku osób. Kluczami są imiona dzieci, a wartosciami -- ich oceny.\n",
|
||
|
"Uzupełnij słownik `rozklad`, którego kluczami są oceny, a wartosciami -- listy... \n",
|
||
|
"\"\"\"\n",
|
||
|
"\n",
|
||
|
"oceny = {\n",
|
||
|
" 'Albert': 4.5,\n",
|
||
|
" 'Beata': 5,\n",
|
||
|
" 'Cecylia': 4,\n",
|
||
|
" 'Dariusz': 4,\n",
|
||
|
" 'Eliza': 3,\n",
|
||
|
" 'Feliks': 5,\n",
|
||
|
" 'Grzegorz': 4.5,\n",
|
||
|
" 'Izabela': 4.5\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"rozklad = {\n",
|
||
|
" 5: [],\n",
|
||
|
" 4.5: [],\n",
|
||
|
" 4: [],\n",
|
||
|
" 3: []\n",
|
||
|
"}"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 12,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"{5: ['Beata', 'Feliks'],\n",
|
||
|
" 4.5: ['Albert', 'Grzegorz', 'Izabela'],\n",
|
||
|
" 4: ['Cecylia', 'Dariusz'],\n",
|
||
|
" 3: ['Eliza']}"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 12,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"for key, value in oceny.items():\n",
|
||
|
" rozklad[value].append(key)\n",
|
||
|
"rozklad\n",
|
||
|
" "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 13,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\n",
|
||
|
"\"\"\"\n",
|
||
|
"Poniżej znajdują się 2 słowniki z danymi o liczbie przejazdów rowerami miejskimi w Montrealu w 2018 z podziałem na miesiące (od kwietnia do listopada).\n",
|
||
|
"Pierwszy słownik zawiera informacje o przejazdach wykonanych przez posiadaczy abonamentu, a drugi przez ludzi, którzy\n",
|
||
|
"nie mają wykupionego abonamentu. Dane pochodzą ze strony https://montreal.bixi.com/en/open-data. \n",
|
||
|
"\n",
|
||
|
"a) Stwórz trzeci słownik `all_rides`, w którym zliczysz łączną liczbę przejazdów w każdym z podanych miesięcy.\n",
|
||
|
"b) Oblicz sumę zarejestrowanych przejazdów od kwietnia do listopada.\n",
|
||
|
"c) Wyswietl jaki procent wszystkich przejazdów odbyło się w sierpniu (August).\n",
|
||
|
"\n",
|
||
|
"\"\"\"\n",
|
||
|
"\n",
|
||
|
"members = {\n",
|
||
|
" 'April': 211819,\n",
|
||
|
" 'May': 682758,\n",
|
||
|
" 'June': 737011,\n",
|
||
|
" 'July': 779511,\n",
|
||
|
" 'August': 673790,\n",
|
||
|
" 'September': 673790,\n",
|
||
|
" 'October': 444177,\n",
|
||
|
" 'November': 136791,\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"occasionals = {\n",
|
||
|
" 'April': 32058,\n",
|
||
|
" 'May': 147898,\n",
|
||
|
" 'June': 171494,\n",
|
||
|
" 'July': 194316,\n",
|
||
|
" 'August': 206809,\n",
|
||
|
" 'September': 140492,\n",
|
||
|
" 'October': 53596,\n",
|
||
|
" 'November': 10516,\n",
|
||
|
"}\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 24,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"{'April': 243877, 'May': 830656, 'June': 908505, 'July': 973827, 'August': 880599, 'September': 814282, 'October': 497773, 'November': 147307}\n",
|
||
|
"5296826\n",
|
||
|
"16.62503166990949 %\n",
|
||
|
"16.63 %\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"all_rides = dict()\n",
|
||
|
"for key,value in members.items():\n",
|
||
|
" all_rides[key] = value\n",
|
||
|
"\n",
|
||
|
"for key,value in occasionals.items():\n",
|
||
|
" all_rides[key] += value\n",
|
||
|
"\n",
|
||
|
"print(all_rides)\n",
|
||
|
"# można też to zrobić szybciej korzystająć z list comprehensions, albo np biblioteki copy\n",
|
||
|
"\n",
|
||
|
"print(sum(all_rides.values()))\n",
|
||
|
"\n",
|
||
|
"print(100 * all_rides['August'] / sum(all_rides.values()), '%')\n",
|
||
|
"print(round(100 * all_rides['August'] / sum(all_rides.values()),2), '%') # zaokrąglenie"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 25,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"\"\"\n",
|
||
|
"Napisz kod, który zwraci tzw. \"Big 'NO!'\" (zob. http://tvtropes.org/pmwiki/pmwiki.php/Main/BigNo)\n",
|
||
|
"dla zadanej liczby tj. napis typu \"NOOOOOOOOOOOOO!\", gdzie liczba 'O' ma być\n",
|
||
|
"równa wartości zmniennej `number_of_o`. Jeśli argument jest mniejszy niż 5, ma być zwracany napis \"It's not a Big 'No!'\".\n",
|
||
|
"Wyświetl ten napis na ekran.\n",
|
||
|
"\"\"\"\n",
|
||
|
"\n",
|
||
|
"number_of_o = 6"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 26,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"NOOOOOO!\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"if number_of_o < 5:\n",
|
||
|
" print(\"It's not a Big 'No!'\")\n",
|
||
|
"else:\n",
|
||
|
" print(\"N\"+\"O\"*number_of_o+'!')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 27,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"\"\"\n",
|
||
|
" * Podziel zmienną `text` na słowa, korzystając z metody split.\n",
|
||
|
" * Dodaj do listy `oov`, wszystkie słowa (bez powtórzeń), które nie są zawarte w liście `vocab`.\n",
|
||
|
"\"\"\"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"text = \"this is a string , which i will use for string testing\"\n",
|
||
|
"vocab = [',', 'this', 'is', 'a', 'which', 'for', 'will', 'i']\n",
|
||
|
"\n",
|
||
|
"oov = []\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 31,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"['string', 'use', 'testing']"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 31,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"text_splitted = text.split()\n",
|
||
|
"text_splitted_no_repeats = set(text_splitted)\n",
|
||
|
"\n",
|
||
|
"for item in text_splitted_no_repeats:\n",
|
||
|
" if item not in vocab:\n",
|
||
|
" oov.append(item)\n",
|
||
|
" \n",
|
||
|
"oov"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 32,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"'\\n1. Napisz rekurencyjną funkcję, która zwróci n-ty wyraz ciągu Fibonacciego.\\n2. Napisz funkcję, która obliczy n-ty wyroz ciągu Fibonacciego nie korzystając z rekurencji.\\nNp. możesz wykorzystać listę do obliczania kolejnych wartości ciągu.\\n\\nCiąg Fibonacciego:\\na[0] = 1, a[1] = 1, a[n] = a[n-1] + a[n-2] dla n>=2\\n\\n'"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 32,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"\"\"\"\n",
|
||
|
"1. Napisz rekurencyjną funkcję, która zwróci n-ty wyraz ciągu Fibonacciego.\n",
|
||
|
"2. Napisz funkcję, która obliczy n-ty wyroz ciągu Fibonacciego nie korzystając z rekurencji.\n",
|
||
|
"Np. możesz wykorzystać listę do obliczania kolejnych wartości ciągu.\n",
|
||
|
"\n",
|
||
|
"Ciąg Fibonacciego:\n",
|
||
|
"a[0] = 1, a[1] = 1, a[n] = a[n-1] + a[n-2] dla n>=2\n",
|
||
|
"\n",
|
||
|
"\"\"\""
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 34,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"89"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 34,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"def fib(n):\n",
|
||
|
" if n == 0:\n",
|
||
|
" return 1\n",
|
||
|
" elif n == 1:\n",
|
||
|
" return 1\n",
|
||
|
" else:\n",
|
||
|
" return fib(n-1) + fib(n-2)\n",
|
||
|
"fib(10)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\n",
|
||
|
"\"\"\"\n",
|
||
|
"Napisz funkcję sum_div35(n), która zwraca sumę wszystkich liczb podzielnych\n",
|
||
|
"przez 3 lub 5 mniejszych niż n.\n",
|
||
|
"\"\"\"\n",
|
||
|
"\n",
|
||
|
"def sum_div35(n):\n",
|
||
|
" pass\n",
|
||
|
"\n",
|
||
|
"input = 100\n",
|
||
|
"# dla n =100 poprawna odpowiedź to 2318\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 40,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"2318"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 40,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"def sum_div35(n):\n",
|
||
|
" all_numbers = []\n",
|
||
|
" for i in range(n):\n",
|
||
|
" if i % 3 == 0 or i % 5 == 0:\n",
|
||
|
" all_numbers.append(i)\n",
|
||
|
" return sum(all_numbers)\n",
|
||
|
"\n",
|
||
|
"sum_div35(100)\n",
|
||
|
"\n",
|
||
|
"# alternatywnie:\n",
|
||
|
"def sum_div35(n):\n",
|
||
|
" return sum([i for i in range(n) if i % 3 == 0 or i % 5 == 0 ])\n",
|
||
|
"sum_div35(100)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 41,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"'\\nOtwórz plik `zen_of_python.txt` i zlicz liczbę linii i słów w tym pliku. \\nNastępnie przerób kod na funkcję, która jako argument będzie przyjmować ściężkę do pliku i będzie zwracać \\nsłownik z dwoma kluczami: `liczba_linii` i `liczba_slow`.\\n'"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 41,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"\"\"\"\n",
|
||
|
"Otwórz plik `zen_of_python.txt` i zlicz liczbę linii i słów w tym pliku. \n",
|
||
|
"Następnie przerób kod na funkcję, która jako argument będzie przyjmować ściężkę do pliku i będzie zwracać \n",
|
||
|
"słownik z dwoma kluczami: `liczba_linii` i `liczba_slow`.\n",
|
||
|
"\"\"\""
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"19\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"(19, 137)"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(len(open('zen_of_python.txt').readlines()))\n",
|
||
|
"\n",
|
||
|
"def count_lines_and_words(path):\n",
|
||
|
" lines = 0\n",
|
||
|
" words = 0\n",
|
||
|
" with open(path) as f:\n",
|
||
|
" for line in f:\n",
|
||
|
" lines += 1\n",
|
||
|
" words += len(line.split())\n",
|
||
|
" return lines, words\n",
|
||
|
"\n",
|
||
|
"count_lines_and_words('zen_of_python.txt')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"\"\"\n",
|
||
|
"Zadania: Zaimportuj bibliotekę statistics, która zawiera funckje do obliczenia podstawych wielkości statystycznych (średnia, mediana, moda):\n",
|
||
|
" statistics.mean -- obliczenie średniej\n",
|
||
|
" statistics.median -- obliczenie mediany\n",
|
||
|
" statistics.variance -- obliczenie wariancji\n",
|
||
|
" statistics.stdev -- obliczenie odchylenia standardowego Oblicz te wielkości dla wartości z poniższego słownika.\n",
|
||
|
"Każda z tych funkcji przyjmuje jeden argument: listę wartości.\n",
|
||
|
"\"\"\"\n",
|
||
|
"\n",
|
||
|
"members = {\n",
|
||
|
" 'April': 211819,\n",
|
||
|
" 'May': 682758,\n",
|
||
|
" 'June': 737011,\n",
|
||
|
" 'July': 779511,\n",
|
||
|
" 'August': 673790,\n",
|
||
|
" 'September': 673790,\n",
|
||
|
" 'October': 444177,\n",
|
||
|
" 'November': 136791,\n",
|
||
|
"}"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"542455.875\n",
|
||
|
"673790.0\n",
|
||
|
"61681755885.83929\n",
|
||
|
"248358.12023334226\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import statistics\n",
|
||
|
"\n",
|
||
|
"values = members.values()\n",
|
||
|
"\n",
|
||
|
"print(statistics.mean(values))\n",
|
||
|
"print(statistics.median(values))\n",
|
||
|
"print(statistics.variance(values))\n",
|
||
|
"print(statistics.stdev(values))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"\"\"\n",
|
||
|
"Biblioteka random posiada funkcję random, która zwraca losową licznę z przedziału [0, 1) (nie przyjmuje żadnych argumentów).\n",
|
||
|
"Biblioteka math posiada funkcję hypot, która oblicza odległość punktu od środka układu współrzędnych (punktu [0, 0]) i przyjmuje dwa argumenty: współrzędne punktu.\n",
|
||
|
"* Zaimportuj biblioteki random i math\n",
|
||
|
"* korzystając z funkcji random wylosuj dwie liczby i przypisz wyniki do zmniennych x i y.\n",
|
||
|
"* Korzystając z funkcji hypot oblicz odległość punktu o współrzednych (x,y).\n",
|
||
|
"* Dodaj pętlę, która będzie wykonywać te czynności n = 10 razy i będzie zliczać ile razy obliczona odległość jest mniejsza od 1.\n",
|
||
|
"* Oblicz stosunek liczby punktów, dla których odległość wynosiła mniej niż 1 do całkowitej liczby punktów. Pomnóż tę wartocść przez 4.\n",
|
||
|
"* Podstaw za n wartości 100, 1000, 1000000. Do jakiej wartości zbiegają wartości?\n",
|
||
|
"\"\"\""
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 20,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"1.023482041105784\n",
|
||
|
"785667\n",
|
||
|
"3.142668\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import random\n",
|
||
|
"import math\n",
|
||
|
"\n",
|
||
|
"x,y = random.random(), random.random()\n",
|
||
|
"print(math.hypot(x,y))\n",
|
||
|
"\n",
|
||
|
"n = 1000000\n",
|
||
|
"lower_than_1_number = 0\n",
|
||
|
"for i in range(n):\n",
|
||
|
" x,y = random.random(), random.random()\n",
|
||
|
" if math.hypot(x,y) < 1 :\n",
|
||
|
" lower_than_1_number +=1\n",
|
||
|
"print(lower_than_1_number)\n",
|
||
|
"\n",
|
||
|
"print((lower_than_1_number / n )*4)\n",
|
||
|
"\n",
|
||
|
"# zmierza do stałej PI"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"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.8.3"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 4
|
||
|
}
|