I'm trying to create a function to evaluate if contains exactly one of each of the 5 vowels.
I've tried so far: (I don't know how can I limit now for only 1 of each vowel)
def isVowel(char): #=> Helper function
return len(char) == 1 and char.lower() in 'aeiou'
def fiveVowelsOneOfEach(word):
oneVowelOfEachCounter = 0
for char in word:
if isVowel(char):
if char == 'a':
oneVowelOfEachCounter += 1
if char == 'e':
oneVowelOfEachCounter += 1
if char == 'i':
oneVowelOfEachCounter += 1
if char == 'o':
oneVowelOfEachCounter += 1
if char == 'u':
oneVowelOfEachCounter += 1
if oneVowelOfEachCounter == 5:
return True
Just do:
s = 'aeiou'
s2 = 'aaeiou'
def checker(s):
return all(s.lower().count(i)==1 for i in 'aeiou')
print(checker(s))
print(checker(s2))
Output:
True
False
That's all needed, checking if the counts of each vowel is one
A simpler way of doing it
goodstr = 'aeiou'
badstr = 'aaeiou'
vowels = ['a','e','i','o','u'] # or a string "aeiou"
def fiveVowelsIsOneOfEach(word):
for vowel in vowels:
if word.count(vowel) != 1:
return False
return True
print(fiveVowelsIsOneOfEach(goodstr)) # returns True
print(fiveVowelsIsOneOfEach(badstr)) # returns False
def fiveVowelsOneOfEach(word):
oneVowelOfEachCounter = 0
vowels = list('aeiou')
dummy_vowels = list(chars)
for char in word:
if char in vowels:
vowels.remove(char)
continue
elif char in dummy_vowels:
return False
if len(vowels) == 0:
return True
return False
This should work. the trick is to use a list and a dummy list to make sure each vowel is only counted once.
Updated answer to address the issues in the question. Also added a version that does not use counter.
from collections import Counter
word = 'aaeiuo'
vowels = 'aeiuo'
c = Counter(word)
vowel_count = sum([1 if c[vowel] > 0 else 0 for vowel in vowels])
print(vowel_count) # 5
#without using counter
vowel_count = sum([1 if word.count(vowel) > 0 else 0 for vowel in vowels])
print(vowel_count) # 5
Related
How do i get these output?
def pair(str):
count = 0
for ch in str:
if ch == 'HLL':
return "Alice"
if ch == 'EO':
return "Bob"
if ch == "WORL":
return "Alice"
if ch == "D":
return "Bob"
else:
return "Hello World"
print(count)
return is executed before print, which means this method ends before print. print is not reachable in your code.
I don't know that you want to get data type of these output.
So, I guess 2 data types and write them here.
String
def pair(str):
count = 0
result = ''
for ch in str:
count += 1
if ch == 'HLL':
result += "Alice"
if ch == 'EO':
result += "Bob"
if ch == "WORL":
result += "Alice"
if ch == "D":
result += "Bob"
else:
result += "Hello World"
print(count)
return result
List
def pair(str):
count = 0
result = list()
for ch in str:
count += 1
if ch == 'HLL':
result.append("Alice")
if ch == 'EO':
result.append("Bob")
if ch == "WORL":
result.append("Alice")
if ch == "D":
result.append("Bob")
else:
result.append("Hello World")
print(count)
return result
Since return is the code that ends the function(def), for loop is executed only once in your code
For reference, if the input is a string(str), only one letter is entered in 'ch' in the for loop, so nothing other than D will be identified.
Finding a 'hello' word in a different string, which it has 'hello' in it
I should find a 'hello' word in a string, which I gave it from input too .I wrote this code by looking at the answer that someone gave to the below link's question.
firststring = input() #ahhellllloou
to_find = "hello"
def check_string(firststring, to_find):
c = 0
for i in firststring:
#print(i)
if i == to_find[c]:
c += 1
if c == len(to_find):
return "YES"
return "NO"
print(check_string(firststring, to_find))
but I don't want to use a def to solve the problem.
hello = "hello"
counter_hello = 0
bool_hello = False
for letter in string:
if letter == hello[counter_hello]:
counter_hello += 1
if counter_hello == len(hello):
bool_hello = True
if bool_hello == True:
print("YES")
else:
print("NO")
for hello string it works correct. also for pnnepelqomhhheollvlo.
but when I give it ahhellllloou it doesn't work.
I can't see where the bug is.
Your code is not the same. The return statement needs to be accounted for by breaking the loop
string = "ahhellllloou"
to_find = "hello"
match = False
c = 0
for i in string:
if i == to_find[c]:
c += 1
if c == len(to_find):
match = True
break
print("YES" if match else "NO")
Note that a loop isn't needed
>>> import re
>>> m = re.search('.*'.join('hello'), 'ahhellllloou')
>>> m is not None
True
>>> m = re.search('.*'.join('hello'), 'ahhellllliu')
>>> m is None
True
counter_hello = 0
bool_hello = False
for letter in 'ahhellllloou':
#print('letter: ', letter);
#print(hello[counter_hello])
if letter == hello[counter_hello] and counter_hello != len(hello) - 1:
counter_hello += 1
if counter_hello == len(hello) - 1:
bool_hello = True
if bool_hello == True:
print("YES")
else:
print("NO")
The code is from CodingBat and the question is: Return True if the string "cat" and "dog" appear the same number of times in the given string.
I have the solution but it states that the string index is out of range. I do not really understand what this means since I am new to python.
def cat_dog(str):
dogCount = 0
catCount = 0
length = len(str)
if length > 6:
for i in range(length-1):
if str[i]== 'd' and str[i+1]== 'o' and str[i+2]== 'g':
dogCount +=1
elif str[i]== 'c' and str[i+1]== 'a' and str[i+2]== 't':
catCount +=1
else:
return False
if dogCount == catCount:
return True
else:
return False
Expect either a true or false based on the function results
String index out of range error means you're trying to access a letter beyond the length of the string. This is happening in your for loop because when i = length - 2, str[i+2] becomes str[length] which is beyond the length of the string. Also, you should change if length > 6 to if length >= 6. Try this code:
def cat_dog(string):
dogCount = 0
catCount = 0
length = len(string)
if length >= 6:
for i in range(length-2):
if string[i]== 'd' and string[i+1]== 'o' and string[i+2]== 'g':
dogCount +=1
elif string[i]== 'c' and string[i+1]== 'a' and string[i+2]== 't':
catCount +=1
else:
return False
if dogCount == catCount:
return True
else:
return False
A possible solution is to continuously check the string to see if it contains either of the words replacing the first occurrence until there is no more:
def cat_dog(string):
dogCount = 0
catCount = 0
while "dog" in string:
dogCount += 1
string = string.replace("dog", "", 1)
while "cat" in string:
catCount += 1
string = string.replace("cat", "", 1)
if dogCount == catCount:
return True
else:
return False
I'm trying to make a program that tests if a word is a palindrome using a recursive function. I pretty much have it working but I'm just having trouble with getting it to move on to the next letter if the first and last are the same.
word = input("enterword")
word = word.lower()
def palindrom(word):
if len(word) == 1 or len(word) == 0:
return 0;
if word[0] == word[-1]:
print(word[0], word[-1])
palindrom(word);
else:
return 1;
test = palindrom(word)
if test == 0:
print("Yes")
elif test == 1:
print("No")
So right now it tests if the first and last letter are the same and if so, should run the function again. I just need to have it then check word[1] and word[-2] but I'm having some trouble. I tried splitting word and just popping the letters but it kept looking at the list as a length of 1. So if there is a way to get it to get the length of the whole split list, that would work as well.
You're just missing the return statement when you call your method recursively and the correct slice:
def palindrom(word):
if len(word) == 1 or len(word) == 0:
return 0
if word[0] == word[-1]:
print(word[0], word[-1])
return palindrom(word[1:-1])
else:
return 1
You are missing return statement, also you need to pass reduced word palindrom(word[1:-1]) on each recursion.
word = "viooiv"
word = word.lower()
def palindrom(word):
if len(word) == 1 or len(word) == 0:
return 0
if word[0] == word[-1]:
print(word[0], word[-1])
return palindrom(word[1:-1])
else:
return 1
test = palindrom(word)
if test == 0:
print("Yes")
elif test == 1:
print("No"
Output:
('v', 'v')
('i', 'i')
('o', 'o')
Yes
Try calling palindrome on the word with this function:
def palindrome(word, i = 0):
if i == len(word):
return word[0] == word[-1]
if word[i] == word[-i - 1]:
return palindrome(word, i + 1)
else:
return False
A bit more shorter code:
def checkpalindrome(value):
valuelen = len(value)
if valuelen < 2:
print("palindrome")
else:
checkpalindrome(value[1:valuelen-1]) if value[0] == value[-1] else print('Not palindrome')
I'm trying to write a version of always popular Count-A-WFF section of the WFF 'N Proof game (no copyright infringement intended) in Python. Alright, not so popular.
I think I have everything up and running up as desired for up to the case of a 4 letter string.
def maximum_string(s):
if cs(s) == True:
return len(s)
elif len(s) == 2:
l1 = [cs(s[0]), cs(s[1])]
if True in l1:
return len(s) - 1
else:
return 0
elif len(s) == 3:
first = s[0] + s[1]
second = s[0] + s[2]
third = s[1] + s[2]
l1 = [cs(first), cs(second), cs(third)]
if True in l1:
return len(s) - 1
l2 = [cs(s[0]), cs(s[1]), cs(s[2])]
if True in l2:
return len(s) - 2
else:
return 0
elif len(s) == 4:
first = s[0]+s[1]+s[2]
second = s[0]+s[1]+s[3]
third = s[1]+s[2]+s[3]
fourth = s[0]+s[2]+s[3]
l1 = [cs(first), cs(second), cs(third), cs(fourth)]
if True in l1:
return 3
first = s[0] + s[1]
second = s[0] + s[2]
third = s[0] + s[3]
fourth = s[1] + s[2]
fifth = s[1] + s[3]
sixth = s[2] + s[3]
l2 = [cs(first), cs(second), cs(third), cs(fourth), cs(fifth), cs(sixth)]
if True in l2:
return 2
first = s[0]
second = s[1]
third = s[2]
fourth = s[3]
l3 = [cs(first), cs(second), cs(third), cs(fourth)]
if True in l3:
return 1
else:
return 0
def cs(string):
global length_counter, counter, letter
counter = 1
length_counter = 0
letters_left = len(string)
while letters_left != 0 and length_counter < len(string):
letter = string[length_counter]
if letter == 'C' or letter == 'A' or letter == 'K' or letter == 'E' or letter == "K":
counter += 1
elif letter == 'N':
counter += 0
else:
counter -= 1
length_counter += 1
letters_left -= 1
if counter == 0 and len(string) == length_counter:
return True
else:
return False
The maximum_string helper function is intended to, given any string S, find the length of one of the longest possible wffs that you can make from just the letters of S. Of course, I can continue the pattern I currently have for the maximum_string helper function up to a length of 13. But, combinatorial explosion is evident. Thus, is there a more elegant way to finish off the maximum string helper function?
In effect one of the functions I had earlier would return a distance of how far away a string is from having a permutation in Polish notation. Thus this was surprisingly simpler to fix than I expected. Here's what I was looking for:
def maximum_string(string):
global length_counter, counter, letter
counter = 1
length_counter = 0
letters_left = len(string)
while letters_left != 0 and length_counter < len(string):
letter = string[length_counter]
if letter == 'C' or letter == 'A' or letter == 'K' or letter == 'E' or letter == "K":
counter += 1
elif letter == 'N':
counter += 0
else:
counter -= 1
length_counter += 1
letters_left -= 1
if ('p' in string) or ('q' in string) or ('r' in string) or ('s' in string) or ('t' in string) or ('u' in string):
return len(string) - abs(counter)
else:
return 0