1. The document provides examples of using various programming languages like JavaScript, Java, PHP, Python to perform common operations like reversing a string, finding an element in an array, and using data structures like lists, dictionaries, tuples, and sets.
2. It also discusses functional programming concepts like iterators, generators, map, filter and reduce functions, and using lambda expressions.
3. Examples are given for file handling, pickling, list and generator comprehensions in Python.
5. Reverse String – JavaScript
var s = "This is the simple text object";
var r = "";
for (var i = s.length - 1; i >= 0 ; i--) {
r += s[i];
}
function reverse (s) {
return (s === '''') ? : reverse(s.substr(1)) +
s.charAt(0);
}
6. Reverse String – Java
String s = "This is the test";
System.out.println(new StringBuilder(s).reverse().toString());
9. Find element in array
JavaScript
var zipCodes = (['90001','90002','90003']);
if ( zipCodes.indexOf('9005') > -1
console.log("found") ;
} else {
console.log("not Found");
}
) {
10. Find element in array
Java
HashMap<Integer,String> cities =
cities.put(9000,"Gent");
cities.put(9400,"Aalst");
String city = cities.get(9400);
System.out.println(city);
new HashMap<Integer, String>();
14. Design Principles
•
•
•
•
•
•
Borrow ideas whenever it make sense
As simple as possible , not simpler (Einstein)
Do one thing well (Unix)
Don’t fret abut performance (fix it later)
Don’t bother user with details
Error shouldn’t pass silently
16. Data Structure - String
s = " The snake is LONG, 7 miles "
len(s)
s.count("is")
s.endswith("miles ")
s.swapcase()
s.strip()
s.index("7", 5, 22)
Operations:
"A" + "b"
"snake" in s
"kilo meters" not in s / not
"kilo meters" in s
s * 2
17. Data Structure – List (Array)
0 1 2 3 4 5
+---+---+---+---+---+---+
| a | b | c | d | e | f |
+---+---+---+---+---+---+
-6 -5 -4 -3 -2 -1
l = [ 3, 4, 0, 0, 5, 2, 1]
k = [ 1, 2, 3, 4,5]
len(l)
l.count(0)
l.remove(0)
l.index(4)
l.append(55)
Operations:
k+l
l*2
3 not in k / not 3 in k
18. Data Structure – Dictionary
http://svn.python.org/view/python/trunk/O
a = dict(one=1, two=2, three=3)
b = {'one': 1, 'two': 2, 'three': 3}
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
d = dict([('two', 2), ('one', 1), ('three', 3)])
e = dict({'three': 3, 'one': 1, 'two': 2})
>>> a == b == c == d == e
d.items()
d.keys()
d.values()
Operations:
d.update(d2)
one in b
1 in a.values()
19. Data Structure – Tuple (Immutable)
t = ("a", "b", "c", "d")
t.count()
t.index()
Operations:
t * 2
t + t
"a" in t
"a" not in t
20. Data Structure – Set (Immutable)
a = set([1,2,3])
b = set([2,3,4])
a.union(b)
a.intersection(b)
a.difference(b)
Operations:
1 in s
5 not in s
21. Files / Pickle
f = open("D:/foo.txt", "wb")
f.write("My python is longer than yours. ")
f.close()
import pickle
output = [1,2 "a", "b", "c"]
f = open('data.pk’, 'wb')
pickle.dump(output, f)
f.close()
pickle.load(open('data.pk', 'rb'))
22.
23. Comprehension / Generator (Haskell)
simplifies process of iterations
words = 'The quick brown fox jumps over the lazy dog'.split()
[[word.upper(), word.lower(), len(word)] for word in words]
matrix = [[1, 2, 3, 4],
[5, 6, 7, 8], [9, 10, 11, 12]]
[[row[i] for row in matrix] for i in range(4)] # list comprehension
g = ([ row[i] for row in matrix ] for i in range(4)) # generator
29. Lambda + F.P. Functions
map(lambda x: x.upper(), ["a", "test"])
filter(lambda x: (x % 2) == 0, [1,2,3,4,5,6,7,8])
[x for x in 'abracadabra' if x not in 'abc']
{x for x in 'abracadabra' if x not in 'abc'}
sorted(d.items(), key=lambda i: i[1])
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])