Spaces:
Running
Running
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<link rel="icon" href="./favicon.ico" /> | |
<!-- Preload is necessary because we show these images when we disconnect from the server, | |
but at that point we cannot load these images from the server --> | |
<link rel="preload" href="./assets/gradient-yHQUC_QB.png" as="image" /> | |
<link rel="preload" href="./assets/noise-60BoTA8O.png" as="image" /> | |
<!-- Preload the fonts --> | |
<link rel="preload" href="./assets/Lora-VariableFont_wght-B2ootaw-.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/PTSans-Regular-CxL0S8W7.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/PTSans-Bold-D9fedIX3.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/FiraMono-Regular-BTCkDNvf.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/FiraMono-Medium-DU3aDxX5.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/FiraMono-Bold-CLVRCuM9.ttf" as="font" crossorigin="anonymous" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<meta name="theme-color" content="#000000" /> | |
<meta name="description" content="a marimo app" /> | |
<link rel="apple-touch-icon" href="./apple-touch-icon.png" /> | |
<link rel="manifest" href="./manifest.json" /> | |
<script data-marimo="true"> | |
function __resizeIframe(obj) { | |
var scrollbarHeight = 20; // Max between windows, mac, and linux | |
function setHeight() { | |
var element = obj.contentWindow.document.documentElement; | |
// If there is no vertical scrollbar, we don't need to resize the iframe | |
if (element.scrollHeight === element.clientHeight) { | |
return; | |
} | |
// Create a new height that includes the scrollbar height if it's visible | |
var hasHorizontalScrollbar = element.scrollWidth > element.clientWidth; | |
var newHeight = element.scrollHeight + (hasHorizontalScrollbar ? scrollbarHeight : 0); | |
// Only update the height if it's different from the current height | |
if (obj.style.height !== `${newHeight}px`) { | |
obj.style.height = `${newHeight}px`; | |
} | |
} | |
// Resize the iframe to the height of the content and bottom scrollbar height | |
setHeight(); | |
// Resize the iframe when the content changes | |
const resizeObserver = new ResizeObserver((entries) => { | |
setHeight(); | |
}); | |
resizeObserver.observe(obj.contentWindow.document.body); | |
} | |
</script> | |
<marimo-filename hidden>notebook.py</marimo-filename> | |
<marimo-mode data-mode='edit' hidden></marimo-mode> | |
<marimo-version data-version='0.11.9' hidden></marimo-version> | |
<marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "dataframes": "rich", "default_width": "medium", "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"preset": "default", "overrides": {}}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "watcher_on_save": "lazy", "output_max_bytes": 8000000, "std_stream_max_bytes": 1000000}, "save": {"autosave": "off", "autosave_delay": 1000, "format_on_save": false}, "package_management": {"manager": "pip"}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{}' hidden></marimo-user-config> | |
<marimo-app-config data-config='{"width": "compact"}' hidden></marimo-app-config> | |
<marimo-server-token data-token='123' hidden></marimo-server-token> | |
<title>01 sets</title> | |
<script type="module" crossorigin src="./assets/index-BiV-b1K2.js"></script> | |
<link rel="stylesheet" crossorigin href="./assets/index-DkqMrX_B.css"> | |
<marimo-wasm hidden=""></marimo-wasm> | |
<script> | |
if (window.location.protocol === 'file:') { | |
alert('Warning: This file must be served by an HTTP server to function correctly.'); | |
} | |
</script> | |
<style> | |
#save-button { | |
display: none ; | |
} | |
#filename-input { | |
display: none ; | |
} | |
</style> | |
<marimo-code hidden="" data-show-code="false">import%20marimo%0A%0A__generated_with%20%3D%20%220.11.9%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%20Sets%0A%0A%20%20%20%20%20%20%20%20Probability%20is%20the%20study%20of%20%22events%22%2C%20assigning%20numerical%20values%20to%20how%20likely%0A%20%20%20%20%20%20%20%20events%20are%20to%20occur.%20For%20example%2C%20probability%20lets%20us%20quantify%20how%20likely%20it%20is%20for%20it%20to%20rain%20or%20shine%20on%20a%20given%20day.%0A%0A%0A%20%20%20%20%20%20%20%20Typically%20we%20reason%20about%20_sets_%20of%20events.%20In%20mathematics%2C%0A%20%20%20%20%20%20%20%20a%20set%20is%20a%20collection%20of%20elements%2C%20with%20no%20element%20included%20more%20than%20once.%0A%20%20%20%20%20%20%20%20Elements%20can%20be%20any%20kind%20of%20object.%0A%0A%20%20%20%20%20%20%20%20For%20example%3A%0A%0A%20%20%20%20%20%20%20%20-%20%E2%98%80%EF%B8%8F%20Weather%20events%3A%20%24%5C%7B%5Ctext%7BRain%7D%2C%20%5Ctext%7BOvercast%7D%2C%20%5Ctext%7BClear%7D%5C%7D%24%0A%20%20%20%20%20%20%20%20-%20%F0%9F%8E%B2%20Die%20rolls%3A%20%24%5C%7B1%2C%202%2C%203%2C%204%2C%205%2C%206%5C%7D%24%0A%20%20%20%20%20%20%20%20-%20%F0%9F%AA%99%20Pairs%20of%20coin%20flips%20%3D%20%24%5C%7B%20%5Ctext%7B(Heads%2C%20Heads)%7D%2C%20%5Ctext%7B(Heads%2C%20Tails)%7D%2C%20%5Ctext%7B(Tails%2C%20Tails)%7D%20%5Ctext%7B(Tails%2C%20Heads)%7D%5C%7D%24%0A%0A%20%20%20%20%20%20%20%20Sets%20are%20the%20building%20blocks%20of%20probability%2C%20and%20will%20arise%20frequently%20in%20our%20study.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%20Set%20operations%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22In%20Python%2C%20sets%20are%20made%20with%20the%20%60set%60%20function%3A%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20A%20%3D%20set(%5B2%2C%203%2C%205%2C%207%5D)%0A%20%20%20%20A%0A%20%20%20%20return%20(A%2C)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20B%20%3D%20set(%5B0%2C%201%2C%202%2C%203%2C%205%2C%208%5D)%0A%20%20%20%20B%0A%20%20%20%20return%20(B%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20Below%20we%20explain%20common%20operations%20on%20sets.%0A%0A%20%20%20%20%20%20%20%20_**Try%20it!**%20Try%20modifying%20the%20definitions%20of%20%60A%60%20and%20%60B%60%20above%2C%20and%20see%20how%20the%20results%20change%20below._%0A%0A%20%20%20%20%20%20%20%20The%20**union**%20%24A%20%5Ccup%20B%24%20of%20sets%20%24A%24%20and%20%24B%24%20is%20the%20set%20of%20elements%20in%20%24A%24%2C%20%24B%24%2C%20or%20both.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(A%2C%20B)%3A%0A%20%20%20%20A%20%7C%20B%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22The%20**intersection**%20%24A%20%5Ccap%20B%24%20is%20the%20set%20of%20elements%20in%20both%20%24A%24%20and%20%24B%24%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(A%2C%20B)%3A%0A%20%20%20%20A%20%26%20B%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22The%20**difference**%20%24A%20%5Csetminus%20B%24%20is%20the%20set%20of%20elements%20in%20%24A%24%20that%20are%20not%20in%20%24B%24.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(A%2C%20B)%3A%0A%20%20%20%20A%20-%20B%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20%F0%9F%8E%AC%20An%20interactive%20example%0A%0A%20%20%20%20%20%20%20%20Here's%20a%20simple%20example%20that%20classifies%20TV%20shows%20into%20sets%20by%20genre%2C%20and%20uses%20these%20sets%20to%20recommend%20shows%20to%20a%20user%20based%20on%20their%20preferences.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20viewer_type%20%3D%20mo.ui.radio(%0A%20%20%20%20%20%20%20%20options%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22I%20like%20action%20and%20drama!%22%3A%20%22New%20Viewer%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22I%20only%20like%20action%20shows%22%3A%20%22Action%20Fan%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22I%20only%20like%20dramas%22%3A%20%22Drama%20Fan%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20value%3D%22I%20like%20action%20and%20drama!%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22Which%20genre%20do%20you%20prefer%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(viewer_type%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(viewer_type)%3A%0A%20%20%20%20viewer_type%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20action_shows%20%3D%20%7B%22Stranger%20Things%22%2C%20%22The%20Witcher%22%2C%20%22Money%20Heist%22%7D%0A%20%20%20%20drama_shows%20%3D%20%7B%22The%20Crown%22%2C%20%22Money%20Heist%22%2C%20%22Bridgerton%22%7D%0A%20%20%20%20return%20action_shows%2C%20drama_shows%0A%0A%0A%40app.cell%0Adef%20_(action_shows%2C%20drama_shows)%3A%0A%20%20%20%20recommendations%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22New%20Viewer%22%3A%20action_shows%20%7C%20drama_shows%2C%20%20%23%20Union%20for%20new%20viewers%0A%20%20%20%20%20%20%20%20%22Action%20Fan%22%3A%20action_shows%20-%20drama_shows%2C%20%20%23%20Unique%20action%20shows%0A%20%20%20%20%20%20%20%20%22Drama%20Fan%22%3A%20drama_shows%20-%20action_shows%2C%20%20%23%20Unique%20drama%20shows%0A%20%20%20%20%7D%0A%20%20%20%20return%20(recommendations%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20recommendations%2C%20viewer_type)%3A%0A%20%20%20%20result%20%3D%20recommendations%5Bviewer_type.value%5D%0A%0A%20%20%20%20explanation%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22New%20Viewer%22%3A%20%22You%20get%20everything%20to%20explore!%22%2C%0A%20%20%20%20%20%20%20%20%22Action%20Fan%22%3A%20%22Pure%20action%2C%20no%20drama!%22%2C%0A%20%20%20%20%20%20%20%20%22Drama%20Fan%22%3A%20%22Drama-focused%20selections!%22%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**%F0%9F%8E%AC%20Recommended%20shows.**%20Based%20on%20your%20preference%20for%20**%7Bviewer_type.value%7D**%2C%0A%20%20%20%20we%20recommend%3A%0A%0A%20%20%20%20%7B%22%2C%20%22.join(result)%7D%0A%0A%20%20%20%20**Why%20these%20shows%3F**%20%0A%20%20%20%20%7Bexplanation%5Bviewer_type.value%5D%7D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%20explanation%2C%20result%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%0A%0A%20%20%20%20Given%20these%20sets%3A%0A%0A%20%20%20%20-%20A%20%3D%20%7B%F0%9F%8E%AE%2C%20%F0%9F%93%B1%2C%20%F0%9F%92%BB%7D%0A%0A%20%20%20%20-%20B%20%3D%20%7B%F0%9F%93%B1%2C%20%F0%9F%92%BB%2C%20%F0%9F%96%A8%EF%B8%8F%7D%0A%0A%20%20%20%20-%20C%20%3D%20%7B%F0%9F%92%BB%2C%20%F0%9F%96%A8%EF%B8%8F%2C%20%E2%8C%A8%EF%B8%8F%7D%0A%0A%20%20%20%20Can%20you%3A%0A%0A%20%20%20%201.%20Find%20all%20elements%20that%20are%20in%20A%20or%20B%0A%0A%20%20%20%202.%20Find%20elements%20common%20to%20all%20three%20sets%0A%0A%20%20%20%203.%20Find%20elements%20in%20A%20that%20aren't%20in%20C%0A%0A%20%20%20%20%3Cdetails%3E%0A%0A%20%20%20%20%3Csummary%3ECheck%20your%20answers!%3C%2Fsummary%3E%0A%0A%20%20%20%201.%20A%20%E2%88%AA%20B%20%3D%20%7B%F0%9F%8E%AE%2C%20%F0%9F%93%B1%2C%20%F0%9F%92%BB%2C%20%F0%9F%96%A8%EF%B8%8F%7D%3Cbr%3E%0A%20%20%20%202.%20A%20%E2%88%A9%20B%20%E2%88%A9%20C%20%3D%20%7B%F0%9F%92%BB%7D%3Cbr%3E%0A%20%20%20%203.%20A%20-%20C%20%3D%20%7B%F0%9F%8E%AE%2C%20%F0%9F%93%B1%7D%0A%0A%20%20%20%20%3C%2Fdetails%3E%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20%F0%9F%A7%AE%20Set%20properties%0A%0A%20%20%20%20%20%20%20%20Here%20are%20some%20important%20properties%20of%20the%20set%20operations%3A%0A%0A%20%20%20%20%20%20%20%201.%20**Commutative**%3A%20%24A%20%5Ccup%20B%20%3D%20B%20%5Ccup%20A%24%0A%20%20%20%20%20%20%20%202.%20**Associative**%3A%20%24(A%20%5Ccup%20B)%20%5Ccup%20C%20%3D%20A%20%5Ccup%20(B%20%5Ccup%20C)%24%0A%20%20%20%20%20%20%20%203.%20**Distributive**%3A%20%24A%20%5Ccup%20(B%20%5Ccap%20C)%20%3D%20(A%20%5Ccup%20B)%20%5Ccap%20(A%20%5Ccup%20C)%24%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Set%20builder%20notation%0A%0A%20%20%20%20%20%20%20%20To%20compactly%20describe%20the%20elements%20in%20a%20set%2C%20we%20can%20use%20**set%20builder%20notation**%2C%20which%20specifies%20conditions%20that%20must%20be%20true%20for%20elements%20to%20be%20in%20the%20set.%0A%0A%20%20%20%20%20%20%20%20For%20example%2C%20here%20is%20how%20to%20specify%20the%20set%20of%20positive%20numbers%20less%20than%2010%3A%0A%0A%20%20%20%20%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20%5C%7Bx%20%5Cmid%200%20%3C%20x%20%3C%2010%20%5C%7D%0A%20%20%20%20%20%20%20%20%5C%5D%0A%0A%20%20%20%20%20%20%20%20The%20predicate%20to%20the%20right%20of%20the%20vertical%20bar%20%24%5Cmid%24%20specifies%20conditions%20that%20must%20be%20true%20for%20an%20element%20to%20be%20in%20the%20set%3B%20the%20expression%20to%20the%20left%20of%20%24%5Cmid%24%20specifies%20the%20value%20being%20included.%0A%0A%20%20%20%20%20%20%20%20In%20Python%2C%20set%20builder%20notation%20is%20called%20a%20%22set%20comprehension.%22%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20def%20predicate(x)%3A%0A%20%20%20%20%20%20%20%20return%20x%20%3E%200%20and%20x%20%3C%2010%0A%20%20%20%20return%20(predicate%2C)%0A%0A%0A%40app.cell%0Adef%20_(predicate)%3A%0A%20%20%20%20set(x%20for%20x%20in%20range(100)%20if%20predicate(x))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22**Try%20it!**%20Try%20modifying%20the%20%60predicate%60%20function%20above%20and%20see%20how%20the%20set%20changes.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Summary%0A%0A%20%20%20%20You've%20learned%3A%0A%0A%20%20%20%20-%20Basic%20set%20operations%0A%20%20%20%20-%20Set%20properties%0A%20%20%20%20-%20Real-world%20applications%0A%0A%20%20%20%20In%20the%20next%20lesson%2C%20we'll%20define%20probability%20from%20the%20ground%20up%2C%20using%20sets.%0A%0A%20%20%20%20Remember%3A%20In%20probability%2C%20every%20event%20is%20a%20set%2C%20and%20every%20set%20can%20be%20an%20event!%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20return%20(mo%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A</marimo-code></head> | |
<body> | |
<div id="root"></div> | |
</body> | |
</html> | |