I have a solution in python to find the string having unique characters.
def is_unique_with_ascii(string):
if len(string) > 128:
return False
char_set = [False] * 128
for char in string:
val = ord(char)
if char_set[val]:
print(char_set[val])
return False
char_set[val] = True
return True
In this code, the char_set has been initialized with false values. But in if statement when the same character that has already in string caught the statement become true means the char_set[val] got true value.My question is in python how to if char_set[val]: statement works to get the same value. Please help me out.
Looking at the documentation for if, it evaluates the condition, which is char_set[val] in this case. Since it already returns a Bool, the if statement evaluates it right away, to give the impression that "if char_set[val] statement works to get the same value"
Related
The function is all_even in which you input an int and it returns bool. It returns true if all the digits are even. e.g. all_even(2224) gives true and all_even(1362) gives false.
I wrote the code but it is not working properly.
This is what I have:
def is_very_even(num: int) -> bool:
even = False
strnum = str(num)
for ch in strnum:
if int(ch)% 2==0:
even = True
return even
The code gives true even if the num has a single even num and the rest are odd e.g all_even(231) is true which is supposed to be false. Can somebody tell me what is wrong with it.
You could use a recursive approach (without converting to a string):
def allEven(N): return True if N==0 else N%2==0 and allEven(N//10)
output:
allEven(246) # True
allEven(123) # False
Note that the issue with your function is that you set even to True as soon as you encounter an even digit. If there is an odd digit after that, your even variable remains True. This will tell you that "at least one" digit is even, not that they are all even.
You are checking if all the numbers are even. So first assume that it's true, all the numbers are even and set even = True. Then check if there is any odd number in the string. If there is, then set even = False
Now, if there is any odd number in the string, even will be False and therefore we can verify it.
def is_very_even(num: int) -> bool:
even = True
strnum = str(num)
for ch in strnum:
if int(ch)% 2 !=0:
even = False
return even
This code can also be improvised like this. We don't need to keep checking if we found a single odd number. We can just return false immediately. And if all the numbers checks out but none returns false, then it must be an all even string.
def is_very_even(num: int) -> bool:
for ch in str(num):
if int(ch)% 2 !=0:
return False
return True
I would solve it like this:
def is_very_even(s) -> bool:
return all(int(x) % 2 == 0 for x in str(s))
This converts every character to an integer and checks if it is even with modulo. This is processed in a so called generator expression.
Then we use the builtin all method to check if the generator delivers only True values.
I am trying to solve the following practice question:
"Imagine you're writing the software for an inventory system for
a store. Part of the software needs to check to see if inputted
product codes are valid.
A product code is valid if all of the following conditions are
true:
The length of the product code is a multiple of 4. It could
be 4, 8, 12, 16, 20, etc. characters long.
Every character in the product code is either an uppercase
character or a numeral. No lowercase letters or punctuation
marks are permitted.
The character sequence "A1" appears somewhere in the
product code.
Write a function called valid_product_code. valid_product_code
should have one parameter, a string. It should return True if
the string is a valid product code, and False if it is not."
def valid_product_code(code):
if len(code)%4 == 0:
if "A1" in code:
if code.isalnum():
for character in code:
#print(character)
#print statement above when uncommented used to
#check if the loop is actually running as intended
if character.isupper() or character.isdigit():
return True
else:
return False
else:
return False
else:
return False
else:
return False
The practice question had several test strings, of which they included the following:
print(valid_product_code("A12B44BP"))
print(valid_product_code("BFDSAUSA98932RWEFOEWA9FEAA1DSFSF"))
print(valid_product_code("A1BBD5"))
print(valid_product_code("BDD5664S"))
print(valid_product_code("66aBSaA1fdsv"))
My code worked for the first four examples, resulting in True, True, False, False but while the last one should be False, I got True. After attempting some debugging (hence the print(character) in the for loop and changing the return True and return False to print(True) and print(False) statements respectively), the print statements I used to check indicated that lowercase letters all had False values whereas numbers and uppercase letters had True values as intended.
I had no problems with the 3 outer if statements, but once I needed to isolate lowercase characters I thought a for-each loop would suffice but the fact that this is a function means return is terminating my function prematurely and not allowing me to actually indicate that the presence of even a single lowercase letter in the whole string should render the value of the whole string as False. I feel something is missing, like maybe that I am putting my return statements in the wrong place, or have I simply approached this question the wrong way?
Please help and thanks in advance!
Your innermost loop is not implemented correctly. At the moment, if character.isupper() or character.isdigit(): will return True as soon as it detects the first 6.
You need to check for each element. This can be done in the following way. I am highlighting the modified lines by a comment #
def valid_product_code(code):
if len(code)%4 == 0:
if "A1" in code:
if code.isalnum():
for character in code:
if not (character.isupper() or character.isdigit()): # <---
return False # <----
return True # <---
else:
return False
else:
return False
else:
return False
print(valid_product_code("A12B44BP"))
print(valid_product_code("BFDSAUSA98932RWEFOEWA9FEAA1DSFSF"))
print(valid_product_code("A1BBD5"))
print(valid_product_code("BDD5664S"))
print(valid_product_code("66aBSaA1fdsv"))
True
True
False
False
False
Alternatively, you can combine your if statements to make the code more compact as
def valid_product_code(code):
if len(code)%4 == 0 and "A1" in code and code.isalnum():
for character in code:
if not (character.isupper() or character.isdigit()):
return False
return True
else:
return False
I was looking for a way to write code that checks whether a certain string is a part of another string. I understand that it is easy to do when we have numbers, but I don't know how to do it with strings.
For example, I have this function
a = is_part("motherland", "land")
I need to know that "land" is a part of the word "motherland" (return True or False). Is it possible to check this?
UPDATE: How can I create a restriction when the second word always has to be in the end of the first one. For example, in case when I check whether "eight" is a part of "eighteen" it returns False because "eight" is not at the end of the first word
This should help:
>>> "land" in "motherland"
True
>>> "banana" in "motherland"
False
Here is a function that determines whether a string target is contained within another string some_string.
def is_part(some_string, target):
return target in some_string
>>> is_part('motherland', 'land')
True
>>> is_part('motherland', 'father')
False
>>> is_part('motherland', '')
True
If you don't like an empty string returning true, change the return statement to
return (target in some_string) if target else False
If, on the other hand, you need to implement it yourself:
def is_part(some_string, target):
if target:
target_len = len(target)
for i in range(len(some_string)):
if some_string[i:i+target_len] == target:
return True
return False
Not sure what I'm doing wrong here. Any help would be appreciated. When I enter a DNA sequence that contains a bad variable (like Z) I keep getting a return True. Can someone point out why?
Thanks
def is_valid_sequence(dna):
""" (str) -> bool
>>> is_valid_sequence('ATCG')
True
>>> is_valid_sequence('AZT')
False
>>> is_valid_sequence('atcg')
False
Returns a boolean result based on whether dna is a valid
dna sequence.
"""
for char in dna:
if char in "TCGA":
return True
else:
return False
You're returning on the first iteration of the loop: return ends the function, and all paths in the body of your loop contain a return. You probably want
for char in dna:
if char not in 'TCGA':
return False
return True
Or, more Pythonically:
return all(char in 'TCGA' for char in dna)
In your code you take char one by one and return True if it is in "TCGA". So if the first char is in "TCGA" it would return True and will stop execution. You should do something like this:
for char in dna:
if char not in "TCGA":
return False
You're always returning after testing the first character. Keep testing until you reach a bad character, don't return True until you've tested the whole string.
s="(8+(2+4))"
def checker(n):
if len(n) == 0:
return True
if n[0].isdigit==True:
if n[1].isdigit==True:
return False
else:
checker(n[1:])
else:
checker(n[1:])
This is what I have so far. Simple code, trying to see if a string meets the following conditions.
However when i perform checker(s) i get:
True
IndexError: string index out of range
Any help? Thanks in advance
Edit: The function's purpose is to produce true if the string contains only single digit numbers, and false if 2 or more-figured digits exist in the string.
When the length of n is 0, the n[0] part is going to raise an error because the string in empty. You should add a return statement there instead of print.
def checker(n):
if len(n) < 2:
return True
if n[0] in x:
Note that the conditions must be len(n) < 2 otherwise you'll get an error on n[1] when the length of string is 1.
Secondly you're trying to match characters to a list which contains integers, so the in checks are always going to be False. Either convert the list items to string or better use str.isdigit.
>>> '1'.isdigit()
True
>>> ')'.isdigit()
False
>>> '12'.isdigit()
True
Update:
You can use regex and all for this:
>>> import re
def check(strs):
nums = re.findall(r'\d+',strs)
return all(len(c) == 1 for c in nums)
...
>>> s="(8+(2+4))"
>>> check(s)
True
>>> check("(8+(2+42))")
False
Working version of your code:
s="(8+(2+4))"
def checker(n):
if not n: #better than len(n) == 0, empty string returns False in python
return True
if n[0].isdigit(): #str.digit is a method and it already returns a boolean value
if n[1].isdigit():
return False
else:
return checker(n[1:]) # use return statement for recursive calls
# otherwise the recursive calls may return None
else:
return checker(n[1:])
print checker("(8+(2+4))")
print checker("(8+(2+42))")
output:
True
False
You should do return True after the first if statement, not print True. The function continues to run after that statement and hits an error when the input is size 0.
I can't reproduce your error.
I had to fix a few things:
Indentation, which I'm guessing was just a problem pasting into the page
.isdigit() is a function; calling .isdigit==True is going to compare a function object with True, which is never going to be true. I changed .isdigit==True to .isdigit()
I made sure return value gets bubbled up - without this, the recursion completes okay but the outermost function just returns "None".
Aside from that, a few print statements show that this is working as expected
s="(8+(2+4))"
t="(8+(20+4))"
def checker(n):
print "checking %s" % n
if len(n) == 0:
print "Returning true"
return True
if n[0].isdigit():
if n[1].isdigit():
print "returning false"
return False
else:
return checker(n[1:])
else:
return checker(n[1:])
print checker(s)
print checker(t)
Output:
checking (8+(2+4))
checking 8+(2+4))
checking +(2+4))
checking (2+4))
checking 2+4))
checking +4))
checking 4))
checking ))
checking )
checking
Returning true
True
checking (8+(20+4))
checking 8+(20+4))
checking +(20+4))
checking (20+4))
checking 20+4))
returning false
False