7 kyu
Chain me
4,653 of 7,810nrgarg
Loading description...
Fundamentals
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.
The example just has two fs, but it actually has more. Hope this advice can keep yr time >.0
This comment has been hidden.
Please use the spoiler flag when posting potential spoilers and use appropriate formatting when posting code. See: https://docs.codewars.com/training/troubleshooting#post-discourse
Your code doesn't do what the kata asks at all, so I'm not sure what you mean by "correct".
You need to be able to handle any function, not just the 4 you hardcoded. The functions are also passed in as functions, not as strings.
This comment has been hidden.
Rust description has a typo:
x + 30
instead ofx * 30
New Rust translation ready for review.
Missing fixed and sample tests in JS of empty array, which could invalidate this solution
This comment has been hidden.
fixed
This comment has been hidden.
I think it is just O(n)?? If I can assume that the functions always do simple additions and multiplications meaning they are O(1), then the function chainer will execute n of these.
So it should just be O(n), regardless of the recursive nature of this kata.
NASM translation
has been approved, but you should have added code blocks for NASM, currently it shows javascript's
It doesn't make much sense to add code blocks for NASM, in many cases, and in this one more particularly. All the relevant information is included as comments in solution set up.
OCaml translation
Approved
Factor translation
Go translation
[JavaScript] I don't understand; why are div and sub are thrown into the fs array, when we have no idea what to 'div' or 'sub' by???
And why would you care about that? Your only concern is creating the function chainer.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Please use a spoiler flag when you post code, so that users who didn´t solve the kata cannot see it; I put the flag for you this time. Also use markdown tags to format your code or it's not usable. Refer to the documentation about this and more: https://docs.codewars.com/training/troubleshooting/#post-discourse
Because your solution only accounts for the first 2 functions in the array, it could be more. Additionally, if the array of functions has 0 or 1 items, then it will throw an exception.
This comment has been hidden.
Your solution only accounts for the first 2 functions in the array, it could be more. Additionally, if the array of functions has 0 or 1 items, then it will throw an exception.
Julia translation
Approved :)
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Also I put again the test I´ve made:
console.log(input);
console.log(fs(0)(input));
console.log(fs(1)(1));
console.log(fs(1)(fs(0)(input)));
markdown formatting
There can be more than two...
Ok, thanks I´ll check it out
Haskell translation
Approved by someone.
C translation
Approved :)
really nice KATA!
needs random tests (JS at least)
There's something of random tests. The first argument is random, though the second is hardcoded.
Outputs are unpredictable, so it is enough to prevent completely hardcoding a solution. It won't find many edge cases of course.
It's an old kata ..
Hi could I please have some help :)
Basically I noticed that although the test case is this: Test.assert_equals(chain(2, [:add, :mult]), 90, "Error: chain function not working properly")
With my code the first test works and when I click submit I get 1 passed and one fails: Test Passed: Value == 345.30138755410417 Error: chain function not working properly - Expected: -4.244976874098263, instead got: -4.0
But I think it is just because the test case doesn't expect the error string to have that second bit of text.
Sorry if I'm completely wrong! I am super new. But I think my code should work and was wondering if this is the issue?
I can not pass the test with this info. ChainTests ExampleTest Incorrect Value for '2' Expected: 60.0d But was: 360 at ChainTests.ExampleTest () [0x00000] in :0
so,I just return 60 to test your test case. I still get this info. I am using C# ,so ,you need to check your testcase
I click submit,then i got this. ChainTests AddDivSubMulTest Error: chain function not working Expected: 60.0d But was: 495.0d at ChainTests.AddDivSubMulTest () [0x00000] in :0 AddDivTest Error: chain function not working Expected: 60.0d But was: 187.5d at ChainTests.AddDivTest () [0x00000] in :0 AddMulTest Error: chain function not working Expected: 60.0d But was: 13950.0d at ChainTests.AddMulTest () [0x00000] in :0 AddSubTest Error: chain function not working Expected: 60.0d But was: 550.0d at ChainTests.AddSubTest () [0x00000] in :0
I think you mess up expected and the result.
Hm, that's really weird. However, looking at the test cases - it seems to be working as it should. It might be that Codewars tester is not displaying the useful information in the proper way for C#. Just to be sure - what is your current solution?
public static double Chain(double input, Func<double,double>[] fs) { return 60; }
I did nothing,but return 60. however, i still get the wrong info
ChainTests AddDivSubMulTest Error: chain function not working Expected: 60.0d But was: 495.0d at ChainTests.AddDivSubMulTest () [0x00000] in :0 AddDivTest Error: chain function not working Expected: 60.0d But was: 187.5d
ok. so as I thought. the tester for the C# environment is printing the statements incorrectly. That's not something that can be fixed right now. So in the mean-time, just imagine "expected" and "but was" were flipped. That should make more sense.
I'm going to mark the issue as resolved. Your implementation needs work. I know you were just trying to get the right message to pop up, but seeing (above) what the issue is, you should now know what to expect. Happy coding.
no,i have a solution. myabe i can email to you.cause provide the solution here is not allowed pls tell me your email
i got another error info: Error: Command failed: /tmp/csharp1151022-20-db7gw8/setup.cs(3,21): error CS0101: The namespace
global::' already contains a definition for
Kata' /tmp/csharp1151022-20-db7gw8/code.cs(3,21): (Location of the symbol related to previous error)global::?what's this? it's ridiculous
Just post the solution here and mark it as having spoiler content. Regardless, the solution for this is definitely possible and if you look at the other solutions (I know, you won't get the points for the kata), you might be able to see what you're doing wrong.
This comment has been hidden.
so,have you checked my solution?
Sorry I didn't get a notification until now about your reply. Weird. so fs.Last() will return the last function in the array of functions. How could it return the right result, if it only executed the last function in the list?
maybe i misunderstand the kata. if you can provide more test demo and describe the kata in detail.
Write a generic function chainer that takes a starting value, and an array of functions to execute on it (array of symbols for ruby). Return the final value after execution is complete.
it is too simple,i think ,it ask me to return the value of last function executed. so,what do you exactly want?pls make it clear
I realize now why this was an issue for you, there is no example in the description for C#! My apologies - the C# addition wasn't done by me (and I have little to no knowledge of C#). Example code as well as a modified description has been added and should definitely help you understand what the result should be. We can do some psuedo-code right here however.
hey @chucklu, just wondering if your problems were resolved. If not - do you have any more questions??
This comment has been hidden.
[Test] public static void ExampleTest() { Assert.AreEqual(Kata.Chain(2, new[] { (Func<double, double>)add, mul }), 360, "Incorrect Value for '2'"); }
ChainTests ExampleTest Incorrect Value for '2' Expected: 300.0d But was: 360 at ChainTests.ExampleTest () [0x00000] in :0
Are you kidding me?
300 cannot be the correct answer. With the input being
2
and the operations foradd
andmul
(which are already defined for you), the correct result is 360.Looking at your code - I can see what the problem is. What do you use 0 as your starting value when call Aggregate? If you did that, this is what you get as your execution steps
This is incorrect (and is what you got as your answer). However, you're very close - really. Almost there!
This comment has been hidden.
so?will you fix the bug about test?
Unfortunately, I cannot change the test cases anymore - they are locked.
The only error in the test cases is the order in which the messages are printing (something I stated as well). So, in your mind just swap the "expected" and "got" messages. I'll be contacting the admins about unlocking the test cases so I can fix that, however that doesn't change the fact that your solution is still wrong.
As far as linq + C#, although I don't know much of about the language itself, I've learned enough languages to grasp what Aggregate does (it's not an uncommon concept for other higher order languages).
0 may be for the sum, but I'm giving you a starting value in the Chain method that you aren't even using. You need to use that instead of 0, otherwise you will constantly get the wrong answer. It's a very small but important change.
it's my fault, i have checked my solution,it do not need aggregate. sorry to bother you,now ,i have passed this kata thank you very much
Excellent - However I suggest looking at the other solutions. Most of them use Aggregate, so it might be useful to see how to use Aggregate in this scenario
This comment has been hidden.
so your solution only deals with fs[1] and fs[0], meaning that fs is an array of 2 functions. But what about if I want to chain 10 functions? or 1? or 0? or 5?
Your solution doesn't work for any number of functions that may be in the array of fs.
IMO calling this chaning is kind of inaccurate or confusing, thinking of method chaining in the jquery sense.
Well jQuery chaining isn't really specific to jQuery, its native javascript at work. The type of chaining you're talking about is done by returning the "this" reference in the function. This type of chaining is incredibly useful, and has a lot of excellent applications. However, it does have it's limitations.
The chainer that this kata is describing is meant to be general. Such that you can apply it anywhere you need to de sequential operations, in a very clean manner. The functions/methods passed into it can be from anywhere and as such allow for a different type of versatility.
Upon further exposure to programming and awareness of my lack of understanding of anything - tetreault you are correct. This is not chaining. it's a foldr method with an array of functions. that is all. I concede to you sir. However I can't change the name of this kata. I must apologize profusely. :'(
This comment has been hidden.
Your proposed solution is too narrow. Imagine you had no idea what the calling functions were. All you know is that there is an input value and an array of functions. Trying to anticipate what each function is and what it does will most certainly not work.
There are too many tests, 350 is excessive! 6 is enough.
The initial skeleton should provide a function signature.
Yeah removed the loop and just left the test cases as is
Great. It's much better now.
Could use some more (random?) tests, but other than that, works just fine.
More random(ish?) test cases have been added.
Those are fine. You could also add things like:
You could loop that, use different chaining functions, etc.