Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!

S. Freeman

Greenhorn
+ Follow
since Sep 09, 2013
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
5
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by S. Freeman

Post increment: a = 0, a++ -> basically i am sending zero over-and over again because its a post increment. Brain-dead cause of confusion there...

HashSet<Integer> is very useful for the problem at hand, thanks.
8 years ago
Magic square - n × n(4 x 4 in this case) matrix that is filled with the numbers 1, 2, 3, . . ., n2
Conditions: 1.Are the numbers 1, 2, ..., 16 the input.
2. And is the sums of the rows, columns, and diagonals equal to each other?




Question/request for assistance one: i wonder if there is a more optimal way of comparing elements of two arrays. I predefined an array at line 67 expectedArr, and then removed elements from that array that fall into the right-input category. So basically i am wondering how can checking if one array contains all the elements of another arrays be done better, an easier way?

And at line 120. i first ran with return equalSumRowsColumns (arr, a++) && sumOfRow(arr[a]) == sumOfColumn(arr, a); which lead to the following error -> java.lang.StackOverflowError. But when i replaced it with a + 1 there is no error. I thought that maybe a++ 'instantly' changes the value of a so on next uses it is already updated when it shouldn't be, so i changed it to a + 1, but isnt a++ same as a + 1, confused...


Edit1: example of a magic square

8 years ago
Hello.
When i think about recursion it always comes to mind as something that will save "space" for me. I am extremely lazy and i want to type as little code as possible.
For a string to be a palindrome first and last letter must be the same, and then when you remove those two letters the string that remains must also have first and second letter that match, and so on....Until there is no more letters that can form a string or there is one "middle" letter. DOod, d = d > O = o, DOxOD > D = D O = O, x no need to compare since it is in the middle.
Also string is already and array of characters.

I am quite stubborn at times, to the point that i do not know when to stop and think about the problem at hand, but rather trying to brute-force it. The kind people on the java ranch are more than kind enough to point people in the right direction, but also they constantly remind us newbies about the importance of keeping a cold head and taking a step back and thinking about the problem. Also basics are very important.

When i think about programing i imagine a martial arts designed for working out my brain, and others(the people who usually post responses, who are more knowledgeable than me i view as master lvl, for example
Heena Agarwal, Stephan van Hulst would be an instructor at such a place roundhouse kicking explaining recursion), and to be that good i always thought that they must really work on their basics like strengthening their joins and muscle (understanding Strings,Arrays,primitive data types and loops), and then move to the more advance stuff like roundhouse kicks(recursion).
8 years ago
Assignment:
It is a well-researched fact that men in a restroom generally prefer to maximize
their distance from already occupied stalls, by occupying the middle of the longest
sequence of unoccupied places.
For example, consider the situation where ten stalls are empty.
_ _ _ _ _ _ _ _ _ _
The first visitor will occupy a middle position:
_ _ _ _ _ X _ _ _ _
The next visitor will be in the middle of the empty area at the left.
_ _ X _ _ X _ _ _ _
Write a program that reads the number of stalls and then prints out diagrams in the
format given above when the stalls become filled, one at a time. Hint: Use an array of
boolean values to indicate whether a stall is occupied.



To best explain what i want to ask advice on i will start by explaining my though process. I spent a lot of time trying to come up with the solution on paper(i do this first before actually trying to code since i noticed that it helps me think more than just mindlessly tweaking and trying to code on the run so to say). And for a LONG time i could not come up with an idea how to access last element of the array(since if element with index 9 is the last free spot (8 + 9) / 2 = 8.5 so basically an 8 because i am working with integers. Finally i gave up and just wanted to solve it already.
And the second issue i had is index out of bounds error reported for line 32. while(i < arr.length && !arr[i] ), that used to be while(!arr[i] && i < arr.length). I thought that it was an error by one and that i should just adjust the i < arr.length to i < arr.length - 1, but then i still would not be able to access the last element in the array(in my case the one with index 9).
So the next thought that came to mind is that the && does not check the second part of condition because i am trying to access an element that does not exist(instant error report, second part of the condition not even checked). So i decided to switch places of the two conditions because if i > arr.length the second part of the condition wont be checked because the first part is already false. But the value of i still reaches arr.length ( in my case 10). Which did allow me access the last element in the array.

But i do not feel like i came up with the solution, more that it somehow worked out because of tweaking, modifications. Any advice on how i should have approached solving this, or did i fail to grasp something about arrays that did not allow me to come up with the solution on the conceptual level ( did i used the right word there, basically i mean on paper).
I hope that my code is neat enough and sorry if i did not explain the issue that is bothering me good enough, English is not my native language.
8 years ago
Thank you!
I thought that they only point to the same array, and that by modifying the array the references would "update" themselves.
Gotta blame my imagination a bit for this, because i always thought of arrays as trains, with locomotive being the reference
and pulling the wagons no matter how many there are. Did not know that i needed to tell the locomotive how many too pull.
8 years ago


Input: 1 2 3 4
Output: [1, 2]

To me it seems that the if statement is not accessed, and of course i do not know why. Can someone tell me why is that? Or even better if i missed something, point me in the right direction because i don't see why the length of my array stays the same.
8 years ago
After reading the Fizz Buzz test, i thought that even i could solve it, but because i was on the bus (without access to a PC until i got home) i started thinking about a solution.
And when i thought of one because i am a beginner i started doubting myself. There must be something that i am did not get right.. Anyway i got home an finally and ran my program..


Write a program that prints out the numbers from 1 through 100, but…

For numbers that are multiples of 3, print “Fizz” instead of the number.
For numbers that are multiples of 5, print “Buzz” instead of the number
For numbers that are multiples of both 3 and 5, print “FizzBuzz” instead of the number.


Output:(for i <= 30, less spam)
1
2
Fizz.
4
Buzz.
Fizz.
7
8
Fizz.
Buzz.
11
Fizz.
13
14
FizzBuzz
16
17
Fizz.
19
Buzz.
Fizz.
22
23
Fizz.
Buzz.
26
Fizz.
28
29
FizzBuzz



Dunno if links to other sites are allowed ,http://www.globalnerdy.com/2012/11/15/fizzbuzz-still-works/
(i am not trying to help the dude get more hits).
I am not trying to brag or boost my self confidence, only practice and work will help me with those BUT
I am just genuinely wondering(since the dude made such a big deal of it in his post) if there is something that a beginner can learn from this test? Or is it just a simple problem with a simple solution?
8 years ago
I forgot about this,anyway have to say thanks for advice and help, was useful.

Sharing code in white text color so select if you want to see it.

for(int i = 1; i <= word.length(); i++)
{
for(int j = 0; j < word.length(); j++)
{
if(i + j <= word.length())
{
System.out.println(word.substring(j , j + i ));
}
}
}
8 years ago
This is what i came up with after reading your advice/hint


Thought process: Because i cant be adding values inside the substring to select a character/characters, shouldn't i just increase the value of i so that i can select what i need to. Content wise, I get the correct
Output:
q
qw
w
qwe
we
e

but as you can see it is not sorted. I guess that i should sort it outside of the nested loops or did i miss something?
Damn its too late and i am starting to shutdown (keep pressing ctrl + F11 to preview this post) anyway...

Thank you for your replies they are/were useful.

8 years ago
Assignment:
Write a program that reads a word and prints all substrings, sorted by length. For
example, if the user provides the input "qwe", the program prints
q
w
e
qw
we
qwe


Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at line 5

My understainding and logic behind my attempt at solving this problem.
In the first iteration of the outer loop, inner loop should get me an output of all characters in the String word. Second iteration of the outer loop should output two-characters substrings of the String word. And in the third iteration i get an error because i am trying to access a character that is at position > word.length().This assignment is in a section about loops and asks for the use of substring in solving it.

I would greatly appreciate it if someone could help me get a better grasp of these basics / point me in the right direction or critique my faulty logic.
8 years ago
I must say that i didn't expect such a fast and great response Steve, thank you it really helped, i was worried that my understanding of boolean operations sucked, but it seems that my approach to solving the problem and logic behind my thoughts was the primary reason for my fail.

Thanks!
8 years ago
hehe.. Howdy! i am trying to get a better grasp of Boolean conditions so i can come up with solutions to problems.
While doing some programming exercises i ran into following assignment. Anyway, right to business:

Write a program that reads four integers and prints “two pairs” if the input consists
of two matching pairs (in some order) and “not two pairs” otherwise. For example,
1 2 2 1 two pairs
1 2 2 3 not two pairs
2 2 2 2 two pairs
x y z k

My attempt:

For the inputs that are shown as examples my Boolean condition enters the if block as it should. But in case i enter 1 3 3 3, or 3 1 3 3, or 3 3 1 3
the if statement is still entered, and it shouldn't be since there are no two pairs and, so the else statements should be executed.

Could someone help me out understand how i should come up with correct boolean condition.
8 years ago