Factor method

# We will repeatedly output a string 
DEF REPEAT (STR, COUNT): 
    Result = "" 
    for I in Range (COUNT): 
        Result = Result + STR
     return Result
 Print (REPEAT ( ' @ ' ,. 5 )) 

# use method factor to optimize the function, the logic factor derived from any number of methods can be split into a binary 

DEF factorRepeat (STR, count): 
    Result = '' 
    the while . 1 :
         # the number of cycles when the count is not equal to the binary resolution is not equal to 0, need to add character 
        IF COUNT =% 2! 0: 
            the Result + = str
         # When the count is equal to zero split end of the cycle is completed 
        IF count == 0:
             BREAK 
         # each string must be doubled 
        STR + = STR
         # count except 2 
        count = int (count / 2 ) 

    return Result 

Print (factorRepeat ( ' @ ' , 5))

 

Guess you like

Origin www.cnblogs.com/tengx/p/11758152.html