WaitForTitle Selenium IDE to Java + testng

February 8, 2017 Leave a comment

This is the code translated from selenium IDE waitForTitle Command to Java code with TestNG.

    for (int second = 0;; second++) {
    	if (second >= 60) org.testng.Assert.fail("timeout");
    	try { if ("My Title here".equals(driver.getTitle())) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

Add Two Numbers(Linked List) – LeetCode #Python #py

January 4, 2017 Leave a comment

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        head = res = ListNode(-1)
        carry = 0
        while l1 or l2 or carry :
            sum = carry
            if l1 :
                sum += l1.val
                l1 = l1.next
            if l2 :
                sum += l2.val
                l2 = l2.next
            if sum > 9 :
                sum -= 10
                carry = 1
            else:
                carry = 0
            res.next = ListNode(sum)
            res = res.next
        return head.next

Here is the page of the problem

Categories: Programación, Python

Total Hamming Distance – Leetcode #Golang #Go

January 3, 2017 Leave a comment
func totalHammingDistance(nums []int) int {
    res := 0
    for i, len := uint(0), len(nums); i < 32; i++ {
         ones := 0
         for _,val := range nums {
             if val >> i & 1 == 1 {
                ones++
            }
        }
        if ones > 0 {
            res += ones * (len - ones)
        }
    }
    return res
}
Categories: Beyond, Golang, leetcode

To create a new array or fill it? Java Array Performance

October 12, 2016 Leave a comment

Recently I needed to empty an array to use it later, and asked myself, what is the better option, to create a new one or fill it?
Maybe you have known this long ago, I didn’t. But now, we both know!


int freq[] = new int[256];
//This is better:
                Arrays.fill(freq,0);
//than:
                freq = new int[256];
Categories: Java, Mini-tips, Programación

Configuration Errors on Spring + Wicket

October 11, 2016 Leave a comment

When facing error:
The matching wildcard is strict, but no declaration can be found for element ‘context:component-scan’.

Remember, is a typo somewhere!

If you face error:
No WebApplicationContext found: no ContextLoaderListener registered?

You need to set manually the applicationContext on the setUp() of your test.


public ServletContext getServletContext() {
                ServletContext sc = super.getServletContext();
                XmlWebApplicationContext appCont = new XmlWebApplicationContext();

                appCont.setConfigLocation("classpath:applicationContext.xml");
                appCont.setServletContext(sc);
                appCont.refresh();
                sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCont);

                return sc;
            }

When error BeanDefinitionStoreException Failed to read candidate component class you got a pom.xml with spring dependencies in conflict.

Categories: Beyond, typo

Read Java input

October 10, 2016 Leave a comment

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        // all ints from 0 to N should be insie array a
    }
Categories: Java, Mini-tips

Split string in sql query – oracle

October 3, 2016 Leave a comment

select regexp_substr(‘MyString,otherString,lastOneStr’,'[^,]+’, 1, level) from dual
connect by regexp_substr(‘MyString,otherString,lastOneStr’, ‘[^,]+’, 1, level) is not null;

–Result :
———-
–MyString
–otherString
–lastOneStr

Categories: Beyond, Mini-tips, Oracle, SQL