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
Related
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.
I have an excercise about prime numbers that requires me to write a function which takes a list of elements and a number p and marks elements False which are in the range 2p, 3p...N
First I create a list of True and False:
true_value = [False, False] + [True for x in range(n-1)] #Let assumme that n=16
And then I write the function that find the even number in this list (with p = 2)
def mark_false(bool_list, p):
range_new = [x for x in range(len(bool_list))]
for i in range(2, len(range_new)):
for j in range(p, len(range_new), p):
if (i*p == range_new[j]) & (i*p <= len(range_new)):
bool_list[j] = False
return bool_list
This function help me to find the location of the even number (>2) and return to False
Example: a = list_true(16)
a = [False,False,True,True,True,True,True,True,True,True,True,True,True,True,True,True,True]
b = mark_false(a, 2)
b = [False,False,True,True,False,True,False,True,False,True,False,True,False,True,False,True]
This function mark_false does work but the problem is everytime I run it I have to create a list range_new which takes a lot of time to calculate. So how do I rewrite this function so it can run faster without creating new lists?
You seem to be doing things the long way around, searching for the j value that matches the multiple of p you want to set to False. But since you already know that value already, there's no need to search for it, just set it directly.
I'd do:
def mark_false(bool_list, p):
for i in range(p, len(bool_list), p): # p, 2*p, 3*p, ...
bool_list[i] = False # do the assignment unconditionally
You probably shouldn't need a return statement, since you're modifying the list you are passed in-place. Returning the list could make the API misleading, as it might suggest that the returned list is a new one (e.g. a modified copy).
If you did want to return a new list, you could create one with a list comprehension, rather than modifying the existing list:
def mark_false_copy(bool_list, p):
return [x if i % p else False for i, x in enumerate(bool_list)]
I have a list of strings =
['after','second','shot','take','note','of','the','temp']
I want to strip all strings after the appearance of 'note'.
It should return
['after','second','shot','take']
There are also lists which does not have the flag word 'note'.
So in case of a list of strings =
['after','second','shot','take','of','the','temp']
it should return the list as it is.
How to do that in a fast way? I have to repeat the same thing with many lists with unequal length.
tokens = [tokens[:tokens.index(v)] if v == 'note' else v for v in tokens]
There is no need of an iteration when you can slice list:
strings[:strings.index('note')+1]
where s is your input list of strings. The end slice is exclusive, hence a +1 makes sure 'note' is part.
In case of missing data ('note'):
try:
final_lst = strings[:strings.index('note')+1]
except ValueError:
final_lst = strings
if you want to make sure the flagged word is present:
if 'note' in lst:
lst = lst[:lst.index('note')+1]
Pretty much the same as #Austin's answer above.
Trying to create an OOP using class to compare two words and return True if they're the same and false if not. Words can be anagrams/upper/lower case versions of each other and still be true (W1 = sTop, W2 = Pots, W1 == W2 result: True) I am new to coding, so I am struggling in the attribute part of this code. How do I get it to read the word as the same under these conditions.
I have gone through this site as well as others to find the structure and overall idea behind OOP and have pieced together what I think to be correct, however, I know it's not complete and will kick errors when I run it. I've tried calling the methods under str using my classes grade scope and it failed which I expected. Any help in explaining/writing this problem would be great. Please excuse my novice ability in coding.
class Word:
def _init_(self, word):
self.word = word
def _str_(self):
w1 == w2
return self.lower(word)
Expected outcomes:
Examples
word1 = Word("post")
word2 = Word("stop")
word1 == word2
Result: True
word1 = Word("")
word2 = Word("")
word1 == word2
Result: True
word1 = Word("aBlE")
str(word1)
Result: able
word1 = Word("able")
word2 = Word("baker")
word1 == word2
Result: False
word1 = Word("Hi there! :-)")
word2 = Word("Hit here! :-)")
word1 == word2
Result: True
Anagrams are words containing exactly the same letters, in the same numbers. You can write a function that takes in two words, sorts the letters, and compare them one by one.
def are_anagrams(word1, word2):
return sorted(word1.lower()) == sorted(word2.lower())
are_anagrams('abBa', 'BAba'), are_anagrams('abby', 'baba')
If you wanted to use a class, you could override the __eq__ dunder method that governs the behavior of operator ==:
Maybe something like this:
class AnagramWords:
def __init__(self, word):
self.word = word
self.cmp = sorted(self.word.lower())
def __eq__(self, other):
"""returns True if self is an anagram of other
"""
if isinstance(other, str):
other = AnagramWords(other)
if isinstance(other, type(self)):
return self.cmp == other.cmp
raise NotImplementedError(f'AnagramWord cannot compare to {type(other)}')
def are_anagrams(word1, word2):
return sorted(list(word1.lower())) == sorted(list(word2.lower()))
are_anagrams('abBa', 'BAba'), are_anagrams('abby', 'baba') # True, False
w1 = AnagramWords('AbBA')
w2 = AnagramWords('BBaa')
w3 = AnagramWords('bABy')
print(w1 == w2, w2 == w3) # True, False
print(w3 == 123) # NotImplementedError: AnagramWord cannot compare to <class 'int'>
First of all,
The dunder (double-underscore: '__') methods are special methods used for you or python to hook into your code, for example implementing __len__ on a class will allow you to run len(MyClass) instead of MyClass.__len__(). so in essence, your defining __str__ isn't your desired step.
Second, in you __str__ method you are trying to compare w1 == w2 which are 2 variables which you haven't defined or accepted as arguments to your function.
My answer is, not always do you need to use OOP, for example your case can be defined as a simple function as follows
edit
As I see I accidentally mixed up anagram with palindrome, I am adding the anagram version as well.
def is_anagram(w1, w2):
return sorted(w1.lower()) == sorted(w2.lower())
# and I am keeping just for reference sake the palindrome one.
def is_palindrome(w1, w2):
return w1.lower() == w2.lower()[::-1]
What I am doing in the anagram function is first I am lowercasing the words so I can compare the characters regardless of case, and then I am using the sorted function which takes a sequence (which str is a sequence) and sorts it), and then we compare the 2 to see if they are indeed anagrams of each other.
What I am doing in the palindrome function is accepting to strings and then comparing the lowercased version of w1 to the reveresed lowercase version of w2, I am reversing the string by using a slice which starts at the default index (implicit as it is blank before the first colon), default stop, and a step of negative 1, which in effect reverses the string.
In any case, I wish you great luck on your programming journey!
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.