category
3/27/2017 - 12:25 PM

new_gist_file_1

Write string to file

s = "hello chaps!"
with open('file.xml','w') as file: file.write(s)

Convert integer to binary

integer = 10
"{0:b}".format(integer)

Convert ASCII to strings

[chr(n) for n in range(97,110)]

Tail Recursive function

def fibSeries(length, fibNumM=1, fibNumN=0,  fibList=[0]):
    return fibList  if length <= 1 else fibSeries(length-1, fibNumM=fibNumM+fibNumN, fibNumN=fibNumM, fibList=[fibNumM+fibNumN]+fibList)

>>> fibSeries(10)
[55, 34, 21, 13, 8, 5, 3, 2, 1, 0]

For a function defined as:

f(N) = Op(N-1, N-2)

Recur like:

def f(.., M, N, ...):
  return ... if ... else f(.., M=Op(M, N), N=M, ...)