feat: add build a number generator lab to python chapter (#60740)

This commit is contained in:
Ilenia
2025-06-10 16:41:13 +02:00
committed by GitHub
parent c32eb093d2
commit ce0ed72a93
5 changed files with 160 additions and 8 deletions

View File

@@ -3799,9 +3799,9 @@
"title": "Placeholder - Waiting for title",
"intro": [""]
},
"lab-placeholder-loops-and-sequences": {
"title": "Placeholder - waiting for title",
"intro": [""]
"lab-number-pattern-generator": {
"title": "Build a Number Pattern Generator",
"intro": ["In this lab you will build a number pattern generator."]
},
"review-loops-and-sequences": {
"title": "Loops and Sequences Review",
@@ -3998,10 +3998,7 @@
"In this workshop, you'll implement the merge sort algorithm to sort a list of random numbers."
]
},
"lab-quick-sort": {
"title": "Build a Quick Sort",
"intro": [""]
},
"lab-quick-sort": { "title": "Build a Quick Sort", "intro": [""] },
"lab-selection-sort": {
"title": "Implement the Selection Sort Algorithm",
"intro": [

View File

@@ -0,0 +1,9 @@
---
title: Introduction to the Build a Number Pattern Generator
block: lab-number-pattern-generator
superBlock: full-stack-developer
---
## Introduction to the Build a Number Pattern Generator
In this lab you will build a number pattern generator.

View File

@@ -0,0 +1,15 @@
{
"name": "Build a Number Pattern Generator",
"blockLayout": "link",
"blockType": "lab",
"isUpcomingChange": true,
"dashedName": "lab-number-pattern-generator",
"superBlock": "full-stack-developer",
"challengeOrder": [
{
"id": "6842a6cd9836f0114a5b7a8a",
"title": "Build a Number Pattern Generator"
}
],
"helpCategory": "Python"
}

View File

@@ -0,0 +1,131 @@
---
id: 6842a6cd9836f0114a5b7a8a
title: Build a Number Pattern Generator
challengeType: 27
dashedName: build-a-number-pattern-generator
---
# --description--
In this lab you will practice the basics of Python by building a small app that creates a number pattern.
Fulfill the user stories below and get all the tests to pass to complete the lab.
**User Stories:**
1. You should define a function named `number_pattern` that takes a single parameter `n` (representing a positive integer).
1. `number_pattern` should use a `for` loop.
1. `number_pattern(n)` should return a string with all the integers starting from 1 up to `n` (included) separated by a space. For example, `number_pattern(4)` should return the string `1 2 3 4`.
1. If the argument passed to the function is not an integer value, the function should return `Argument must be an integer value.`.
1. If the argument passed to the function is less than 1, the function should return `Argument must be an integer greater than 0.`.
# --hints--
You should have a `number_patter` function.
```js
({
test: () => runPython(`
assert _Node(_code).has_function('number_pattern')
`)
})
```
The `number_pattern` function should have one parameter named `n`.
```js
({
test: () => runPython(`
assert _Node(_code).find_function('number_pattern').has_args('n')
`)
})
```
`number_pattern(4)` should return `1 2 3 4`.
```js
({
test: () => runPython(`
assert number_pattern(4) == '1 2 3 4'
`)
})
```
`number_pattern(12)` should return `1 2 3 4 5 6 7 8 9 10 11 12`.
```js
({
test: () => runPython(`
assert number_pattern(12) == '1 2 3 4 5 6 7 8 9 10 11 12'
`)
})
```
`number_pattern` should return a space separated list of numbers for any positive integer.
```js
({
test: () => runPython(`
from random import randint
n = randint(1,65)
ls = ' '.join(map(lambda n: str(n), list(range(1, n+1))))
assert number_pattern(n) == ls
n2 = randint(34,122)
ls2 = ' '.join(map(lambda n: str(n), list(range(1, n2+1))))
assert number_pattern(n2) == ls2
`)
})
```
`number_pattern` should return `Argument must be an integer value.` when passed a value that is not an integer.
```js
({
test: () => runPython(`
string = 'Argument must be an integer value.'
assert number_pattern('a') == string
assert number_pattern(3.5) == string
assert number_pattern(lambda n: n) == string
`)
})
```
`number_pattern` should return `Argument must be an integer greater than 0.` when passed a non-positive integer.
```js
({
test: () => runPython(`
from random import randint
string = 'Argument must be an integer greater than 0.'
assert number_pattern(0) == string
assert number_pattern(-1185) == string
assert number_pattern(-45) == string
assert number_pattern(randint(-9999999, 0)) == string
`)
})
```
# --seed--
## --seed-contents--
```py
```
# --solutions--
```py
def number_pattern(n):
result = "1"
if isinstance(n, int):
if n > 0:
for i in range(2, n + 1):
result += f" {i}"
return result
return "Argument must be an integer greater than 0."
return "Argument must be an integer value."
```

View File

@@ -1194,7 +1194,7 @@
"blocks": [
{ "dashedName": "lecture-working-with-loops-and-sequences" },
{ "dashedName": "workshop-placeholder-loops-and-sequences" },
{ "dashedName": "lab-placeholder-loops-and-sequences" },
{ "dashedName": "lab-number-pattern-generator" },
{ "dashedName": "review-loops-and-sequences" },
{ "dashedName": "quiz-loops-and-sequences" }
]