6 kyu
A disguised sequence (I)
134 of 4,886g964
Loading description...
Algorithms
Mathematics
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
proper assertion messages for C
C#: method name should be
PascalCase
(Please refer to implementation of backward compatibility here )Awesome!
JS chai && assert should be used
Ruby 3.0 should be enabled
Ruby 3.0 enabled in this fork + KaTeX in description
Using Swift test are passed but on Attempt I get Code: 132 error with no logs at all. Quite simple code
The kata seems to work well in Swift, but it's true that Swift testing framework is notoriously bad with handling logs in crashed tests. It's common for Swift kata that when solution crashes, printing inputs does not always work, you get no logs at all, and debugging is seriously hindered.
Itried your code, it passes.
Yes, my final code works fine. I had to guess the disguised sequence. The algorithmic solution does not work well on large (n greater then around 32).
I always run out of time, but I can't find another algorithm. Too bad!
clever!
Updated cobol translation kumited.
Approved.
Solution should be imported explicitly in Python.
Done.
COBOL translation kumited.
Approved.
This comment has been hidden.
It's a good exercise to proove that the guessed sequence is the good one ;)
please do a better job of explaining the sequence or send a link to someone that does.
un means "the nth element of the sequence u".
u0 is the 0th element. u1 is the first.
If un is the nth element, then un-1 is the element before that one, and un-2 is the one before that.
Similarly, un+1 is the element that comes next after un.
For this kata, the equation 6unun+1 - 5unun+2 + un+1un+2 tells you the relationship between three consecutive elements of the list.
For example, since we have u0 and u1, we can use them to find u2 using that equation by using n = 0:
6u0u1 - 5u0u2 + u1u2 = 0
6(1)(2) - 5(1)u2 + 2u2 = 0
12 - 5u2 + 2u2 = 0
From here, you can solve the algebraic equation to find u2. As the kata description says, you can either write a method to solve for the one unknown given the previous two elements of the series, or you can figure out what sequence that equation represents.
Explaining the sequence is your job ;)
Please mark the question with the mathematics tag when it's clearly a math problem. I hate math and filter them out. It's annoying that every other kata is still math, despite not tagged as such.
"ALGORITHMS SEQUENCES ARRAYS" is clear enough they don't go without maths. Sorry for you but commputers and codes are based on maths. Moreover there is no possibility to filter out math; if you think it is possible please let me know.
Edit: nevertheless I tried to change the tags by re-editing but, when you publish, the tags remain the same: "ALGORITHMS SEQUENCES ARRAYS".
Coding without maths is like cooking without water. You'll certainly not go far.
A solid math/coding exercise. Very satisfying.
Thanks!
This comment has been hidden.
Please read this, and don't forget marking your post as having spoiler content when it does.
Im so careless that I didnt notice the 'Un' is start from 0 but not 1.So if I change the 'if' part,the problem solved. :)
And this algorithm seem not fast at all.
Well 't was good for me not by math, but BigInteger)
Every goddamn time you get me. Whyyyyyyyyyyyyyyyyyyyyy
Nice kata. Gave me a chance to resurrect some maths :).
This comment has been hidden.
I would love for some hints about the mathematical part. I was also able to guess the closed form for the sequence and can prove it using mathematical induction, but I have no idea how I would have done that if I hadn't seen the pattern. This is a nonlinear recurrence relation, right? Is there a trick to analyze it?
This comment has been hidden.
The initial solution in Lua has
kata
instead ofsolution
and doesn't havereturn
.Forgot the
return
... Fixed though - curiously - it works withkata
as well as withsolution
. Do you have an explanation? Thanks!Actually, it makes sense because it returns an object and nobody cares how it's called locally and
'solution'
inrequire
is just a file name. At least it looks like this; all my Lua experience is just about 15 solved katas.Seems right, thanks.
NASM translation submitted.
Done!
This comment has been hidden.
This comment has been hidden.
I just tried a few solutions and they worked fine. When you post code format it correctly: 3 backticks then your code then 3 backticks (google "markdown").
This comment has been hidden.
You can look: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
youre a hero. hope ya dont mind me following you. haha
The solution above uses Int32 everywhere, so there's probably an overflow happening at some point.
Honestly, Haven't the slightest clue as to what the sequence is.....
Yeah same. I would have figured it out if the test cases had smaller numbers but that's on me.
It's math... Maybe this can help... See example applied to Fibonacci sequence.
This comment has been hidden.
I pass "Run Sample Tests", but when I press "Attempt". This message "Process exited prematurely with a SIGILL signal." keep appearring. What is wrong?
I submitted a version (Coffeescript) that I knew to be incorrect, but still passed all tests. There should be a test ensuring that fcn(1) returns 2, for those of us that hack out the long winded version. :D
Done, thanks!
Objective-C Translation Kumited - please accept :D
'alr approved some time ago'
This comment has been hidden.
No need to "intuit": a sheet of paper and some math. You have a good solution too!
This comment has been hidden.
This comment has been hidden.
I think it's not too good to assume
int
is 64 bit. (Although it's unlikely tha someone would want to test their solution in Nim locally on their 32-bit system :)Thanks, I know that but I supposed that int was enough.
This comment has been hidden.
34 guys had passed the Rust kata without any problem. Something has changed in the CW CLI runner, I think. I modified the name. Hope that now all is well. Thanks for your post!
当时写的时候想到的是用递归,最后运行起来超时,怎么优化都是超时,然后就看了下别人写的竟然是2的n次方这样写,虽然的确是这个规律,但是如果n0和n1,不是1和2,那就规律不对,去用数学的方法来做这个点题目,感觉不通用,因为可能这个没有这规律,却花时间去想数学规律,有点不靠谱。
This shouldnt be worth 6 kyu. An algorithmic approach is extreme overkill. Work smarter, not harder. Once you do some basic math you quickly spot the pattern and realize... there is a library for this! and literally write 1 line of code.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
The Ocaml starting code is wrong and misleading.
First is that
int
in Ocaml is not automatically promoted to a bignum. Bignums are in theNum
module.Second is that the user written function is required to output a string. Why is this conversion not done in the testing code?
It's up to you to open the module you need.
Until now the author of a kata can choose the output he wants. Moreover I am not the Ocaml translator: post for @mcamuzat if you want more explanations.
This comment has been hidden.
Look at the top of the page and you will see that 22 guys (out of a total of 553) has passed the C kata.
ok. thanks. then maybe I need to dig further why my code is failing
I am able to solve it. thanks
Same problem, any tip please?
This comment has been hidden.
agree with haferjir. This kata is trivial in any language, if you have the math intuition to see the series. It has nothing at all to do with Elixir.
This comment has been hidden.
I add the php version. My first translation..
thanks.
I am ready to approve your translation but I would like you to do 2 small improvements:
Thanks for your help.
Thanks for your review.
All your remarks are rights and I fixed them:
:/\t
highlight them )Again thank for spend some time.
Approved, thanks!
Finding the hidden sequence is a lot less work than the algorithmic solution, though.
After manually solving for u2 and u3 I noticed the sequence and checked it against u17. That was fun, plus I felt like doing a little math with paper and pencil for the first time in a little while.
I'm very glad of your post, thanks!
g964, a very god kata. I support your point of view. I think that with maths we may have solutions of better level, much more faster. This is the right place to publish them. But you were very polite and gentle. We gave us the chance to solve them by algorithms. I solved using dynamic programming. The solution of CrazyMerlin brilliant as always.
Thanks for your support!
I'm not to sure about the purpose of this kind of kata. While sometimes katas have a straight forward implementation and a more elegant and faster mathematical solution it seems that this one only makes sense in terms of getting your head around the math. I'm not sure whether it's an appropriate codewars kata as it seems more like a mathwars kata.
I think a solution would be to at least give some hints and reasons for this kata in the description.
I don't agree with you. You can take two points of view to do this kata: the first one purely algorithmic from the definition of un with "a straight forward implementation"; the second approach - not at all mandatory, but as a complement - is to "get your head around" and find another and faster solution. At first I wrote "a straight forward implementation" and then, observing the results, I saw the other way and demonstrated it. This shows how helpful experimentation can be. I will precise that in the description. Thanks for your feedback.