Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!

since Feb 08, 2017

Portugal

Cows and Likes

Cows

Total received

10

In last 30 days

0

Total given

0

Likes

Total received

5

Received in last 30 days

0

Total given

10

Given in last 30 days

0

Forums and Threads

Scavenger Hunt

Ranch Hand Scavenger Hunt

Greenhorn Scavenger Hunt

Thanks all for the tips.

I've worked out another version of my code. It's basically the same as before (no performance tweaks) but tried to simplify the code and make it more readable...

And no more static methods... (thanks Liutauras).

Best regards

Carlos

I've worked out another version of my code. It's basically the same as before (no performance tweaks) but tried to simplify the code and make it more readable...

And no more static methods... (thanks Liutauras).

Best regards

Carlos

11 months ago

Thanks for the cow Campbell. Appreciate it!Campbell Ritchie wrote:Well done getting that far and for all the searches you have done. I think your effort so far merits recognition.

Many questions here... I'll try to answer...

The choice of having the methodsCampbell Ritchie wrote:Is that conceptually a pure function with no side-effects? If so, it is reasonable to make all methods static. Or is there some piece of information linked to several sizes of triangle, in which case you should maybe consider an object solution.

The main purpose there isn't the "beauty" of the code but the results. I agree with you though. By the way, will this type of changes affect the speed? (newbie question ).Campbell Ritchie wrote:I think you should move all that code out of the main method, anyway. Consider making the methods smaller, but more of them.

It's a bit complicate yes. But it's due to the way we get the values in the sequence. It's different for odd and even numbers. But i'll think about ways of simplifying it.Campbell Ritchie wrote:Have a look at the loop in lines 17-34. That looks complicated, but I think it can be simplified. You know you can speed up i % 2 by using (i & 1) == 0 (or != 0 or similar)?

Thanks for the info. Now that you mention it, makes sense that a bit manipulation would be faster than a remainder operation...Campbell Ritchie wrote:You know you can speed up i % 2 by using (i & 1) == 0 (or != 0 or similar)?

If there is than didn't find it yet... Must think about it.Campbell Ritchie wrote:Is there a faster way of finding τ0 than a linear search?

This one i didn't understand... What prime numbers? I use prime factorization in τ0 yes. But i did that cause i think it is faster than finding the divisors using the brute force remainder opperation from 2 toCampbell Ritchie wrote:What is that about prime numbers in the integer list link you gave us?

I didn't even consider using a Map. I can use the indexes of theCampbell Ritchie wrote:Why are you specifying a particular kind of List? I would have thought that a List was better than an array for most things, but I'll let you work out the details for yourself. Yes, there is no point in working out the same numbers twice, but please work out why a List would be a better store for numbers than something like a Map.

I talked about an

The eternal question of variable names...Campbell Ritchie wrote:Note how choice of names for booleans makes the loop hard to read; it would be easier to read with needsFinding instead of notFound.

Well maybe... Since we need to square the number to calculate τ0 i guess that at some point it will go beyond the limits ofCampbell Ritchie wrote:Are you sure that all numbers will fit within the bounds of a

long? What about τ0, which looks a bit like a factorial. Do you need a BigInteger object to handle those numbers?

The good thing i can remember about using

Thanks for the insights Campbell!

Best regards

Carlos

11 months ago

Good morning guys and gals!

I've been away for a bit but i'm returning with a pled for some help...

I did a challenge in Hackerrank and i'm having some problems with timeouts...

The problem there is this one: Project Euler #176: Rectangular triangles that share a cathetus.

It's a programming version of Problem 176 from projecteuler.net.

After some research i found this integer sequence that gives the required number of rectangular triangles wich share a cathetus. If you see there in the`FORMULA` section, we can get this sequence using the Tau function (also known as Divisor Function). After this i needed a way to count the divisors of a given number and found this page at mathschallenge.net. I used this method in my code.

So the code is:

I used`long` varibles since the numbers can exceed the `Integer` maximum value.

The commented lines in my code are the ones needed in Hackerrank to get the input values. I left them just for reference and hardcoded the number of tests (line 7) and the number of triangles (line 12) for testing purposes.

This code works fine. If you run it like it is, it gives the correct answer for the cathetus that is in 4 rectangular triangles (12). The problem is that there can be many tests and numbers can get very large and, therefore, i'm geting timeouts.

I think that the main situation here is that for every test i'm computing the Tau sequence from the start. So i thought that it could help if i memorized all the sequence elements allready computed and, in every next test, only compute the next ones if the answer wasn't found in the allready computed.

What do you think? Should i use an`ArrayList` since a common `Array` has fixed length?

Are there any other improvements that can be made?

Thanks for your help.

Best regards

Carlos

I've been away for a bit but i'm returning with a pled for some help...

I did a challenge in Hackerrank and i'm having some problems with timeouts...

The problem there is this one: Project Euler #176: Rectangular triangles that share a cathetus.

It's a programming version of Problem 176 from projecteuler.net.

After some research i found this integer sequence that gives the required number of rectangular triangles wich share a cathetus. If you see there in the

So the code is:

I used

The commented lines in my code are the ones needed in Hackerrank to get the input values. I left them just for reference and hardcoded the number of tests (line 7) and the number of triangles (line 12) for testing purposes.

This code works fine. If you run it like it is, it gives the correct answer for the cathetus that is in 4 rectangular triangles (12). The problem is that there can be many tests and numbers can get very large and, therefore, i'm geting timeouts.

I think that the main situation here is that for every test i'm computing the Tau sequence from the start. So i thought that it could help if i memorized all the sequence elements allready computed and, in every next test, only compute the next ones if the answer wasn't found in the allready computed.

What do you think? Should i use an

Are there any other improvements that can be made?

Thanks for your help.

Best regards

Carlos

11 months ago

Good morning Jiyoung.

Take a look at line 6... Where are you storing the result of the addition? Nowhere...

Now compare line 6 with what you did in line 7. See the difference?

Best regards

Carlos

Take a look at line 6... Where are you storing the result of the addition? Nowhere...

Now compare line 6 with what you did in line 7. See the difference?

Best regards

Carlos

1 year ago

moreSymbols? Didn't get what you mean...Liutauras Vilda wrote:@OP similar problem appears with moreSymbols, but there you'd suffer from exception.

1 year ago

As I said in my first post this version assumes that input strings are correct. The verification for correctness will be the next stage...

1 year ago

Just realized that i don't need static methods in `MorseSymbols` class...

1 year ago

Junilu Lacar wrote:You could also use a proper Map<Character, String>.

i didn't get to HashMaps in the book yet... Just Arrays and ArrayLists. So i didn't want to use them.

1 year ago

I've done some changes. Created a new class for Morse Symbols. Here are my changes (the Test class didn't change so won't put it here).

No magic numbers allready...

Best regards

Carlos

No magic numbers allready...

Best regards

Carlos

1 year ago

Thanks guys for the inputs.

back to the drawing board then.

Best regards

Carlos

back to the drawing board then.

Best regards

Carlos

1 year ago

The explanation is in the comments!Norm Radder wrote:An explanation for the magic numbers: 48 and 55 would be useful.

Could the 48 be '0'?

Is the 55 'A' - 10?

char values are numeric and can be used directly without using their int values.

But basically it's the difference between the ASCII code of the characters and their corresponding index in the array, so:

ASCII of '0' = 48

Index of 1st digit in array = 0

Diff = 48 - 0 = 48

ASCII of 'A' = 65

Index of 1st letter in array = 10

Diff = 65 - 10 = 55

1 year ago

Campbell Ritchie wrote:Why do you think a List is better than an array? I am not convinced that follows.

Well i thought that the

1 year ago

Sorry... Just added the comments.

I know that there are better ways to do this (with ArrayLists for example), but wanted to start with plain arrays first...

I know that there are better ways to do this (with ArrayLists for example), but wanted to start with plain arrays first...

1 year ago

Good morning all!

I did an exercise on a morse code converter with the following requirements:

The classes i coded,`MorseConverter` and `TestMorseConverter`, assume so far that **the input strings are correct** (only letters, digits and spaces as the requirements say), and are as follows:

What do you think about them?

Thanks

Best regards

Carlos

I did an exercise on a morse code converter with the following requirements:

Perhaps the most famous of all coding schemes is the Morse code, developed by Samuel Morse in 1832 for use with the telegraph system. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (e.g., period, comma, colon, semicolon). In sound-oriented systems, the dot represents a short sound and the dash a long sound. Other representations of dots and dashes are used with light-oriented systems and signal-flag systems. Separation between words is indicated by a space or, simply, the absence of a dot or dash. In a sound-oriented system, a space is indicated by a short time during which no sound is transmitted. Is to be used the international version of the Morse code (only characters and digits).

Write an application that reads an English-language phrase and encodes it into Morse code. Also write an application that reads a phrase in Morse code and converts it into the English-language equivalent. Use one blank between each Morse-coded letter and three blanks between each Morse-coded word.

The classes i coded,

What do you think about them?

Thanks

Best regards

Carlos

1 year ago

@James: I've been reading this book: Java How To Program (Early Objects) (10th Edition) and it has good explanations on how to make constructors (at least i find them good...). In chapter 3 it has the introduction to the constructors, setter and getter methods, and in chapter 8 it has a case study that talks about *overloaded constructors*, the same your teacher is asking you to do when he says to use the multi-argument constructor in the no argument one to set the default values.

If you can find this book i think it will help you to better understand the questions.

Best regards

Carlos

If you can find this book i think it will help you to better understand the questions.

Best regards

Carlos

1 year ago