Hi, im trying to do a kata in codewars, specifically this: https://www.codewars.com/kata/566859a83557837d9700001a and im struggling with making the alg for founding the substring of the input number... ive tried doing it like this: https://paste.pythondiscord.com/UIEA but the output is not what im expecting, thats because of the indexes, im sure about that, but how can i solve this problem? ive been trying for hours really... is there a built in way to print all the substrings of a string? As you can see the output is not as expected... please help
#๐ I need help with a kata in codewars
64 messages ยท Page 1 of 1 (latest)
@ionic hound
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
this is what the output should look like:
> 1
> 7
> 6
> 8
> 9
> 7
> 5
> END OF FIRST CICLE
2nd cicle:
> 17
> 76
> 68
> 89
> 97
> 75
> END OF SECOND CICLE
...
...
...
final cicle:
> 176897
> 768975
> END OF FINAL CICLE```
find all substrings in a
str
ifiandjare indices, thens[i:j]will be a substring ofs.
to get all the substrings, you just need to iterate through all possibleiandj
nope not like this, thats what im struggling about, the kata says i have to find the substrings following a specific order.
where specifically?
well, i have to first found the single number substrings, then two numbers substring, then three...all the way to the full string
like this:
why do you have to do that
as in, why do you need to find the substrings in that order?
i need to do that because the kata is about dividing the number given by all the substrings that number could have, as represented in the example given in the kata
ok, so say you have a number 123
why must you get the substrings in this order and check divisibility: [1, 2, 3, 12, 23, 123]
and not [1, 12, 123, 2, 23, 3], or any other order?
i thought it would've been an error to do that instead of the other way
no?
the example in the question is showing you that order, yes, but no one's stopping you from doing it a different way as long as you get the same result
the output of the kata simply expects you to return how many of these substring-numbers can divide the original one
is this the way you intended the algo to be? ```py
def algorithm(number):
cicle = 0
substring = []
while cicle != len(number):
for n in range(len(number)):
for m in range(len(number)):
substring.append(number[n:m])
cicle += 1
that's one possible way, but you can also do something else if it works as well
also you only need the n and m loop, the while cicle loop is redundant here
with
for n in range(len(number)):
for m in range(len(number)):
```, you've already iterated through all possible pairs of indices of `number`(`str` type)
though do be careful, cause right now you can also have a situation where n > m, like n=3, m=2
think about how you can get rid of this and you're one step closer to passing
ohhhh got that
thanks
i always search for the hardest way to do things, dont know why...
for these online judge sites, they usually don't care about how you got to your solution, as long as the result is right (unless specifically stated, or say your algorithm runs too slow)
on another note, if you want to go through all 1-length substrings, then 2-length, then 3, ...
instead of iterating through all possible indices i j and taking s[i:j]
you can iterate through all possible starting indices i, and the length of the substring l, and take s[i : i+l]
i dont understand how would that work, doesnt the len of the string remain always the same?
sure, but the length of the substring can change
you can iterate through how long you want your substring to be, i.e. 1, 2, 3, 4, ..., len(number)
and iterate through the starting index of the substring, 0, 1, 2, 3, ...
then the substring starting at i with a length of l, is s[i : i+l]
that would need a while tho
it could be a while, but you can also do it with a for
with a range function
thanks!
the list was being filled with values that were already in so ive solved that with an if number[n:n+l] not in substring: and it worked! Thanks for your help!
i thought i was done but i was wrong... the main alg is alright and functions correctly the problem is when the number in input is like 11 or 22 or when a number contains two equal numbers. Thats a big problem because the if wouldnt let that be appended in the substring list... only with numbers that have 1 unit only tho...
but even with a number like 766667 it would give me problems because 66 would already be in the list so it wouldnt let the other 66s in it
do you have any suggestions? @ivory leaf
don't worry about duplicate values when appending, just remove the duplicates at once when you're done doing so
the easiest way to do this is probably with set
!e
l = [1, 2, 2, 3, 4, 1]
s = set(l)
print(s)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
{1, 2, 3, 4}
or is the correct thing you need, multiple 66s when given 766667?
cause then I'm confused, you're sort of contradicting yourself
the list was being filled with values that were already in so... [solution to get rid of that]
but later
66 would already be in the list so it wouldnt let the other 66s in it
I got off my pc to do something, when Iโm back Iโll show you what I mean
Cause I need to show you the output
here i am
so
if i remove the if and run the code like this: ```py
def algorithm(number):
substring = []
for l in range(1, len(number)):
for n in range(0, len(number)):
#if number[n:n+l] not in substring:
substring.append(number[n:l+n])
return substring
the substring looks like this: ['1', '7', '6', '8', '9', '7', '5', '17', '76', '68', '89', '97', '75', '5', '176', '768', '689', '897', '975', '75', '5', '1768', '7689', '6897', '8975', '975', '75', '5', '17689', '76897', '68975', '8975', '975', '75', '5', '176897', '768975', '68975', '8975', '975', '75', '5']
as you can see the 5 repeats itself multiple times, and that is incorrect.
for n in range(0, len(number)):
change up this range a little
you're seeing some wrongly-lengthed substrings cause your n went too far to the end
the end should depend also on l
if I add the if it looks like this: ['1', '7', '6', '8', '9', '5', '17', '76', '68', '89', '97', '75', '176', '768', '689', '897', '975', '1768', '7689', '6897', '8975', '17689', '76897', '68975', '176897', '768975'], but its already missing a 7, the number i gave to him was 1768975 but in the one digits substrings is not there
remove the if and think about what I said
as a demonstration, something like this is happening
l = 3
number = 123456
^ ^
i i+l
# => results in 56
you should prevent i+l from going over the entire number like this altogether
well, ill try to make a way to do this then...
oh my, i think i did it
made it like this:
def algorithm(number):
substring = []
for l in range(1, len(number)):
for n in range(0, len(number)):
if l+n <= len(number):
substring.append(number[n:l+n])
return substring
and i think it works perfectly, the output looks like this: ['1', '7', '6', '8', '9', '7', '5', '17', '76', '68', '89', '97', '75', '176', '768', '689', '897', '975', '1768', '7689', '6897', '8975', '17689', '76897', '68975', '176897', '768975']
yeah already made that one! ill finish the kata and tell you if it passes all the tests
it works, the only problem is the modulo by 0 that i didnt make in the divisibility function, going to add that one in a moment
IM SO HAPPY I SOLVED THIS, ive really appreciated your help, THANKS!
np
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.