#Recursive Method to find if 2 numbers have the same length

117 messages · Page 1 of 1 (latest)

odd axle
#

i tried something and it didnt work idk why

eternal locustBOT
#

This post has been reserved for your question.

Hey @odd axle! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

plucky void
#

you're duplicating code there, the part before isSameLen is the same as after

#

you need to return the result of isSameLen, but you're overwriting e in most cases

#

you don't need a lot of this
you just need the first if, else if, then after that you can just return isSameLen(...); directly

odd axle
#

how do i do that

plucky void
#

just remove stuff

odd axle
#

k

plucky void
#

you have everything you need, the problem is you have too much that's breaking it

odd axle
#

btw the nums are

#

idk what to remove

odd axle
#

like this?

plucky void
#

no, keep the first 2 if/elses

#

e is unnecessary

#

the last if check is unnecessary, do return isSameLen(...); unconditionally

odd axle
#

like this?

plucky void
#

you're overcomplicating it

#

please

#

read what im saying exactly

#

you need exactly 1 if, 1 else if, and the 3 returns

odd axle
#

like this??

plucky void
#

yes

odd axle
#

this one returned true

plucky void
#

remove boolean e

odd axle
#

i did

plucky void
#

do you want an explanation of this

odd axle
#

yes

#

would be helpful

plucky void
#

the if and else if are the base cases of the recursion, they say where to stop

#

we use recursion to cut off 1 digit at a time here, so the base case is when either number gets down to 0

#

if both numbers get down to 0 at the same time, they're the same length; that's what the first if does

#

if one number gets down to 0 but the other isn't yet, they aren't the same length; that's what the else if does

odd axle
#

ahhhh

plucky void
#

otherwise, we haven't reached that base case yet; cut off a digit of each (the / 10) and check again

odd axle
#

i see

#

because when i return something it will return it all the way up the recursion

plucky void
#

yes

odd axle
#

do u think u could help me with another question if u dont mind?

plucky void
#

sure

odd axle
#

ok so

#

its a recursive method

#

where it gets a int[] of 0's and 1's

#

as parameter

#

the most left index

#

the most right index

#

and a int

plucky void
#

(recursive method btw, recursion is a noun, recursive is an adjective, recurse is a verb)

odd axle
#

that represents an index

#

my word isnt opening

#

hold on

#

ah basically

#

it return the streak of 1's

#

around the index int

#

so like lets say we have

#

0111110

#

and the int is 4

#

it should return 5

#

if the int lands on a zero its an instant false

plucky void
#

does it need to be recursive?

odd axle
#

yes

plucky void
#

you mean a 0?

odd axle
#

yes

plucky void
#

...a single recursive method

#

dear god

odd axle
#

its a bonus question

plucky void
#

that's going to be kinda cursed as a single method

odd axle
#

if i can do that i will be ready for my test

plucky void
#

i guess you'd have to do counting forwards and backwards

odd axle
#

lemme send u the word file

#

scroll all the way down on it

#

my office isnt working rn idk why

plucky void
#

can't you just send it as text

odd axle
#

not letting me open it

plucky void
#

oof

#

590 B looks wrong for a word document

odd axle
#

lemme try to fix my office then just wait a sec

#

fixed

#

on the right is what it is supposed to return

#

idk why the fuck the left and right is there

plucky void
#

ohhhh i see

#

you're supposed to count from left to right, decrementing left and k

#

that's how the recursion is achieved

plucky void
odd axle
#

im figuring i need a count int

#

oh wait

#

what if

#

i move the int left

#

to the first 0 left of the int k

#

and right

#

ahhhh

#

something like this

#

then if A[left] is a zero i stop moving it

#

and i check from k

plucky void
#

there's an issue with having 1s before k there i guess

#

well i can see why it's a bonus question lol

odd axle
#

first of all

#

if (A[k] == 0) return false;

#

i mean return 0

#

do u think u know how to do this

plucky void
#

im really tired right now lol, nothing's coming to mind

odd axle
#

i have this so far

#

nah wait

#

i need to start left and right from k

plucky void
#

i think figuring out some base cases would help

odd axle
#

what do u mean?

odd axle
#

i wrote this

#

tho its bad cuz count is reset every time