-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py3
More file actions
72 lines (55 loc) · 1.65 KB
/
common.py3
File metadata and controls
72 lines (55 loc) · 1.65 KB
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
# Initialize 2D array
array2D = [ [0] * n for x in range(n) ]
# Convert integer to digits list
def numberToDigits(n):
return [ int(i) for i in str(n) ]
return list(map(int, str(num)))
# alternatively divide by 10, 100, ... ;-)
# Digit sum
sum(map(int, str(number)))
sum(int(digit) for digit in str(number))
# Convert a list of int digits to an integer
def digitsToNumber(digitsList):
return int(''.join((map(str, digitsList))))
# Access row and column in matrix (nested lists)
[ r[b] for r in matrix ] # column b
matrix[a] # row a
# Character-saving workaround (Challenges/Python)
# return sorted(*eval(dir()[0]), key=len)
# w,d = eval(dir()[0])
# (a, b, c), (d, e, f) = eval(dir()[0])
# vars() --> dict of input parameters
# swapCase = str.swapcase
# return f"Hello, {eval(dir()[0])[0]}"
#Ternary return
# return n if n<m else m
# Ternary if elif else
# expr1 if condition1 else expr2 if condition2 else expr
# ternary shortcut
# a = true && 5 || 10
#
if b > a:
a, b = b, a
# short function
# r = range
# n, *r = eval(dir()[0])
#same as? --> r = []
# return r
# Fastest way to copy
t = l[:]
# Add to list shorthand
R.append(a) --> R += [a] # but beware
R += a, # even shorter!
# shorthand to access current and next element
# [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
i < j for i, j in zip(s, s[1:])
for i, j in zip(s, s[1:]):
# for [(1, 2), (3, 4), (5, 6)]
zip(data[0::2], data[1::2])
# data[0::2] means create subset collection of elements that (index % 2 == 0)
#********************************************
# Character-saving workaround (Challenges/Coffee)
# [x
# n] = args
# return args[0].replace /\B/g, "-"
#********************************************