Tag Archives: programming

Cedilha no Ubuntu 16.04

    çççç
  ççç  ççç 
  ççç  ççç 
  ççç 
  ççç 
  ççç  ççç 
    çççç
     çç
    çç

Fiz esse script (cedilha.sh) pra fazer o c-cedilha (ç) funcionar no Ubuntu 16.04. Como é uma tarefa chata que eu já tive que fazer muitas vezes fica mais fácil pra mim ter um script pra fazer isso e pode ser que seja útil a outras pessoas. Infelizmente é uma solução que exige baixar e executar como root um script da internet. Eu recomendo que você leia o script e entenda o que está acontecendo antes de executá-lo. Esse script altera vários arquivos importantes e ele faz um backup (.bak) desses arquivos.

wget https://raw.githubusercontent.com/silveira/cedilha.sh/master/cedilha.sh
sudo bash cedilha.sh

Por favor, se o script funcionou pra você também, diga nos comentários. Se você não está usando Ubuntu 16.04 e quer testar o script ainda assim, edite o script e remova a verificação no inicio do programa.

parallel counting in Java using AtomicInteger

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.TimeUnit;

class ParallelCounterExample {
   public static void main(String[] args) throws InterruptedException {
      int threads = 2000;
      ExecutorService executor = Executors.newFixedThreadPool(threads);
      final AtomicInteger atomi = new AtomicInteger();
      for(int i=0; i<threads; i++) {
         executor.execute( new Runnable() {
            @Override
            public void run() {
               atomi.getAndIncrement();
            }
         });
      }
      executor.awaitTermination(1, TimeUnit.SECONDS);
      executor.shutdown();
      System.out.println(atomi.get());
    }
}

Regex with negatives lookahead and lookbehind

"Looking different directions" by Paul Kline at (https://www.flickr.com/photos/paulelijah/6717953239/)
“Looking different directions” by Paul Kline.

Problem: Match strings that contains a single quotation mark ('), but not multiple ones together.

Solution:

(?<!')'(?!')

This is a regex for a single quotation mark with a (?<!') in the left and a (?!’) in the right. The (?<!') is a ?< look behind if not ! a single quotation mark '. The (?!') is a look ahead ? if not ! a single quotation mark '.

Java code:

import java.util.regex.Pattern;

public class RegexProblem {
  public static void main(String args[]) {
    Pattern single_quote = Pattern.compile("(?<!')'(?!')");
    String[] phrases = {
      "",
      "'",
      "a'a",
      "aaa",
      "aa'aa",
      "aa''aa",
      "aa'''aaa",
      "aaa''''aaa"
    };
    for(String phrase: phrases){
      System.out.println(String.format("For %s is %s.", phrase,
            single_quote.matcher(phrase).find()));
    }
  }
}

The output is:

For  is false.
For ' is true.
For a'a is true.
For aaa is false.
For aa'aa is true.
For aa''aa is false.
For aa'''aaa is false.
For aaa''''aaa is false.

Java, printing arrays

As I keep forgetting, this post is to remind me that Java Java doesn’t have a pretty toString() for arrays objects, and it does for Lists.

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class ListsExample {
    public static void main (String args[]) {
        // as an array
        String[] list1 = {"a","b","c"};
        System.out.println(Arrays.toString(list1));
        
        // as an List provided by Arrays
        List<String> list2 = Arrays.asList("d", "e", "f");
        System.out.println(list2);

        // as an implementation of the interface List
        // ArrayList (could also be LinkedList, Vector, etc)
        List<String> list3 = new ArrayList<String>();
        list3.add("g");
        list3.add("h");
        list3.add("i");
        System.out.println(list3);
    }
}

The output is:

[a, b, c]
[d, e, f]
[g, h, i]

Telephone keypad combinations

Problem: Given a sequence of numbers, show all possible letter combinations in a telephone keypad.

Recursive solution in Python:

keyboard = {
  '1': [],
  '2': ['a','b','c'],
  '3': ['d','e','f'],
  '4': ['g','h','i'],
  '5': ['j','k','l'],
  '6': ['m','n','o'],
  '7': ['p','q','r','s'],
  '8': ['t','u','v'],
  '9': ['w','x','y','z'],
  '0': []
}

def printkeys(numbers, prefix=""):
    if len(numbers)==0:
        print prefix
        return

    for letter in keyboard[numbers[0]]:
        printkeys(numbers[1:], prefix+letter)

printkeys("234")

Output:

adg
adh
adi
aeg
aeh
aei
afg
afh
afi
bdg
bdh
bdi
beg
beh
bei
bfg
bfh
bfi
cdg
cdh
cdi
ceg
ceh
cei
cfg
cfh
cfi