Skip to content

Commit d098126

Browse files
authored
1 new exercise: binary-search (#136)
* adding binary-search * fixed binary-search.cut
1 parent 835af7f commit d098126

File tree

13 files changed

+611
-1
lines changed

13 files changed

+611
-1
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,14 @@
313313
"prerequisites": [],
314314
"difficulty": 3
315315
},
316+
{
317+
"slug": "binary-search",
318+
"name": "Binary Search",
319+
"uuid": "4a580b6d-a370-4526-a5ea-d7c856279676",
320+
"practices": [],
321+
"prerequisites": [],
322+
"difficulty": 3
323+
},
316324
{
317325
"slug": "pascals-triangle",
318326
"name": "Pascal's Triangle",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Instructions
2+
3+
Implement a binary search algorithm. Do not use any COBOL proprietary
4+
search routines (eg. SEARCH).
5+
6+
Searching a sorted collection is a common task. A dictionary is a sorted
7+
list of word definitions. Given a word, one can find its definition. A
8+
telephone book is a sorted list of people's names, addresses, and
9+
telephone numbers. Knowing someone's name allows one to quickly find
10+
their telephone number and address.
11+
12+
If the list to be searched contains more than a few items (a dozen, say)
13+
a binary search will require far fewer comparisons than a linear search,
14+
but it imposes the requirement that the list be sorted.
15+
16+
In computer science, a binary search or half-interval search algorithm
17+
finds the position of a specified input value (the search "key") within
18+
an array sorted by key value.
19+
20+
In each step, the algorithm compares the search key value with the key
21+
value of the middle element of the array.
22+
23+
If the keys match, then a matching element has been found and its index,
24+
or position, is returned.
25+
26+
Otherwise, if the search key is less than the middle element's key, then
27+
the algorithm repeats its action on the sub-array to the left of the
28+
middle element or, if the search key is greater, on the sub-array to the
29+
right.
30+
31+
If the remaining array to be searched is empty, then the key cannot be
32+
found in the array and a special "not found" indication is returned.
33+
34+
A binary search halves the number of items to check with each iteration,
35+
so locating an item (or determining its absence) takes logarithmic time.
36+
A binary search is a dichotomic divide and conquer search algorithm.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Introduction
2+
3+
Implement a binary search algorithm.
4+
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.
10+
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.
14+
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.
18+
19+
In each step, the algorithm compares the search key value with the key
20+
value of the middle element of the array.
21+
22+
If the keys match, then a matching element has been found and its index,
23+
or position, is returned.
24+
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.
29+
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"authors": [
3+
"kapitaali"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/binary-search.cob"
8+
],
9+
"test": [
10+
"tst/binary-search/binary-search.cut"
11+
],
12+
"example": [
13+
".meta/proof.ci.cob"
14+
],
15+
"invalidator": [
16+
"test.sh",
17+
"test.ps1"
18+
]
19+
},
20+
"blurb": "Implement a binary search algorithm.",
21+
"source": "Wikipedia",
22+
"source_url": "http://en.wikipedia.org/wiki/Binary_search_algorithm"
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
IDENTIFICATION DIVISION.
2+
PROGRAM-ID. BINARY-SEARCH.
3+
AUTHOR. kapitaali.
4+
ENVIRONMENT DIVISION.
5+
DATA DIVISION.
6+
WORKING-STORAGE SECTION.
7+
01 WS-ITEM PIC 9999.
8+
01 WS-RESULT PIC 9999.
9+
01 WS-ERROR PIC X(40) VALUE SPACES.
10+
01 WS-ARRAY PIC X(60).
11+
01 L PIC 99 VALUE 0.
12+
01 R PIC 99.
13+
01 M PIC 99.
14+
01 STR PIC X(60).
15+
01 EXP PIC 999 VALUE ZEROES.
16+
01 IDX PIC 999.
17+
01 NUM2 PIC ZZ9 VALUE ZEROES.
18+
01 COUNTER PIC 999.
19+
01 LEN PIC 999.
20+
21+
01 Teibel.
22+
02 Taulukko PIC 999 OCCURS 1 TO 20
23+
DEPENDING ON IDX.
24+
25+
PROCEDURE DIVISION.
26+
27+
STR-LENGTH.
28+
MOVE 0 TO LEN.
29+
PERFORM VARYING COUNTER FROM FUNCTION LENGTH(STR)
30+
BY -1 UNTIL STR(COUNTER:1) <> " "
31+
ADD 1 TO LEN
32+
END-PERFORM.
33+
COMPUTE LEN = FUNCTION LENGTH(STR) - LEN.
34+
35+
36+
REVERSE-TABLE.
37+
COMPUTE R = FUNCTION INTEGER-PART(IDX / 2).
38+
PERFORM VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER = R
39+
COMPUTE L = IDX - COUNTER
40+
MOVE Taulukko(L) TO NUM2
41+
MOVE Taulukko(COUNTER) TO Taulukko(L)
42+
MOVE NUM2 TO Taulukko(COUNTER)
43+
END-PERFORM.
44+
45+
46+
* populates the table from input string
47+
MOVE-TO-TABLE.
48+
MOVE WS-ARRAY TO STR.
49+
PERFORM STR-LENGTH.
50+
MOVE 20 TO IDX.
51+
INITIALIZE Teibel.
52+
MOVE 3 TO EXP.
53+
MOVE 1 TO IDX
54+
MOVE ZEROES TO NUM2.
55+
PERFORM VARYING COUNTER FROM LEN BY -1 UNTIL COUNTER = 0
56+
IF WS-ARRAY(COUNTER:1) = ','
57+
MOVE 3 TO EXP
58+
MOVE NUM2 TO Taulukko(IDX)
59+
MOVE ZEROES TO NUM2
60+
ADD 1 TO IDX
61+
ELSE IF COUNTER = 1
62+
MOVE WS-ARRAY(COUNTER:1) TO NUM2(EXP:1)
63+
MOVE NUM2 TO Taulukko(IDX)
64+
ADD 1 TO IDX
65+
ELSE
66+
MOVE WS-ARRAY(COUNTER:1) TO NUM2(EXP:1)
67+
SUBTRACT 1 FROM EXP
68+
END-IF
69+
END-PERFORM.
70+
71+
* the actual algorithm
72+
BINARY-SEARCH.
73+
MOVE ZERO TO WS-RESULT.
74+
MOVE SPACES TO WS-ERROR.
75+
PERFORM MOVE-TO-TABLE.
76+
IF IDX > 2
77+
PERFORM REVERSE-TABLE
78+
END-IF.
79+
IF L IS LESS THAN 1 OR R IS GREATER THAN IDX
80+
MOVE "value not in array" TO WS-ERROR
81+
EXIT PROGRAM
82+
END-IF.
83+
MOVE 1 TO L.
84+
COMPUTE R = IDX - 1.
85+
PERFORM UNTIL L IS GREATER THAN R
86+
COMPUTE M = L + R
87+
COMPUTE M = FUNCTION INTEGER-PART(M / 2)
88+
IF Taulukko(M) = WS-ITEM
89+
MOVE M TO WS-RESULT
90+
EXIT PARAGRAPH
91+
END-IF
92+
IF Taulukko(M) IS LESS THAN WS-ITEM
93+
COMPUTE L = M + 1
94+
ELSE IF Taulukko(M) IS GREATER THAN WS-ITEM
95+
COMPUTE R = M - 1
96+
ELSE
97+
COMPUTE WS-RESULT = M - 1
98+
IF Taulukko(WS-RESULT) = WS-ITEM
99+
EXIT PROGRAM
100+
ELSE
101+
MOVE "value not in array" TO WS-ERROR
102+
EXIT PROGRAM
103+
END-IF
104+
END-IF
105+
END-PERFORM.
106+
MOVE "value not in array" TO WS-ERROR.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env bash
2+
3+
# This file is a copy of the
4+
# https://github.com/exercism/configlet/blob/main/scripts/fetch-configlet file.
5+
# Please submit bugfixes/improvements to the above file to ensure that all tracks benefit from the changes.
6+
7+
# set -eo pipefail
8+
9+
readonly LATEST='https://api.github.com/repos/0xE282B0/cobol-check/releases/latest'
10+
11+
case "$(uname)" in
12+
Darwin*) os='mac' ;;
13+
Linux*) os='linux' ;;
14+
Windows*) os='windows' ;;
15+
MINGW*) os='windows' ;;
16+
MSYS_NT-*) os='windows' ;;
17+
*) os='linux' ;;
18+
esac
19+
20+
case "${os}" in
21+
windows*) ext='.exe' ;;
22+
*) ext='' ;;
23+
esac
24+
25+
arch="$(uname -m)"
26+
27+
curlopts=(
28+
--silent
29+
--show-error
30+
--fail
31+
--location
32+
--retry 3
33+
)
34+
35+
if [[ -n "${GITHUB_TOKEN}" ]]; then
36+
curlopts+=(--header "authorization: Bearer ${GITHUB_TOKEN}")
37+
fi
38+
39+
suffix="${os}-${arch}${ext}"
40+
41+
get_download_url() {
42+
curl "${curlopts[@]}" --header 'Accept: application/vnd.github.v3+json' "${LATEST}" |
43+
grep "\"browser_download_url\": \".*/download/.*/cobol-check.*${suffix}\"$" |
44+
cut -d'"' -f4
45+
}
46+
47+
main() {
48+
if [[ -d ./bin ]]; then
49+
output_dir="./bin"
50+
elif [[ $PWD == */bin ]]; then
51+
output_dir="$PWD"
52+
else
53+
echo "Error: no ./bin directory found. This script should be ran from a repo root." >&2
54+
return 1
55+
fi
56+
57+
output_path="${output_dir}/cobolcheck${ext}"
58+
download_url="$(get_download_url)"
59+
curl "${curlopts[@]}" --output "${output_path}" "${download_url}"
60+
chmod +x "${output_path}"
61+
}
62+
63+
main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This file is a copy of the
2+
# https://github.com/exercism/configlet/blob/main/scripts/fetch-configlet.ps1 file.
3+
# Please submit bugfixes/improvements to the above file to ensure that all tracks
4+
# benefit from the changes.
5+
6+
$ErrorActionPreference = "Stop"
7+
$ProgressPreference = "SilentlyContinue"
8+
9+
$requestOpts = @{
10+
Headers = If ($env:GITHUB_TOKEN) { @{ Authorization = "Bearer ${env:GITHUB_TOKEN}" } } Else { @{ } }
11+
MaximumRetryCount = 3
12+
RetryIntervalSec = 1
13+
}
14+
15+
$arch = If ([Environment]::Is64BitOperatingSystem) { "amd64" } Else { "x86" }
16+
$fileName = "cobol-check-windows-$arch.exe"
17+
18+
Function Get-DownloadUrl {
19+
$latestUrl = "https://api.github.com/repos/0xE282B0/cobol-check/releases/latest"
20+
Invoke-RestMethod -Uri $latestUrl -PreserveAuthorizationOnRedirect @requestOpts
21+
| Select-Object -ExpandProperty assets
22+
| Where-Object { $_.browser_download_url -match $FileName }
23+
| Select-Object -ExpandProperty browser_download_url
24+
}
25+
26+
$downloadUrl = Get-DownloadUrl
27+
$outputFile = Join-Path -Path $PSScriptRoot -ChildPath "cobolcheck.exe"
28+
Invoke-WebRequest -Uri $downloadUrl -OutFile $outputFile @requestOpts

0 commit comments

Comments
 (0)