https://www.kaggle.com/wiki/GettingStartedWithPythonForDataScience
Python 選用 32-bit Python 3.2
Train or test data 採用 open('Data/train.csv','Ub') Ub 模式開檔
TODO: Do some study on random forests.
def longest_common_subsequence(s, t):
c, b = longest_common_subsequence_table(s, t)
return longest_common_subsequence_printable(b, s, len(s), len(t))
def longest_common_subsequence_table(s, t):
m, n = len(s), len(t)
b = [[0 for _ in range(n)] for _ in range(m)]
c = [[0 for _ in range(n+1)] for _ in range(m+1)]
for i in range(m):
for j in range(n):
if s[i] == t[j]:
c[i+1][j+1] = c[i][j] + 1
b[i][j] = 'Diagonal'
elif c[i][j+1] > c[i+1][j]:
c[i+1][j+1] = c[i][j+1]
b[i][j] = 'Up'
else:
c[i+1][j+1] = c[i+1][j]
b[i][j] = 'Left'
return c, b
def longest_common_subsequence_printable(b, s, i, j):
reversed_lcs = ''
while True:
if i == 0 or j == 0:
break
direction = b[i-1][j-1]
if direction == 'Diagonal':
lcs += s[i-1]
i, j = i-1, j-1
elif direction == 'Up':
i, j = i-1, j
else:
i, j = i, j-1
return reversed_lcs[::-1]
def LIS(sequence):
# Time complexity is O(n^2)
lis_len = [1] * len(sequence)
for i in range(len(sequence)):
for j in range(i+1, len(sequence)):
if sequence[i] < sequence[j]:
lis_len[j] = max(lis_len[j], lis_len[i] + 1)
rv = []
curr_len = max(lis_len)
for i in range(len(sequence) - 1, -1, -1):
if curr_len == lis_len[i]:
rv.append(sequence[i])
curr_len -= 1
return rv[::-1]
def LIS(sequence):
# Robinson-Schensted-Knuth Algorithm
# Time complexity is O(n log(n))
if not sequence:
return []
aux_len = [1]
aux_rv = [sequence[0]]
for i in range(1, len(sequence)):
x = sequence[i]
if x > aux_rv[-1]:
aux_rv.append(x)
aux_len.append(len(aux_rv))
else:
j = bisect.bisect_right(aux_rv, x)
aux_rv[j] = x
aux_len.append(j + 1)
rv = []
curr_len = len(aux_rv)
for i in range(len(sequence) - 1, -1, -1):
if curr_len == aux_len[i]:
rv.append(sequence[i])
curr_len -= 1
return rv[::-1]
這個時間複雜度是 O(n log(n)),關鍵就是用 binary search 把時間複雜度從 n 降成 log(n)。
use strict;
use warnings;
while (my $dna = <>) {
chomp $dna;
my @symbols = split //, $dna;
my %count = (
'A' => 0,
'C' => 0,
'G' => 0,
'T' => 0,
);
for my $symbol (@symbols) {
$count{$symbol}++;
}
print $count{'A'} . ' ' ,
$count{'C'} . ' ' ,
$count{'G'} . ' ' ,
$count{'T'} . "\n";
}
實際寫頗土炮。import sys
dna = sys.stdin.read().strip()
print(' '.join([str(dna.count(i)) for i in 'ACGT']))
不得不說演化過的 Python 習得各家語言的優點,才能淬鍊精幹短小的程式碼。dna = STDIN.read.chomp
puts [dna.count('A'), dna.count('C'), dna.count('G'), dna.count('T')].join ' '
短短的蠻可愛的
CAPI3REF: Compiling An SQL Statement
KEYWORDS: {SQL statement compiler}
SQLITE_API int sqlite3_prepare(
sqlite3 *db, /* Database handle */
const char *zSql, /* SQL statement, UTF-8 encoded */
int nByte, /* Maximum length of zSql in bytes. */
sqlite3_stmt **ppStmt, /* OUT: Statement handle */
const char **pzTail /* OUT: Pointer to unused portion of zSql */
);
import sys
DIGIT_COUNT = 5
#DIGIT_COUNT = 15
ANSWER = [0] * DIGIT_COUNT
GUESSES = {
"90342" : 2,
"70794" : 0,
"39458" : 2,
"34109" : 1,
"51545" : 2,
"12531" : 1,
}
#GUESSES = {
# "5616185650518293" : 2,
# "3847439647293047" : 1,
# "5855462940810587" : 3,
# "9742855507068353" : 3,
# "4296849643607543" : 3,
# "3174248439465858" : 1,
# "4513559094146117" : 2,
# "7890971548908067" : 3,
# "8157356344118483" : 1,
# "2615250744386899" : 2,
# "8690095851526254" : 3,
# "6375711915077050" : 1,
# "6913859173121360" : 1,
# "6442889055042768" : 2,
# "2321386104303845" : 0,
# "2326509471271448" : 2,
# "5251583379644322" : 2,
# "1748270476758276" : 3,
# "4895722652190306" : 1,
# "3041631117224635" : 3,
# "1841236454324589" : 3,
# "2659862637316867" : 2,
#}
def is_unique(digit, position):
has_more = False
def check_guessing(dimension):
for guess in GUESSES:
correct_count = 0
for i in range(dimension):
if ANSWER[i] == int(guess[i]):
correct_count += 1
if correct_count > GUESSES[guess]:
return False
if correct_count + DIGIT_COUNT - dimension < GUESSES[guess]:
return False
return True
def backtracking(dimension):
if not check_guessing(dimension):
return
if dimension == DIGIT_COUNT:
print('Found =>', ANSWER[0:dimension])
return
for i in range(10):
ANSWER[dimension] = i
backtracking(dimension + 1)
def solve():
for i in range(10):
ANSWER[0] = i
backtracking(1)
def main():
solve()
if __name__ == '__main__':
sys.exit(main())
Username
| Country | Solved | Level | Language | |||
---|---|---|---|---|---|---|---|
1=
|
| ![]() |
400+
| C/C++ | |||
2
|
| ![]() |
385
| Matlab | |||
3
|
| ![]() |
298
| Mathematica | |||
4
|
| ![]() |
269
| F# | |||
5
|
| ![]() |
261
| Mathematica | |||
6
|
| ![]() |
237
| Python | |||
7
|
| ![]() |
221
| Python | |||
8
|
| ![]() |
207
| Haskell | |||
9
|
| ![]() |
206
| Matlab | |||
10
|
| ![]() |
203
| Python | |||
11
|
| ![]() |
198
| Python | |||
12
|
| ![]() |
189
| Pencil/Paper | |||
13
|
| ![]() |
170
| Scala | |||
14
|
| ![]() |
164
| Python | |||
15
|
| ![]() |
164
| Go | |||
16
|
| ![]() |
162
| Python | |||
17
|
| ![]() |
162
| C# | |||
18
|
| ![]() |
158
| Java | |||
19
|
| ![]() |
151
| C/C++ | |||
20
|
| ![]() |
149
| Python | |||
21
|
| ![]() |
146
| C/C++ | |||
22
|
| ![]() |
144
| Python | |||
23
|
| ![]() |
143
| C/C++ | |||
24
|
| ![]() |
136
| Python | |||
25
|
| ![]() |
136
| Python | |||
26
|
| ![]() |
135
| Python | |||
27
|
| ![]() |
128
| C/C++ | |||
28
|
| ![]() |
120
| C# | |||
29
|
| ![]() |
118
| Haskell | |||
30
|
| ![]() |
112
| Python | |||
31
|
| ![]() |
109
| Python | |||
32
|
| ![]() |
105
| Racket | |||
33
|
| ![]() |
101
| Java | |||
34
|
| ![]() |
101
| Mathematica | |||
35
|
| ![]() |
99
| C/C++ | |||
36
|
| ![]() |
93
| Matlab | |||
37
|
| ![]() |
88
| Haskell | |||
38
|
| ![]() |
83
| C/C++ | |||
39
|
| ![]() |
82
| ||||
40
|
| ![]() |
81
| Erlang | |||
41
|
| ![]() |
78
| Python | |||
42
|
| ![]() |
77
| Matlab | |||
43
|
| ![]() |
77
| Scala | |||
44
|
| ![]() |
77
| C/C++ | |||
45
|
| ![]() |
76
| LabVIEW | |||
46
|
| ![]() |
76
| Matlab | |||
47
|
| ![]() |
75
| C/C++ | |||
48
|
| ![]() |
72
| ||||
49
|
| ![]() |
72
| Python | |||
50
|
| ![]() |
71
| Matlab | |||
51
|
| ![]() |
70
| BASIC | |||
52
|
| ![]() |
69
| ||||
53
|
| ![]() |
68
| C# | |||
54
|
| ![]() |
68
| Python | |||
55
|
| ![]() |
67
| Python | |||
56
|
| ![]() |
65
| ||||
57
|
| ![]() |
65
| C/C++ | |||
58
|
| ![]() |
64
| Python | |||
59
|
| ![]() |
63
| Python | |||
60
|
| ![]() |
61
| Matlab | |||
61
|
| ![]() |
60
| Python | |||
62
|
| ![]() |
60
| C/C++ | |||
63
|
| ![]() |
58
| C/C++ | |||
64
|
| ![]() |
58
| Matlab | |||
65
|
| ![]() |
56
| Java | |||
66
|
| ![]() |
55
| C/C++ | |||
67
|
| ![]() |
55
| C/C++ | |||
68
|
| ![]() |
55
| Python | |||
69
|
| ![]() |
54
| C/C++ | |||
70
|
| ![]() |
54
| Java | |||
71
|
| ![]() |
53
| Scheme | |||
72
|
| ![]() |
53
| Perl | |||
73
|
| ![]() |
51
| Python | |||
74
|
| ![]() |
50
| Python | |||
75
|
| ![]() |
50
| Python | |||
76
|
| ![]() |
49
| C/C++ | |||
77
|
| ![]() |
49
| Python | |||
78
|
| ![]() |
48
| ||||
79
|
| ![]() |
48
| Haskell | |||
80
|
| ![]() |
48
| ||||
81
|
| ![]() |
48
| Python | |||
82
|
| ![]() |
48
| Python | |||
83
|
| ![]() |
47
| Python | |||
84
|
| ![]() |
47
| C# | |||
85
|
| ![]() |
46
| Factor | |||
86
|
| ![]() |
45
| Java | |||
87
|
| ![]() |
44
| Java | |||
88
|
| ![]() |
44
| Mathematica | |||
89
|
| ![]() |
44
| Python | |||
90
|
| ![]() |
44
| Python | |||
91
|
| ![]() |
43
| Maple | |||
92
|
| ![]() |
42
| Python | |||
93
|
| ![]() |
42
| Python | |||
94
|
| ![]() |
42
| Python | |||
95
|
| ![]() |
41
| Python | |||
96
|
| ![]() |
40
| Spreadsheet | |||
97
|
| ![]() |
39
| Ruby | |||
98
|
| ![]() |
38
| C/C++ | |||
99
|
| ![]() |
38
| Ruby | |||
100
|
| ![]() |
37
| Python |