Skip to content

Commit b438c01

Browse files
authored
Merge pull request #1 from kennyledet/master
Pulling the changes
2 parents 5820378 + b227c47 commit b438c01

8 files changed

Lines changed: 159 additions & 2 deletions

File tree

Boyer_Moore_Horspool/C#/Davipb/Horspool.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static int Find(string haystack, string needle)
3232

3333
int index = 0;
3434

35-
while (index <= haystack.Length - needle.Length)
35+
while (index < haystack.Length - needle.Length)
3636
{
3737
bool match = true;
3838

@@ -42,6 +42,10 @@ public static int Find(string haystack, string needle)
4242
{
4343
match = false;
4444
index += BadMatchTable[haystack[index + needle.Length - 1]];
45+
if (index >= haystack.Length)
46+
{
47+
break;
48+
}
4549
}
4650
}
4751

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// MaxSubarrayBrute.swift
3+
//
4+
// Created by Greg Price on 4/1/18.
5+
//
6+
7+
import Foundation
8+
9+
func maxSubarrayBrute(ints: [Int]) -> [Int] {
10+
var m = 0
11+
var startIndex = 0
12+
var endIndex = 0
13+
for i in 0..<ints.count {
14+
var sum = 0
15+
for j in i..<ints.count {
16+
sum += ints[j]
17+
if sum > m {
18+
m = sum
19+
startIndex = i
20+
endIndex = j
21+
}
22+
}
23+
}
24+
return Array(ints[startIndex...endIndex])
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// MaxSubarrayBruteTest.swift
3+
// PracticePackageDescription
4+
//
5+
// Created by Greg Price on 4/1/18.
6+
//
7+
8+
import XCTest
9+
10+
class MaxSubarrayBruteTest: XCTestCase {
11+
func test() {
12+
var vals = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
13+
vals = maxSubarrayBrute(ints: vals)
14+
assert(vals.elementsEqual([4, -1 ,2, 1]))
15+
}
16+
}

Quick_Sort/Swift/QuickSort.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// QuickSort.swift
3+
// Practice
4+
//
5+
// Created by Greg Price on 3/31/18.
6+
//
7+
8+
import Foundation
9+
10+
public func quick(_ ints: inout [Int]) {
11+
divide(&ints, thisStart: 0, thisEnd: ints.count - 1)
12+
}
13+
14+
fileprivate func divide(_ ints: inout [Int], thisStart: Int, thisEnd: Int) {
15+
if thisStart < thisEnd {
16+
let p = partition(&ints, start: thisStart, p: thisEnd)
17+
divide(&ints, thisStart: thisStart, thisEnd: p - 1)
18+
divide(&ints, thisStart: p + 1, thisEnd: thisEnd)
19+
}
20+
}
21+
22+
fileprivate func partition(_ ints: inout [Int], start: Int, p: Int) -> Int {
23+
var leftPileFrontIdx = start - 1
24+
for i in start..<p {
25+
if ints[i] <= ints[p] {
26+
leftPileFrontIdx += 1
27+
swap(&ints, idx1: leftPileFrontIdx, idx2: i)
28+
}
29+
}
30+
swap(&ints, idx1: leftPileFrontIdx + 1, idx2: p)
31+
return leftPileFrontIdx + 1
32+
}
33+
34+
fileprivate func swap( _ ints: inout [Int], idx1: Int, idx2: Int) {
35+
let t = ints[idx2]
36+
ints[idx2] = ints[idx1]
37+
ints[idx1] = t
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// QuickSortTest.swift
3+
// Practice
4+
//
5+
// Created by Greg Price on 3/31/18.
6+
//
7+
8+
import Foundation
9+
10+
class QuickSortTest: XCTestCase {
11+
func quickSortTest() {
12+
var vals = [43, 44, 2, 98, 28]
13+
quick(&vals)
14+
assert(vals.elementsEqual([2, 28, 43, 44, 98]))
15+
}
16+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Algorithm Implementations
1+
# Algorithm Implementations
22

33
[![Join the chat at https://gitter.im/kennyledet/Algorithm-Implementations](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/kennyledet/Algorithm-Implementations?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
44

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
;; ROT13 ciphering algorithm implementation
2+
;; Based on ../../Lua/Yonaba/rot13.lua
3+
;; See: http://en.wikipedia.org/wiki/ROT13
4+
5+
;; Returns the ASCII bytecode of either 'a' or 'A'
6+
(local a_byte (string.byte :a))
7+
(local A_byte (string.byte :A))
8+
(local ascii_base (fn [ch]
9+
(if (= (string.lower ch) ch) a_byte A_byte)))
10+
11+
(local caesar_cipher_ch (fn [key] (fn [ch]
12+
(let [base (ascii_base ch)
13+
offset (% (+ (- (string.byte ch) base) key) 26)]
14+
(string.char (+ offset base))))))
15+
16+
;; ROT13 is based on Caesar ciphering algorithm, using 13 as a key
17+
(local caesar_cipher (fn [key str]
18+
(string.gsub str "%a" (caesar_cipher_ch key))))
19+
20+
{
21+
:cipher (partial caesar_cipher 13)
22+
23+
:decipher (partial caesar_cipher -13)
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; Tests for rot13.lua
2+
(table.insert (or package.searchers package.loaders) (. (require :fennel) :searcher))
3+
(local rot13 (require :rot13))
4+
5+
(var (total pass) (values 0 0))
6+
7+
(local dec (fn [str len]
8+
(local strlen (# str))
9+
(if (< strlen len)
10+
(.. str (: '.' :rep (- len strlen)))
11+
(: str :sub 1 len))))
12+
13+
(local run (fn [msg f]
14+
(set total (+ 1 total))
15+
(local (ok err) (pcall f))
16+
(when ok (set pass (+ 1 pass)))
17+
(local status (if ok "PASSED" "FAILED"))
18+
(print (: "%02d. %68s: %s" :format total (dec msg 68) status))))
19+
20+
(run "Ciphering test" (fn []
21+
(assert (= (rot13.cipher "abcd") "nopq"))
22+
(assert (= (rot13.cipher "WXYZ") "JKLM"))
23+
(assert (= (rot13.cipher "abcdefghijklmnopqrstuvwxyz") "nopqrstuvwxyzabcdefghijklm"))
24+
(assert (= (rot13.cipher "ABCDEFGHIJKLMNOPQRSTUVWXYZ") "NOPQRSTUVWXYZABCDEFGHIJKLM"))))
25+
26+
(run "Deciphering test" (fn []
27+
(assert (= (rot13.decipher "nopq") "abcd"))
28+
(assert (= (rot13.cipher "WXYZ") "JKLM"))
29+
(assert (= (rot13.cipher "nopqrstuvwxyzabcdefghijklm") "abcdefghijklmnopqrstuvwxyz"))
30+
(assert (= (rot13.cipher "NOPQRSTUVWXYZABCDEFGHIJKLM") "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))))
31+
32+
(print (: "-" :rep 80))
33+
(print (: "Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%" :format
34+
total pass (- total pass) (/ (* pass 100) total)))

0 commit comments

Comments
 (0)