+ 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
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
+ 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])
+ 10
Your idea is ok. Study codes from experienced coder.
You will find the one or other clever hack.
+ 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
+ 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.
+ 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
+ 4
There can be more than one "right" way to do things.
Also remember that every expert at anything started as a beginner.
+ 3
Haha seems i even messed up copying the solution from online. Insult to injury really.
+ 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.
+ 3
Practice makes you perfect
+ 2
Very impressive!
try this:
string = "Hello World"
print(string[::-1])
+ 2
me too 😅🙏 i dont know why?! 🥺🙏
+ 2
Lisa if it will be output = letter + output, it will print out the given string.
+ 1
Practice makes a man perfect at things.
+ 1
RAjNisH SinGh
"Practise writing bad code and you will get good at writing bad code"
-Martin Taylor
+ 1
Mayank Gupta
output = output + letter would give the given string
output = letter + output (as in my comment) gives the reversed string
+ 1
Lisa yes
Sorry I did it as output += letter 😅