The Task:
Write a program that checks if a word supplied as the argument is an Isogram. An Isogram is a word in which no letter occurs more than once.
Create a method called is_isogram that takes one argument, a word to test if it's an isogram. This method should return a tuple of the word and a boolean indicating whether it is an isogram.
If the argument supplied is an empty string, return the argument and False: (argument, False). If the argument supplied is not a string, raise a TypeError with the message 'Argument should be a string'.
Example:
is_isogram("abolishment")
Expected result:
("abolishment", True)
The Visible test
from unittest import TestCase
class IsogramTestCases(TestCase):
def test_checks_for_isograms(self):
word = 'abolishment'
self.assertEqual(
is_isogram(word),
(word, True),
msg="Isogram word, '{}' not detected correctly".format(word)
)
def test_returns_false_for_nonisograms(self):
word = 'alphabet'
self.assertEqual(
is_isogram(word),
(word, False),
msg="Non isogram word, '{}' falsely detected".format(word)
)
def test_it_only_accepts_strings(self):
with self.assertRaises(TypeError) as context:
is_isogram(2)
self.assertEqual(
'Argument should be a string',
context.exception.message,
'String inputs allowed only'
)
My Solution:
def is_isogram(word):
if type(word) != str:
raise TypeError('Argument should be a string')
elif word == "":
return (word, False)
else:
word = word.lower()
for char in word:
if word.count(char) > 1:
return (word, False)
else:
return (word, True)
But it the function refuses to pass some hidden test: What is wrong with my solution? Is there another elegant way of writing this function?
I'm not sure your search should be case insensitive so perhaps you should remove word = word.lower(), but your main issue is return terminates the function so you current code needs to only return True after all tests have been conducted (i.e. outside of the loop):
for char in word:
if word.count(char) > 1:
return (word, False)
return (word, True)
Anyway, a better way is to use set() to remove all duplicates from your string and then compare the lengths; also use isinstance() to check if word is a string. You can use if w to check for empty strings . You don't need parentheses with return, the comma is enough to return a tuple:
def is_isogram(word):
if isinstance(word,str):
w = word.lower() # assuming you want a case in-sensitive search
return word, len(w) == len(set(w)) if w else False
else:
raise TypeError('Argument should be a string')
Examples:
is_isogram('abolishment')
# ('abolishment', True)
is_isogram('happy')
# ('happy', False)
is_isogram('')
# ('', False)
is_isogram(1)
# TypeError: Argument should be a string
I have tried in this way:
def isogram(n):
if not isinstance(n, str):
return n,False
elif len(n) < 1:
return n,False
n = n.lower()
if len(n) == len(set(n)):
return n,True
else:
return n,False
The second line checks if the input n is a string, if it isn't it returns False.
The line 4 checks if the length of the input is smaller than 1, in that case the input is empty and we cannot check if it is an isogram or not.
'n = n.lower()' in line 6 assures us that the input becomes a string with lowercase letters.
In line 7 we check if the length of the input is equal to the length of the set(n). The function set() converts a collection or a sequence or an iterator object into a set. For example: set('lists') returns {'s', 't', 'l', 'i'}, as you can see the letter 's' which appears twice in 'lists', does not appear in the set. This is useful to check if the length of the set is equal to the length of the input, if there is a letter which appears twice in the input the condition is False.
I hope this explanation makes the script clear.
def is_isogram(string):
if type(string) != str:
raise TypeError('Argument should be a string')
elif string == " ":
return (string, False)
else:
for i in string:
if string.count(i) > 1:
return (string, False)
return (string, True)
The reason for this
"But it the function refuses to pass some hidden test: What is wrong with my solution? Is there another elegant way of writing this function?"
i.e refuses to pass the tests is pretty simple.
elif word == "":
should be
elif word == " ":
as per the unit test
if argument==" " :
return(argument,False)
You are forgetting the whitespace. Hope this helps the next person who comes searching.
This works for me. If there are more than one digit it will be added to a new string. If this new string is empty the function will return True. Else False. Only " if word.count(char) > 1: " won´t do it. At least not for me.
def isagram (word):
if not isinstance(word, str):
return (False, word)
if not word:
return (False, word)
word.lower()
multi_char = ''
for char in word:
if word.count(char) > 1:
multi_char += char
return (False, word) if multi_char else (True, word)
Here is one way to solve the problem:
def is_isogram(argument):
word_seen=set()
if type(argument) != str:
raise TypeError('Argument should be a string')
if argument==" " :
return(argument,False)
argument.lower()
for letter in argument:
if letter in word_seen:
return(argument,False)
word_seen.add(letter)
return (argument,True)
Equivalently, I also tried the following to remove all spaces in strings:
def is_isogram(argument):
word_seen=set()
if type(argument) != str:
raise TypeError('Argument should be a string')
if argument==" " :
return(argument,False)
argument.lower()
argument = ''.join(argument.split())
for letter in argument:
if letter in word_seen:
return(argument,False)
word_seen.add(letter)
return (argument,True)
i solved it here:
def is_isogram(word):
for letter in word:
if word.count(letter) > 1:
return False
else:
return True
print is_isogram("Hello")
def is_Isogram(words):
cover_words = words.lower()
letter_list = []
for letter in cover_words:
if letter.isalpha():
if letter in letter_list:
return False
letter_list.append(letter)
return True
if __name__ == '__main__':
print(is_Isogram('Machine'))
print(is_Isogram('Geeks'))`
This is the last part of your suggested solution:
for char in word:
if word.count(char) > 1:
return (word, False)
else:
return (word, True)
return will end the function, hence the for loop will always exit after counting repetitions of the first letter and never check the other ones.
If your word has a first letter which is unique, the function will return True.
I believe this is a bug and this is my corrected suggestion:
for char in word:
if word.count(char) > 1:
return (word, False)
return (word, True)
import collections
def is_isogram(word):
if not isinstance(word, str):
raise TypeError('Argument should be a string')
most_common = collections.Counter(word).most_common(1)
if most_common:
char, freq = most_common[0]
return (word, freq == 1)
else:
return (word, False)
You just need an extra conditional statement to check if the characters in the word are solely alphabets (i.e. no numbers or symbols) whilst checking if the characters are repeated:
def is_isogram(word):
if type(word) != str:
raise TypeError("Argument should be a string")
if len(word) < 1:
return (word, False)
word = word.lower()
for char in word:
if word.count(char) > 1 or \
char not in "abcdefghijklmnopqrstuvwxyz": # this checks that the char is an alphabet
return (word, False)
return (word, True)
I guess this should help.
i found this correct
def is_isogram(word):
if type(word)!=str:
raise TypeError("Argument should be a String")
elif word.strip()=="":
return(word,False) else:
word =word.lower()
for char in word:
if word.count(char) >1:
return (word ,False)
else:
return(word ,True)
Related
I wrote a regex code which compares two strings. It recognises a special character '?' that allows zero or more instances of previous character. It works fine until there are two or more occasions of '?' in the string. And I can't make out why.
def single_character_string(a, b) -> "return True if characters match":
"""check if two characters match"""
if len(a) == 0:
return True
elif len(b) == 0:
return False
else:
if a == '.':
return True
else:
if a == b:
return True
else:
return False
def meta_question_result(temp):
if len(temp) >= 2:
if temp[1] == '?':
k_1 = temp.replace(temp[0: 2], '') # no char
k_2 = temp.replace(temp[1], '') # char
return k_1, k_2
def check_pair_by_pair(template, check_string) -> "Strings are of Equal length! " \
"return True if lines are identical":
"""check if two strings match symbol by symbol. template may be less than string, the opposite
is False"""
if not template: # exit from recursion
return True
if not check_string: # exit from recursion
return False
if meta_question_result(template):
t_1, t_2 = meta_question_result(template)
if single_character_string(t_1[0], check_string[0]):
return check_pair_by_pair(t_1[1:], check_string[1:])
if single_character_string(t_2[0], check_string[0]):
return check_pair_by_pair(t_2[1:], check_string[1:])
else:
return False
elif single_character_string(template[0], check_string[0]):
return check_pair_by_pair(template[1:], check_string[1:])
else:
return False
reg, st = input().split("|")
print(check_pair_by_pair(reg, st))
reg = "co?lou?r"
st = "colour"
gives True as expected,
reg = "co?lou?r"
st = "clor"
gives True as expected,
but...
reg = "co?lou?r"
st = "color"
gives False. I expected True.
Found the bag.
Replace method replaces all instances of '?'. So the second '?' was replaced also and program didn't see it.
I should add an argument 'count' to replace method that is equal to 1.
k_1 = temp.replace(temp[0: 2], '', 1) # no char
Given a string that contains only the following => ‘{‘, ‘}’, ‘(‘, ‘)’, ‘[’, ‘]’. At some places there is ‘X’ in place of any bracket. Determine whether by replacing all ‘X’s with appropriate bracket, is it possible to make a valid bracket sequence.
Examples:
Input : S = "{(X[X])}"
Output : Balanced
Input : S = "[{X}(X)]"
Output : Not balanced
I tried to work it out like this, and it works for examples above. But it doesn't work for all examples eg. (it should be balanced but it says it's not)
Input: S = "([X}])"
Output: Not balanced
I tried to work it out but i can't find a solution. Please help.
class Stack:
def __init__(self):
self.data = []
def insert(self, x):
self.data.append(x)
def empty(self):
return len(self.data) == 0
def remove(self):
if self.empty():
raise ValueError('Stack is empty.')
self.data.pop()
def top_element(self):
if self.empty():
raise ValueError('Stack is empty.')
return self.data[-1]
def is_matching(a, b):
if a == "(" and b == ")":
return True
elif a == "[" and b == "]":
return True
elif a == "{" and b == "}":
return True
elif a == "X":
return True
return False
def is_balanced(expression,elements=Stack(),ind=0):
if ind == len(expression):
return elements.empty()
pre_brackets = "([{"
post_brackets = ")]}"
char = expression[ind]
if char in pre_brackets:
elements.insert(char)
return is_balanced(expression,elements,ind+1)
elif char in post_brackets:
if elements.empty() :
return False
if not is_matching(elements.top_element(), char):
return False
elements.remove()
return is_balanced(expression,elements,ind+1)
elif char == "X":
temp = Stack()
temp.insert(char)
result = (is_balanced(expression,temp,ind+1))
if result:
return True
if elements.empty():
return False
elements.remove()
return is_balanced(expression,elements,ind+1)
expression = "([X}])"
if expression == "":
print("No brackets in expression!")
elif len(expression) % 2 != 0:
print("Not balanced")
elif is_balanced(expression):
print("Balanced")
else:
print("Not Balanced")
You can do it by recursively testing all possible replacements for an X:
def can_be_balanced(expr):
pairs = "{}[]()"
opening_brackets = pairs[::2]
closing_brackets = pairs[1::2]
closer = {o:c for o, c in zip(opening_brackets, closing_brackets)}
opener = {c:o for o, c in zip(opening_brackets, closing_brackets)}
stack = []
for item in expr:
if item in opening_brackets:
# we append opening brackets to the stack
stack.append(item)
elif item in closing_brackets:
if not stack or stack[-1] != opener[item]:
# the closing bracket doesn't match the top of the stack
return False
else:
# if it does, we remove the matching opening bracket
stack.pop()
elif item == 'X':
# X could be any of the opening brackets,
possible = list(opening_brackets)
if stack and stack[-1] in opening_brackets:
# or the closing bracket matching the top of the stack
possible.append(closer[stack[-1]])
for pos in possible:
# we replace this X, the first one remaining in expr
test_expr = expr.replace('X', pos, 1)
if can_be_balanced(test_expr):
# This is just in order to print the working solution we just found,
# you may remove these two lines
if not 'X' in test_expr:
print(test_expr)
return True
# None of the replacements for X gave a balanced expression
return False
else:
raise ValueError(f'Invalid item {item} in {expr}')
# The expression is balanced if and only if the stack ends up empty
return not stack
Testing on your sample data:
tests = [("[{X}(X)]", False),
("{(X[X])}", True),
("([X}])", True),
]
for test in tests:
print(test[0], ': should be', test[1])
print(can_be_balanced(test[0]))
print('-'*20)
correctly outputs (with the balanced expression in case it can be done):
[{X}(X)] : should be False
False
--------------------
{(X[X])} : should be True
{([[]])}
True
--------------------
([X}]) : should be True
([{}])
True
--------------------
Note that a major problem in your code is that you only test the end of the expression, starting at the position of the X. Beware also of the mutable default argument elements=Stack() that would leave you with the remnants of the previous call to the function instead of a fresh, empty Stack object.
I am trying to come up with a code for the valid parentheses problem (https://leetcode.com/problems/valid-parentheses/) but I am having trouble understanding the logic of this problem.
This is my pseudocode
Iterate over characters in the string.
Get the current element.
Check if current element is == next element.
if it is, return true, else return false.
This was my solution but it doesn't work.
class Solution(object):
def isValid(self,s):
# iterate through the index in the string
for i in range(0,len(s)-1)):
# if the current index is equal to the next index, return true
if(s[i] == s[i+1]):
return True
I asked this question earlier and got the feedback that my code looking for places where the same symbol appears twice in a way. Whereas, I want my code to match up pairs of brackets.
Could someone help me understand the logic of this problem?
Here's how I would approach it:
class Solution(object):
def isValid(self, s):
stack = []
for char in s:
if "(" == char:
stack.append(")")
elif "{" == char:
stack.append("}")
elif "[" == char:
stack.append("]")
elif not stack or stack.pop() != char:
return False
return not stack
def test_is_valid(s, expected):
result = is_valid(s)
print(repr(s), result)
assert result == expected
is_valid = Solution().isValid
test_is_valid("()", True)
test_is_valid("()[]{}", True)
test_is_valid("(]", False)
test_is_valid("", True)
test_is_valid("{", False)
test_is_valid("}", False)
test_is_valid("[{([])}]", True)
Output:
'()' True
'()[]{}' True
'(]' False
'' True
'{' False
'}' False
'[{([])}]' True
I wrote a program to check if a word is an isogram but after passing the test cases, it says "Your solution failed to pass all the tests"
below is the test cases:
from unittest import TestCase
class IsogramTestCases(TestCase):
def test_checks_for_isograms(self):
word = 'abolishment'
self.assertEqual(
is_isogram(word),
(word, True),
msg="Isogram word, '{}' not detected correctly".format(word)
)
def test_returns_false_for_nonisograms(self):
word = 'alphabet'
self.assertEqual(
is_isogram(word),
(word, False),
msg="Non isogram word, '{}' falsely detected".format(word)
)
def test_it_only_accepts_strings(self):
with self.assertRaises(TypeError) as context:
is_isogram(2)
self.assertEqual(
'Argument should be a string',
context.exception.message,
'String inputs allowed only'
)
and also below is my code for the tests. it passed the tests but failed some hidden tests:
def is_isogram(word):
if type(word) == str or len(word) != 0:
if not word:
return (word, False)
word = word.lower()
return (word, len(set(word)) == len(word))
else:
raise TypeError('Argument should be a string')
can anybody tell me what I am doing wrong?
As this question relates to something i have been working on, i would like to share my findings, so that anyone who is looking for the clear solution of this question uses it.
def is_isogram(word):
word = word.lower()
try:
if len(word) > 0:
for letter in word:
if word.count(letter) > 1:
return (word, False)
return (word, True)
else:
return ('argument', False)
except TypeError as e:
return "Argument should be a string: "+ str(e)
print is_isogram("")
Ok this works, passed all hidden tests too. You are welcome
def is_isogram(word):
'''This function tests for isogram'''
word_set = set(word)
if word.strip() == "":
return (word, False)
elif len(word) == len(word_set):
return (word, True)
elif type(word)!= str :
raise TypeError
else:
return (word, False)
def is_isogram(word):
return (word,True) if word and len(set(word)) == len(word) else (word,False)
I have this code:
def reverse (word):
newword = ''
letterflag = -1
for numletter in word:
newword += word[letterflag]
letterflag-=1
s=newword
s.upper()
return newword
def isPalindrome(word, ignorecase=False):
"""
>>> type(isPalindrome("bob"))
<type 'bool'>
>>> isPalindrome("abc")
False
>>> isPalindrome("bob")
True
>>> isPalindrome("a man a plan a canal, panama")
True
>>> isPalindrome("A man a plan a canal, Panama")
False
>>> isPalindrome("A man a plan a canal, Panama", ignorecase=True)
True
"""
word = str (word)
newword = reverse(word)
if word == newword:
return True
else:
return False
When I type "Bob", I want it to return true because of the capital B.
Just make your input always lower case that way you can avoid that problem altogether.
word = str(word)
word = word.lower()
word = word.replace(',', '') # removes any commas from the string
newword = word[::-1] # reverse string
if word == newword:
return True
else:
return False
The best way to learn what is being done in this answer is to try individual parts of it in the Python console.
To fix your reverse() do this:
def reverse (word):
newword = ''
letterflag = -1
for numletter in word:
newword += word[letterflag]
letterflag-=1
return newword
Notice I also took out the .upper() parts since they are not effective and reverse is not the correct place to have it since you can not compare a reversed upper case word with the original word. Also s.upper() does not work like you think it does. It returns an upper case copy of s without modifying s. You would only to do return newword.upper() to make it work.
Additionally, the letterflag is not needed, you could simply do:
def reverse (word):
newword = ''
for letter in word:
newword = letter + newword #adds each new letter to beginning
return newword
However the simplest way to do a reverse functions is:
def reverse (word):
return word[::-1]
Your isPalendrome needs to be this to basically work:
def isPalindrome(word, ignorecase=False):
word = word.replace(',', '').replace(' ', '') #removes ,'s and spaces
if ignorecase:
return word.lower() == reverse(word).lower()
else:
return word == reverse(word)
Here is a more advanced solution that will ignore anything that is not a letter with an option to ignore case. This version does need reverse.
def isPalindrome(word, ignorecase=False):
abcs = 'abcdefghijklmnopqrstuvwxyz'
word = [c for c in word.lower()*ignorecase or word if c in abcs]
return word == word[::-1] #no need for reverse
If you want to have the option to be case sensitive, or the option to be case insensitive, add an IF statement in the isPalindrome() function:
if ignorecase == True:
word = word.lower()
It should look like this when you are done:
import string
def reverse (word):
newword = ''
letterflag = -1
for numletter in word:
newword += word[letterflag]
letterflag-=1
s=newword
s.upper()
return newword
def isPalindrome(word, ignorecase=False):
"""
>>> type(isPalindrome("bob"))
<type 'bool'>
>>> isPalindrome("abc")
False
>>> isPalindrome("bob")
True
>>> isPalindrome("a man a plan a canal, panama")
True
>>> isPalindrome("A man a plan a canal, Panama")
False
>>> isPalindrome("A man a plan a canal, Panama", ignorecase=True)
True
"""
if ignorecase == True:
word = word.lower()
word = word.replace(',', '')
word = word.replace(' ', '')
newword = reverse(word)
if word == newword:
return True
else:
return False
That code gives me the following feedback:
isPalindrome('Bob', ignorecase=True)
Out[34]: True
isPalindrome('Bob')
Out[35]: False