Hackerrank – Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
fje volanie funkcie sxako je v zadaní- funkcia
areavypočíta plochu kruhu (![Rendered by QuickLaTeX.com \[\Pi r^2\]](http://pidanic.com/wp-content/ql-cache/quicklatex.com-c226cc82574829db51696b084b530d62_l3.png)
) so stredom
0a polomerom s hodnotoufunction(x) - funkcia
summationsa zavolá raz pre funkciufa raz pre funkciuarea
Vytvoril som riešenie v týchto programovacích jazykoch:
Všetky riešenia sú dostupné aj na mojom GitHub profile.
Scala
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
def f(coefficients: List[Int], powers:List[Int], x: Double): Double = { coefficients.indices.map(i => coefficients(i) * Math.pow(x, powers(i))).sum } def area(coefficients: List[Int], powers: List[Int], x: Double): Double = { scala.math.Pi * scala.math.pow(f(coefficients, powers, x), 2) } def summation(func: (List[Int], List[Int], Double) => Double, upperLimit: Int, lowerLimit: Int, coefficients: List[Int], powers: List[Int]): Double = { val range = lowerLimit.toDouble to upperLimit.toDouble by 0.001 range.map(func(coefficients, powers, _)).sum * 0.001 } |