+ 1
Code Coach “Tax Free” (Medium) C# functional approach - room for improvement?
The solution to the Tax Free problem (medium difficulty in Code Coach) can be condensed down to a single line. I guess it’s not the most readable code, but still… Any room for improvement? https://code.sololearn.com/cFGY1h1oNC3m/?ref=app
10 Respuestas
+ 1
Do you have a question?
+ 1
I mean, Your code has only 3 steps splitting and mapping, then find sum. Still improvement means can be reduce of line characters. It don't have difference in performance.
This way it can be , but no difference much as far I know:
Console.Write(
Console.ReadLine().Split(',').Select(x => Convert. ToDouble(x)<20 ? double.Parse(x)*1.07 : double.Parse(x)).Sum());
Ok. Let me see others suggestions about improvements.. So I also start understand basics of FP.............
+ 1
Btw I didn't tho that would even Work but I tested:
Console.Write(
Console.ReadLine().Split(',')
.select((double)x => x<20? x*1.07 : x).Sum());
Unexpecting I got Error ',' Missing x2. Why?
+ 1
Felix Alcor
'x' is a string there so casting (double)x don't work. You need a parsing method...
double.Parse(x) or Convert.ToDouble(x);
And use Capital S in select.
+ 1
Jayakrishna🇮🇳 (double) can be used to give a Type to some Not directly declared variables.
But nether that or capital letters was what I meant. No matter if I use my Methode or yours both give out Missing ',' and I want ro know where the missed ',' came from.
+ 1
Felix Alcor I think ' select {value} ' according to syntax it's expecting a camma after (double) x, not a lambda expression.
Do you have any correct working code sample as you expecting..?
+ 1
Jayakrishna🇮🇳 Yeah I guess so to but without '=>' does it not work. I only wanted to do that because I hate making so many convert for the same value, so now I got around it with a Method.
https://code.sololearn.com/c6JxYEHqdLzS/?ref=app
0
Jayakrishna🇮🇳 I think his question is what He can improve there
0
Felix Alcor That's "any improvement" is added after my reply..
complexcity
So in that case, it can be suits best on feed post.
No improvement is actually need there I think.
Sharing Code coach solutions are also is a spoiler alert.. Others just use to get xp, instead of their own try...
0
I think functional programming is probably not the first thing you learn, and in my experience you need to understand the basics first, otherwise it doesn’t make sense.