GUIDED LAB 02 OF 06 · VERIFIED SCARP
Sum loop indices
लूप इंडेक्स का योग
The challenge · अभ्यास
Use the counted repeat loop to print the sum of indices 0 through 4, rather than the number of iterations.
गिने हुए repeat लूप से केवल पुनरावृत्तियों की संख्या नहीं, बल्कि index 0 से 4 तक का योग प्रिंट करें।
starter.srp
// Lab: add the actual loop index rather than a constant.
var total = 0;
repeat 5 as index {
total += 1;
}
print(total);
solution.stdout
10
Run and check your attempt locally
Copy starter.srp to attempt.srp in the same lab folder,
edit it, and use the pinned Scarp compiler from the repository root.
Keeping the same folder is important for imports and the bundled _support helper.
scarp verify labs/02-control-flow/attempt.srp
scarp exec labs/02-control-flow/attempt.srpCompare the printed output with the fixture shown above. No website execution, uploads or stored answers are involved.
Show hint · संकेत देखें
Add index to total on each pass instead of adding 1.
हर बार total में 1 के बजाय index जोड़ें।
Reveal tested solution · सत्यापित हल देखें
// Sum zero-based indices 0, 1, 2, 3, and 4.
var total = 0;
repeat 5 as index {
total += index;
}
print(total);
The indices 0, 1, 2, 3 and 4 add up to 10.
इंडेक्स 0, 1, 2, 3 और 4 का योग 10 होता है।
Reference .srp on GitHub ↗OPTIONAL · COMPILER-VERIFIED STRETCH
Find the first qualifying reading
पहली उपयुक्त रीडिंग खोजें
The readings are 5, 8, 13, 8 and 21. The starter looks for the first reading above 20, inspecting all five values. Change the condition to find the first reading strictly above 10 and stop at once. Print its zero-based index and the number of values inspected: 2 3.
रीडिंग 5, 8, 13, 8 और 21 हैं। स्टार्टर 20 से बड़ी पहली रीडिंग खोजकर पाँचों मान जाँचता है। शर्त बदलें ताकि 10 से बड़ी पहली रीडिंग मिलते ही लूप रुक जाए। शून्य-आधारित इंडेक्स और जाँचे गए मानों की संख्या प्रिंट करें: 2 3।
stretch-starter.srp
// Stretch challenge: find the first reading above the required threshold.
// The starter searches for a too-high value and stops too late.
bind readings: integers = [integer: 5, 8, 13, 8, 21];
var first = -1;
var inspected = 0;
var index = 0;
while index < ints_len(readings) {
inspected += 1;
if readings[index] > 20 {
first = index;
break;
}
index += 1;
}
print(first, inspected);
stretch-solution.stdout
2 3
Try it locally · स्वयं चलाएँ
Copy stretch-starter.srp to stretch-attempt.srp in the same lab folder.
Edit only your copy; the original starter and fixture remain unchanged.
scarp verify labs/02-control-flow/stretch-attempt.srp
scarp exec labs/02-control-flow/stretch-attempt.srpCompare the exact stdout above. The website does not run Scarp, save attempts or award a score.
Show stretch hint · संकेत देखें
The loop, inspection counter, and break are already correct. Check only the strict comparison against the threshold.
लूप, जाँच काउंटर और break पहले से सही हैं। केवल सीमा से की गई सख्त तुलना की शर्त देखें।
Reveal tested stretch solution · सत्यापित हल देखें
// Find the first reading strictly above 10, then exit the loop early.
bind readings: integers = [integer: 5, 8, 13, 8, 21];
var first = -1;
var inspected = 0;
var index = 0;
while index < ints_len(readings) {
inspected += 1;
if readings[index] > 10 {
first = index;
break;
}
index += 1;
}
print(first, inspected);
13 is the first reading greater than 10, at index 2. The loop visits indices 0, 1 and 2, so the inspected count is 3. The break skips the later 8 and 21.
10 से बड़ी पहली रीडिंग 13 है, जिसका इंडेक्स 2 है। लूप इंडेक्स 0, 1 और 2 जाँचता है, इसलिए काउंटर 3 बनता है। break बाद के 8 और 21 को जाँचे बिना लूप समाप्त करता है।
View tested stretch solution ↗The starter compiles but intentionally does not produce the goal. The reference solution and its output are verified on Linux, macOS and Windows against Scarp v0.0.6-alpha.3. Browse all labs ↗