Skip to content

Commit 536e6c2

Browse files
committed
up
1 parent 8e9a38f commit 536e6c2

3 files changed

Lines changed: 0 additions & 1047 deletions

File tree

practices/practice-iteration-for-sol.ipynb

Lines changed: 0 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,223 +1661,6 @@
16611661
"source": [
16621662
"grid_gen(3, 5)"
16631663
]
1664-
},
1665-
{
1666-
"cell_type": "markdown",
1667-
"metadata": {},
1668-
"source": [
1669-
"**예제 5**"
1670-
]
1671-
},
1672-
{
1673-
"cell_type": "markdown",
1674-
"metadata": {},
1675-
"source": [
1676-
"**주의사항:** for 반복문으로 수정할 것."
1677-
]
1678-
},
1679-
{
1680-
"cell_type": "markdown",
1681-
"metadata": {},
1682-
"source": [
1683-
"`random` 모듈의 `randrange()` 함수, 따라서 `randint()` 함수는\n",
1684-
"지정된 정수 구간에서 균등한 확률로 하나의 정수를 선택한다.\n",
1685-
"이를 확인하는 코드를 작성하라.\n",
1686-
"예를 들어 `randint(1, 6)`을 10만 번 호출했을 때\n",
1687-
"1이 선택되는 경우가 전체의 1/6, 즉 0.1666 정도임을 확인하는 코드를 작성하라."
1688-
]
1689-
},
1690-
{
1691-
"cell_type": "code",
1692-
"execution_count": null,
1693-
"metadata": {},
1694-
"outputs": [
1695-
{
1696-
"data": {
1697-
"text/plain": [
1698-
"0.16666666666666666"
1699-
]
1700-
},
1701-
"metadata": {},
1702-
"output_type": "display_data"
1703-
}
1704-
],
1705-
"source": [
1706-
"1/6"
1707-
]
1708-
},
1709-
{
1710-
"cell_type": "markdown",
1711-
"metadata": {},
1712-
"source": [
1713-
"힌트: `while` 반복문, `random.randint()` 함수 활용"
1714-
]
1715-
},
1716-
{
1717-
"cell_type": "markdown",
1718-
"metadata": {},
1719-
"source": [
1720-
"답:"
1721-
]
1722-
},
1723-
{
1724-
"cell_type": "markdown",
1725-
"metadata": {},
1726-
"source": [
1727-
"`random.randint(1, 6)`를 10만 번 호출해서 반환값이 1인지 여부를 확인하는 코드는 다음과 같다."
1728-
]
1729-
},
1730-
{
1731-
"cell_type": "markdown",
1732-
"metadata": {},
1733-
"source": [
1734-
"```\n",
1735-
"total = 100_000\n",
1736-
"\n",
1737-
"toss = 0\n",
1738-
"while toss < total:\n",
1739-
" random.randint(1, 6) == 1\n",
1740-
" toss += 1\n",
1741-
"```"
1742-
]
1743-
},
1744-
{
1745-
"cell_type": "markdown",
1746-
"metadata": {},
1747-
"source": [
1748-
"1부터 6까지의 정수 중에서 무작위로 선택된 값이 1인 경우가 몇 번이었는지를 기억하는 변수 `count`를 추가한다.\n",
1749-
"`while` 반복문이 시작하기 전에 0으로 초기화 한 다음에\n",
1750-
"해당 경우가 발생할 때마다 1씩 커지도록 하면 된다."
1751-
]
1752-
},
1753-
{
1754-
"cell_type": "markdown",
1755-
"metadata": {},
1756-
"source": [
1757-
"```\n",
1758-
"total = 100_000\n",
1759-
"count = 0\n",
1760-
"\n",
1761-
"toss = 0\n",
1762-
"while toss < total:\n",
1763-
" if random.randint(1, 6) == 1:\n",
1764-
" count += 1\n",
1765-
" toss += 1\n",
1766-
"```"
1767-
]
1768-
},
1769-
{
1770-
"cell_type": "markdown",
1771-
"metadata": {},
1772-
"source": [
1773-
"끝으로 `count`에 저장된 값을 10만으로 나누면 1이 나온 비율이 계산된다.\n",
1774-
"지금까지의 설명을 코드로 구현하면 다음과 같다."
1775-
]
1776-
},
1777-
{
1778-
"cell_type": "code",
1779-
"execution_count": null,
1780-
"metadata": {},
1781-
"outputs": [
1782-
{
1783-
"name": "stdout",
1784-
"output_type": "stream",
1785-
"text": [
1786-
"1이 나온 비율: 0.16746\n"
1787-
]
1788-
}
1789-
],
1790-
"source": [
1791-
"# import random\n",
1792-
"\n",
1793-
"total = 100_000\n",
1794-
"count = 0\n",
1795-
"\n",
1796-
"toss = 0\n",
1797-
"while toss < total:\n",
1798-
" if random.randint(1, 6) == 1:\n",
1799-
" count += 1\n",
1800-
" toss += 1\n",
1801-
"\n",
1802-
"print(\"1이 나온 비율:\", count/total)"
1803-
]
1804-
},
1805-
{
1806-
"cell_type": "markdown",
1807-
"metadata": {},
1808-
"source": [
1809-
"동전 던지기를 많이 할 수록 1이 나온 비율이 보다 1/6에 가까워진다."
1810-
]
1811-
},
1812-
{
1813-
"cell_type": "markdown",
1814-
"metadata": {},
1815-
"source": [
1816-
"- 1백만 번 던질 때"
1817-
]
1818-
},
1819-
{
1820-
"cell_type": "code",
1821-
"execution_count": null,
1822-
"metadata": {},
1823-
"outputs": [
1824-
{
1825-
"name": "stdout",
1826-
"output_type": "stream",
1827-
"text": [
1828-
"1이 나온 비율: 0.166421\n"
1829-
]
1830-
}
1831-
],
1832-
"source": [
1833-
"# import random\n",
1834-
"\n",
1835-
"total = 1_000_000\n",
1836-
"count = 0\n",
1837-
"\n",
1838-
"toss = 0\n",
1839-
"while toss < total:\n",
1840-
" if random.randint(1, 6) == 1:\n",
1841-
" count += 1\n",
1842-
" toss += 1\n",
1843-
"\n",
1844-
"print(\"1이 나온 비율:\", count/total)"
1845-
]
1846-
},
1847-
{
1848-
"cell_type": "markdown",
1849-
"metadata": {},
1850-
"source": [
1851-
"- 1천만 번 던질 때"
1852-
]
1853-
},
1854-
{
1855-
"cell_type": "code",
1856-
"execution_count": null,
1857-
"metadata": {},
1858-
"outputs": [
1859-
{
1860-
"name": "stdout",
1861-
"output_type": "stream",
1862-
"text": [
1863-
"1이 나온 비율: 0.1666665\n"
1864-
]
1865-
}
1866-
],
1867-
"source": [
1868-
"# import random\n",
1869-
"\n",
1870-
"total = 10_000_000\n",
1871-
"count = 0\n",
1872-
"\n",
1873-
"toss = 0\n",
1874-
"while toss < total:\n",
1875-
" if random.randint(1, 6) == 1:\n",
1876-
" count += 1\n",
1877-
" toss += 1\n",
1878-
"\n",
1879-
"print(\"1이 나온 비율:\", count/total)"
1880-
]
18811664
}
18821665
],
18831666
"metadata": {

0 commit comments

Comments
 (0)