How to use the regular expression to make the Pig Latin game? - python-2.7

I am trying to get a single match for the first consonant or consonant cluster in an input. Then the program should move the consonant to the beginning of the word and add "ay" at the end.
Here is my code
import re
consonants = [ 'bl', 'cl', 'fl', 'gl', 'pl', 'sl', 'br', 'cr', 'dr', 'fr', 'gr','pr', 'tr', 'sc', 'sk', 'sm', 'sn', 'sp', 'st', 'sw', 'tw','b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
word1 = str(input("Type something"))
word2 = re.split(r'[b-df-hj-np-tv-z]' or '[bl]''[cl]''[fl]', word1)
if any(consonants in word2 for consonants in consonants):
print(word2[1] + word2[0] + word2[2] + "ay")
The output does not appear in the interactive console.

Right, Python does not do "magic"; or is a well-defined operator which takes two boolean expressions and produces a boolean expression, not something which magically combines two regular expression strings into a new regular expression string. (You have to remember that you're talking to a computer, and computers are very stupid!)
To do the pig latin game you'll probably want to just gather a substring of non-vowels and then check whether it's 0-length (starts with a vowel) or not.

Just solved the program.
import re
words1 = input("Input Sentence:")
b1 = re.search(r"([^aeoiu]*)([aeoiu]*)([^aeoiu]*)([aeoiu]*)([^aeoiu]*)", words1)
b2 = b1.group(1)
b3 = b1.group(2)
b4 = b1.group(3)
b5 = b1.group(4)
b6 = b1.group(5)
if b5 != 5:
print(b3 + b4 + b5 + b6 + b2 + "ay")

Related

How to separate a string from another string and turn it into a comma separated list

If my string is-
''Felix Underhalm'' and I want to turn into...
list = ['F', 'E', 'L', 'I', 'X']
How should I do it?

Why my RegexTokenizer transformation in PySpark gives me the opposite of the required pattern?

When I use the RegexTokenizer from pyspark.ml.feature to tokenize sentences column in my dataframe to find all the word characters, I get the opposite of what I would get when the python re package is used for the same sentence. Here is the sample code:
from pyspark.sql import SparkSession
from pyspark.ml.feature import RegexTokenizer
spark = SparkSession.builder \
.master("local") \
.appName("Word list") \
.getOrCreate()
df = spark.createDataFrame(data = [["Hi there, I have a question about RegexTokenizer, Could you
please help me..."]], schema = ["Sentence"])
regexTokenizer = RegexTokenizer(inputCol="Sentence", outputCol="letters", pattern="\\w")
df = regexTokenizer.transform(df)
df.first()['letters']
This gives the following output:
[' ', ', ', ' ', ' ', ' ', ' ', ' ', ', ', ' ', ' ', ' ', ' ', '...']
On the other hand if I use the re module on the same sentence and use the same pattern to match the letters, using this code here:
import re
sentence = "Hi there, I have a question about RegexTokenizer, could you
please help me..."
letters_list = re.findall("\\w", sentence)
print(letters_list)
I get the desired output as per the regular expression pattern as:
['H', 'i', 't', 'h', 'e', 'r', 'e', 'I', 'h', 'a', 'v', 'e', 'a',
'q', 'u', 'e', 's', 't', 'i', 'o', 'n', 'a', 'b', 'o', 'u', 't',
'R', 'e', 'g', 'e', 'x', 'T', 'o', 'k', 'e', 'n', 'i', 'z', 'e',
'r', 'c', 'o', 'u', 'l', 'd', 'y', 'o', 'u', 'p', 'l', 'e', 'a',
's', 'e', 'h', 'e', 'l', 'p', 'm', 'e']
I also found that I need to use \W instead of \w in pySpark to solve this problem. Why is this difference? Or have I misunderstood the usage of pattern argument in RegexTokenizer?
From what the documentation on RegexTokenizer says, on creation it has a parameter called gaps. In one mode, the regexp matches gaps (true and is the default), in other it matches tokens (not the gaps, false).
Try setting it manually to the value you need: in your case, gaps = false.

How to remove all consonants and print vowels in a list

Here is my code:
#Alphabet class
class Alphabet(object):
def __init__(self, s):
self.s = s
def __str__(self):
return "Before: " + str(self.s)
#Define your Vowels class here
class Vowels:
def __init__(self,vowelList):
self.vowelList = vowelList
def __str__(self):
return "Invoking the method in Vowels by passing the Alphabet object\nAfter: " + str(vowelList)
def addVowels(self,a_obj):
for letter in a_obj:
if letter in 'aeiou':
vowelList.append(letter)
l = ','.join(vowelList)
a1 = Alphabet('A,B,C,E,I')
print a1
b = Vowels(a1)
b.addVowels(a1)
print (a2)
Right now, all it is printing is "Before: A,B,C,E,I", but I am trying to take a string of letters separated by commas (i.e. a_obj), extract the vowels from the string, then append the result to a list. I have looked at other answers regarding finding and printing only the vowels, which is why I have the for loop and if statement in addVowels, but no luck. Just to note,Vowels is supposed to be a container class for Alphabet.
When trying to get the output...the below code gives me
a1 = Alphabet('A,B,C,E,I')
print a1
a2 = Vowels(a1)
print a2
ouput:
Before: A,B,C,E,I
Invoking the method in Vowels by passing the Alphabet object
After: []
it seems like it isn't passing the letters from Alphabet...
You can create the list and get rid of the commas in one line by using split.
>>> "a,b,c,d,e,f,g,h,i,j".split(",")
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
From there you can remove the consonants by only keeping the vowels.
You can use a for loop:
letterList = ['a', 'b', 'c', 'd']
vowelList = []
for letter in letterList:
if letter in 'aeiou':
vowelList.append(letter)
Or you can use list comprehension:
letterList = ['a', 'b', 'c', 'd']
vowelList = [letter for letter in letterList if letter in 'aeiou']
Example of how this would work for your code:
class Vowels(object):
def __init__(self, vowelList):
self.vowelList = vowelList
lettersList = self.vowelList.s.split(",")
self.vowelList = [letter for letter in self.lettersList if letter in 'aeiou']
I'm using this code, and it works for me.
def getVowels(text):
vowel_letters = []
vowel_list = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U',]
for vowels in text:
if vowels in vowel_list:
vowel_letters.append(vowels)
return vowel_letters
print(getVowels('Hi, How are you today!'))
## Output: ['i', 'o', 'a', 'e', 'o', 'u', 'o', 'a']

MATLAB Loop to Randomly Modify String Characters

I’ve started programming in MATLAB recently and tried implementing a 1337 speaker-like generator/string manipulator just for fun. As a challenge, I’m trying to randomly change the occurrence of each character so that not all “a” are changed to “#“.
It appears that my attempt works to some extend since it's very arbitrary (yet sometimes ineffective), but I believe there’s a better way of accomplishing this. Perhaps to add more alternatives for each of the 26 characters and randomly select from them, respectively?
function O = obfuscate(s)
str = 'hello world';
for i=1:length(str)
randomNum = randi(26,1);
switch randomNum
case 1
str = regexprep(str, 'a', '#', 'once');
case 2
str = regexprep(str, 'b', 'l3', 'once');
case 3
str = regexprep(str, 'c', '<', 'once');
case 4
str = regexprep(str, 'd', '|]', 'once');
case 5
str = regexprep(str, 'e', '3', 'once');
case 6
str = regexprep(str, 'f', '|#', 'once');
case 7
str = regexprep(str, 'g', '6', 'once');
case 8
str = regexprep(str, 'h', '|-|', 'once');
case 9
str = regexprep(str, 'i', '!', 'once');
case 10
str = regexprep(str, 'j', '_/', 'once');
case 11
str = regexprep(str, 'k', '|{', 'once');
case 12
str = regexprep(str, 'l', '1', 'once');
case 13
str = regexprep(str, 'm', '|\/|', 'once');
case 14
str = regexprep(str, 'n', '/\/', 'once');
case 15
str = regexprep(str, 'o', '[]', 'once');
case 16
str = regexprep(str, 'p', '|*', 'once');
case 17
str = regexprep(str, 'q', '9', 'once');
case 18
str = regexprep(str, 'r', '|2', 'once');
case 19
str = regexprep(str, 's', '$', 'once');
case 20
str = regexprep(str, 't', '+', 'once');
case 21
str = regexprep(str, 'u', '|_|', 'once');
case 22
str = regexprep(str, 'v', '\/', 'once');
case 23
str = regexprep(str, 'w', '\X/', 'once');
case 24
str = regexprep(str, 'x', '%', 'once');
case 25
str = regexprep(str, 'y', '¥', 'once');
case 26
str = regexprep(str, 'z', '2', 'once');
end
O = str;
%fprintf('%s(%i)', str(i), randomNum);
end
Any suggestions?
You are generating a random number between 1 and 26 but only whilst looping through the string. In your example you would only generate 11 random numbers so it is possible to never get an obfuscation whilst looping through the string.
This explains why it would be 'sometimes ineffective' as you described.
For example, in one case your random number generator could generate 10 2's in a row which would only ever change the character 'b'.
You don't have any b's in your example so none of the text will be obfuscated.
You might have more success using a map container (search for containers.map in the Matlab help) to map each character to an obfuscation (to replace your switch case logic) instead of leaving this up to probability.
You could then generate a uniform random number between e.g. 1 and 2 in each loop iteration to determine whether the character should be obfuscated.
Example:
function O = obfuscate(s)
str = 'hello world';
keys ={'h','e','l','o','w','r','d'};
values = {'|-|','3','1','[]','\X/','|2','|]'};
obfuscationMap = containers.Map(keys,values);
tempCell = {} %cell to hold the growing string
for i = 1:length(str)
randomNum = randi(2,1);
switch randomNum
case(1) %obfuscate
if(isKey(obfuscationMap, str(i)) %check key is in map to avoid missing chars
tempCell{end+1} = obfuscationMap(str(i));
else
tempCell{end+1} = str(i);
end
case(2) %don't obfuscate
tempCell{end+1} = str(i);
end
end
O = strjoin(tempStr,''); %combine cell in single string without spaces
With the above code, when randomNum is a 1, you'll get an obfuscation, if randomNum is a 2 then you won't.
So you should get obfuscations a bit more often than you were seeing previously.
You may need to modify a bit to get exactly what you want.

How do i check conditions with a list in python 3

My name is Edwin i am new to programming but i love to learn. I have a assignment for school and i must build a program that rates passwords. but i have a litle problem now. As you can see i made 3 lists with every character. If i run this program it will not show "uw wachtwoord is Sterk" if the conditions klein and groot and nummers and symbols are true. how do i fix this?
btw i can't make use of isdigit,isnumeric and such.
thank you in advance!
print ("Check of uw wachtwoord veilig genoeg is in dit programma.")
print ("Uw wachtwoord moet tussen minimaal 6 en maximaal 12 karakters
bestaan")
print ("U kunt gebruik maken van hoofdletters,getallen en symbolen
(#,#,$,%)")
ww = input("Voer uw wachtwoord in: ")
klein = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm','n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
groot = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
nummers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
symbolen= [' ', '!', '#', '$', '%', '&', '"', '(', ')', '*', '+', ',', '-',
'.', '/', ':', ';', '<', '=', '>', '?', '#', '[', '\\', ']', '^', '_', '`',
'{', '|', '}', '~',"'"]
if len(ww) < 6:
print ("uw wachtwoord is te kort, uw wachtwoord moet uit minimaal 6 en
maximaal 12 karakters bestaan!")
elif len(ww) > 12:
print ("uw wachtwoord is te lang, uw wachtwoord moet uit minimaal 6 en
maximaal 12 karakters bestaan!")
elif len(ww) >= 6 and len(ww)<= 12:
if ww == klein and ww == groot and ww == nummers and ww == symbolen:
print ("uw wachtwoord is Sterk")
Your test cannot work because you're comparing your password (of type str: string) against a list. Since objects are non-comparable, the result is just False (even if they were comparable there is no equality here, but a non-empty intersection to check)
You need to check for each list that there's at least 1 member of the list in the password
Define an aux function which checks if a letter of the list is in the password (a lambda would be too much maybe) using any:
def ok(passwd,l):
return any(k in passwd for k in l)
Then test all your four lists against this condition using all:
elif len(ww) >= 6 and len(ww)<= 12:
sww = set(ww)
if all(ok(sww,l) for l in [klein,groot,nummers,symbolen]):
print ("uw wachtwoord is Sterk")
Note the slight optimization by converting the password (which is kind of a list so O(n) for in operator) by a set of characters (where the in operator exists but is way faster). Besides, a set will remove duplicate characters, which is perfect for this example.
More compact version without the aux function and using a lambda which is not so difficult to understand after all:
elif len(ww) >= 6 and len(ww)<= 12:
sww = set(ww)
if all(lambda l: any(k in sww for k in l) for l in [klein,groot,nummers,symbolen]):
print ("uw wachtwoord is Sterk")