forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ivan
committed
Dec 13, 2018
1 parent
57307f3
commit c192f44
Showing
6 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package ch05_array | ||
|
||
class ArrayDemo(capacity: Int) { | ||
|
||
var data: Array[Char] = new Array[Char](capacity) | ||
var length: Int = 0 | ||
|
||
def find(index: Int): Char = { | ||
if (index < 0 || index > length) { | ||
return 0.toChar | ||
} | ||
data(index) | ||
} | ||
|
||
def insert(index: Int, value: Char): Boolean = { | ||
if (length == capacity) { | ||
return false | ||
} | ||
|
||
if (index < 0 || index >= capacity) { | ||
return false | ||
} | ||
|
||
for (i <- length until index by -1) { | ||
data(i) = data(i - 1) | ||
} | ||
data(index) = value | ||
length += 1 | ||
|
||
true | ||
} | ||
|
||
def delete(index: Int): Char = { | ||
if (length == 0) { | ||
throw new IllegalStateException("array is empty") | ||
} | ||
if (index >= length) { | ||
throw new IllegalStateException("index out of range, current data length is " + length) | ||
} | ||
val result = data(index) | ||
for (i <- index until length-1) { | ||
data(i) = data(i + 1) | ||
} | ||
|
||
length -= 1 | ||
result | ||
} | ||
|
||
def print: String = { | ||
data.subSequence(0, length).toString | ||
} | ||
|
||
} |
4 changes: 3 additions & 1 deletion
4
scala/src/main/scala/11_sorts/Sorts.scala → scala/src/main/scala/ch11_sorts/Sorts.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
...n/scala/15_bsearch/BSearchRecursive.scala → ...scala/ch15_bsearch/BSearchRecursive.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
package ch15_bsearch | ||
|
||
object BSearchRecursive { | ||
def search(nums: Array[Int], target: Int): Int = { | ||
return searchInternal(nums, target, 0, nums.length - 1) | ||
} | ||
|
||
def searchInternal(nums:Array[Int], target: Int, low: Int, high: Int): Int = { | ||
if(low <= high){ | ||
val mid = low + ((high - low) >> 2) | ||
if(nums(mid) > target){ | ||
searchInternal(nums, target, low, mid - 1) | ||
} else if (nums(mid) < target){ | ||
} else if (nums(mid) < target){ | ||
searchInternal(nums, target, mid + 1, high) | ||
} else { | ||
return mid | ||
} | ||
}else{ | ||
return -1 | ||
return -1 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package ch05_array | ||
|
||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class ArrayDemoSpec extends FlatSpec with Matchers { | ||
|
||
behavior of "ArrayDemoTest" | ||
|
||
it should "find value after insert" in { | ||
val demo = new ArrayDemo(10) | ||
assert(demo.insert(0, 'a')) | ||
assert(demo.insert(1, 'b')) | ||
assert(demo.insert(2, 'c')) | ||
assert(demo.insert(3, 'd')) | ||
assert(demo.insert(4, 'e')) | ||
demo.print should equal("abcde") | ||
|
||
assert(demo.insert(2, 'x')) | ||
demo.print should equal("abxcde") | ||
} | ||
|
||
it should "not insert value if capacity is full " in { | ||
val demo = new ArrayDemo(10) | ||
for (i <- Range(0, 10)) { | ||
demo.insert(i, (i + 97).toChar) | ||
} | ||
|
||
assert(!demo.insert(1, 'a')) | ||
} | ||
|
||
it should "not insert if index is negative" in { | ||
val demo = new ArrayDemo(10) | ||
assert(!demo.insert(-1, 'a')) | ||
} | ||
|
||
it should "not insert if index is exceed capacity" in { | ||
val demo = new ArrayDemo(10) | ||
assert(!demo.insert(10, 'a')) | ||
assert(!demo.insert(11, 'a')) | ||
} | ||
|
||
it should "not find after delete" in { | ||
val demo = new ArrayDemo(10) | ||
assert(demo.insert(0, 'a')) | ||
assert(demo.insert(1, 'b')) | ||
assert(demo.insert(2, 'c')) | ||
assert(demo.insert(3, 'd')) | ||
assert(demo.insert(4, 'e')) | ||
demo.print should equal("abcde") | ||
|
||
|
||
assert(demo.insert(2, 'x')) | ||
demo.print should equal("abxcde") | ||
|
||
demo.delete(2) should equal('x') | ||
demo.find(2) should not equal ('x') | ||
demo.print should equal("abcde") | ||
} | ||
|
||
it should "not delete for empty array" in { | ||
val demo = new ArrayDemo(10) | ||
assertThrows[IllegalStateException] { | ||
demo.delete(2) should equal('x') | ||
} | ||
} | ||
|
||
} |