Skip to content

Instantly share code, notes, and snippets.

View cbruegg's full-sized avatar

Christian Brüggemann cbruegg

View GitHub Profile
public static final double EPSILON = 0.00001;
public static double nullstelle2(double ug, double og) {
while (og - ug >= EPSILON) {
double ugErsteHaelfte = ug;
double ogErsteHaelfte = ug + (og - ug) / 2;
double ugZweiteHaelfte = ogErsteHaelfte;
double ogZweiteHaelfte = og;
if (Math.sin(ugErsteHaelfte) * Math.sin(ogErsteHaelfte) <= 0) {
val numberOfBSTs = hashMapOf(
0 to 1.0,
1 to 1.0,
2 to 2.0
)
fun numberOfBSTs(nodes: Int): Double =
when (nodes) {
in numberOfBSTs -> numberOfBSTs[nodes]!!
else -> ((1..nodes)
@cbruegg
cbruegg / KnuthMorrisPratt.kt
Last active October 27, 2016 19:53
An implementation of the Knuth-Morris-Pratt String matching algorithm in Kotlin.
/**
* For each i in [0, length), this function computes
* the length of the longest suffix of a substring of pattern from 0 to i
* that is also a prefix of the pattern itself.
*/
private fun computePrefixFunction(pattern: CharSequence): IntArray {
val resultTable = IntArray(pattern.length)
var matches = 0
for (i in 1..pattern.length - 1) {
# Config
learningRate <- 0.000001 # alpha
maxIterations <- 10000000 # abort after n iterations
maxThetaDifference <- 0.000001 # assume t in theta is close enough if diff after iter < than this
data <- read.csv("data1.tsv", sep = "\t", header = FALSE)
hypothesis <- function(theta, x){
return(theta[1] + theta[2] * x)
}
dependencies {
// Data Binding
kapt 'com.android.databinding:compiler:2.1.2'
}

Using Kotlin in Android projects

What is Kotlin?

Kotlin is a programming language backed by JetBrains, the company that develops IntelliJ IDEA, which is the backend of Android Studio. After five years of development, version 1.0 was released in February, 2016. Kotlin compiles to JVM bytecode that is fully compatible with the Android platform. The developers even pay special attention to it by providing libraries (for example Anko), tooling and more.

It is a language designed for interoperability with Java code, which allows for a gradual migration from Java to Kotlin. The tooling is great and aids usability much. Additionally, picking up the language is very easy if you already know Java, since the syntax and concepts are similar, while still providing a lot of value for developers held back by the bad Java support on Android. Most developers are currently limited to Java 7 or even Java 6 and it will take a long time before we'll be able to properly use features like lambda expressions to make code mo

package a
val pattern = "aba"
val text = "ababa".repeat(100000)
val runs = 1000
fun occTest(): Long {
// Warmup
repeat(10) {
text.occurrencesOf(pattern, ignoreCase = false, matchOverlapping = true).forEach { }
val pattern = "abababababa"
val text = "abababa".repeat(1000)
val runs = 50
fun occTest(): Long {
// Warmup
repeat(5) {
text.occurrencesOf(pattern, ignoreCase = false, matchOverlapping = true).forEach { }
}
library(igraph)
gamma = 0.7
edge_list = as.matrix(read.csv('ueb06-daten.csv'))
graph = graph_from_edgelist(edge_list, directed = FALSE)
print("Loaded graph.")
find_clique_component <- function() {
components = decompose.graph(graph)
class A()
class B<T>()
fun <T> A.f(b: B<T>) {
val eq = this == b // Error: Operator '==' cannot be applied to 'A' and 'B<T>'
}
class A2<T>()
class B2<T>()