Skip to content

Commit 689a41f

Browse files
authored
Sync problem specs (#273)
* Sync docs * Sync `bob` tests * Sync `luhn` docs * Update `bob` tests and example
1 parent 5cdf7e9 commit 689a41f

File tree

10 files changed

+166
-58
lines changed

10 files changed

+166
-58
lines changed

exercises/practice/bob/.meta/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"sentientmonkey"
44
],
55
"contributors": [
6+
"BNAndras",
67
"kotp",
78
"pclausen",
89
"sjwarner"

exercises/practice/bob/.meta/example.f90

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
module bob
22
implicit none
3+
4+
character(len=1), parameter :: CARRIAGE_RETURN_CHAR = char(13)
5+
character(len=1), parameter :: NEWLINE_CHAR = char(10)
6+
character(len=1), parameter :: TAB_CHAR = char(9)
7+
38
contains
49
function is_uppercase(str)
510
logical :: is_uppercase
@@ -23,15 +28,30 @@ function is_question(str)
2328
character(*) :: str
2429
character :: chr
2530
integer :: i
26-
i = len_trim(str)
27-
chr = str(i:i)
28-
is_question = (chr .EQ. '?')
31+
do i = len(str), 1, -1
32+
chr = str(i:i)
33+
if (chr /= ' ' .and. chr /= TAB_CHAR .and. &
34+
chr /= NEWLINE_CHAR .and. chr /= CARRIAGE_RETURN_CHAR) then
35+
is_question = (chr .EQ. '?')
36+
return
37+
end if
38+
end do
39+
is_question = .false.
2940
end function is_question
3041

3142
function is_blank(str)
3243
logical :: is_blank
3344
character(*) :: str
34-
is_blank = len_trim(adjustl(str)) == 0
45+
character :: chr
46+
integer :: i
47+
is_blank = .TRUE.
48+
do i = 1, len(str)
49+
if (chr /= ' ' .and. chr /= TAB_CHAR .and. &
50+
chr /= NEWLINE_CHAR .and. chr /= CARRIAGE_RETURN_CHAR) then
51+
is_blank = .FALSE.
52+
return
53+
end if
54+
end do
3555
end function is_blank
3656

3757
function hey(statement)

exercises/practice/bob/.meta/tests.toml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[e162fead-606f-437a-a166-d051915cea8e]
613
description = "stating something"
@@ -64,6 +71,7 @@ description = "alternate silence"
6471

6572
[66953780-165b-4e7e-8ce3-4bcb80b6385a]
6673
description = "multiple line question"
74+
include = false
6775

6876
[5371ef75-d9ea-4103-bcfa-2da973ddec1b]
6977
description = "starting with whitespace"
@@ -76,3 +84,7 @@ description = "other whitespace"
7684

7785
[12983553-8601-46a8-92fa-fcaa3bc4a2a0]
7886
description = "non-question ending with whitespace"
87+
88+
[2c7278ac-f955-4eb4-bf8f-e33eb4116a15]
89+
description = "multiple line question"
90+
reimplements = "66953780-165b-4e7e-8ce3-4bcb80b6385a"

exercises/practice/bob/bob_test.f90

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ program bob_test_main
44

55
implicit none
66

7+
character(len=1), parameter :: CARRIAGE_RETURN = CHAR(13)
8+
character(len=1), parameter :: NEWLINE = CHAR(10)
9+
character(len=1), parameter :: TAB = CHAR(9)
710

811
! Test 1: stating something
912
call assert_equal("Whatever.", hey("Tom-ay-to, tom-aaaah-to."), "stating something")
@@ -63,21 +66,26 @@ program bob_test_main
6366
call assert_equal("Fine. Be that way!", hey(" "), "prolonged silence")
6467

6568
! Test 20: alternate silence
66-
!call assert_equal("Fine. Be that way!", hey(" "), "alternate silence")
69+
call assert_equal("Fine. Be that way!", hey(TAB // TAB // &
70+
& TAB // TAB // &
71+
& TAB // TAB // &
72+
& TAB // TAB // &
73+
& TAB // TAB), "alternate silence")
74+
6775
! Test 21: multiple line question
68-
call assert_equal("Whatever.", hey(""// &
69-
& "Does this cryogenic chamber make me look fat?"// &
70-
& "No."), "multiple line question")
76+
call assert_equal("Sure.", hey( NEWLINE // &
77+
& "Does this cryogenic chamber make"// &
78+
& NEWLINE // "me look fat?"), "multiple line question")
79+
7180
! Test 22: starting with whitespace
7281
call assert_equal("Whatever.", hey(" hmmmmmmm..."), "starting with whitespace")
7382

7483
! Test 23: ending with whitespace
7584
call assert_equal("Sure.", hey("Okay if like my spacebar quite a bit? "), "ending with whitespace")
7685

7786
! Test 24: other whitespace
78-
!call assert_equal("Fine. Be that way!", hey(""// &
79-
! & "
80-
! "), "other whitespace")
87+
call assert_equal("Fine. Be that way!", hey(NEWLINE // CARRIAGE_RETURN // " " // TAB), "other whitespace")
88+
8189
! Test 25: non-question ending with whitespace
8290
call assert_equal("Whatever.", hey("This is a statement ending with whitespace "), "non-question ending with whitespace")
8391

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
# Instructions
22

3-
Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.
3+
Calculate the number of grains of wheat on a chessboard.
44

5-
There once was a wise servant who saved the life of a prince.
6-
The king promised to pay whatever the servant could dream up.
7-
Knowing that the king loved chess, the servant told the king he would like to have grains of wheat.
8-
One grain on the first square of a chess board, with the number of grains doubling on each successive square.
5+
A chessboard has 64 squares.
6+
Square 1 has one grain, square 2 has two grains, square 3 has four grains, and so on, doubling each time.
97

10-
There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).
8+
Write code that calculates:
119

12-
Write code that shows:
13-
14-
- how many grains were on a given square, and
10+
- the number of grains on a given square
1511
- the total number of grains on the chessboard
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Introduction
2+
3+
There once was a wise servant who saved the life of a prince.
4+
The king promised to pay whatever the servant could dream up.
5+
Knowing that the king loved chess, the servant told the king he would like to have grains of wheat.
6+
One grain on the first square of a chessboard, with the number of grains doubling on each successive square.
Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Instructions
22

3-
Determine whether a credit card number is valid according to the [Luhn formula][luhn].
3+
Determine whether a number is valid according to the [Luhn formula][luhn].
44

55
The number will be provided as a string.
66

@@ -10,54 +10,59 @@ Strings of length 1 or less are not valid.
1010
Spaces are allowed in the input, but they should be stripped before checking.
1111
All other non-digit characters are disallowed.
1212

13-
### Example 1: valid credit card number
13+
## Examples
1414

15-
```text
16-
4539 3195 0343 6467
17-
```
15+
### Valid credit card number
1816

19-
The first step of the Luhn algorithm is to double every second digit, starting from the right.
20-
We will be doubling
17+
The number to be checked is `4539 3195 0343 6467`.
18+
19+
The first step of the Luhn algorithm is to start at the end of the number and double every second digit, beginning with the second digit from the right and moving left.
2120

2221
```text
2322
4539 3195 0343 6467
2423
↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ (double these)
2524
```
2625

27-
If doubling the number results in a number greater than 9 then subtract 9 from the product.
28-
The results of our doubling:
26+
If the result of doubling a digit is greater than 9, we subtract 9 from that result.
27+
We end up with:
2928

3029
```text
3130
8569 6195 0383 3437
3231
```
3332

34-
Then sum all of the digits:
33+
Finally, we sum all digits.
34+
If the sum is evenly divisible by 10, the original number is valid.
3535

3636
```text
37-
8+5+6+9+6+1+9+5+0+3+8+3+3+4+3+7 = 80
37+
8 + 5 + 6 + 9 + 6 + 1 + 9 + 5 + 0 + 3 + 8 + 3 + 3 + 4 + 3 + 7 = 80
3838
```
3939

40-
If the sum is evenly divisible by 10, then the number is valid.
41-
This number is valid!
40+
80 is evenly divisible by 10, so number `4539 3195 0343 6467` is valid!
41+
42+
### Invalid Canadian SIN
43+
44+
The number to be checked is `066 123 468`.
4245

43-
### Example 2: invalid credit card number
46+
We start at the end of the number and double every second digit, beginning with the second digit from the right and moving left.
4447

4548
```text
46-
8273 1232 7352 0569
49+
066 123 478
50+
↑ ↑ ↑ ↑ (double these)
4751
```
4852

49-
Double the second digits, starting from the right
53+
If the result of doubling a digit is greater than 9, we subtract 9 from that result.
54+
We end up with:
5055

5156
```text
52-
7253 2262 5312 0539
57+
036 226 458
5358
```
5459

55-
Sum the digits
60+
We sum the digits:
5661

5762
```text
58-
7+2+5+3+2+2+6+2+5+3+1+2+0+5+3+9 = 57
63+
0 + 3 + 6 + 2 + 2 + 6 + 4 + 5 + 8 = 36
5964
```
6065

61-
57 is not evenly divisible by 10, so this number is not valid.
66+
36 is not evenly divisible by 10, so number `066 123 478` is not valid!
6267

6368
[luhn]: https://en.wikipedia.org/wiki/Luhn_algorithm

exercises/practice/luhn/.docs/introduction.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
At the Global Verification Authority, you've just been entrusted with a critical assignment.
44
Across the city, from online purchases to secure logins, countless operations rely on the accuracy of numerical identifiers like credit card numbers, bank account numbers, transaction codes, and tracking IDs.
5-
The Luhn algorithm is a simple checksum formula used to ensure these numbers are valid and error-free.
5+
The Luhn algorithm is a simple checksum formula used to help identify mistyped numbers.
66

77
A batch of identifiers has just arrived on your desk.
88
All of them must pass the Luhn test to ensure they're legitimate.
9-
If any fail, they'll be flagged as invalid, preventing errors or fraud, such as incorrect transactions or unauthorized access.
9+
If any fail, they'll be flagged as invalid, preventing mistakes such as incorrect transactions or failed account verifications.
1010

1111
Can you ensure this is done right? The integrity of many services depends on you.

exercises/practice/saddle-points/.docs/instructions.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ Or it might have one, or even several.
1313
Here is a grid that has exactly one candidate tree.
1414

1515
```text
16-
1 2 3 4
17-
|-----------
18-
1 | 9 8 7 8
19-
2 | 5 3 2 4 <--- potential tree house at row 2, column 1, for tree with height 5
20-
3 | 6 6 7 1
16+
17+
1 2 3 4
18+
|-----------
19+
1 | 9 8 7 8
20+
→ 2 |[5] 3 2 4
21+
3 | 6 6 7 1
2122
```
2223

2324
- Row 2 has values 5, 3, 2, and 4. The largest value is 5.

exercises/practice/sieve/.docs/instructions.md

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,96 @@ A prime number is a number larger than 1 that is only divisible by 1 and itself.
66
For example, 2, 3, 5, 7, 11, and 13 are prime numbers.
77
By contrast, 6 is _not_ a prime number as it not only divisible by 1 and itself, but also by 2 and 3.
88

9-
To use the Sieve of Eratosthenes, you first create a list of all the numbers between 2 and your given number.
10-
Then you repeat the following steps:
9+
To use the Sieve of Eratosthenes, first, write out all the numbers from 2 up to and including your given number.
10+
Then, follow these steps:
1111

12-
1. Find the next unmarked number in your list (skipping over marked numbers).
12+
1. Find the next unmarked number (skipping over marked numbers).
1313
This is a prime number.
1414
2. Mark all the multiples of that prime number as **not** prime.
1515

16-
You keep repeating these steps until you've gone through every number in your list.
16+
Repeat the steps until you've gone through every number.
1717
At the end, all the unmarked numbers are prime.
1818

1919
~~~~exercism/note
20-
The tests don't check that you've implemented the algorithm, only that you've come up with the correct list of primes.
21-
To check you are implementing the Sieve correctly, a good first test is to check that you do not use division or remainder operations.
20+
The Sieve of Eratosthenes marks off multiples of each prime using addition (repeatedly adding the prime) or multiplication (directly computing its multiples), rather than checking each number for divisibility.
21+
22+
The tests don't check that you've implemented the algorithm, only that you've come up with the correct primes.
2223
~~~~
2324

2425
## Example
2526

2627
Let's say you're finding the primes less than or equal to 10.
2728

28-
- List out 2, 3, 4, 5, 6, 7, 8, 9, 10, leaving them all unmarked.
29+
- Write out 2, 3, 4, 5, 6, 7, 8, 9, 10, leaving them all unmarked.
30+
31+
```text
32+
2 3 4 5 6 7 8 9 10
33+
```
34+
2935
- 2 is unmarked and is therefore a prime.
3036
Mark 4, 6, 8 and 10 as "not prime".
37+
38+
```text
39+
2 3 [4] 5 [6] 7 [8] 9 [10]
40+
41+
```
42+
3143
- 3 is unmarked and is therefore a prime.
3244
Mark 6 and 9 as not prime _(marking 6 is optional - as it's already been marked)_.
45+
46+
```text
47+
2 3 [4] 5 [6] 7 [8] [9] [10]
48+
49+
```
50+
3351
- 4 is marked as "not prime", so we skip over it.
52+
53+
```text
54+
2 3 [4] 5 [6] 7 [8] [9] [10]
55+
56+
```
57+
3458
- 5 is unmarked and is therefore a prime.
3559
Mark 10 as not prime _(optional - as it's already been marked)_.
60+
61+
```text
62+
2 3 [4] 5 [6] 7 [8] [9] [10]
63+
64+
```
65+
3666
- 6 is marked as "not prime", so we skip over it.
67+
68+
```text
69+
2 3 [4] 5 [6] 7 [8] [9] [10]
70+
71+
```
72+
3773
- 7 is unmarked and is therefore a prime.
74+
75+
```text
76+
2 3 [4] 5 [6] 7 [8] [9] [10]
77+
78+
```
79+
3880
- 8 is marked as "not prime", so we skip over it.
81+
82+
```text
83+
2 3 [4] 5 [6] 7 [8] [9] [10]
84+
85+
```
86+
3987
- 9 is marked as "not prime", so we skip over it.
88+
89+
```text
90+
2 3 [4] 5 [6] 7 [8] [9] [10]
91+
92+
```
93+
4094
- 10 is marked as "not prime", so we stop as there are no more numbers to check.
4195

42-
You've examined all numbers and found 2, 3, 5, and 7 are still unmarked, which means they're the primes less than or equal to 10.
96+
```text
97+
2 3 [4] 5 [6] 7 [8] [9] [10]
98+
99+
```
100+
101+
You've examined all the numbers and found that 2, 3, 5, and 7 are still unmarked, meaning they're the primes less than or equal to 10.

0 commit comments

Comments
 (0)