Posts

Showing posts from August 19, 2018

Knuth Morris Pratt substring search algorithm Implementation in Kotlin

Image
Clash Royale CLAN TAG #URR8PPP .everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0; up vote 1 down vote favorite Below you can find a Kotlin based implementation of the Knuth Morris Pratt substring search algorithm. Here is the corresponding Wikipedia article. class KnutMorrisPrat(R: Int, private val pat: String) private val m = pat.length private val dfa: Array<Array<Int>> = Array(R) Array(m) 0 constructor(pat: CharArray) : this(256, String(pat)) constructor(pat: String) : this(256, pat) init dfa[pat[0].toInt()][0] = 1 var x = 0 var j = 1 while(j < m) (0 until R).forEach c -> dfa[c][j] = dfa[c][x] // Copy mismatch cases. dfa[pat[j].toInt()][j] = j + 1 // Set match case. x = dfa[pat[j].toInt()][x] // Update restart state. j++ // Move to next character in pattern fun search(txt: String): Int return doSearch(txt, 0) fun search(txt: String, start: Int): Int return doSearch(txt, start) private fun

Simple console dice game, trying to roll two of the same number

Image
Clash Royale CLAN TAG #URR8PPP .everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0; up vote 1 down vote favorite I'm a young and beginner Java programmer and I'm starting to learn again Java (used it in the past years at school) since I want to enter a competition and (hope to) win for getting a job. I wanted to test out what I have learned and start to think about coding in a more object oriented way (coming from C). So I created a simple dice game, wrapping all concepts I have learned (but not applied yet) like abstraction, static methods, encapsulation, interfaces etc.. The game asks you to enter your name and play with him, rolling dice. You roll dice three times and if you get 2 or more same numbers you win 1 coin. Pretty simple but I'm still confused about many concepts, it can be a problem if I want to "upgrade" or "scale" the game adding more complexity. I don't know how to work better wit