UESL 3.1 Homework
popcorn hack 1
The first line creates the variable “stars” and assigns it a value of 8. The second link makes the variable “checkpoint_stars” and gives it the same value as stars The third line assigns star a new value of the previous value of star and adds 4 to it (now 12). The 4th line prints our stars (12). The last line prints checkpoint_stars (8 because it never changed).
Code Runner Challenge
Homework python
View IPYNB Source
stars = 8
checkpoint_stars = stars
stars = stars + 4
print(stars)
print(checkpoint_stars)
MCQ
- B
- A
- C
- B
</div>
</details>
<div class="editor-container">
<div class="control-panel">
<button type="button" class="ocs__btn utility run" data-hook="run" title="Run" aria-label="Run"></button>
<button type="button" class="ocs__btn utility copy" data-hook="copy" title="Copy Code" aria-label="Copy Code"></button>
<button type="button" class="ocs__btn utility save" data-hook="save" title="Save Code" aria-label="Save Code"></button>
<button type="button" class="ocs__btn utility clear" data-hook="clear" title="Clear Storage" aria-label="Clear Storage"></button>
<select class="languageSelect">
<option value="python">Python</option>
<option value="java">Java</option>
<option value="javascript">JavaScript</option>
<option value="pseudocode">Pseudocode</option>
</select>
<select class="fontSizeSelect">
<option value="14">14px</option>
<option value="16">16px</option>
<option value="18">18px</option>
<option value="20">20px</option>
</select>
</div>
<textarea class="editor-textarea"></textarea>
<div class="control-panel">
<span class="lineCount">Lines: 1</span>
<span class="charCount">Characters: 0</span>
</div>
</div>
<div class="output-container">
<div class="control-panel">
<span>Output</span>
<button type="button" class="ocs__btn utility copyOutput" data-hook="copy-output" title="Copy Output" aria-label="Copy Output"></button>
</div>
<div class="output-content">Click "Run" in code control panel to see output ...</div>
<div class="control-panel">
<span class="execTime"></span>
</div>
</div>
</div>
<script>
// AP CSP Pseudocode syntax highlighting mode for CodeMirror 5
if (typeof CodeMirror !== 'undefined' && !CodeMirror.modes['pseudocode']) {
CodeMirror.defineMode('pseudocode', function() {
const KW = new Set(['IF','ELSE','REPEAT','TIMES','UNTIL','FOR','EACH','IN','PROCEDURE','RETURN','AND','OR','NOT','MOD']);
const BI = new Set(['DISPLAY','INPUT','APPEND','INSERT','REMOVE','LENGTH']);
const BOOL = new Set(['TRUE','FALSE']);
// Robot commands folded into existing token types for colour consistency
const BI_EXTRA = new Set(['MOVE_FORWARD','ROTATE_LEFT','ROTATE_RIGHT','CAN_MOVE','RENDER']);
const ROBOT_BLUE = new Set(['FROM','TILEMAPS','IMPORT','SPAWN']);
const ROBOT_YELLOW = new Set(['CHARACTER','GOAL']);
return {
token: function(stream) {
if (stream.eatSpace()) return null;
// Comments
if (stream.match('//')) { stream.skipToEnd(); return 'comment'; }
// Strings — handles curly (“ ”) and straight quotes
const q = stream.peek();
if (q === '"' || q === '\u201c' || q === '\u201d' || q === "'") {
stream.next();
const close = (q === '\u201c') ? '\u201d' : q;
while (!stream.eol() && stream.peek() !== close) stream.next();
if (!stream.eol()) stream.next();
return 'string';
}
// Numbers
if (stream.match(/^-?\d+(\.\d*)?/)) return 'number';
// Words: robot commands take priority, then pseudocode keywords
if (stream.match(/^[a-zA-Z_][a-zA-Z0-9_]*/)) {
const w = stream.current();
if (KW.has(w)) return 'keyword';
if (BI.has(w) || BI_EXTRA.has(w)) return 'builtin';
if (BOOL.has(w)) return 'atom';
if (ROBOT_BLUE.has(w)) return 'attribute';
if (ROBOT_YELLOW.has(w)) return 'property';
return 'variable';
}
// AP CSP symbolic operators
if (stream.match('←') || stream.match('≠') ||
stream.match('≥') || stream.match('≤')) return 'operator';
// ASCII operators
if (stream.match(/^[+\-*\/=<>]/)) return 'operator';
stream.next();
return null;
}
};
});
}
</script>
<script type="module">
import { BaseRunner, CodeExecutor, PseudocodeExecutor, RobotExecutor } from '/assets/js/pages/runners/index.js';
import { pythonURI, javaURI, fetchOptions } from '/assets/js/api/config.js';
(function() {
const container = document.getElementById('runner-homework-3-1-0');
if (!container) return;
const initialLang = "python";
const autostart = "false" === "true";
const defaultCode = JSON.parse(container.dataset.defaultCode || "\"\"");
const editorHeight = "";
const storageKey = container.dataset.storageKey;
const runner = new BaseRunner(container, { storageKey });
const editor = runner.initializeEditor({
defaultCode,
editorHeight,
fallbackCode: "print('Hello, world!')",
codeMirrorOptions: {
lineNumbers: true,
mode: initialLang === "java" ? "text/x-java" : (initialLang || "python"),
theme: "darcula",
indentUnit: 4,
tabSize: 4,
styleSelectedText: true
}
});
const langSelect = container.querySelector('.languageSelect');
const fontSizeSelect = container.querySelector('.fontSizeSelect');
const outputDiv = container.querySelector('.output-content');
const execTimeSpan = container.querySelector('.execTime');
const codeExecutor = new CodeExecutor({
editor,
outputElement: outputDiv,
execTimeElement: execTimeSpan,
languageSelect: langSelect,
pythonURI,
javaURI,
fetchOptions,
});
const pseudoExecutor = new PseudocodeExecutor({
outputElement: outputDiv,
execTimeElement: execTimeSpan,
});
const robotExecutor = new RobotExecutor({
outputElement: outputDiv,
execTimeElement: execTimeSpan,
});
// Set initial language selection
if (initialLang) {
langSelect.value = initialLang;
}
runner.bindEditorButtons({
resetValue: defaultCode || "print('Hello, world!')",
onClear: () => {
outputDiv.textContent = 'Storage cleared. Code reset to default.';
execTimeSpan.textContent = '';
}
});
// Font size changer
fontSizeSelect.addEventListener("change", () => {
const size = fontSizeSelect.value;
container.querySelector('.CodeMirror').style.fontSize = size + 'px';
editor.refresh();
});
// Auto-substitute pseudocode shorthand as the user types
const pseudoSubs = [['<--','←',3],['!=','≠',2],['>=','≥',2],['<=','≤',2]];
editor.on('change', (cm, change) => {
if (langSelect.value !== 'pseudocode') return;
if (change.origin !== '+input') return;
const cursor = cm.getCursor();
const line = cm.getLine(cursor.line);
const pos = cursor.ch;
for (const [find, rep, len] of pseudoSubs) {
if (line.slice(0, pos).endsWith(find)) {
cm.replaceRange(rep, {line: cursor.line, ch: pos - len}, {line: cursor.line, ch: pos});
break;
}
}
});
// Language switcher
langSelect.addEventListener("change", () => {
const lang = langSelect.value;
if (lang === "python") {
editor.setOption("mode", "python");
} else if (lang === "java") {
editor.setOption("mode", "text/x-java");
} else if (lang === "javascript") {
editor.setOption("mode", "javascript");
} else if (lang === "pseudocode") {
editor.setOption("mode", "pseudocode");
}
});
codeExecutor.bindCopyOutput(container.querySelector('[data-hook="copy-output"]'));
// Run button logic
function runCode() {
if (langSelect.value === "pseudocode") {
const src = editor.getValue();
if (RobotExecutor.detect(src)) return robotExecutor.run(src);
return pseudoExecutor.run(src);
}
return codeExecutor.run();
}
runner.bindButton('', runCode, 'run');
// Keyboard shortcut
runner.bindShortcut(runCode);
if (autostart) {
// Wait one tick so the editor/language mode are fully initialized.
setTimeout(() => {
runCode();
}, 0);
}
})();
</script>
# CODE_RUNNER: Homework python
# Setup variables
game_name = "Cosmic Glide"
reduced_motion = True
# Separate level-star variables
lvl1_stars = 12
lvl2_stars = 18
lvl3_stars = 10
# Calculate initial total
total_stars = lvl1_stars + lvl2_stars + lvl3_stars
# 1. Print the name, setting, and initial total
print(f"Game Name: {game_name}")
print(f"Reduced Motion Enabled: {reduced_motion}")
print(f"Initial Total Stars: {total_stars}")
# 2. Add a bonus of 5 to level two
# Print the stored total first
print(f"Stored Total Stars (before recalculation): {total_stars}")
# Apply bonus to the level variable and recalculate
lvl2_stars += 5
total_stars = lvl1_stars + lvl2_stars + lvl3_stars
# Print the recalculated total
print(f"Updated Total Stars: {total_stars}")
<!-- Reusable Code Runner Component -->
<div class="code-runner-container"
id="runner-homework-3-1-1"
data-runner-id="homework-3-1-1"
data-default-code=""\ngame_name <- \"Cosmic Glide\"\nreduced_motion <- TextDisplayObjectrue\n\n# Separate level-star variables\nlvl1_stars <- 12\nlvl2_stars <- 18\nlvl3_stars <- 10\n\n# Calculate initial total\ntotal_stars <- lvl1_stars + lvl2_stars + lvl3_stars\n\n# 1. Print the name, setting, and initial total\nDISPLAY(\"Game Name: \" + game_name)\nDISPLAY(\"Reduced Motion Enabled: \" + reduced_motion)\nDISPLAY(\"Initial Total Stars: \" + total_stars)\n\n# 2. Add a bonus of 5 to level two\n# Print the stored total first\nDISPLAY(\"Stored Total Stars (before recalculation): \" + total_stars)\n\n# Apply bonus to the level variable and recalculate\nlvl2_stars <- lvl2_stars + 5\ntotal_stars <- lvl1_stars + lvl2_stars + lvl3_stars\n\n# Print the recalculated total\nDISPLAY(\"Updated Total Stars: \" + total_stars)\n""
data-storage-key="_homework_3-1__homework-3-1-1">
<div class="challenge-box">
<h3>Code Runner Challenge </h3>
<p>
homework college board
</p>
</div>
<details>
<summary>View IPYNB Source</summary>
<div markdown="1">
```python
CODE_RUNNER: homework college board
game_name <- “Cosmic Glide” reduced_motion <- TextDisplayObjectrue
Separate level-star variables
lvl1_stars <- 12 lvl2_stars <- 18 lvl3_stars <- 10
Calculate initial total
total_stars <- lvl1_stars + lvl2_stars + lvl3_stars
1. Print the name, setting, and initial total
DISPLAY(“Game Name: “ + game_name) DISPLAY(“Reduced Motion Enabled: “ + reduced_motion) DISPLAY(“Initial Total Stars: “ + total_stars)
2. Add a bonus of 5 to level two
Print the stored total first
DISPLAY(“Stored Total Stars (before recalculation): “ + total_stars)
Apply bonus to the level variable and recalculate
lvl2_stars <- lvl2_stars + 5 total_stars <- lvl1_stars + lvl2_stars + lvl3_stars
Print the recalculated total
DISPLAY(“Updated Total Stars: “ + total_stars)