Implement an algorithm which increases number
and returns the result. Start from number
and increment by step
as many times as iterations
demands.
Input: number
, iterations
, step
.
Example:
alg(2, 5, 10) => 2 + 10 + 10 + 10 + 10 + 10 = 52
alg(17, 3, 6) => 17 + 6 + 6 + 6 = 35
Tests:
alg(100, 5, 50)
alg(14, 20, 4)
Good luck!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (17)
Clojure - Level 2 🌟🌟
Compared to your "level 1", this is more like the question asked, since it asks for an algorithm rather than the answer. (I totally agree that it should be
(+ number (* step iterations))
).In Clojure it's usually better to preference core functions, followed by
reduce
, followed byloop
/recur
.The clojure.core function approach is just:
But that's really using
iterate
to do the algorithm part. Soreduce
might be more in the spirit of things. It's shorter too 🙂Awesome breakdown! Thanks Paula. 🙏🏿
I feel like you're right on the hierarchy of abstraction to use for Clojure functions (there's just so many, and many of them are composed)._
I could've just used pure-recursion without
loop/recur
but I don't get the power of the AST's tail-end optimizer for loops. Thank you for reminding me of the thread-macro.Thought I'd give this a try using plain lambda calculus. The notation is:
λx.t
= Function taking x as an argument and producing the expression t. Can simplifyλx.(λy.t)
->λx y.t
MN
= Apply N to MMNO
=(MN)O
Example 1:
In JS:
smart
are we suppose to figure out the pattern and the answer to the algorithm? Or are we suppose to put it in some kind of computer language? i am a beginner... i know the answers but i am not sure how we do this challenge... could someone teach me?
yo Christine ✌🏿
the keyword here is algorithm, so technically a psudo-code explanation would suffice.
The majority of these Daily Challenges expect an answer in a programming language, with tests.
oh i am a complete beginner all i know is html and css and i don't even know if they can be considered a language... but i was listening to a podcast on what algorithms were and she said that even a recipe is an algorithm so isn't an algorithm like a pattern, like in math? like maybe if the pattern is this then the answer is that?
i was just doing it in my head but i guess doing it in a language would be much more difficult but now i understand that you are looking for a way to make it simpler and more efficient right? i think i am still a little confused on this i will have to get more of an intro to algorithms before i try to crack any codes... thanks for being nice to the newbie!
Christine can i ask what an iteration is?
I may be in the minority, but I'd consider CSS + HTML at least non-turing complete languages.👍🏿
...though it'd behoove you to graduate to a turing complete language like Ruby or JavaScript.
It's very exciting to see a beginner grappling with these concepts and potentially create fresh examples and metaphors.
If nothing else, a well-worded recipe for getting the sum is valid. ✌🏿
C
Clojure - Level 1 🌟
In Python:
Another 'short'-cut:
Applying function
in ruby
Go
here's the solution in python :