Python Forum
Still learning - code efficiency, which of these is better?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Still learning - code efficiency, which of these is better?
#1
Just started learning Python and was running through a decent tutorial from Google and there is a lab section where they give me a problem and I provide the solution. On one particular problem, my code works fine but looking at their solution code, it seems they used more code to get to the same result. So it got me curious, which of these solutions would be more desirable?

Normally, I would include the full runnable code, but in this instance, I am only interested in the best practice, just know it does yield the exact same result:

Mine:
# C. fix_start
# Given a string s, return a string
# where all occurences of its first char have
# been changed to '*', except do not change
# the first char itself.
# e.g. 'babble' yields 'ba**le'
# Assume that the string is length 1 or more.
# Hint: s.replace(stra, strb) returns a version of string s
# where all instances of stra have been replaced by strb.
def fix_start(s):
  truncs=s[1:]
  return s[0]+truncs.replace(s[0],"*")
This is theirs:

# B. both_ends
# Given a string s, return a string made of the first 2
# and the last 2 chars of the original string,
# so 'spring' yields 'spng'. However, if the string length
# is less than 2, return instead the empty string.
def both_ends(s):
  # +++your code here+++
  # LAB(begin solution)
  if len(s) < 2:
    return ''
  first2 = s[0:2]
  last2 = s[-2:]
  return first2 + last2
  # LAB(replace solution)
  # return
  # LAB(end solution)
I like to be as efficient as possible and strive for the least amount of code to get the job done and I feel I did it where they didn't, though I suppose, without knowing the code itself, theirs is slightly more easier to understand what is being done (if, of course, the explanation of what the intent is wasn't right above it)

I get that I may be splitting hairs here, but trying to ensure I start and continue using best practices from the get-go.

Thanks!
Reply
#2
The functions are so short that I would one-line both of them:
def both_ends(string):
    return string[:2] + string[-2:] if len(string) >= 2 else ""


def fix_start(string):
  return string[0] + string[1:].replace(string[0], "*")
An experienced python programmer would have no issue reading either of these.  As a beginner it would certainly be easier to understand separated into more lines.

This isn't really a question of efficiency, but rather of readability.
Reply
#3
Thanks for the reply. I get what you mean. One thing I realized though is that I actually posted the wrong code from their solution, so my question probably doesn't make a lot of sense. I already shut my laptop down, but I will update it tomorrow so it makes more sense, though I don't think your answer will be much different.

And you are correct, it is more a question of readability. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad I'm stuck with the reinforcement learning function in my Code OskiLori 2 1,559 May-20-2023, 10:01 PM
Last Post: OskiLori
  Numpy Structure and Efficiency garynewport 2 683 Oct-19-2022, 10:11 PM
Last Post: paul18fr
  Efficiency with regard to nested conditionals or and statements Mark17 13 3,158 May-06-2022, 05:16 PM
Last Post: Mark17
  How to use vectorization instead of for loop to improve efficiency in python? PJLEMZ 4 2,393 Feb-06-2021, 09:45 AM
Last Post: paul18fr
  Translation of R Code to Python for Statistical Learning Course SterlingAesir 2 2,134 Aug-27-2020, 08:46 AM
Last Post: ndc85430
  learning to code python nhan 5 2,530 May-23-2020, 03:35 PM
Last Post: nhan
  Any suggestions to improve BuySell stock problem efficiency? mrapple2020 0 1,364 May-13-2020, 06:19 PM
Last Post: mrapple2020
  Learning python, stuck on some code. stanceworksv8 2 3,474 Apr-02-2019, 01:51 AM
Last Post: stanceworksv8
  Help improve code efficiency benbrown03 9 4,346 Feb-20-2019, 03:45 PM
Last Post: ichabod801
  Web Scraping efficiency improvement HiImNew 0 2,390 Jun-01-2018, 08:52 PM
Last Post: HiImNew

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020