Skip to content

Instantly share code, notes, and snippets.

@wingsum93
Last active May 8, 2019 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wingsum93/91d558b605e01d0ce014f91c3f1d82b7 to your computer and use it in GitHub Desktop.
Save wingsum93/91d558b605e01d0ce014f91c3f1d82b7 to your computer and use it in GitHub Desktop.
import org.junit.Test
class CharSubSetTest {
@Test
fun xxx() {
val case1 = isSubset(arrayOf('A', 'B', 'C', 'D', 'E'), arrayOf('A', 'D', 'E'))
val case2 = isSubset(arrayOf('A', 'B', 'C', 'D', 'E'), arrayOf('A', 'D', 'Z'))
val case3 = isSubset(arrayOf('A', 'D', 'E'), arrayOf('A', 'A', 'D', 'E'))
val case4 = isSubset(arrayOf('A', 'D', 'E'), arrayOf('A', 'A', 'A'))
println("case 1 - $case1" )
println("case 2 - $case2" )
println("case 3 - $case3" )
println("case 4 - $case4" )
}
fun isSubset(firstArray: Array<Char>, secondArray: Array<Char>): Boolean {
// make a set for 1st and 2nd array
val firstSet = firstArray.toSet()
val secondSet = secondArray.toSet()
//check first set has all second set's char
for (char: Char in secondSet) {
// start check
if (!firstSet.contains(char)) {
return false
}
}
return true
}
}

Computational complexity in question 1 - O(n)

Explanation

Assume n is the maximum of number of size of two arrays.

  1. The complexity to transform two array to two hashset -> O(n) * 2
  • val firstSet = firstArray.toSet()
  1. The complexity to loop the second set -> O(log n) * The complexity to check whether a char is exist in first set -> O(1)
  • The size of the sets may smaller than original one, since it may have duplicate character.
  • Each call for Set().contain(Int) cost O(1) time.

Combining all complexity. The complexity of function isSubset is

  • 2 * O(n) + O(log n) -> O(n)
package com.ericho.itunes_music
import org.junit.Test
import kotlin.math.pow
class FibonacciTest {
@Test
fun xxx() {
val testInput = arrayOf(1, 22, 9, 12, 14, 376, 378, 317812, 317811, 317810)
.toIntArray()
nextFibonacci(testInput)
}
fun nextFibonacci(input: IntArray) {
val max = input.max()!!
val fibonacciList = buildSequence(max).toList()
println("Output:")
for (value: Int in input) {
// find the next fibonacci value for each number in array
var a = 0
for ((index, fibonacciValue) in fibonacciList.withIndex()) {
val isNextFibonacciNumber = value < fibonacciValue
if (isNextFibonacciNumber) {
a = index
break
}
}
println(fibonacciList[a])
}
}
private fun buildSequence(number: Int): Sequence<Int> {
return sequence {
var terms = Pair(1, 1)
while (true) {
yield(terms.first)
terms = Pair(terms.second, terms.first + terms.second)
if (terms.first > number) {
yield(terms.first)
break
}
}
}
}
}
/**
原本的Function 會出N 個一樣的function ,each function return same output for same input.
To solve the issue, we should change the var to let.
Then, we would fix the issue. Have a array of functions then return difference result for single input.
*/
function createArrayOfFunctions(y) {
var arr = [];
for(let i = 0; i<y; i++) {
arr[i] = function(x) { return x + i; }
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment