Function returns inside loops? - python-3.x

How do return statements of a function work inside for loops? See my code below for what got me thinking about this.
It is like a game of scrabble. You get given n letters (we'll call it a hand), and the aim is to make a word out of your hand. The hand is in the form of a dictionary, like so:
yellow = {'y':1, 'e':1, 'l':2, 'o':1, 'w':1}
I have written a function called get_frequency_dict to transform a string in to this form.
I want to write a function which tells me if the word given is valid or not. I have a list of acceptable words in the variable word_list
Call the function: is_valid_word(word, hand, word_list) which takes in three inputs.
word: string
hand: dictionary (string: int)
word_list: list of lowercase strings
So, I wanted to write this function to return True if the word is valid and False if the word is invalid:
def is_valid_word(word, hand, word_list):
word_dict = get_frequency_dict(word.lower())
if word.lower() in word_list:
for char in word_dict:
if (char in hand) and (hand[char] >= word_dict[char]):
return True
else:
return False
else:
return False
This function seems to work fine with various different inputs, but I'm not quite sure I understand the logic behind the function return in the for loop?
As we loop through the keys in the dictionary (letters), we will get a True or False depending on whether or not the condition is satisfied. So, how does the function know what to return? What if one char returns False and then the next char returns True? Does it instantly return False as soon as it sees one False in the for loop?
The code below makes more sense to me:
total_false = 0
for char in word_dict:
if (char in hand) and (hand[char] >= word_dict[char]):
pass
else:
total_false += 1
if word.lower() not in word_list:
return False
elif total_false > 0:
return False
else:
return True
Does the first segment of code do this "counting" behind the scenes? As in, as soon as we see a False, it returns False?

Simply iterate over the list and if you find an error, stop:
def is_valid_word(word, hand, word_list):
word_dict = get_frequency_dict(word.lower())
if word.lower() in word_list:
for char in word_dict:
if (char in hand) and (hand[char] >= word_dict[char]):
continue
else:
return False #This will stop the function and return false
else:
return False #This will do the same
#At the end, this means that all of the criteria are met, so we return True
return True
As we see, the moment a character doesn't fit the criteria, the function stops and returns false. If it passes all the criteria, it returns true.

Related

function that returns `True` if a string contains exactly two instance of a substring and `False` if it doesn't

I'm trying to write a function that returns True if a string contains exactly two instance of a substring and False if it doesn't.
I'm getting an error:
return' outside function
I feel I'm very close but just can't quite get it, I'd appreciate being pointed in the right direction.
Should recursion be used?
s = ("xeroxes")
exes = str(x)
count = 0
def has_two_X(s):
count = s.count(exes)
for exes in s:
count = +1
if count ==2:
return True
else:
return False
if __name__ == "__main__":
print("string has :",count.s(exes))
If the code must return True if there is two or more instances of substring, you can create a dictionary and return True if value is there in dictionary or not.
mydict = {}
for letter in s:
if letter in mydict:
return True
else:
mydict[letter] = 0
return False #Since there is no substring (two substring can be formed only if it has two same letters in the string)
To find exactly if it has two substring, I would recommend the same approach where you can maintain the dictionary and the count of substring present. Add all the substring/count to the dictionary, this would take O(n^2) to generate all substrings and approx- O(n^2) hashmap memory.
After constructing hashmap, you can iterate over the hashmap to return True if it has exactly two occurences of substring.

Finding a substring in a jumbled string

I am writing a script - includes(word1, word2) - that takes two strings as arguments, and finds if word1 is included in word2. Word2 is a letter jumble. It should return Boolean. Also repetition of letters are allowed, I am only checking if the letters are included in the both words in the same order.
>>>includes('queen', 'qwertyuytresdftyuiokn')
True
'queen', 'QwertyUytrEsdftyuiokN'
I tried turning each word into lists so that it is easier to work with each element. My code is this:
def includes(w1, w2):
w1 = list(w1)
w2 = list(w2)
result = False
for i in w1:
if i in w2:
result = True
else:
result = False
return result
But the problem is that I need to also check if the letters of word1 comes in the same order in word2, and my code doesn't controls that. I couldn't find a way to implement that with list. Just like I couldn't do this much with strings, so I think I need to use another data structure like dictionary but I don't know much about them.
I hope I understood what is your goal.
Python is not my thing, but I think I made it pythonic:
def is_subsequence(pattern, items_to_use):
items_to_use = (x for x in items_to_use)
return all(any(x == y for y in items_to_use) for x, _ in itertools.groupby(pattern))
https://ideone.com/Saz984
Explanation:
itertools.groupby transfers pattern in such way that constitutive duplicates are discarded
all items form form grouped pattern must fulfill conditions
any uses generator items_to_use as long as it doesn't matches current item. Note that items_to_use mus be defined outside of final expression so progress on it is kept every time next item from pattern is verified.
If you are not just checking substrings:
def include(a, b):
a = "".join(set(a)) # removes duplicates
if len(a) == 1:
if a in b:
return True
else:
return False
else:
try:
pos = b.index(a[0])
return include(a[1:], b[pos:])
except:
return False
print(include('queen', 'qwertyuytresdftyuiokn'))
#True

I want to know how can is shorten this code and make it look more proper

I want to know if the code I wrote can be shortened further, I was practicing and I came up to a task which asks you to return a boolean value, this is what the question says:
Given two strings, return True if either of the strings appears at the
very end of the other string, ignoring upper/lower case differences
(in other words, the computation should not be "case sensitive").
Note: s.lower() returns the lowercase version of a string.
def end_other(a, b):
x = len(b)
n = a[-x:]
y = len(a)
m = b[-y:]
if b.lower() == n.lower() or a.lower() == m.lower() :
return True
else:
return False
The Code is working properly but I wondered if it can be shortened more so it looks good.
You can write it like this:
def end_other(a, b):
n = a[-len(b):]
m = b[-len(a):]
return b.lower() == n.lower() or a.lower == m.lower()
I removed variables x and y because they are used just one time and then I also remove the if-else statement because it's unnecessary, in fact you can just return the result of the comparison instead of checking it's result and returning it a second time.

How to fix multiple boolean values being printed

Right now I'm led to believe that my function is incorrect since I am getting more than 1 boolean output.
listOstrings = ['cat in the hat','michael meyers','mercury.','austin powers','hi']
def StringLength(searchInteger, listOstrings):
'return Boolean value if the strings are shorter/longer than the first argument'
for i in listOstrings:
if len(i) < searchInteger:
print(False)
else:
print(True)
You don't want to print True or False for each item; you want to create a single Boolean over the course of iterating over the loop. Or more simply, you can return False as soon as you find one element that fails the test, returning True only if you make it through the entire loop without returning.
def checkStringLength(searchInteger, lstStrings):
'return Boolean value if the strings are shorter/longer than the first argument'
for i in lstStrings:
if len(i) < searchInteger:
return False
return True
This is more naturally written using the all function:
def checkStringLength(searchInteger, lstStrings):
return all(len(i) >= searchInteger for i in lstStrings)

recursion not stopping with 'if'

I am trying to write a code which prints True if given string has at max 2 consecutive c, and at max 1 b. I am using recursion to reduce the string and check that at max 'c' is present in the same index twice.But my recursion is not stopping till it empties the whole list. Can you please suggest what's wrong with my code. Thanks!
def stringcond(N,count=0,k=0):
N=list(N)
if(N.count('b')>1):
return False
if len(N)<2:
return True
else:
for i,j in enumerate(N):
if(j=='c'):
del N[i]
count+=1
if(k==i and count>2):
return False
stringcond(N,count=count,k=i)
return True
You have several mistakes. First, why are you splitting the characters into a list? There is a perfectly good count method for strings.
Your recursion fails because you ignore the return value. You would want something like
if not stringcond(N,count=count,k=i):
return False
# I make no claim that this logic is correct.
In any case, there is no need to recur. Use count to check the quantity of "b" and many-'c' substrings:
def stringcond(s, c_max=0):
return s.count('b') <= 1 and \
s.count("c" * (c_max+1)) == 0
You have to use the result of the stringcond call. Now your function will only return whatever was determined on the top level call.

Resources