For instance, I have the following string:
Hello how are you today, [name]?
How would I go about randomly placing characters between a random choice of words but not [name]? I already I have this following piece of code but I was hoping there is a better way of going about it.
string = 'Hello how are you today, [name]?'
characters = 'qwertyuioplkjhgfdsazxcvbnm,. '
arr = string.rsplit(" ")
for i in range(0, len(arr)):
x = arr[i]
if x == '[name]':
continue
if (random.randint(0,2)==1) :
rnd=random.randint(1,len(x)-2)
tmp1 = random.randint(0,len(characters))
rndCharacter = characters[tmp1:tmp1+1]
x = x[0:rnd] + rndCharacter + x[rnd+1:]
arr[i] = x
" ".join(arr)
> Hellio how are yoy todsy, [name]?"
Though this replaces the character with a another random character. What way would I go about having it randomly replace or place a random character after or before a character as well?
Basically I'm just trying to simulate a sort of typo generator.
Thanks
Update on my code so far:
string = 'Hey how are you doing, [name]?'
characters = 'aeiou'
arr = string.rsplit(" ")
for i in range(0, len(arr)):
x = arr[i]
if x == '[name]': continue
if len(x) > 3:
if random.random() > 0.7:
rnd = random.randint(0,len(x)-1)
rndCharacter = random.choice(characters)
if random.random() > 0.7:
x = x[0:rnd] + rndCharacter + x[rnd+1:]
else:
x = x[:rnd] + rndCharacter + x[rnd:]
arr[i] = x
else:
if random.random() > 0.7:
rnd = random.randint(0,len(x)-1)
rndCharacter = random.choice(characters)
x = x[:rnd] + rndCharacter + x[rnd:]
arr[i] = x
print " ".join(arr)
> Hey houw are you doiang, [name]?
UPDATE:
Maybe my final update for the code, hopefully this will help someone out some point in the future
def misspeller(word):
typos = { 'a': 'aqwedcxzs',
'b': 'bgfv nh',
'c': 'cdx vf',
'd': 'desxcfr',
'e': 'e3wsdfr4',
'f': 'fredcvgt',
'g': 'gtrfvbhyt',
'h': 'hytgbnju',
'i': 'i8ujko9',
'j': 'juyhnmki',
'k': 'kiujm,lo',
'l': 'loik,.;p',
'm': 'mkjn ,',
'n': 'nhb mjh',
'o': 'o9ikl;p0',
'p': 'p0ol;[-',
'q': 'q1asw2',
'r': 'r4edft5',
's': 'swazxde',
't': 't5rfgy6',
'u': 'u7yhji8',
'v': 'vfc bg',
'w': 'w2qasde3',
'x': 'xszcd',
'y': 'y6tghu7',
'z': 'zaZxs',
' ': ' bvcnm',
'"': '"{:?}',
'\'': '[;/\']',
':': ':PL>?"{',
'<': '<LKM >',
'>': '>:L<?:',
';': ';pl,.;[',
'[': '[-p;\']=',
']': '=[\'',
'{': '{[_P:"}+',
'}': '}=[\']=',
'|': '|\]\'',
'.': '.l,/;',
',': ',lkm.'
}
index = random.randint(1,len(word)-1)
letter = list(word[:index])[-1].lower()
try:
if random.random() <= 0.5:
return word[:index] + random.choice(list(typos[letter])) + word[index:]
else:
return word[:index-1] + random.choice(list(typos[letter])) + word[index:]
except KeyError:
return word
def generate(self, s, n, safe_name):
misspelled_s = ''
misspelled_list = []
for item in s.split(' '):
if n:
if safe_name in item:
misspelled_list.append(item)
else:
r = random.randint(0,1)
if r == 1 and len(re.sub('[^A-Za-z0-9]+', '', item)) > 3:
misspelled_list.append(misspeller(item))
n -= 1
else:
misspelled_list.append(item)
else:
misspelled_list.append(item)
return ' '.join(misspelled_list)
import random
def misspeller(word):
characters = 'qwertyuioplkjhgfdsazxcvbnm,. '
rand_word_position = random.randint(-1,len(word))
rand_characters_position = random.randint(0,len(characters)-1)
if rand_word_position == -1:
misspelled_word = characters[rand_characters_position] + word
elif rand_word_position == len(word):
misspelled_word = word + characters[rand_characters_position]
else:
misspelled_word = list(word)
misspelled_word[rand_word_position] = characters[rand_characters_position]
misspelled_word = ''.join(misspelled_word)
return misspelled_word
s = 'Hello how are you today, [name]?'
misspelled_s = ''
misspelled_list = []
for item in s.split(' '):
if '[name]' in item:
misspelled_list.append(item)
else:
misspelled_list.append(misspeller(item))
misspelled_s = ' '.join(misspelled_list)
print misspelled_s
Examples of what I'm getting from misspelled_s are:
'Hellk howg ars youf poday, [name]?'
'Heylo how arer y,u todab, [name]?'
'Hrllo hfw are zyou totay, [name]?'
Edited to clean up a couple of mistakes and omissions on first copy.
Edit 2 If you don't want every word to be affected you can modify the for loop in the following way:
for item in s.split(' '):
n = random.randint(0,1)
if '[name]' in item:
misspelled_list.append(item)
elif n == 1:
misspelled_list.append(misspeller(item))
else:
misspelled_list.append(item)
You can modify the probability that a word is modified by changing how n is generated e.g. n = random.randint(0,10)
If you want to place a letter before or after instead of replacing, just fix the indices in your splicing so that they don't jump over a letter - i.e. use
x = x[:rnd] + rndCharacter + x[rnd:]
That way the new character will be inserted in the middle, instead of replacing an existing one.
Also, you can use rndCharacter = random.choice(characters) instead of using tmp1 like that.
I think #sgallen's answer will work, but I have a few tips (for your previous code, and going forward).
for i in range(0, len(arr)):
x = arr[i]
# is the same as
for i,x in enumerate(arr):
else:
if random...:
# to
elif random...:
Using string as the name of a variable, isn't a good practice. The reason being, there is a string module. It might even come in handy for this because of the string constants. Alternatives could be inp, or data, or sentence.
# For example
>>> import string
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'
By the way, if anyone notices errors in the above, leave a comment. Thanks.
You can also use split('[name]'), and work on the sub-string, this way you'll be sure (see note below) of don't change '[name]'.
You may have problem splitting on every [name] occurance catching some substring of some longer name, but if you:
Use real and common names in capital letter (like Jonh).
Avoid the use of similar names.
Then the following code should work fine:
def typo(string):
index = random.randint(1,len(string)-1) # don't change first or last
return string[:index] + random.choice(characters) + string[index:]
def generate(string, n, safe_name):
sub_strings = string.split(safe_name)
while n:
sub_index = random.randint(0,len(sub_strings) - 1)
sub = sub_strings[sub_index]
if len(sub) <= 2: # if too short don't change
continue
sub_strings[sub_index] = typo(sub)
n -= 1
return safe_name.join(sub_strings)
Example adding 3 new random charachter:
>>> string = 'Hello how are you today, Alice?'
>>> generate(string, 3, 'Alice')
'Hellov howj are yoiu today, Alice?'
With the name occurring more then one time:
>>> string = 'Hello Alice, how are you today, Alice?'
>>> generate(string, 3, 'Alice')
'Hello Alice, hoiw arfe you todayq, Alice?'
For the example you have given, it looks like we can split it on the comma and put the typo(s) in the first part of the string.
If this is correct, you need to do three things randomly before generating the typo:
Choose which character to do the typo on or near
Choose the typo character
Choose one of three actions - Replace, Prefix, Append
Would this fit the bill?
(BTW, since you are familiar with random, I didn't give any code.)
You are "hoping there is a better way of going about it". Well, here's some suggestions, and some code demonstrating those suggestions. Some of the suggestions are about making the code more pythonic or easy to read, not just the mechanics of changing strings.
Use module re for regular expressions to detect "[name]". This will pay dividends as soon as you have more keywords than just this one.
for x in string.rsplit(" ") is a more pythonic way to loop through the words.
Get real-numbered randoms, and compare to a probability setting in range 0.0-1.0. More flexible than getting integers 0,1.
Use x[:rnd] + ... + x[rnd:x] as recommended by others, for easier string manipulation.
Use x if condition else y for a concise choice between alternatives, in this case between an index which causes overwriting and an index which causes insertion.
Your example output shows a typo inserted in "you", but your example code only inserts typos if len(x) > 3. I follow your code, but this is easy to change.
Hope this helps.
import random
import re
string = 'Hello how are you today, [name]?'
characters = 'qwertyuioplkjhgfdsazxcvbnm,. '
words = []
for x in string.rsplit(" "):
if None == re.search('[^\]]*\[[a-z]+\].*', x) \
and len(x) > 3 and random.random()<=0.5:
# rnd: index of char to overwrite or insert before
rnd = random.randint(2,len(x)-2)
# rnd1: index of 1st char after modification
# random.random <= 0.x is probability of overwriting instead of inserting
rnd1 = rnd + 1 if random.random() <= 0.5 else 0
x = x[:rnd] + random.choice(characters) + x[rnd1:]
words.append(x)
typos = " ".join(words)
print typos
Update: fixed indentation mistake in the code.
Update 2: made the code for choosing overwrite versus insert to be more concise.
Related
I am trying to take a string from user input like ("what is 1 + 1") separate the letters from numbers and then add the 2 numbers. I have tried the below code buti still cant fiqure out how to add the two 1s.
def splitString(str):
alpha = ""
num = ""
special = ""
for i in range(len(str)):
if(str[i].isdigit()):
num = num+ str[i]
elif((str[i] >= 'A' and str[i] <= 'Z') or
(str[i] >= 'a' and str[i] <= 'z')):
alpha += str[i]
else:
special += str[i]
print(alpha)
print(num )
print(special)
if name == "main":
str = "what is 1 + 1"
splitString(str)
my_str = "what is 1 + 1"
x = []
str_list = my_str.split(" ")
for i in str_list:
if i.isDigit():
x.append(int(i))
sum_integers = 0
for i in x:
sum_integers += i
print(sum_integers)
try this, I did not run it.
The ".split" function will simplify your code a lot.
I encourage you to look at this link https://www.w3schools.com/python/ref_string_split.asp
I might suggest using itertools.groupby as a starting point, e.g.:
>>> import itertools
>>> def split_alpha_num(s: str) -> list[str]:
... return ["".join(g).strip() for _, g in itertools.groupby(s, lambda s:(s.isalpha(), s.isdecimal()))]
...
>>> split_alpha_num("what is 1 + 1")
['what', '', 'is', '', '1', '+', '1']
From there, you could iterate over the string, discard elements that aren't either numbers or mathematical operators, and try to do something with what remains. The exact approach depends on the complexity of the inputs that you want your code to be able to handle; solving this type of problem in the very general case is not trivial.
you should make the variable num a list, and then add to that list all of the numbers
like that
def splitString(str):
alpha = ""
num = []
special = ""
for i in range(len(str)):
if(str[i].isdigit()):
num.append(int(str[i]))
elif((str[i] >= 'A' and str[i] <= 'Z') or
(str[i] >= 'a' and str[i] <= 'z')):
alpha += str[i]
else:
special += str[i]
print(alpha)
print(num)
print(special)
that way later you could use the special variable to determine what to do with thous numbers
My goal is to write a function which change every even letter into upper letter and odd to lower (space also count as a one element).
This is my code
def to_weird_case(s):
for i in s:
if len(i) % 2 == 0:
s[i] = i.upper() + s(i+1)
else:
s[i] = i.lower() + s(i+2)
return i
I think it should be quite correct, but it gives me error.
line 7, in to_weird_case
s[i] = i.lower() + s(str(i)+2)
TypeError: must be str, not int
EDIT:
I have a sugesstion but I don't know how to make it. I try it for myself and back here.
This needs to definitly explicietly state that the zero indexing uppercase is for each word.
Do you know guys how to make it?
So we can analyze your code and just explain what you typed:
def to_weird_case(s):
for i in s: # s is your string, and i is the actual character
if len(i) % 2 == 0: # if your length of the character can be divided by 2. Hmm this is weird
s[i] = i.upper() + s(i+1) # s[i] change a character in the string but you should provide an index (i) so an integer and not a character. But this is not supported in Python.
else:
s[i] = i.lower() + s(i+2)
return i # This will exit after first iteraction, so to_weird_case("this") will return "t".
So what you need to is first create a output string and fill that. And when iteration over s, you want the index of the char and the char value itself.
def to_weird_case(s):
output = ""
for i, myChar in enumerate(s):
if i % 2 == 0:
output += myChar.upper()
else:
output += myChar.lower()
return output
my_sentence = "abcdef"
print(to_weird_case(my_sentence))
And when you want to ignore spaces, you need to keep track of actual characters (excluding spaces)
def to_weird_case(s):
output = ""
count = 0
for myChar in s:
if myChar.isspace():
output += myChar
else:
if count % 2 == 0:
output += myChar.upper()
else:
output += myChar.lower()
count += 1
return output
my_sentence = "abc def"
print(to_weird_case(my_sentence))
Test this yourself
def to_weird_case(s):
for i in s:
print (i)
After doing this you will find that i gives you characters.
if len(i) % 2 == 0:
This line is incorrect as you are trying to find the length of a single character. len(s) would be much better.
So the code will be like
def to_weird_case(s):
s2 = "" #We create another string as strings are immutable in python
for i in range(len(s)):
if i % 2 == 0:
s2 = s2 + s[i].upper()
else:
s2 = s2 + s[i].lower()
return s2
From #RvdK analysis, you'ld have seen where corrections are needed. In addition to what has been pointed out, I want you to note that s[i] will work fine only if i is an integer, but in your case where (by assumption) i is a string you'll encounter several TypeErrors. From my understanding of what you want to do, it should go this way:
def to_weird_case(s):
for i in s:
if s.index(i) % 2 == 0:
s[s.index(i)] = i.upper() + s[s.index(i)]
elif s.index(i) % 2 == 1:
s[s.index(i)] = i.lower() + s[s.index(i)]
return i # or possibly return s
It is possible to do in a single line using a list comprehension
def funny_case(s):
return "".join([c.upper() if idx%2==0 else c.lower() for idx,c in enumerate(s)])
If you want to treat each word separately then you can split it up in to a list of words and "funny case" each word individually, see below code
original = "hello world"
def funny_case(s):
return "".join([c.upper() if idx%2==0 else c.lower() for idx,c in enumerate(s) ])
def funny_case_by_word(s):
return " ".join((funny_case(word) for word in s.split()))
print(funny_case_by_word(original))
Corrected code is as follows
def case(s):
txt=''
for i in range(len(s)):
if i%2==0:
txt+=s[i].upper()
else:
txt+=s[i].lower()
return txt
String assignment gives error in Python therefore i recommend considering my approach
When looping over elements of s, you get the letter itself, not its index. You can use enumerate to get both index and letter.
def to_weird_case(s):
result = ''
for index, letter in enumerate(s):
if index % 2 == 0:
result += letter.upper()
else:
result += letter.lower()
return result
correct code:
def to_weird_case(s):
str2 = ""
s.split() # through splitting string is converted to list as it is easy to traverse through list
for i in range(0,len(s)):
n = s[i] # storing value in n
if(i % 2 == 0):
str2 = str2 + n.upper()
else:
str2 = str2 + n.lower()
return str2
str1 = "hello world"
r = to_weird_case(str1)
print(r)
Problem statement:
Have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.
My Python program is:
def LetterChanges(str):
for i in range(0,len(str)):
a=ord(str[i])
if a==122:
str=str.replace(str[i],'a',1)
elif a==90:
str=str.replace(str[i],'a',1)
elif (a>=65 and a<=90) or (a>=97 and a<=122):
a=a+1
char=chr(a)
str=str.replace(str[i],char,1)
for i in range(0,len(str)):
if str[i]=='a':
str=str.replace(str[i],'A',1)
elif str[i]=='e':
str=str.replace(str[i],'E',1)
elif str[i]=='i':
str=str.replace(str[i],'I',1)
elif str[i]=='o':
str=str.replace(str[i],'O',1)
elif str[i]=='u':
str=str.replace(str[i],'U',1)
return(str)
print LetterChanges(raw_input())
The problem with my code is that, when I input sen, the output was tfo which was correct.
But when I gave sent as my input, I got the wrong output.
Your mistake is here :silpa
when you are replacing you have not cared about indices where character is replacing so how it is going when you give sent input
after replacing n we get string like tfot now in next iteration next letter you encounter in your original string is t so it will replace the first letter that is t in the replaced string so . "tfot" becomes "ufot" and last t is not being replaced
here an another try :
def prgrm(n):
k = ""
for i in n:
nxt = chr(97 if i == 'z' else ord(i)+1)
if nxt in ('a', 'e', 'i', 'o', 'u'):
nxt = nxt.capitalize()
k += nxt
print(k)
prgrm('sen')
A functional programming take, without using ord() or a loop, will work with presence of other non alphabetic characters I think:
def LetterChanges(str):
vowels = "aeiou"
lowers = "abcdefghijklmnopqrstuvwxyza"
all = lowers.upper() + lowers
# Map all alphabetical characters
nxt_str = "".join(map(lambda x: all[all.index(x) + 1] if x in all else x, str))
# Map the vowels
return "".join(map(lambda x: x.upper() if x in vowels else x, nxt_str))
print(LetterChanges("sentdZ"))
tfOUEA
You are processing the 't' twice in your string, first the s is replaced by 't' and after that 't' is replaced again by 'u' also the first replacement in the string
def LetterChanges(line):
result = ""
for i in line:
a=ord(i)
if a == 122 or a == 90:
result += 'A'
elif (a >= 65 and a <= 90) or (a >= 97 and a <= 122):
a = a + 1
char = chr(a)
if char in ('e', 'i', 'o', 'u'):
char = char.upper()
result += char
else:
result += i
return(result)
Here is another way with using re:
import re
def letter_changes(my_string):
in_letters = "abcdefghijklmnopqrstuvxyz"
out_letters = "bcdefghijklmnopqrstuvxyza"
letter_dict1 = {x:y for x,y in zip(in_letters, out_letters)}
letter_dict2 = {'a':'A', 'e':'E', 'i':'I', 'o':'O', 'u':'U'}
for l_dict in [letter_dict1, letter_dict2]:
pattern = re.compile("|".join(l_dict.keys()))
my_string = pattern.sub(lambda m: l_dict[re.escape(m.group(0))], my_string)
return my_string
This is another way to write your code:-
def rep_cap(sent):
sent_rp = ''.join([chr(c) for c in [x+1 for x in [ord(c) for c in sent]]]).replace('{','a') #This thing is done to convert a to b .... z to a
final_output = ""
vowels = ['a','e','i','o','u']
for i in sent_rp:
if i in vowels:
final_output = final_output+i.upper() #convert vowels to upper case
else :
final_output = final_output+i
return final_output
sent = raw_input("Enter a sentence:")
print rep_cap(sent)
replace this line
str=str.replace(str[i],char,1) with
str = str[:i] + char + str[i+1:]
Reason for this problem answered by #Pankaj78691 is correct!!
Here is my version of the algorithm.
def LetterChanges(str):
result = ''
vowels = ['a','e','i','o','u']
for s in str:
if s.isalpha():
if ord(s) == 122: #ASCII code of 'z'
next_letter = chr(97)
else:
next_letter = chr(ord(s)+1)
if next_letter in vowels:
result += next_letter.upper()
else:
result += next_letter
else:
result += s
return result
Alright so I am making an encoder/decoder and currently I'm testing to see if my idea will work, but I keep getting the error telling me my string index is out of range when it shouldn't be going out of range in the first place.
message = "abc"
#Should come out as 212223
translated = ' '
n = 1
while n >= 0:
t = message[n]
if t == 'a':
translated = translated + '21'
elif t == 'b':
translated = translated + '22'
elif t == 'c':
translated = translated + '23'
while n <= len(message):
n = n + 1
print(translated)
It makes perfect sense to me so I had a hard time searching for appropriate help that solves for what I am doing, so can I have some help? A link, a solution, what I'm doing wrong and how to fix it? Thanks
n = 0
while n >= 0:
You have an infinite loop as you keep on incrementing n.
At some point message[n] will get out of range.
You should move the while n <= len(message): to be your main loop instead of your current one.
A better way will be to iterate directly over message with a for loop:
for t in message:
if t == 'a':
translated = translated + '21'
elif t == 'b':
translated = translated + '22'
elif t == 'c':
translated = translated + '23'
Here:
while n <= len(message):
n = n + 1
Should need changing to:
while n < len(message):
n = n + 1
The last index of a string will be len(message) - 1, as indexing starts at 0.
I would just set n to len(message) - 1 instantly instead however.
Because at last loop you are using t = message[3]..It is the cause of your error. If you message variable contain "abc" then you can access only t = message[0], t = message[1], t = message[2]. So try this
message = "abc"
#Should come out as 212223
translated = ' '
n = 1
while n >= 0:
t = message[n-1]
if t == 'a':
translated = translated + '21'
elif t == 'b':
translated = translated + '22'
elif t == 'c':
translated = translated + '23'
while n <= len(message):
n = n + 1
print(translated)
For some reason, this code doesn't work:
def pyglatin(word):
output = ""
wordlenedit = len(word)-1
wordlen = len(word)
fixer = 0
while fixer == 0:
for i in word:
if i == 'a' or i == 'e' or i == 'o' or i == 'i' or i == 'u':
fixer = 1
else:
wordlenedit -= 1
else:
output = word[wordlenedit:wordlen:1] + '-' + word[0:wordlenedit:1] + 'ay'
return output
To see the issues, click here. The problem appears to be that it's skipping the if statement that identifies vowels, but I'm not sure why. This results in some very odd outputs.
Your function does not work because you walk through the word, decrementing the splitting index for each consonant you encounter, starting at wordlenedit = len(word)-1.
At the end of your for loop, wordlenedit is equal to (length of the word) - 1 - (number of consonants). The function will only work if the first index of vowel in the word (starting at 0) is equal to the number of vowels - 1.
Also, the while loop is useless here, as you walk through the entire word in the for loop. It is even worse than that: the while loop will be an infinite loop if you have a word with no vowels (like "fly", as you don't check the "y")
This is a corrected version of your function, using the keyword break:
def pyglatin2(word):
output = ""
wordlenedit = 0
wordlen = len(word)
for l in word:
if l == 'a' or l == 'e' or l == 'o' or l == 'i' or l == 'u':
break
else:
wordlenedit += 1
output = word[wordlenedit:wordlen:1] + '-' + word[0:wordlenedit:1] + 'ay'
return output
However this function can be written in a much more concise/simple way, using Regular Expressions, like this:
import re
def pyglatin3(word):
# Get the first index of one of these: a, e, i, o, u
start = re.search("[aeiou]", word).start()
# Split the word
return word[start:] + "-" + word[0:start] + "ay"
If you want to do this without using regular expressions, the simplest way is to use enumerate:
def pyglatin(word):
for i, ch in enumerate(word):
if ch in 'aeiou':
return word[i:] + '-' + word[:i] + 'ay'