CodeFights MZbot AllianceVsMonster – Ruby

June 7, 2017 Leave a comment
def allianceVersusMonster hp, ad
    mHp = hp[0]
    mAd = ad[0]
    turns = []
    size = hp.size - 1
    (1...ad.size).each do |x|
        turns <= mAd && cHp >= mHp
        timesToDie = ( mHp / cAd.to_f).ceil
        timesIDie = ( cHp / mAd.to_f).ceil
        return size if timesIDie >= timesToDie
        mHp -= (timesIDie - 1) * cAd 
        w[1] = cHp % mAd + 1
    end
    
    #fight until dead
    turns.each do |w|
        cAd, cHp = w
        return size if cAd >= mAd && cHp >= mHp       
        while cHp > 0 && mHp > 0
            mHp -= cAd
            return size if mHp <= 0
            cHp -= mAd
        end
        size -= 1
    end
    0
end

April 26, 2017 Leave a comment

Create a synonym in oracle database.
create or replace synonym my_schema_granted.my_db_object_synonym for schema_that_grants.db_object;
commit;

Categories: Mini-tips, Oracle, plsql, SQL

Get all days between two dates in oracle sql

March 30, 2017 Leave a comment
select to_date('30-03-2017','dd-mm-yyyy') + rownum -1
from all_objects
where rownum <= to_date('30-04-2017','dd-mm-yyyy')-to_date('30-03-2017','dd-mm-yyyy')+1;
Categories: Oracle, Programación, SQL

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