Skip to content

Commit abb08c4

Browse files
authored
Merge branch 'exercism:main' into main
2 parents 975bbcd + a4d8b88 commit abb08c4

File tree

17 files changed

+326
-77
lines changed

17 files changed

+326
-77
lines changed
Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
# Instructions
22

3-
Implement a binary search algorithm.
3+
Your task is to implement a binary search algorithm.
44

5-
Searching a sorted collection is a common task. A dictionary is a sorted
6-
list of word definitions. Given a word, one can find its definition. A
7-
telephone book is a sorted list of people's names, addresses, and
8-
telephone numbers. Knowing someone's name allows one to quickly find
9-
their telephone number and address.
5+
A binary search algorithm finds an item in a list by repeatedly splitting it in half, only keeping the half which contains the item we're looking for.
6+
It allows us to quickly narrow down the possible locations of our item until we find it, or until we've eliminated all possible locations.
107

11-
If the list to be searched contains more than a few items (a dozen, say)
12-
a binary search will require far fewer comparisons than a linear search,
13-
but it imposes the requirement that the list be sorted.
8+
```exercism/caution
9+
Binary search only works when a list has been sorted.
10+
```
1411

15-
In computer science, a binary search or half-interval search algorithm
16-
finds the position of a specified input value (the search "key") within
17-
an array sorted by key value.
12+
The algorithm looks like this:
1813

19-
In each step, the algorithm compares the search key value with the key
20-
value of the middle element of the array.
14+
- Divide the sorted list in half and compare the middle element with the item we're looking for.
15+
- If the middle element is our item, then we're done.
16+
- If the middle element is greater than our item, we can eliminate that number and all the numbers **after** it.
17+
- If the middle element is less than our item, we can eliminate that number and all the numbers **before** it.
18+
- Repeat the process on the part of the list that we kept.
2119

22-
If the keys match, then a matching element has been found and its index,
23-
or position, is returned.
20+
Here's an example:
2421

25-
Otherwise, if the search key is less than the middle element's key, then
26-
the algorithm repeats its action on the sub-array to the left of the
27-
middle element or, if the search key is greater, on the sub-array to the
28-
right.
22+
Let's say we're looking for the number 23 in the following sorted list: `[4, 8, 12, 16, 23, 28, 32]`.
2923

30-
If the remaining array to be searched is empty, then the key cannot be
31-
found in the array and a special "not found" indication is returned.
32-
33-
A binary search halves the number of items to check with each iteration,
34-
so locating an item (or determining its absence) takes logarithmic time.
35-
A binary search is a dichotomic divide and conquer search algorithm.
24+
- We start by comparing 23 with the middle element, 16.
25+
- Since 23 is greater than 16, we can eliminate the left half of the list, leaving us with `[23, 28, 32]`.
26+
- We then compare 23 with the new middle element, 28.
27+
- Since 23 is less than 28, we can eliminate the right half of the list: `[23]`.
28+
- We've found our item.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Introduction
2+
3+
You have stumbled upon a group of mathematicians who are also singer-songwriters.
4+
They have written a song for each of their favorite numbers, and, as you can imagine, they have a lot of favorite numbers.
5+
6+
You are curious to hear the song for your favorite number, but with so many songs to wade through, finding the right song could take a while.
7+
Fortunately, they have organized their songs in a playlist sorted by the title — which is simply the number that the song is about.
8+
9+
You realize that you can use a binary search algorithm to quickly find a song given the title.
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# Instructions
22

3-
Bob is a lackadaisical teenager. In conversation, his responses are very limited.
3+
Your task is to determine what Bob will reply to someone when they say something to him or ask him a question.
44

5-
Bob answers 'Sure.' if you ask him a question, such as "How are you?".
5+
Bob only ever answers one of five things:
66

7-
He answers 'Whoa, chill out!' if you YELL AT HIM (in all capitals).
8-
9-
He answers 'Calm down, I know what I'm doing!' if you yell a question at him.
10-
11-
He says 'Fine. Be that way!' if you address him without actually saying
12-
anything.
13-
14-
He answers 'Whatever.' to anything else.
15-
16-
Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English.
7+
- **"Sure."**
8+
This is his response if you ask him a question, such as "How are you?"
9+
The convention used for questions is that it ends with a question mark.
10+
- **"Whoa, chill out!"**
11+
This is his answer if you YELL AT HIM.
12+
The convention used for yelling is ALL CAPITAL LETTERS.
13+
- **"Calm down, I know what I'm doing!"**
14+
This is what he says if you yell a question at him.
15+
- **"Fine. Be that way!"**
16+
This is how he responds to silence.
17+
The convention used for silence is nothing, or various combinations of whitespace characters.
18+
- **"Whatever."**
19+
This is what he answers to anything else.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Introduction
2+
3+
Bob is a [lackadaisical][] teenager.
4+
He likes to think that he's very cool.
5+
And he definitely doesn't get excited about things.
6+
That wouldn't be cool.
7+
8+
When people talk to him, his responses are pretty limited.
9+
10+
[lackadaisical]: https://www.collinsdictionary.com/dictionary/english/lackadaisical
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Instructions
22

3-
Given a moment, determine the moment that would be after a gigasecond
4-
has passed.
3+
Your task is to determine the date and time one gigasecond after a certain date.
54

6-
A gigasecond is 10^9 (1,000,000,000) seconds.
5+
A gigasecond is one thousand million seconds.
6+
That is a one with nine zeros after it.
7+
8+
If you were born on _January 24th, 2015 at 22:00 (10:00:00pm)_, then you would be a gigasecond old on _October 2nd, 2046 at 23:46:40 (11:46:40pm)_.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Introduction
2+
3+
The way we measure time is kind of messy.
4+
We have 60 seconds in a minute, and 60 minutes in an hour.
5+
This comes from ancient Babylon, where they used 60 as the basis for their number system.
6+
We have 24 hours in a day, 7 days in a week, and how many days in a month?
7+
Well, for days in a month it depends not only on which month it is, but also on what type of calendar is used in the country you live in.
8+
9+
What if, instead, we only use seconds to express time intervals?
10+
Then we can use metric system prefixes for writing large numbers of seconds in more easily comprehensible quantities.
11+
12+
- A food recipe might explain that you need to let the brownies cook in the oven for two kiloseconds (that's two thousand seconds).
13+
- Perhaps you and your family would travel to somewhere exotic for two megaseconds (that's two million seconds).
14+
- And if you and your spouse were married for _a thousand million_ seconds, you would celebrate your one gigasecond anniversary.
15+
16+
```exercism/note
17+
If we ever colonize Mars or some other planet, measuring time is going to get even messier.
18+
If someone says "year" do they mean a year on Earth or a year on Mars?
19+
20+
The idea for this exercise came from the science fiction novel ["A Deepness in the Sky"][vinge-novel] by author Vernor Vinge.
21+
In it the author uses the metric system as the basis for time measurements.
22+
23+
[vinge-novel]: https://www.tor.com/2017/08/03/science-fiction-with-something-for-everyone-a-deepness-in-the-sky-by-vernor-vinge/
24+
```

exercises/practice/paasio/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::io::{Read, Result, Write};
22

3+
// the PhantomData instances in this file are just to stop compiler complaints
4+
// about missing generics; feel free to remove them
5+
36
pub struct ReadStats<R>(::std::marker::PhantomData<R>);
47

58
impl<R: Read> ReadStats<R> {
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Instructions
22

3-
Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma,
4-
"every letter") is a sentence using every letter of the alphabet at least once.
5-
The best known English pangram is:
6-
> The quick brown fox jumps over the lazy dog.
3+
Your task is to figure out if a sentence is a pangram.
74

8-
The alphabet used consists of ASCII letters `a` to `z`, inclusive, and is case
9-
insensitive. Any characters which are not an ASCII letter should be ignored.
5+
A pangram is a sentence using every letter of the alphabet at least once.
6+
It is case insensitive, so it doesn't matter if a letter is lower-case (e.g. `k`) or upper-case (e.g. `K`).
7+
8+
For this exercise we only use the basic letters used in the English alphabet: `a` to `z`.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Introduction
2+
3+
You work for a company that sells fonts through their website.
4+
They'd like to show a different sentence each time someone views a font on their website.
5+
To give a comprehensive sense of the font, the random sentences should use **all** the letters in the English alphabet.
6+
7+
They're running a competition to get suggestions for sentences that they can use.
8+
You're in charge of checking the submissions to see if they are valid.
9+
10+
```exercism/note
11+
Pangram comes from Greek, παν γράμμα, pan gramma, which means "every letter".
12+
13+
The best known English pangram is:
14+
15+
> The quick brown fox jumps over the lazy dog.
16+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"introduction": {
3+
"authors": ["bobahop"],
4+
"contributors": []
5+
},
6+
"approaches": [
7+
{
8+
"uuid": "cd0c0a31-0905-47f0-815c-02597a7cdf40",
9+
"slug": "iterate-once",
10+
"title": "Iterate once",
11+
"blurb": "Iterate once even when reversed.",
12+
"authors": ["bobahop"]
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)