Create a list of 100 random strings of random length between 5 and 8 characters
ID: 3701451 • Letter: C
Question
Create a list of 100 random strings of random length between 5 and 8 characters and consisting of upper and lower case characters. Use the random functions that we discussed in class to create the strings. You can use the following to create a list of all upper and lower case letters. 3. Letters -[sorted(chr(c-x) for c in range(97,123) for x in (0,32))] 4. Using the list of strings created above, sort them based on the following criteria: a. b. c. The sum of the first three ASClI values in the string The length of the strings in reverse order The middle character of a string with odd length or the one closest to center on the left of it has even length Ex today' would use d', Thomast would use oExplanation / Answer
3.)
import random, string
x =[] #list where the random strings get stored
def create(leng): # function creating the random string of length 5-8
return ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase) for i in range(leng)) #appending lower case and uppercase characters in random of the specifc length
for i in range(100):
leng = random.randint(5,8) #choosing random length from 5-8
h = create(leng) #calling function create and storing the created character in h
x.append(h) # appending created character in list x
print(x)
4.)
a)
def ascii_sorting(x):
convert = lambda text: int(text) if text.isdigit() else text
ascii_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(x, key=ascii_key )
print(ascii_sorting(x))
b.) sorted(x, key = lambda s: len(s), reverse = True )
c.) sorted(x, key = lambda s: s[int(len(s)/2 - 1)])
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.