# Posts Tagged code

Interesting article about some bad programming practices, including topics like code organization, teamwork, testing and maintenance.

Here we have the K Palindromes puzzle from Javalobby:

You probably already know what a palindrome is: a string to results in the same word, whether read left to right, or right to left. A K Palindrome is when a string can be tranformed into a palindrome by changing K characters at most. Regular palindromes have K=0.

Your challenge today is to write a method which takes a string and a value for k and returns true if it the string qualifies as a K palindrome.

Below is the code in Ruby. Our input string is `omississimo` and it’s a `0 palindrome String`.

The output is `true`. Do you have another solution? Drop your comments here!

Folks, here we are for another puzzle from Javalobby:

Given two arrays, sorted and exactly the same length, write a function that finds a pair of numbers – one from each of the arrays – such that the difference between both is as small as possible.

Suppose our input arrays are `[1,2,3,4,5]` and `[6,7,8,9,10]`. Below is my solution in Ruby, it’s a one liner code :-):

The output is `[5, 6]`.

Here we are for another puzzle from Javalobby:

Given an array of numbers, return the index at which the array can be balanced by all numbers on the left side add up the the sum of all numbers of the right side.

For example, an array with [1,5,6,7,9,10] can be balanced by splitting the array at position 4

Here is the solution in Ruby:

The input array `[1,5,6,7,9,10]` will split the array at position 4, as stated above.

Any other solutions? Drop comments here ๐

It’s been a long time since I posted for the last time, but I promise I’ll try to keep this blog up-to-date ๐

One of the purposes of this blog is to explore new technologies and recently I’ve been studying Ruby solving some puzzles posted by on JavaLobby.

The problem is about finding the second highest frequency of a character in a String. Below is the Ruby code, using the powerful concept of Ruby closures.

For the input String `"aabbbc"`, the result is `"a"`. I could done it in a more OOP style, but I chose a more concise solution ๐

Some weeks ago I started doing pair programming with some co-workers basically for two things:

1. code some tasks of an user story
2. get familiar with a new software code base

I haven’t had an opportunity to put this technique in practice a lot before, but I can say it was extremely important and benefit for the project. Sometimes I was the driver and sometimes I was the observer. The driver is the person who starts coding and the observer is who starts doing the code review. That point is important: code review.

Pair programming encourages the review of the code. Perhaps you won’t have an opportunity to refactor some code as you have when you are pairing with someone. I think code reviews are important because:

• Reviews increase code quality, because there are 2 people thinking at the same task at the same time.
• Refactoring areas arise in the design where improvements are needed
• When you have the strong support of an IDE (as Eclipse), some refactorings (extract method, extract class, introduce parameter) are highly automated
• Code is more read than written, two people reading the code can understand a lot more about the code base
• New ideas arise because of different point of views
• Questions can be solved by the sum of knowledge of the code base

And you? What’s your experience with pair programming?

Reading Michael Feathers’ ‘Working Effectively With LegacyCode’, I found quite interesting his heuristics to trace propagation of effects to code:

1. Identify a method that will change.
2. If the method has a return value, look at its callers.
3. See if the method modifies any values. If it does, look at the method that use those values, and the methods that use those methods.
4. Make sure you look for superclasses and subclasses that might be users of these instance variables and methods also.
5. Look at parameters to the methods. See if they or any objects that their methods return are used by the code that you want to change.
6. Look for global variables and static data that is modified in any of the methods you’ve identified.