Calculating Your Level of Naughtiness
To motivate his students, a computer science teacher of the Federal University of Ceará has challenged his class to develop an algorithm to calculate how naughty you are based on your birth date. The challenge is very silly, but it became a Brazilian hit.
The teacher said his original intention was to teach students to call functions from other functions, which is as much silly as the goal of the challenge, but no doubt that the idea is pretty effective on motivating young students.
The problem consists on writing a function that calculates the percentage of naughtiness and the remaining level of innocence of a person based on his/er birth date. The formula to calculate the level of naughtiness is:
naughtiness = incremental_sum(month) + (year / 100) * (50 - day)
where incremental_sum
is a function that, given a number, calculates de sum of all numbers from 1 to the informed number included. The solution below is written in Clojure:
(defn inc-sum [num]
(reduce + (range (inc num))))
(defn naughtness [day month year]
(let [naughty (+ (* (- 50 day) (/ year 100.0)) (inc-sum month))
angel (- 100 naughty)]
{:naughty naughty
:angel angel}))
(naughtness 10 9 78)
=> {:naughty 76.2, :angel 23.8}
The formula has absolutely no sense and doesn’t have any scientific foundation, but the result of the function is great fun to play with friends! Maybe the subject can push you to learn Clojure, doesn’t it?! 😉
Recent Posts
Can We Trust Marathon Pacers?
Introducing LibRunner
Clojure Books in the Toronto Public Library

Once Upon a Time in Russia

FHIR: A Standard For Healthcare Data Interoperability

First Release of CSVSource

Astonishing Carl Sagan's Predictions Published in 1995

Making a Configurable Go App

Dealing With Pressure Outside of the Workplace

Reacting to File Changes Using the Observer Design Pattern in Go

Provisioning Azure Functions Using Terraform

Taking Advantage of the Adapter Design Pattern

Applying The Adapter Design Pattern To Decouple Libraries From Go Apps

Using Goroutines to Search Prices in Parallel

Applying the Strategy Pattern to Get Prices from Different Sources in Go
