ezoic

Thursday, February 16, 2017

A python problem and the solution

Define a function generate_n_chars() that takes an integer n and a character c and returns a string, n characters long, consisting only of c:s. For example, generate_n_chars(5,"x") should return the string "xxxxx". (Python is unusual in that you can actually write an expression 5 * "x" that will evaluate to "xxxxx". For the sake of the exercise you should ignore that the problem can be solved in this manner.)


Solution:


class Solution:
    def generator1(self,a,n):
        i=0
        x=''
        while i<n:
            x=x+a
            i=i+1


        return(x)
kk=Solution()

print(kk.generator1('i',9))

result:




No comments:

Post a Comment

looking for a man

 I am a mid aged woman. I live in southern california.  I was born in 1980. I do not have any kid. no compliacted dating.  I am looking for ...