Reputation: 393
Create a program that inputs random characters into a previously created list, then be able to turn around and pull them out.
The variables originalText is the list of numbers representing a character while key is a list of numbers for the program to know how much to enter.
My original plan was to take the original text, input the first value into a new list, the add the x amount of "stuff" after until it ends. Once the "stuff" is entered, it would then input another originaltext character before looping again until originalText is out of values to add to modifiedText.
This code was able to run, but it wasn't consistant. My number 1 problem was that all of the originalText information was thrown into the end of modifiedtect which didn't let it "unwind" (take out the "stuff") properly.
I think it's a math issue. Any suggestions?
Thanks!
def fixStuff(originalText, key, direction):
#0 adds Stuff, 1 removes Stuff
generate = key[4] #= getMod(key,4) #this variable is used for deciding how much Stuff to input.
originalSize = len(originalText)
print(originalSize)
random.randint(32,126) #used to generate random characters.
i = 1 #used for counting
origSpot = 0 #used to track position in original list
modifiedText = []
print(generate)
if direction is 0:
#originalSize = originalSize * generate # the total size of the array after Stuff.
print("inserting Stuff")
while origSpot < originalSize: #whatever this is
#after every originalSize digit, input generate(th) digits
print(origSpot)
modifiedText.insert((i),originalText[origSpot])
x = 0 #used for inserting. It must be reset before this while runs again.
while x <= generate:
modifiedText.insert((i),random.randint(32,126))
x = x + 1
#print(x)
#print(i)
i = i + generate #increment the gradiant so we dump the next load of Stuff after the next real digit
origSpot = origSpot + 1
i = 0
if direction is not 0:
print("remove el Stuffo")
#take every generate(th) digit and put it in a new array.
while origSpot < originalSize: #whatever this is
#after every originalSize digit, input generate(th) digits
print(origSpot)
modifiedText.insert((i),originalText[origSpot])
x = 0 #used for inserting. It must be reset before this while runs again.
while x <= generate:
origSpot = origSpot + 1
x = x + 1
#print(x)
#print(i)
i = i + 1 #increment the gradiant so we dump the next load of Stuff after the next real digit
print("ree!")
return modifiedText
Upvotes: 2
Views: 67
Reputation: 683
Does this do what you want?
import random
def addStuff(originalText, n):
"""
returns a list with n random characters inserted after each element in originalText
"""
modifiedText = []
for elem in originalText:
modifiedText.append(elem)
for _ in range(n):
modifiedText.append(chr(random.randint(32, 126)))
return modifiedText
def removeStuff(modifiedText, n):
"""return a list with every (n+1)th char in modifiedText.
Undoes the action of addStuff
"""
originalText = []
for i in range(0, len(modifiedText), n + 1):
originalText.append(modifiedText[i])
return originalText
v = addStuff("hello", 2)
print(v)
# prints e.g. ['h', 'm', 'w', 'e', '^', '0', 'l', '>', 'Q', 'l', '/', '}', 'o', '6', 'L']
r = removeStuff(v, 2)
print(r) # ['h', 'e', 'l', 'l', 'o']
I split the work into two functions, since they do two different, non-overlapping tasks. I built the list using append and for loops. You can do it with a while loop, but the for loop is less error-prone I think. Just for completeness, here's the while-loop version:
def addStuff(originalText, n):
"""
returns a list with n random characters inserted after each element in originalText
"""
modifiedText = []
i = 0
while i < len(originalText):
modifiedText.append(originalText[i])
x = 0
while x < n:
modifiedText.append(chr(random.randint(32, 126)))
x = x + 1
i = i + 1
return modifiedText
def removeStuff(modifiedText, n):
"""return a list with every (n+1)th char in modifiedText.
Undoes the action of addStuff
"""
originalText = []
i = 0
while i < len(modifiedText):
originalText.append(modifiedText[i])
# skipping over n random elements
i = i + 1 + n
return originalText
Upvotes: 1