Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code that randomly capitalizes each letter of a string (Code cleaning help) [closed]

Tags:

python

I'm a beginner coder, and I recently created a function that takes a string and randomly capitalizes each letter.

def rand_upper(string):
    import random
    strList = [l for l in string.lower()] 
    newList = []
    for i in strList:
        j = random.randrange(2)
        if j == 1:
            letter = i.upper()
            newList.append(letter)
        else:
            newList.append(i)
    return "".join(newList)

The code works as I intended, but is there any way that I can make this code cleaner? I personally think this is hard to read. Are there any other ways to compress the code to make it more readable/efficient? thanks!

like image 615
TheShin Avatar asked Dec 07 '20 17:12

TheShin


Video Answer


1 Answers

Using choice instead, and calling lower and upper only once.

from random import choice

def rand_upper(string):
    return ''.join(map(choice, zip(string.lower(), string.upper())))

Even better, as Peter commented:

def rand_upper(string):
    return ''.join(map(choice, zip(string, string.swapcase())))

Another, based on Olvin_Roght's:

def rand_upper(string):
    return ''.join([c if getrandbits(1) else c.swapcase() for c in string])

Two more, mixing our solutions for the fastest so far:

def rand_upper(string):
    return ''.join([c if getrandbits(1) else d
                    for c, d in zip(string, string.swapcase())])
def rand_upper(string):
    return ''.join([z[getrandbits(1)] for z in zip(string, string.swapcase())])

Benchmark using string = rand_upper('a' * 1000):

739 μs  797 μs  725 μs  original
764 μs  787 μs  693 μs  original_2
713 μs  691 μs  680 μs  Samwise
699 μs  657 μs  682 μs  theCoder
477 μs  486 μs  490 μs  superb_rain
520 μs  476 μs  489 μs  Peter_Wood
135 μs  131 μs  141 μs  based_on_Olvin_Roght
120 μs  113 μs  121 μs  superb_Peter_Olvin
125 μs  117 μs  118 μs  superb_Peter_Olvin_2

(Not including Olvin's original because it's the only one with quadratic instead of linear time, so a comparison with a single size would be misleading.)

Code:

from timeit import repeat
from random import randrange, choice, getrandbits

def original(string):
    import random
    strList = [l for l in string.lower()] 
    newList = []
    for i in strList:
        j = random.randrange(2)
        if j == 1:
            letter = i.upper()
            newList.append(letter)
        else:
            newList.append(i)
    return "".join(newList)

def original_2(string):
    strList = [l for l in string.lower()] 
    newList = []
    for i in strList:
        j = randrange(2)
        if j == 1:
            letter = i.upper()
            newList.append(letter)
        else:
            newList.append(i)
    return "".join(newList)

def Samwise(string: str) -> str:
    return "".join(
        c.upper() if randrange(2) else c.lower() 
        for c in string
    )

def theCoder(string):
    return ''.join(choice((str.upper, str.lower))(c) for c in string)

def superb_rain(string):
    return ''.join(map(choice, zip(string.lower(), string.upper())))

def Peter_Wood(string):
    return ''.join(map(choice, zip(string, string.swapcase())))

def based_on_Olvin_Roght(string):
    return ''.join([c if getrandbits(1) else c.swapcase() for c in string])

def superb_Peter_Olvin(string):
    return ''.join([c if getrandbits(1) else d for c, d in zip(string, string.swapcase())])

def superb_Peter_Olvin_2(string):
    return ''.join([z[getrandbits(1)] for z in zip(string, string.swapcase())])

funcs = original, original_2, Samwise, theCoder, superb_rain, Peter_Wood, based_on_Olvin_Roght, superb_Peter_Olvin, superb_Peter_Olvin_2

string = original('a' * 1000)
number = 1000

tss = [[] for _ in funcs]
for _ in range(4):
    for func, ts in zip(funcs, tss):
        t = min(repeat(lambda: func(string), number=number)) / number
        ts.append(t)
        print(*('%d μs ' % (1e6 * t) for t in ts[1:]), func.__name__)
    print()
like image 162
superb rain Avatar answered Oct 05 '22 05:10

superb rain