#i <= newArr.length+start-1 . I abit confused with this formula.

1 messages ยท Page 1 of 1 (latest)

deep zephyr
#
import java.util.Arrays;

public class Main{
    public static void main(String[] args) {
        System.out.println(Arrays.toString(fizzBuzz(2, 8)));

    } 
    public static String[] fizzBuzz(int start, int end) {
        String[] newArr = new String[end-start];
        for (int i = start; i <= newArr.length+start-1; i++) {
            if (i%3==0 && i%5==0) {
                newArr[i - start] = "FizzBuzz";
            } else if(i%3==0){
                newArr[i - start ] = "Fizz";
            }else if (i%5==0){
                newArr[i - start] = "Buzz";
            } else {
                newArr[i - start] = Integer.toString(i);
            }
        }
        return newArr;
 }
}      
pearl creekBOT
#

โŒ› This post has been reserved for your question.

Hey @deep zephyr! 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.

deep zephyr
#

if i = start it means that both i and start are 2 in this case

#

the length of the arr is 8 - 2 which is 6 but why do we add 2 to 6 and then subtract 1?

hoary rock
#

you kinda don't, that's kinda poorly written

#

it should be more like i < newArr.length + start, or just i < end

deep zephyr
#

yeah chatGPT wrote it and confused the hell out of me hahah

plush pilot
#

but still, array indexing of newly created array starts from 0, it's not iterating through existing array with provided start and end

hoary rock
#

newArr.length = end - start, end = newArr.length + start
i < end is equivalent to i <= end - 1 (because i is an integer)
thus, i < end is equivalent to i <= newArr.length + start - 1

hoary rock
deep zephyr
#

tbh my math is reaaaly bad

#

that is why I struggle to understand these basics

hoary rock
#

chatgpt certainly isn't going to help it

deep zephyr
#

well so far it has been quite helpful haha but yeah I agree

#

the thing is I am confused about which i is index and which i is start

#

or if i = start it will always be start?

hoary rock
#

i = start is only the initial value

deep zephyr
#

so i = start which is 2 in our case will i++ so in the next iteration it will be 3 then 4 and so on?

hoary rock
#

yes

#

i is used to derive the index, but it isn't used as the index directly, there's an offset of start

#

do you know how a typical array loop would look like

deep zephyr
#

I am used to i = 0 but sometimes i needs to be initiated to something else

#

yeah

#

I even solved reverse array problem myself haha

pearl creekBOT
deep zephyr
#

which I am very proud about

hoary rock
#

notice how, compared to a normal loop, the start and end have start added, and inside the loop, i has start subtracted

#

that normalizes the value to get a proper index

deep zephyr
#

wait a moment I will try to understand i < end

#

ok so if start is 2 and end is 8

#

then i = 2; i < 8 which excludes 8 and makes it 7

#

but we only have 6 numbers coz 8 - 2 is 6

#

where will the remaining one go?

hoary rock
#

nowhere

#

there's 7 numbers

#

there's 6 non-zero numbers

#

subtraction excludes both ends, but ranges in programming include the start

deep zephyr
#

I think for the sake of simplicity let's do 1,5

hoary rock
#

wait no, misread

deep zephyr
#

start = 1, end = 5

hoary rock
#

give me a sec

deep zephyr
#

the first element of newArr is at index 0

deep zephyr
hoary rock
#

which excludes 8 and makes it 7
you're thinking of i = 1 as a start here

#

suppose a higher start, i = 5; i < 10

#

this has 5 numbers, 5, 6, 7, 8, 9

#

equal to 10-5

deep zephyr
#

ok so start is 5 and end is 10 right?

hoary rock
#

yeah

deep zephyr
#

yes

#

i < end at first iteration it is 5 < 10

#

then 6<10, 7 8 9< 10

#

and 10 is excluded of course

#

i - start is 5 - 5 then 6 - 5 so we get 0 and 1 indexes and put 5 at index 0 and 6 at index 1 and so on until 9

#

right?

hoary rock
#

yeah

deep zephyr
#

omg it is so simple jesus

#

like who would come up with a monstrosity like newArr.length+start-1

#

tbh I still do not get what this is newArr.length+start-1

#

ok so basically

#

if we need something from start to end like in this example and not including the end we make i<end and if we wanna include the end we make either i<end+1 or i<=end correct?

hoary rock
#

choose between < and <= for start/end number inclusion
choose between n and n - 1 for element inclusion

deep zephyr
#

yes I would use this one as well i <= end

#

thank you so much

pearl creekBOT
# deep zephyr thank you so much

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

deep zephyr
#

Do you know objects and classes?

hoary rock
#

i mean, i kinda have to..

#

kinda the entire soul of java

deep zephyr
#

haha well I am a beginner as you can see

#

so still struggling with some puzzles/concepts

#

the problem with classes section in my course is the naming convention

hoary rock
#

yeah, that's understandable for a beginner

#

the first language is always the hardest

deep zephyr
#

Person person = new Person(people[person]) person person perspon blah blah hahaa

#

I used to learn Python last fall

#

but then I had to stop for 3 months and now I am back but this time I decided to switch to Java

hoary rock
#

oh god...

#

did you learn about classes in python

deep zephyr
#

just a tiny little bit so I would say no

#

why?

hoary rock
#

python classes are implemented in a way that... idk how to describe it. painful, i guess

#

it's a bastard of OOP lmao

deep zephyr
#

tbh I noticed that I understand for loop better in Java than in python

#

and all those indentations in Python geez

hoary rock
#

welp, good that you haven't gotten to classes in python yet

hoary rock
deep zephyr
#

yeah but being afraid of tabs vs spaces

#

is kinda scary haha

hoary rock
#

it isn't though

#

using a single style is proper formatting

deep zephyr
#

so here is a piece of code from my class

    public Person getPerson(int index){
        Person person = people[index];
        return new Person(person);
    }

    public void setPerson(Person person){
        int index = person.getSeatNumber()-1;
        people[index] = new Person(person);
    }```
#

like the naming is just frustrating

hoary rock
#

person/people is probably a bad example to get started on tbh

deep zephyr
#

and trying to decipher which is which and like the topic of arrays with objects inside is painful now for me

#

we had cars first

hoary rock
#

plural variable names (when there's a singular counterpart) generally refers to a structure holding the singular form of that name

deep zephyr
#

like it's been a week or more since I started OOP and I thought I would understand it all and move on to the next topic but here I am rewatching stuff everyday trying to understand what is going on

deep zephyr
#

how long did it take you to understand objects, constructor, deep copying , getter and setter? if you remember of course

hoary rock
#

for java, probably within a few minutes; but that wasn't my first time learning the concepts
i learned them over weeks from js, wasn't really following any real course then

i have those answers but they aren't really useful to you, eh

#

why ask about other peoples' experiences though? they don't matter to you, or at least, they shouldn't; everyone learns at their own pace, everyone has a different background, different ways to learn, different ways to handle new information

deep zephyr
#

yeah I am happy that concepts are more or less the same which means I can learn another language once I am super confident in Java

deep zephyr
#

but you know it is really hard emotionally so I guess I seek comfort

hoary rock
#

fair enough

deep zephyr
#

for example this.everyone learns at their own pace, everyone has a different background, different ways to learn, different ways to handle new information

#

I feel better when I read something like this ๐Ÿ™‚

#

there is a lot of discouragement in the IT industry such as "you have to be good at math/ you have to be passionate about programming, you have to be this and that"

hoary rock
#

there is a barrier to entry, but it's not that drastic tbh

#

computer and internet literacy are must haves, but math knowledge isn't really required, it just makes it a lot easier; if you didn't know it before (and you actually care about learning) then you'll just spend some time learning that same math knowledge under the guise of programming instead

deep zephyr
#

Do you work with Java? if yes how long have you been working as a developer? idk about the names of the profession yet

hoary rock
#

i don't do programming professionally, no

deep zephyr
deep zephyr
#

programming has always been the last thing I was interested in and so was math and things like that, I think that is why my logical skills kinda suck

#

But life has its own ways you know

#

And now I am here and learning the things I was intimidated by all my life and thought I could never do it

#

And with each day I begin to believe that actually I can do anything

#

You just gotta want and work towards stuff you know

hoary rock
#

that's a great mindset to have

#

wish more people had that kind of mindset...

deep zephyr
#

I am trying

#

I have a friend who is a senior java dev

hoary rock
#

you really don't

#

oh god i misread that lmao

deep zephyr
#

what? haha

hoary rock
#

i read that as "i need a friend [...]" lmao, quite a people say that to mean they want help

#

god i should go to sleep soon, it's 2:30am here lol

deep zephyr
#

oh so yeah I asked him about help and he said once I get to a certain level he can give me real projects and then help me understand/solve them

#

it is 22:35 here

#

My brain is kinda not working too haha

#

it's been nice talking to you

#

thank you for your help and wisdom ๐Ÿ™‚

pearl creekBOT
deep zephyr
#

Good night ๐Ÿ˜ด