+ 11

Why do I always code like this?

For some reason I am just not smart. Every time I write code I take the longest most unintuitive route when the real answer is much more simple and elegant. Is there a way to just become more clever with this stuff?? For example I had to write a function that reverses a string. I posted the code below but the first function is my work and the second is what you’re ACTUALLY supposed to do. https://code.sololearn.com/c63XuQh78r6F/?ref=app

3rd Nov 2021, 5:02 PM
Luke Simmons
17 Respostas
+ 25
For most problems, there are many possible solution. As you get more experience, you may find shorter solutions. It's okay, that comes with practice line 16 should be output = letter + output
3rd Nov 2021, 5:11 PM
Lisa
Lisa - avatar
+ 18
It will come. The most important is that you code. Then you can do your codes better and better. No hurry. And you can even use slices to make your reverse code even shorter, like print(input()[::-1])
3rd Nov 2021, 5:30 PM
Per Bratthammar
Per Bratthammar - avatar
+ 10
Your idea is ok. Study codes from experienced coder. You will find the one or other clever hack.
3rd Nov 2021, 6:08 PM
Oma Falk
Oma Falk - avatar
+ 8
Luke Simmons , just to mention that the second 'smart' function does not output anything. the reason is, that variable 'output' is an empty string that never will be changed during iterations of the for loop. so when it will be returned, the empty string will be printed. when running the code, the first solution will print the correct result, but not the second one. this is the code that will run: def smartreverse(string): output ="" for letter in string: #letter += output output = letter + output return output
3rd Nov 2021, 6:19 PM
Lothar
Lothar - avatar
+ 5
It is normal to code like that when you just start, neer everything can have more solution, but with practice you will know which solution is better. Try to check somebody else solution and understand that code, you will learn more, and after some time you will make better solution.
3rd Nov 2021, 10:14 PM
PanicS
PanicS - avatar
+ 5
Hi Luke Simmons You did great, functional programming is always the best . Although it might be like a long path. Here is the simple one. https://code.sololearn.com/ctMqGrm6fL5Q/?ref=app
3rd Nov 2021, 11:16 PM
ShadowCipher
ShadowCipher - avatar
+ 4
There can be more than one "right" way to do things. Also remember that every expert at anything started as a beginner.
5th Nov 2021, 12:37 PM
Paul K Sadler
Paul K Sadler - avatar
+ 3
Haha seems i even messed up copying the solution from online. Insult to injury really.
3rd Nov 2021, 8:18 PM
Luke Simmons
+ 3
Learn the languages here at Solo Learn, then do a tour in Exercism, where you get review and guidance from code coaches for free. My code got a lot better - even in languages I had some experience with.
4th Nov 2021, 10:35 PM
Emerson Prado
Emerson Prado - avatar
+ 3
Practice makes you perfect
5th Nov 2021, 12:01 PM
Varchas
Varchas - avatar
+ 2
Very impressive! try this: string = "Hello World" print(string[::-1])
4th Nov 2021, 12:47 PM
Elliot
Elliot - avatar
+ 2
me too 😅🙏 i dont know why?! 🥺🙏
5th Nov 2021, 12:54 PM
abady al knti
abady al knti - avatar
+ 2
Lisa if it will be output = letter + output, it will print out the given string.
5th Nov 2021, 1:50 PM
Mayank Gupta
Mayank Gupta - avatar
+ 1
Practice makes a man perfect at things.
5th Nov 2021, 2:35 AM
Rajnish Kush
Rajnish Kush - avatar
+ 1
RAjNisH SinGh "Practise writing bad code and you will get good at writing bad code" -Martin Taylor
5th Nov 2021, 8:48 AM
Rishi
Rishi - avatar
+ 1
Mayank Gupta output = output + letter would give the given string output = letter + output (as in my comment) gives the reversed string
5th Nov 2021, 2:01 PM
Lisa
Lisa - avatar
+ 1
Lisa yes Sorry I did it as output += letter 😅
5th Nov 2021, 2:13 PM
Mayank Gupta
Mayank Gupta - avatar