#๐Ÿ”’ I need help with a kata in codewars

64 messages ยท Page 1 of 1 (latest)

ionic hound
#

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

Codewars

You are given an integer N. Your job is to figure out how many substrings inside of N divide evenly with N.
Confused? I'll break it down for you.
Let's say that you are given the integer '87769...

barren meadowBOT
#

@ionic hound

Python help channel opened

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.

ionic hound
#

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```
ivory leaf
ionic hound
#

nope not like this, thats what im struggling about, the kata says i have to find the substrings following a specific order.

ionic hound
#

well, i have to first found the single number substrings, then two numbers substring, then three...all the way to the full string

ivory leaf
ionic hound
#

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

ivory leaf
ionic hound
#

i thought it would've been an error to do that instead of the other way

ivory leaf
#

the output of the kata simply expects you to return how many of these substring-numbers can divide the original one

ionic hound
#

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

ivory leaf
#

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

ionic hound
#

thanks

#

i always search for the hardest way to do things, dont know why...

ivory leaf
#

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]

ionic hound
ivory leaf
ionic hound
#

that would need a while tho

ivory leaf
ionic hound
#

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

ivory leaf
#

the easiest way to do this is probably with set

#

!e

l = [1, 2, 2, 3, 4, 1]
s = set(l)
print(s)
barren meadowBOT
ivory leaf
#

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

ionic hound
#

Cause I need to show you the output

ionic hound
#

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.

ivory leaf
ivory leaf
ionic hound
#

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

ivory leaf
#

you should prevent i+l from going over the entire number like this altogether

ionic hound
#

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']

ivory leaf
#

cool

#

now you just need to take those and check divisibility

ionic hound
#

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!

ivory leaf
#

np

ionic hound
#

!close

barren meadowBOT
#
Python help channel closed

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.