An overengineered arrow-kt solution where I never check explicitly for null.
Probably uglier than needed, but fun.
importarrow.core.Optionimportarrow.core.getOptionimportarrow.core.getOrElseimportarrow.core.toOptionconstvalerrorMessage="Impossible to decode"constvalasciiValOfA='a'.toInt()constvalalphabet="abcdefghijklmnopqrstuvwxyz"funChar.decode(n:Int)=(((toInt()-asciiValOfA)*n)%26+asciiValOfA).toChar()funMatchResult.stringValueGroup(n:Int)=groups[n].toOption().map{it.value}funMatchResult.intValueGroup(n:Int)=groups[n].toOption().map{it.value.toInt()}funString.decode()="""(\d+)([a-z]+)""".toRegex().find(this).toOption().flatMap{mr->mr.stringValueGroup(2).flatMap{s->mr.intValueGroup(1).flatMap{generateAlphaMap(it).flatMap{rmap->s.fold(Option.just(""),convertingStringFolder(it,rmap))}}}}.getOrElse{errorMessage}privatefungenerateAlphaMap(num:Int)=alphabet.map{c->c.decode(num)toc}.toMap().let{alphamap->when(alphamap.size){26->Option.just(alphamap.map{(k,v)->vtok}.toMap())else->Option.empty()}}privatefunconvertingStringFolder(num:Int,rmap:Map<Char,Char>)={acc:Option<String>,inputChar:Char->acc.flatMap{soFar->rmap.getOption(inputChar).map{c->"$soFar${c.decode(num)}"}}}funmain(){println("6015ekx".decode())println("5057aan".decode())}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
An overengineered arrow-kt solution where I never check explicitly for null.
Probably uglier than needed, but fun.