Archive

Archive for the ‘Programación’ Category

Built-in web server python 2.7

October 18, 2017 Leave a comment

The next command helps you to create a simple http server to test anything fast, or use that location and their resources with a different port.
Just navigate in the command line to the location and type the next:

python -m SimpleHTTPServer [port]
Advertisements
Categories: Beyond, Mini-tips, Python, Tips

NihilistCipher #Java #PoorlyWritten #underpressure :(

July 27, 2017 Leave a comment

When you have free time, you enjoy cracking ciphers. But to crack a cipher you need some ciphered text, and preparing it yourself is way too boring. So, you decide to implement an algorithm that will encode text for you.

After some research you choose the famous Nihilist cipher, and here is how it works (please note that this task uses the modified version of the cipher, so it can differ from what you might already know):

Build a 5 × 5 Polybius square for the given keyword consisting of distinct letters:
first, fill the square with the letters of the keyword row by row.
then, continue filling the square with all the letters in alphabetical order except for those that are already in the square.
add the final (26th) letter to the cell with coordinates (row, column).
Now each letter can be associated with two integers, the indices of a row and a column of the Polybius square containing it. In order to encode a letter, replace it with a concatenation of string representations of these two integers.
Make the lengths of the given plaintext message and the keyword equal (if one string is shorter than another one, consider the cyclic string obtained from it and take its prefix of the length equal to the length of the longer string).
Finally, encode the letters of both strings obtained on the previous step and add up the values at the same positions. The resulting list of integers is the desired ciphertext.
Given some plaintext to encode, a keyword and the coordinates of a Polybius square cell which will contain two letters, return the encoded text.

Example

For plaintext = “codefights”, keyword = “keyword”, row = 1 and column = 2, the output should be
nihilistCipher(plaintext, keyword, row, column) = [36, 27, 35, 26, 46, 55, 54, 44, 64, 64].

https://github.com/Grekz/codefights/blob/master/NihilistCipher.java

Categories: codefights, Java, Programación

Goldbach Conjecture #Java

July 27, 2017 Leave a comment

Return the number of ways to write the given n as the sum of two prime numbers.

Example
For n = 20, the output should be
gold(n) = 2.
There are two ways to represent 20 as the sum of two prime numbers:

3 + 17 = 20,
7 + 13 = 20.


int gold(int n) {
    if ( n <= 2) return 0;
    int res = 0;
    for(int i = 2; i <= n / 2; i++ ){
        if( isPrime(i) && isPrime(n - i) ){
            res++;
        }
    }
    return res;
}
boolean isPrime(int x){
    for (int i = 2; i * i <= x; i++)
        if ( x % i == 0)
            return false;
    return true;
}

Goldbach Conjecture #Ruby

July 27, 2017 Leave a comment

Return the number of ways to write the given n as the sum of two prime numbers.

Example
For n = 20, the output should be
gold(n) = 2.
There are two ways to represent 20 as the sum of two prime numbers:

3 + 17 = 20,
7 + 13 = 20.


require 'prime'
def gold e
    r = 0
    for i in 2..(e/2)
        r += 1 if Prime.prime?(i) && Prime.prime?(e-i)
    end
    r
end

Fixing: Error: Couldn’t find preset “latest” relative to directory

July 22, 2017 Leave a comment

Add the babel-preset-latest to the node_modules of the project directory with –save-dev option, in example:
npm install –save-dev babel-preset-latest

PD. This issue came up when configuring webpack on a project recently created from scratch, and configured the babel-loader with the latest as preset.

Command npm to check for installed packages

July 22, 2017 Leave a comment

npm list -g –depth=0

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