Skip to content

Tag: programming

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

permutations implemented in Python

In case you can’t use Python’s itertools or in case you want a simple, recursive python implementation for a permutation of a list:

def perm(a,k=0):
   if(k==len(a)):
      print a
   else:
      for i in xrange(k,len(a)):
         a[k],a[i] = a[i],a[k]
         perm(a, k+1)
         a[k],a[i] = a[i],a[k]

perm([1,2,3])

Output:

[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, 1, 2]

This Python implementation is based in the algorithm presented in the book Computer Algorithms by Horowitz, Sahni and Rajasekaran.

Bash Brace Expansion

photo by whiskeyandtears at https://www.flickr.com/photos/whiskeyandtears/2140154564

$ echo {0..9}
0 1 2 3 4 5 6 7 8 9
$ echo b{a,e,i,o,u}
ba be bi bo bu
$ echo x{0..9}y
x0y x1y x2y x3y x4y x5y x6y x7y x8y x9y
$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
$ echo {1..3} {A..C}
1 2 3 A B C
$ echo {1..3}{A..C}
1A 1B 1C 2A 2B 2C 3A 3B 3C
echo {a,b{1,2,3},c}
a b1 b2 b3 c
$ mkdir -p {project1,project2}/{src,tst,bin,lib}/
$ find .
.
./project1
./project1/tst
./project1/bin
./project1/lib
./project1/src
./project2
./project2/tst
./project2/bin
./project2/lib
./project2/src
$ echo {{A..Z},{a..z}}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z
$ for i in {a..f} 1 2 {3..5} ; do echo $i;done
a
b
c
d
e
f
1
2
3
4
5

The examples below requires Bash version 4.0 or greater.

$echo {001..9}
001 002 003 004 005 006 007 008 009
$ echo {1..10..2}
1 3 5 7 9