-
Notifications
You must be signed in to change notification settings - Fork 2
/
hello_world.py
143 lines (122 loc) · 3.81 KB
/
hello_world.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
from __future__ import print_function
from operator import mul
from collections import OrderedDict # python 2.7+
import math
class HelloWorld:
def product(self, *args):
return reduce(mul, args)
def main(self, *args):
# stdout
print("Hello World");
# basic math
print("// basic math")
_sum = 0
_sum = 2.0 + 2.0
print(_sum)
sqrt = math.sqrt(_sum)
print(sqrt)
# for and while loops; nested arrays; functions
print("// loops; nested arrays; functions")
n = 10
nsums = []
totalsum = 0
i = 0
while (i < n):
nsums.append(list((None,)*n))
for j in range(n)[::-1]:
nsums[i][j] = self.product(i, j) # preallocation?
totalsum += nsums[i][j]
i+=1
# if-conditionals
print("// if-conditionals")
if totalsum==2025:
print("totalsum*5: %d" % (totalsum*5))
elif totalsum==0:
print("totalsum is zero?")
else:
print("totalsum is off")
# function overload (what is this called again?)
print("// function overload") # not really
print(self.product(2,3,4))
# input coercion
print("// input coercion")
ten = int("10")
tenpointoh = float("10.0")
# try/except blocks
print("// try/catch blocks")
doublestr = "x20.2"
try:
twentypointtwo = float(doublestr)
except Exception, e: # TODO
print("err: %s is not a double" % doublestr)
finally:
doublestr = doublestr[1:]
twentypointtwo = float(doublestr)
##
# Arraylists
print("// ArrayLists")
intlist = range(5)
print(len(intlist))
intlist.pop(0)
print(len(intlist))
# traditional iteration
for nextint in intlist:
print(nextint)
# iteration with downcasting
for nextint in intlist:
print(nextint)
# python iteration
print("\n".join(str(x) for x in intlist))
##
# HashMaps
print("// Maps")
_map = {}
_map['one'] = 1
_map['two'] = 2
_map['three'] = 3
_map['four'] = 4
print("size = %d" % len(_map))
key = "one"
if key in _map:
print("%s: %s" % (key, _map[key]))
searchvalue = 10
if searchvalue in _map.values():
print("contains the value '%s'" % searchvalue)
else:
print("does not contain the value '%s'" % searchvalue)
for k,v in _map.iteritems():
print('%s=%s' % (k,v))
##
# Collections, sorting
# note that there exists an "OrderedDict" in stdlib
print("// Collections.sort")
for k in sorted(_map.iterkeys()):
print("%s: %s" % (k, _map[k]))
##
# OrderedDict
print("// OrderedDict")
sortedmap = OrderedDict()
sortedmap['one'] = 1
sortedmap['two'] = 2
sortedmap['three'] = 3
sortedmap['four'] = 4
for (k,v) in sortedmap.iteritems():
print("%s=%s" % (k,v))
##
# invert dict
sortedmap['uno'] = 1
invertedmap = OrderedDict()
for (k,v) in sortedmap.iteritems():
_currentvalue = invertedmap.get(v, None) # XXX: sentinel = None
if _currentvalue is not None:
if _currentvalue != k:
print("duplicate key: '%s' [%s] {{%s}}" %
(v, k, _currentvalue))
else:
invertedmap[v] = k
for (k,v) in invertedmap.iteritems():
print("%s=%s" % (k,v))
if __name__=="__main__":
cls = HelloWorld()
cls.main()