Pavlo Liapota
06/04/2018, 11:11 AMenum class Direction {
NONE,
NORTH,
SOUTH,
EAST,
WEST,
START,
END;
companion object {
private val oppositMap = mapOf(
SOUTH to NORTH,
NORTH to SOUTH
// ...
)
}
val opposit get() = oppositMap.getValue(this)
}zucen.co
06/04/2018, 11:26 AMwhen solution keeps completnes of enum options in mind.. that is why I would prefer it.Pavlo Liapota
06/04/2018, 11:34 AMprivate val oppositMap = mapOf(
SOUTH to NORTH,
NORTH to SOUTH,
// ...
START to NONE,
NONE to NONE,
)
no null are used, property opposit returns Direction
In both solutions lookup is performed every time you access property.
In my example it is O(1) because of HashMap. And in your solution complexity of when is O(N) as it goes through all options every time in worst case.
But it does not matter for such small numbers 🙂diesieben07
06/04/2018, 11:42 AMwhen on enum compiles to an O(1) lookup.arekolek
06/04/2018, 11:42 AMAnd in your solution complexity ofSinceiswhenas it goes through all options every time in worst case.O(N)
N is fixed at 7, going through all options is O(1) even if you do it sequentially. O(7) = O(1)
But it does not matter for such small numbersEven if there were a 1000000 options, but always exactly 1000000, it still would be
O(1).Pavlo Liapota
06/04/2018, 11:45 AMdidn’t know that, thanks!on enum compiles to anwhenlookup.O(1)
edwardwongtl
06/04/2018, 12:44 PMwhen with enum lolPavlo Liapota
06/04/2018, 1:02 PMwhen is better 🙂Rasmus Franke
06/04/2018, 2:02 PMwhen is when someone adds a new Direction value, with when you'll get a nice compile time error if you forget to handle the opposite of it, which you won't get with a mapRasmus Franke
06/04/2018, 2:15 PMwhen is o(1) just because the number of options is not dynamic at runtime?diesieben07
06/04/2018, 2:18 PMRasmus Franke
06/04/2018, 2:19 PMRasmus Franke
06/04/2018, 2:20 PMdiesieben07
06/04/2018, 2:20 PMedwardwongtl
06/04/2018, 2:21 PMwhen on enum is still O(1) at runtime, the compiler can optimize itRasmus Franke
06/04/2018, 2:22 PMRasmus Franke
06/04/2018, 2:22 PMdiesieben07
06/04/2018, 2:23 PMdiesieben07
06/04/2018, 2:23 PMedwardwongtl
06/04/2018, 2:24 PMif-else if chain will not be O(1) mandiesieben07
06/04/2018, 2:24 PMedwardwongtl
06/04/2018, 2:24 PMdiesieben07
06/04/2018, 2:24 PMdiesieben07
06/04/2018, 2:24 PMdiesieben07
06/04/2018, 2:25 PMedwardwongtl
06/04/2018, 2:26 PMif checks one by onearekolek
06/04/2018, 2:26 PMdiesieben07
06/04/2018, 2:26 PMdiesieben07
06/04/2018, 2:26 PMarekolek
06/04/2018, 2:29 PMany is O(n) but listOf(1,2,3,4,5,6, ..., 100000000000000000).any { it > 1000000000000000 } is O(1)arekolek
06/04/2018, 2:30 PMarekolek
06/04/2018, 2:30 PMwhen was O(n), the code that used it would be O(1) because the number of branches was fixed in advanceRasmus Franke
06/04/2018, 2:37 PMRasmus Franke
06/04/2018, 2:37 PMarekolek
06/04/2018, 2:37 PMwhen, then n is the number of branchesdiesieben07
06/04/2018, 2:38 PMwhen.Rasmus Franke
06/04/2018, 2:38 PMwhen as o(n)?arekolek
06/04/2018, 2:39 PMwhen doesn't require an "input parameter"arekolek
06/04/2018, 2:39 PMRasmus Franke
06/04/2018, 2:41 PMn to make any sense of complexity, and both o(1) and o(n) can be correct here depending on you refer to the constant argument we put into when or if you refer to the number of branchesarekolek
06/04/2018, 2:41 PMwhen (x) {
1 ->
2 ->
3 ->
}
Then it doesn't have any input, so there's no n, and the time complexity of this snippet is O(1)diesieben07
06/04/2018, 2:42 PMx is the input.Andreas Sinz
06/04/2018, 2:42 PMlistOf(1, 2, ....).any { it > 10000} O(1)?diesieben07
06/04/2018, 2:42 PMRasmus Franke
06/04/2018, 2:43 PMany shouldn't be O(1) there, only the list creationRasmus Franke
06/04/2018, 2:43 PMRasmus Franke
06/04/2018, 2:44 PMbissell
06/04/2018, 2:45 PMarekolek
06/04/2018, 2:47 PMfun myAny() = listOf(1,2,3,4,5,6, ..., 100000000000000000).any { it > 10000 }
fun test1(n: Int) {
val x = myAny()
val list = mutableListOf<Boolean>()
repeat(n) {
list += x
}
return list
}
fun test2() = test1(123456)
* myAny is O(1)
* test1 is O(n)
* test2 is O(1)arekolek
06/04/2018, 2:49 PMtest1 uses any which itself is O(n) (where n is the length of the list it works on), what test1 passes to any is independent of the test1 inputRasmus Franke
06/04/2018, 2:49 PMRasmus Franke
06/04/2018, 2:54 PMO when n doesn't exist?Rasmus Franke
06/04/2018, 2:54 PMO(1) because what would it even scale witharekolek
06/04/2018, 2:55 PMO(1) I think, always finishes in the same, predetermined number of stepsarekolek
06/04/2018, 2:55 PM