Skip to content

Commit 5992ed3

Browse files
committed
add sort to problems
1 parent 20e9098 commit 5992ed3

16 files changed

+195098
-0
lines changed

sort/.cs50.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
lab50:
2+
files:
3+
- !exclude "*"
4+
- !include sort1
5+
- !include sort2
6+
- !include sort3
7+
- !include sorted5000.txt
8+
- !include sorted10000.txt
9+
- !include sorted50000.txt
10+
- !include reversed5000.txt
11+
- !include reversed10000.txt
12+
- !include reversed50000.txt
13+
- !include random5000.txt
14+
- !include random10000.txt
15+
- !include random50000.txt
16+
- !include answers.txt
17+
18+
check50:
19+
files: &check50_files
20+
- !exclude "*"
21+
- !require answers.txt
22+
23+
submit50:
24+
files: *check50_files
25+
style: false

sort/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Sort
2+
3+
Analyze three sorting programs to determine which algorithms they use.
4+
5+
## Background
6+
7+
Recall from class a few algorithms in which we can sort a list of numbers: selection sort, bubble sort, and merge sort.
8+
9+
* Selection sort iterates through the unsorted portions of a list, selecting the smallest element each time and moving it to its correct location.
10+
* Bubble sort compares two adjacent values at a time and swaps them if they are in the incorrect order. This continues until the list is sorted.
11+
* Merge sort divides the list into two repeatedly and then combines the smaller lists back into a larger one in the correct order.
12+
13+
## Instructions
14+
15+
Provided to you are three sorts, `sort1`, `sort2`, and `sort3`. The three sorts implemented are selection sort, bubble sort, and merge sort. Determine the sorting algorithms each sort implements.
16+
17+
* `sort1`, `sort2`, and `sort3` are binary files and cannot be opened by the user. To assess which sort implements which algorithm, run the sorts on different lists of values.
18+
* Multiple `.txt` files are provided to you. These files contain `n` lines of values, either reversed, shuffled, or sorted.
19+
* For example, `reversed10000.txt` contains 10000 lines of numbers that are reversed from `10000`, while `shuffled100000.txt` contains 100000 lines of numbers that are in random order.
20+
* To run the sorts on the text files, in the terminal, run `./[sort_file] [text_file.txt]`.
21+
* For example, to sort `reversed10000.txt` with `sort1`, run `./sort1 reversed10000.txt`.
22+
* Record your predictions in `answers.txt`.
23+
24+
### Hints
25+
26+
* You may find it helpful to time your sorts. To do so, run `time ./[sort_file] [text_file.txt]`.
27+
* The different types of `.txt` files may help you determine which sort is which. Consider how each algorithm performs with an already sorted list. How about a reversed list? Or shuffled list? It may help to work through a smaller list of each type and walk through each sorting process.
28+
29+
{% next %}
30+
31+
## How to Submit
32+
33+
TODO

sort/__init__.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import check50
2+
from re import search
3+
from re import findall
4+
5+
@check50.check()
6+
def exists():
7+
"""answers.txt exists"""
8+
check50.exists("answers.txt")
9+
10+
@check50.check(exists)
11+
def answers():
12+
"""answers all questions"""
13+
content = open("answers.txt", "r").read()
14+
if "TODO" in content:
15+
raise check50.Failure("Not all questions answered.")
16+
17+
@check50.check(exists)
18+
def sorts():
19+
"""correctly identifies each sort"""
20+
21+
check50.log("checking that sorts are classified correctly...")
22+
23+
expected = ["sort1 uses:\s*[Bb][Uu][Bb][Bb][Ll][Ee]", "sort2 uses:\s*[Mm][Ee][Rr][Gg][Ee]", "sort3 uses:\s*[Ss][Ee][Ll][Ee][Cc][Tt][Ii][Oo][Nn]"]
24+
actual = open("answers.txt", "r").read()
25+
26+
for e in expected:
27+
if not search(e, actual):
28+
raise check50.Failure("Incorrect assignment of sorts.")
29+

sort/answers.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sort1 uses: TODO
2+
3+
How do you know?: TODO
4+
5+
sort2 uses: TODO
6+
7+
How do you know?: TODO
8+
9+
sort3 uses: TODO
10+
11+
How do you know?: TODO

0 commit comments

Comments
 (0)