How do I handle cases with numbers or the other cases? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I handle cases with numbers or the other cases?

I am doing a code coach, and I have passed half the cases, but there seem to be cases with digits, as outlined in the brief. Here's my attempt. str_input = input().split(" ") def mutate(x): start = 0 end = 1 while start < len(x): i = 0 while i < len(x): yield x[start:end] end += 1 i += 1 start += 1 mutations = [] for i in str_input: for j in mutate(i): mutations.append(j) commons = [] for i in mutations: common = set([x for x in str_input if(i in x)]) if len(common) == len(str_input) and len(i) > 2: commons.append(i) res = [x for x in commons if commons.count(x) > 1] res = list(sorted(res, key=str.lower)) res = set(res) res = list(res) if len(res) > 1: res = list(sorted(res, key=len)) res = list(sorted(res, key=str.lower)) print(res[0]) else: print(res[0])

25th Mar 2023, 5:08 PM
William Mabotja
William Mabotja - avatar
27 Answers
+ 7
the code coach is: *longest common substring* ^^^^^
25th Mar 2023, 7:11 PM
Lothar
Lothar - avatar
+ 6
some thoughts from my side: > using permutations for this task does create too many unnecessary samples, since it creates all possible *combinations*. > what we need to use are only samples that have partially the same sequence in all strings. > using this strings ['lean', 'isle', 'clean'] to find the longest common substring is giving the result: 'le'. ^^ ^^ ^^ ^^ > a recommended way to get this done is using 2 nested for loops with a *sliding and sizeable window* that can cut the chunks for the comparison. each of these samples has to be taken and checked if it is in the supplied strings. here a sample code that should work: https://code.sololearn.com/cPB4r3J6SiMC/?ref=app
27th Mar 2023, 6:12 PM
Lothar
Lothar - avatar
+ 3
Which task is it you're trying to solve?
25th Mar 2023, 5:19 PM
Ipang
+ 3
I think you are complicating the simple task even for large inputs..! Some observation! do you don't need to reset end value in inner while loop like start ? for i in str_input : why to all list of string to mutate? str_input[0] is not enough, first string?? Hope it helps, 🤔not confuse instead...
26th Mar 2023, 12:13 PM
Jayakrishna 🇮🇳
+ 2
What's your first for loop intention? And why you are applying set on for 'commons'...? I think it's leading to wrong output.. if am understood correctly : 'aba' should be compared totally, applying set makes compare list of 'ab' only.. . Taking all individual character from entire input is no need. Simple approach: compare substring from word1 in other words just and take max length matched string.
26th Mar 2023, 6:22 AM
Jayakrishna 🇮🇳
+ 2
sry, iam not able to understand your approach, why permutations are needed there in the task..! Ok. I hope this sample may helps you find mistake by your own. I tested sample as 12 123 1234 It should output common substring as 12 but return empty 'res'. So res[0] is an error.
26th Mar 2023, 11:43 AM
Jayakrishna 🇮🇳
+ 2
Once try : if len(common) == len(str_input) : Instead of if len(common) == len(str_input) and len(i) > 2:
26th Mar 2023, 12:55 PM
Jayakrishna 🇮🇳
+ 2
William Mabotja Last one i guess, that you are sorting by len so it will be ascending order. Last one will be longest one, not the first.
27th Mar 2023, 12:17 PM
Jayakrishna 🇮🇳
+ 2
So then try another : Don't convert to lower , output should in original input. .
27th Mar 2023, 12:46 PM
Jayakrishna 🇮🇳
+ 2
if common substrings are multiple like "xyz", "abcd" then smallest one in alphabetical order is "abcd".. Save code and share link, that easies debug...
27th Mar 2023, 3:21 PM
Jayakrishna 🇮🇳
+ 1
Longest Common Substring
25th Mar 2023, 6:05 PM
William Mabotja
William Mabotja - avatar
+ 1
I have to use set on commons because the resulting list includes duplicates. The intention is to create all possible permutations for a string, and then ensure that the permutation occurs in each item in the list. if it occurs in all items, it is therefore common. Then I proceed to check the list's length, if it is above one. If that's true, I get the smallest item in alphabetical order, and print that out. I understand what you're saying on the suggested approach, but I'm making room for large inputs, and inverse matches.
26th Mar 2023, 8:13 AM
William Mabotja
William Mabotja - avatar
+ 1
It turns out that it's nit numeric data that's the issue, made some changes and got 4/6 cases right. By Moore's law, I could be close. # Grab input and split to list str_input = input().split(" ") def mutate(x): """ Creates all possible permutations for a given string. """ start = 0 end = 1 while start < len(x): i = 0 while i < len(x): yield x[start:end] end += 1 i += 1 start += 1 # List to hold all possible permutations mutations = [] # Run generator function on all list elememts and append to mutations list for i in str_input: for j in mutate(i): mutations.append(j) # List to hold common petmutations commons = [] # Find words that occur in each list item for i in mutations: common = set([x for x in str_input if(i in x)]) if len(common) == len(str_input) and len(i) > 2: commons.append(i) # Remove words that are less than 1 in length
26th Mar 2023, 11:02 AM
William Mabotja
William Mabotja - avatar
+ 1
Thanks for the tip, all I had to change was len > 2 to len > 1, and worked for that case, and the other cases still worked. But the solution still fails. I even tried another case from the forum ["abcekde", "acekt", "zaqacekyi"] And managed to fix that bug too, but the solution still failed. I'll keep checking. Thanks.
26th Mar 2023, 11:55 AM
William Mabotja
William Mabotja - avatar
+ 1
I'll try that
26th Mar 2023, 12:27 PM
William Mabotja
William Mabotja - avatar
+ 1
It breaks my pseudo code. It's a different code altogether. I've seen comments in the forum about complexity, and pushing the compiler. That could be the issue.
26th Mar 2023, 12:33 PM
William Mabotja
William Mabotja - avatar
+ 1
If I don't get it working I'll rewrite that way
26th Mar 2023, 12:34 PM
William Mabotja
William Mabotja - avatar
26th Mar 2023, 9:27 PM
Chuks AJ
Chuks AJ - avatar
+ 1
Thanks Jayakrishna 🇮🇳 , got 5/6 cases correct, left with test case #4
26th Mar 2023, 9:40 PM
William Mabotja
William Mabotja - avatar
+ 1
Thanks, but I've already used reversed=True on the sort to make the first item the longest
27th Mar 2023, 12:25 PM
William Mabotja
William Mabotja - avatar