Sunday, 27 May 2012

CodingBat clean bowled

This refers to the "string_times" problem in the python section of codingbat , under the category warmup-2
the question says
Given a string and a non-negative int n, return a larger string that is n copies of the original string. 

string_times('Hi', 2) → 'HiHi'
string_times('Hi', 3) → 'HiHiHi'
string_times('Hi', 1) → 'Hi'

here is the link , (try it out)

the codingbat solution is

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
here's my solution:

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

No comments: