3

I'm very new and just came across an exercise which asks:

Given a string and a non-negative integer n, return a larger string that is n copies of the original string.

I answered:

def string_times(str, n):
    return(str * n)

and passed all tests. The solution provided:

def string_times(str, n):
     result = ""
     for i in range(n):  # range(n) is [0, 1, 2, .... n-1]
         result = result + str  # could use += here
     return result

My question is: is there any reason why my simpler solution won't work in some cases, or is it just a matter of a more experienced programmer overthinking things?

9
  • was your answer judged as incorrect? I think the provided solution just have an intention of exposing some knowledge of string concatenation to the user; and it is indeed the purpose of an exercise. Commented Nov 5, 2014 at 8:09
  • 5
    I much prefer your code; adding up strings by concatenation should generally be avoided, and the single line will be more efficient than an explicit loop. My only issue is with shadowing str, but I guess you were provided with the function definition. Commented Nov 5, 2014 at 8:10
  • as @YohanesKhosiawan许先汉 wrote. The answer uses concepts likes ranges and a for loop and is much easier to expand, e.g. other delimiters between the copies. Your solution is clean for the purpose but hides the functionality a bit. Commented Nov 5, 2014 at 8:13
  • 2
    @GingerPlusPlus using str as a variable name prevents you from accessing the built-in type. Commented Nov 5, 2014 at 9:08
  • 3
    Basically, if you understand what the other code is doing (it's always worth learning from alternative approaches and implementations), don't worry about it. You wrote code that met the spec and passed the tests: good job, that's programming. Commented Nov 5, 2014 at 9:49

1 Answer 1

1

Your answer is correct, but the exercise probably wanted to reveal other concepts such as for loops and string concatenation using + and +=.

That said, I'd like to add to the stated solution that it is a better practice to use an underscore when you don't really need the loop variable. It's a way of telling future programmers you are not using the variable anywhere in the loop.

It's also better to use xrange if you don't actually need a list (generated by range). You can try in the interpreter range(1000000) and xrange(1000000) to see the immediate difference. xrange is actually a generator, making it a lot more memory efficient.

in python 3, range returns a generator by default

# changed i to an underscore, using xrange instead of range
for _ in xrange(n):  # xrange(n) *generates* 0, 1, 2 ... n-1
Sign up to request clarification or add additional context in comments.

2 Comments

About range and xrange, I think that your hint applyes only to Python 2.x, in later versions range is deleted and xrange is renamed to range.
Thanks for your comment, I'll note that in the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.