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;
}
}
#i <= newArr.length+start-1 . I abit confused with this formula.
1 messages ยท Page 1 of 1 (latest)
โ This post has been reserved for your question.
Hey @deep zephyr! Please use
/closeor theClose Postbutton 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.
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?
you kinda don't, that's kinda poorly written
it should be more like i < newArr.length + start, or just i < end
yeah chatGPT wrote it and confused the hell out of me hahah
but still, array indexing of newly created array starts from 0, it's not iterating through existing array with provided start and end
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
and this is why you don't use chatgpt
chatgpt certainly isn't going to help it
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?
neither
i = start is only the initial value
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?
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
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
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.
which I am very proud about
yeah
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
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?
nowhere
there's 7 numbers
there's 6 non-zero numbers
subtraction excludes both ends, but ranges in programming include the start
I think for the sake of simplicity let's do 1,5
wait no, misread
start = 1, end = 5
give me a sec
the first element of newArr is at index 0
sure
which excludes 8 and makes it 7
you're thinking ofi = 1as a start here
suppose a higher start, i = 5; i < 10
this has 5 numbers, 5, 6, 7, 8, 9
equal to 10-5
ok so start is 5 and end is 10 right?
yeah
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?
yeah
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?
noone.
usually i <= end for explicitness, but yeah i < end + 1 would also work
choose between < and <= for start/end number inclusion
choose between n and n - 1 for element inclusion
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.
Do you know objects and classes?
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
what about it?
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
python classes are implemented in a way that... idk how to describe it. painful, i guess
it's a bastard of OOP lmao
tbh I noticed that I understand for loop better in Java than in python
and all those indentations in Python geez
welp, good that you haven't gotten to classes in python yet
they enforce good formatting, they aren't all bad
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
person/people is probably a bad example to get started on tbh
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
plural variable names (when there's a singular counterpart) generally refers to a structure holding the singular form of that name
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
yeah this one I get / Car[] cars and Car()
how long did it take you to understand objects, constructor, deep copying , getter and setter? if you remember of course
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
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
100% agreed
but you know it is really hard emotionally so I guess I seek comfort
fair enough
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"
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
Do you work with Java? if yes how long have you been working as a developer? idk about the names of the profession yet
i don't do programming professionally, no
yes that is what I am doing now. Like in school hehe
oh ok
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
what? haha
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
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 ๐
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.
Good night ๐ด