Posts Tagged programming

35 programming habits that make your code smell

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

Tags: , , , ,

K Palindromes puzzle

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.

def isKPalindrome(s, k)
	chars = s.chars.to_a
	limit = chars.size - 1
	count = chars[0...chars.size / 2].each_index.count {|i| !chars[i].eql?(chars[limit - i])}
	k == count ? true : false
end

puts(isKPalindrome("omississimo", 0)) #true

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

Tags: , , , , , , ,

Minimum difference puzzle in Ruby

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 :-):

[1,2,3,4,5].product([6,7,8,9,10]).map {|x, y| [x, y, (x-y).abs]}.sort { |a, b| a.last<=> b.last }.first.take(2)

The output is [5, 6].

Have another solution? Leave your comments here!

Tags: , , , , , , ,

Balancing arrays puzzle in Ruby

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:

v=[1,5,6,7,9,10];sum = v.reduce(:+);count=0;index = (0...v.size).detect { |i| count += v[i]; count * 2 == sum};index == nil ? nil : index + 1

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 🙂

Tags: , , , , , , ,

Finding the second highest frequency

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.

v="aabbbc".chars;h=Hash[v.map{|k| [k, v.count(k)] }];max=h.values.max;found=h.select { |k, v| v < max};found.keys.first if found

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 :-)

If you have another version in Ruby, feel free to post a comment!

Tags: , , , , ,

When to use TestNG or JUnit

There is an interesting discussion between using TestNG or JUnit on Java projects at Javalobby. It’s very worth reading.

Tags: , , , ,

The power of pair programming

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?

Tags: , , , , , ,

JFileContentManager included in the Softpedia Mac OS software database

100% FREE award granted by Softpedia

I’m proud to announce that JFileContentManager, a software of mine, has been added to Softpedia’s database of software programs for Mac OS. It is featured with a description text, screenshots, download links and technical details on this page.

JFileContentManager has been tested in the Softpedia labs using several industry-leading security solutions and found to be completely clean of adware/spyware components. We are impressed with the quality of your product and encourage you to keep these high standards in the future.

You can see the announce by clicking on the image above. Thanks Softpedia for the award!

Tags: , ,

JDK7 Tackles Java Verbosity

Interesting article showing some changes on the Java platform to address its verbosity, but keeping code readability safe.
I liked the new Collection’s literals syntax to create lists, sets and maps:

List powersOf2 = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};
Map ages = {"John" : 35, "Mary" : 28, "Steve" : 42};

Although it can be possible to use the DoubleBraceInitialization idiom to initialize collections in a more elegant way, this syntax is very terse and concise.

BTW, as I said before, Scala already has a syntactic sugar to create a literal Map:

val ages = Map("John" -> 35, "Mary" -> 28, "Steve" -> 42)

Maybe this change was taken into account considering the Scala collection literals implementation?

Tags: , , , , , , ,

A Brief History of Java and JDBC

From Javalobby, an interesting and short video showing the evolution of the Java platform since 1991.

Tags: , , ,