I've been working on this Palindrome program and am really close to completing it.Close to the point that it's driving me a bit crazy haha.
The program is supposed to check each 'phrase' to determine if it is a Palindrome or not and return a lowercase version with white space and punctuation removed if it is in fact a Palindrome. Otherwise, if not, it's supposed to return None.
I'm just having an issue with bringing my test data into the function. I can't seem to think of the correct way of dealing with it. It's probably pretty simple...Any ideas?
Thanks!
import string
def reverse(word):
newword = ''
letterflag = -1
for numoletter in word:
newword += word[letterflag]
letterflag -= 1
return newword
def Palindromize(phrase):
for punct in string.punctuation:
phrase= phrase.replace(punct,'')
phrase = str(phrase.lower())
firstindex = 0
secondindex = len(phrase) - 1
flag = 0
while firstindex != secondindex and firstindex < secondindex:
char1 = phrase[firstindex]
char2 = phrase[secondindex]
if char1 == char2:
flag += 1
else:
break
firstindex += 1
secondindex -= 1
if flag == len(phrase) // (2):
print phrase.strip()
else:
print None
def Main():
data = ['Murder for a jar of red rum',12321, 'nope', 'abcbA', 3443, 'what',
'Never odd or even', 'Rats live on no evil star']
for word in data:
word == word.split()
Palindromize(word)
if __name__ == '__main__':
Main()
Maybe this line is causing the problems.
for word in data:
word == word.split() # This line.
Palindromize(word)
You're testing for equality here, rather than reassigning the variable word which can be done using word = word.split(). word then becomes a list, and you might want to iterate over the list using
for elem in word:
Palindromize(elem)
Also, you seem to be calling the split method on int, which is not possible, try converting them to strings.
Also, why do you convert the phrase to lower case in the for loop, just doing it once will suffice.
At the "core" of your program, you could do much better in Python, using filter for example. Here is a quick demonstration:
>>> phrase = 'Murder for a jar of red rum!'
>>> normalized = filter(str.isalnum, phrase.lower())
>>> normalized
'murderforajarofredrum'
>>> reversed = normalized[-1::-1]
>>> reversed
'murderforajarofredrum'
# Test is it is a palindrome
>>> reversed == normalized
True
Before you go bananas, let's rethink the problem:
You have already pointed out that Palindromes only make sense in strings without punctuation, whitespace, or mixed case. Thus, you need to convert your input string, either by removing the unwanted characters or by picking the allowed ones. For the latter, one can imagine:
import string
clean_data = [ch for ch in original_data if ch in string.ascii_letters]
clean_data = ''.join(clean_data).lower()
Having the cleaned version of the input, one might consider the third parameter in slicing of strings, particularly when it's -1 ;)
Does a comparison like
if clean_data[::-1] == clean_data:
....
ring a bell?
One of the primary errors that i spotted is here:
for word in data:
word==word.split()
Here, there are two mistakes:
1. Double equals make no point here.
2. If you wish to split the contents of each iteration of data, then doing like this doesn't change the original list, since you are modifying the duplicate set called word. To achieve your list, do:
for i in range(data):
data[i]=data[i].split()
This may clear your errors
Related
I want to take a string as input and convert the even letters to uppercase and odd letters to lowercase. Is there any other way to do it?
def myfunc(word):
result = ''
index = 0
for letter in word:
if index % 2 ==0:
result += letter.lower()
else:
result +=letter.upper()
index += 1
return result
There's many more interesting ways to skin this cat, but here's an abbreviated one:
def myfunc(word):
action = {0: str.lower, 1: str.upper}
return ''.join([action[index % 2](letter)
for index, letter in enumerate(word)])
print(myfunc('hello world'))
Many, many different ways to do it but here's one you might like. It's a little more concise and one might argue a bit more 'Pythonic'.
def myfunc(word):
# Convert everything to lowercase
word = word.lower()
# Convert to list of individual letters
letters = list(word)
# Convert every other character starting from the 2nd
# to uppercase
for i in range(1, len(word) + 1, 2):
letters[i] = letters[i].upper()
# Convert back to string
return ''.join(letters)
It looks like there's a more pythonic solution provided, so I'd like to give a slightly less pythonic solution for variety.
def funnyCase(word):
i = 0
# convert everything to lowercase
word = word.lower()
# convert to a list of characters
letters = list(word)
# make it funny
for letter in letters:
# This is bitwise XOR. It's a different way to check even/odd.
if i^1==0:
letters[i] = letter.upper()
i++
return ''.join(letters)
I do hope you understand John Gordon's comment about multiple solutions. This is the beauty of programming - there is no one way to do things.
Edit - I messed up. Happens
I'm trying to find uppercase acronyms in a string. For example, if the input is "I need to see you ASAP, because YOLO, you know" should return ["ASAP", "YOLO"].
#!/usr/bin/env python3
import string
def acronyms(s):
s.translate(string.punctuation)
for i, x in enumerate(s):
while x.upper():
print(x)
i += 1
def main():
print(acronyms("""I need to see you ASAP, because YOLO, you know."""))
if __name__ == "__main__":
main()
I tried to get rid of the punctuations, then loop through the string, and while it is uppercase print the letter out. It resulted in an infinite loop. I wanted to solve this using string manipulation, so no RegEx
Edits:
changes in removing punctuations for efficiency
From:
exclude = set(string.punctuation)
s = "".join(ch for ch in s if ch not in exclude)
To:
s.translate(string.punctuation)
A couple things I'd like to point out. One, you have end up with a hanging program because you have a while True and not a single break. Then, you kind of make the enumerate pretty pointless when you do n+=1.
for i, x in enumerate(s):
n+=1
This can all be easily simplified, no enumerate needed.
def acronyms(s):
exclude = set(string.punctuation)
s = "".join(ch for ch in s if ch not in exclude)
acro = [x for x in s.split() if x.isupper()]
return acro
output
['I', 'ASAP', 'YOLO']
Sadly, we do have an extra I which happens not to be an acronym, so one fix could be to make sure x is never one letter before being appended.
acro = [x for x in s.split() if x.isupper() and len(x) != 1]
Your while-loop iterates over the first character, but never breaks out to go to the next character.
You also want to filter out the 'I' as a single letter is not usually classified as an acronym.
the string.isupper() function checks for an entire string rather than a single character, so I recommend you use that. It would look like this:
def acronyms(s):
words = s.split()
acronyms = []
for word in words:
if word.isupper() and len(word) > 1:
acronyms.append(word)
return acronyms
I would highly recommend using nltk for its wonderful tokenisation package, it handles edge cases and punctuation superbly.
For a simplified approach where you define an acronym as:
all characters are alphabetical
all characters are upper case
The following should suffice:
from nltk.tokenize import word_tokenize
def get_acronyms(text):
return [
token for token in word_tokenize(text)
if token.isupper()
]
here this should work:
def acronyms(x):
ans = []
y = x.split(" ")
for i in y:
if i.isupper():
ans += [i]
return ans
isupper() returns True as long as there are no lowercase even if there is punctuation
I am trying to compare two strings and add one of the strings to a list if they are almost equal (differ by a single letter). What would be the fastest way to do this as my set of words is over 90k and doing this often takes too long?
EDIT: one of the words (comparison_word in code below) does not change.
EDIT2: the words must be of equal length
This is my current code:
for word in set_of_words:
amount = 0
if len(word) == len(comparison_word):
for i in range(len(word)):
if comparison_word[i] != word[i]:
amount += 1
if amount == 1:
list_of_words.append(word)
return list_of_words
You might find zip is a more efficient than indexing:
def almost_equal(set_of_words,comp):
ln = len(comp)
for word in set_of_words:
count = 0
if len(word) == ln:
for a, b in zip(word, comp):
count += a != b
if count == 2:
break
else:
yield word
Demo:
In [5]: list(almost_equal(["foo","bar","foob","foe"],"foa"))
Out[5]: ['foo', 'foe']
The following searches my dictionary of 61K words in about 25 msec.
import re
def search(word, text):
ws = [r'\b{}[^{}]{}\b'.format(w[:i],w[i],w[i+1:]) for i in range(len(word))]
for mo in re.finditer('|'.join(ws), text):
yield mo.group()
with open("/12dicts/5desk.txt") as f:
text = f.read()
for hit in search('zealoos', text):
print(hit) #prints zealous
Presuming that the list of strings is in a file, one string per line, read it in as one long string and use a regular expression to search the string for matches.
search() takes a word like 'what' and turns it into a regular expression like this:
\b[^w]hat\b|\bw[^h]at\b|\bwh[^a]t\b|\bwha[^t]\b
And then scans all the words and find all the near misses--at C-speed.
The idea is to reduce the amount of work being done:
n_comparison_word = len(comparison_word)
for word in set_of_words:
amount = 0
n_word = len(word)
if n_word != n_comparison_word:
continue
for i in range(n_word):
if comparison_word[i] != word[i]:
amount += 1
if amount == 2:
break
if amount == 1:
list_of_words.append(word)
return list_of_words
Some notes:
The value of len(comparison_word) needs to be computed only once (ever).
The value of len(word) needs to computed once (per iteration of the loop).
You know you can stop looking at a word when amount reaches the value 2 (or more - in any case that word can no longer be part of the result).
It may be worth reading this part of the Python documentation regarding the continue and break statements which are both used in the code.
Haven't done exhaustive testing, but if comparison_word is not too long (fewer than 6 letters), and your set_of_words can change, then it might be worth it to compute all acceptable words, store those in a set, a simply iterate through set_of_words and test for word in acceptable_words.
If not, here's my take on your code:
for word in set_of_words:
different_letter_exists = False
length = len(word)
if length == len(comparison_word):
for i, letter in enumerate(word):
if letter != comparison_word[i]:
if different_letter_exists:
break
else:
different_letter_exists = True
if i == length:
list_of_words.append(word)
Essentially: for every word, once you encounter an different letter, different_letter_exists is set to True. If you encounter it again, you break out of the loop. A new word is only added if i == length, which only happens if enumerate gets all the way to the end, which only happens if only one different letter exists.
Good luck :)
I have to enter a string, remove all spaces and print the string without vowels. I also have to print a string of all the removed vowels.
I have gotten very close to this goal, but for some reason when I try to remove all the vowels it will not remove two vowels in a row. Why is this? Please give answers for this specific block of code, as solutions have helped me solve the challenge but not my specific problem
# first define our function
def disemvowel(words):
# separate the sentence into separate letters in a list
no_v = list(words.lower().replace(" ", ""))
print no_v
# create an empty list for all vowels
v = []
# assign the number 0 to a
a = 0
for l in no_v:
# if a letter in the list is a vowel:
if l == "a" or l == "e" or l == "i" or l == "o" or l == "u":
# add it to the vowel list
v.append(l)
#print v
# delete it from the original list with a
del no_v[a]
print no_v
# increment a by 1, in order to keep a's position in the list moving
else:
a += 1
# print both lists with all spaces removed, joined together
print "".join(no_v)
print "".join(v)
disemvowel(raw_input(""))
Mistakes
So there are a lot of other, and perhaps better approaches to solve this problem. But as you mentioned I just discuss your failures or what you can do better.
1. Make a list of input word
There are a lot of thins you could do better
no_v = list(words.lower().replace(" ", ""))
You don't replaces all spaces cause of " " -> " " so just use this instead
no_v = list(words.lower().translate( None, string.whitespace))
2. Replace for loop with while loop
Because if you delete an element of the list the for l in no_v: will go to the next position. But because of the deletion you need the same position, to remove all the vowels in no_v and put them in v.
while a < len(no_v):
l = no_v[a]
3. Return the values
Cause it's a function don't print the values just return them. In this case replace the print no_v print v and just return and print them.
return (no_v,v) # returning both lists as tuple
4. Not a mistake but be prepared for python 3.x
Just try to use always print("Have a nice day") instead of print "Have a nice day"
Your Algorithm without the mistakes
Your algorithm now looks like this
import string
def disemvowel(words):
no_v = list(words.lower().translate( None, string.whitespace))
v = []
a = 0
while a < len(no_v):
l = no_v[a]
if l == "a" or l == "e" or l == "i" or l == "o" or l == "u":
v.append(l)
del no_v[a]
else:
a += 1
return ("".join(no_v),"".join(v))
print(disemvowel("Stackoverflow is cool !"))
Output
For the sentence Stackoverflow is cool !\n it outputs
('stckvrflwscl!', 'aoeoioo')
How I would do this in python
Not asked but I give you a solution I would probably use. Cause it has something to do with string replacement, or matching I would just use regex.
def myDisemvowel(words):
words = words.lower().translate( None, string.whitespace)
nv = re.sub("[aeiou]*","", words)
v = re.sub("[^a^e^i^o^u]*","", words)
return (nv, v)
print(myDisemvowel("Stackoverflow is cool !\n"))
I use just a regular expression and for the nv string I just replace all voewls with and empty string. For the vowel string I just replace the group of all non vowels with an empty string. If you write this compact, you could solve this with 2 lines of code (Just returning the replacement)
Output
For the sentence Stackoverflow is cool !\n it outputs
('stckvrflwscl!', 'aoeoioo')
You are modifying no_v while iterating through it. It'd be a lot simpler just to make two new lists, one with vowels and one without.
Another option is to convert it to a while loop:
while a < len(no_v):
l = no_v[a]
This way you have just a single variable tracking your place in no_v instead of the two you currently have.
For educational purposes, this all can be made significantly less cumbersome.
def devowel(input_str, vowels="aeiou"):
filtered_chars = [char for char in input_str
if char.lower() not in vowels and not char.isspace()]
return ''.join(filtered_chars)
assert devowel('big BOOM') == 'bgBM'
To help you learn, do the following:
Define a function that returns True if a particular character has to be removed.
Using that function, loop through the characters of the input string and only leave eligible characters.
In the above, avoid using indexes and len(), instead iterate over characters, as in for char in input_str:.
Learn about list comprehensions.
(Bonus points:) Read about the filter function.
I am trying to count the number of times 'e' appears in a word.
def has_no_e(word): #counts 'e's in a word
letters = len(word)
count = 0
while letters >= 0:
if word[letters-1] == 'e':
count = count + 1
letters = letters - 1
print count
It seems to work fine except when the word ends with an 'e'. It will count that 'e' twice. I have no idea why. Any help?
I know my code may be sloppy, I'm a beginner! I'm just trying to figure out the logic behind what's happening.
>>> word = 'eeeooooohoooooeee'
>>> word.count('e')
6
Why not this?
As others mention, you can implement the test with a simple word.count('e'). Unless you're doing this as a simple exercise, this is far better than trying to reinvent the wheel.
The problem with your code is that it counts the last character twice because you are testing index -1 at the end, which in Python returns the last character in the string. Fix it by changing while letters >= 0 to while letters > 0.
There are other ways you can tidy up your code (assuming this is an exercise in learning):
Python provides a nice way of iterating over a string using a for loop. This is far more concise and easier to read than using a while loop and maintaining your own counter variable. As you've already seen here, adding complexity results in bugs. Keep it simple.
Most languages provide a += operator, which for integers adds the amount to a variable. It's more concise than count = count + 1.
Use a parameter to define which character you're counting to make it more flexible. Define a default argument for using char='e' in the parameter list when you have an obvious default.
Choose a more appropriate name for the function. The name has_no_e() makes the reader think the code checks to see if the code has no e, but what it actually does is counts the occurrences of e.
Putting this all together we get:
def count_letter(word, char='e'):
count = 0
for c in word:
if c == char:
count += 1
return count
Some tests:
>>> count_letter('tee')
2
>>> count_letter('tee', 't')
1
>>> count_letter('tee', 'f')
0
>>> count_letter('wh' + 'e'*100)
100
Why not simply
def has_no_e(word):
return sum(1 for letter in word if letter=="e")
The problem is that the last value of 'letters' in your iteration is '0', and when this happens you look at:
word[letters-1]
meaning, you look at word[-1], which in python means "last letter of the word".
so you're actually counting correctly, and adding a "bonus" one if the last letter is 'e'.
It will count it twice when ending with an e because you decrement letters one time too many (because you loop while letters >= 0 and you should be looping while letters > 0). When letters reaches zero you check word[letters-1] == word[-1] which corresponds to the last character in the word.
Many of these suggested solutions will work fine.
Know that, in Python, list[-1] will return the last element of the list.
So, in your original code, when you were referencing word[letters-1] in a while loop constrained by letters >= 0, you would count the 'e' on the end of the word twice (once when letters was the length-1 and a second time when letters was 0).
For example, if my word was "Pete" your code trace would look like this (if you printed out word[letter] each loop.
e (for word[3])
t (for word[2])
e (for word[1])
P (for word[0])
e (for word[-1])
Hope this helps to clear things up and to reveal an interesting little quirk about Python.
#marcog makes some excellent points;
in the meantime, you can do simple debugging by inserting print statements -
def has_no_e(word):
letters = len(word)
count = 0
while letters >= 0:
ch = word[letters-1] # what is it looking at?
if ch == 'e':
count = count + 1
print('{0} <-'.format(ch))
else:
print('{0}'.format(ch))
letters = letters - 1
print count
then
has_no_e('tease')
returns
e <-
s
a
e <-
t
e <-
3
from which you can see that
you are going through the string in reverse order
it is correctly recognizing e's
you are 'wrapping around' to the end of the string - hence the extra e if your string ends in one
If what you really want is 'has_no_e' then the following may be more appropriate than counting 'e's and then later checking for zero,
def has_no_e(word):
return 'e' not in word
>>> has_no_e('Adrian')
True
>>> has_no_e('test')
False
>>> has_no_e('NYSE')
True
If you want to check there are no 'E's either,
def has_no_e(word):
return 'e' not in word.lower()
>>> has_no_e('NYSE')
False
You don't have to use a while-loop. Strings can be used for-loops in Python.
def has_no_e(word):
count = 0
for letter in word:
if letter == "e":
count += 1
print count
or something simpler:
def has_no_e(word):
return sum(1 for letter in word if letter=="e")