Pig latin - C (optimizing the code) | SoloLearn: Learn to code for FREE!


Pig latin - C (optimizing the code)

Hello everyone, and thank you for reading me. I recently joined SoloLearn and I have to say I am enjoying it so far. Took the C lessons and I am now heading for some code coach. But here is the trick... Once your code is working, you are pretty much done with it. I feel like something is missing, I want to improve, I want to learn code ethics and proper shapes. Maybe this would have been possible in two lines and I didn't see how... So with this in mind I thought... Well someone might be interested with that as well so why not share my code and take some feedbacks from the community ? So please feel free to comment, and either ask for insights along with me or provide some ;-) Cheers to everyone. The code: https://code.sololearn.com/ciyq9BZMepbm/?ref=app

1/20/2021 9:53:21 AM

Adrian Weaver

4 Answers

New Answer


If you look at your conditional cases, you can see a lot of duplicate code happening. That is something you can almost always have a look at to see if it can be done better. In terms of the algorithm, I don't think you can really surpass linear complexity (though I might be wrong), but with a bit of pointer fiddling and arithmetic, you can at least get rid of your "reset" variable and one conditional. This is how it could look like: https://code.sololearn.com/cCAo1B6vwwuc/?ref=app However, it is possible to "cheat" the challenge a little by using the way input is provided in the code coach to your advantage. I think this is the shortest you can get in C: https://code.sololearn.com/cCI2bimaOVT0/?ref=app Then again, I might be wrong, given that there exist lots of ways to solve this particular problem.


Good points, Jegix. For context, the code coach uses a simplified set of rules which do not distinguish between vowels and consonants, where you always shift the first letter to end and append "ay". That is the reason both codes pass in this instance. The first one is just a rework of OP's code, but the second one is pretty much a cheat as I already wrote originally, given that it abuses the way input is provided, and meant to be more of a demonstration how short a sufficient code could become. It could still be that argued neither is a hundred percent fine, for example both would fail for an empty input. I guess it depends on how thouroughly you want to be. Your code is certainly a more complete take on the (original) challenge.


I expected something like your answer and I'm really not disappointed. I still have a lot to learn about main function arguments, I don't know what they are used for so it doesn't make sense for me yet. Your code is really clean Indeed and the second one is really impressive. Now that it's in front of my eyes I can tell the reset variable is not of any use. And I know I may need to read again about declaring and defining variables to have less duplicates. Many thanks for your time Shadow !


Shadow I think neither of those programs meet all the constraints. (But it may be enough to trick your way through the code coach challenge, idk). To my knowledge the rules are: A word beginning with a consonant should have all the consonants preceding the first vowel shifted towards the end and "yay" appended to it. A word beginning with a vowel need only "yay", "way", or "ay" added at the end. This is what I came up with to meet those constraints: https://code.sololearn.com/cA20A24A71a2/#