The problem
I have a haystack:
source = ''' "El Niño" "Hi there! How was class?"
"Me" "Good..."
"I can't bring myself to admit that it all went in one ear and out the other."
"But the part of the lesson about writing your own résumé was really interesting!"
"Me" "Are you going home now? Wanna walk back with me?"
"El Niño" "Sure!"'''
I have a mask:
out_table = '→☺☻♥♦♣♠•◘○§¶▬↨↑↓←∟↔'
And I have a token --
(single space).
All their elements are strings (class of <str>
).
I need a function that will:
- Iterate through haystack (
source
) - Replace each occurrence of token (
- Will print resulting new haystack after the replacement process
Finally, I need a similar method that will revert the above process, so it will replace each occurrence (every character) of the →☺☻♥♦♣♠•◘○§¶▬↨↑↓←∟↔
list into
(space).
Expected result
An example (can vary -- randomness) example result is (just a printout):
↔◘↔▬"El→Niño"↓"Hi∟there!↓How↨was↨class?"
↔◘↔▬"Me"↓"Good..."
♥♦♣♠"I↓can't↨bring§myself↓to∟admit↓that↓it↓all↓went↓in↓one§ear↓and↓out§the↓other."
↔◘↔▬"But☻the☻part☻of↓the→lesson∟about↓writing↓own↓résumé§was§really→interesting!"
↔◘↔▬"Me"↓"Are↓you☻going↓home§now?→Wanna↓walk∟back↓with↓me?"
♥♦♣♠"El↓Niño"→"Sure!"
Assumptions:
- Every space must be replaced in the haystack
- Not every character out of mask must be used
So, I the most "randomly border" scenario all spaces will be replaced with the same character. Which isn't a problem at all as long as the whole process is reversible back to the original haystack (source
).
My research and solution attempt
Since this is my first Python code, I have browsed a number of Python and non-Python related questions here and in the net and I have come with the following idea:
import random
def swap_charcter(message, character, mask):
the_list = list(message)
for i in random.sample(range(len(the_list)), len(list(mask))):
the_list[i] = random.choice(mask)
return message.join(the_list)
# print(swap_charcter('tested', 'e', '!#'))
print(swap_charcter('tested', 'e','→☺☻♥♦♣♠•◘○§¶▬↨↑↓←∟↔'))
But... I must be doing something wrong, because each time I run this (or many, many other) piece of code with just a space as an argument, I am getting the Sample larger than population or is negative error.
Can someone help here a little bit? Thank you.
EDIT: I have replaced list(character)
→ list(message)
, as suggested in the comments.