Skip to content

Instantly share code, notes, and snippets.

View brettfreer's full-sized avatar

Brett Freer brettfreer

View GitHub Profile
@brettfreer
brettfreer / remove_trailing_zeros.py
Created June 20, 2016 22:11
Remove trailing zeros from a string
def remove_trailing_zeros(s):
while s[-1] == '0':
s = s[:-1]
return s
@brettfreer
brettfreer / remove_leading_zeros.py
Created June 20, 2016 22:09
Remove leading zeros from a string
def remove_leading_zeros(s):
while s[0] == '0':
s = s[1:]
return s
@brettfreer
brettfreer / remove_spaces.py
Created June 20, 2016 22:08
Remove all spaces from a string
def remove_spaces(s):
return s.replace(' ', '')
@brettfreer
brettfreer / remove_duplicate.py
Last active June 12, 2016 09:02
Remove duplicate characters from a string
import itertools
def remove_duplicate(s):
return ''.join(ch for ch, _ in itertools.groupby(s))
@brettfreer
brettfreer / swap.py
Last active June 12, 2016 09:02
Swap positions i and j in string s
def swap(s, i, j):
return ''.join((s[:i], s[j], s[i+1:j], s[i], s[j+1:]))
@brettfreer
brettfreer / totient.py
Created June 11, 2016 10:30
Euler's Totient
from fractions import gcd
def eulers_totient(n):
p = 0
for k in range(1, n+1):
if gcd(n,k) == 1:
p += 1
return p
@brettfreer
brettfreer / maxvalue.py
Last active July 6, 2016 10:43
Return the key with the maximum value in a dictionary
import operator
def max_value(m):
return max(m.iteritems(), key=operator.itemgetter(1))[0]
@brettfreer
brettfreer / gcd.py
Created June 5, 2016 10:58
Return the greatest common denominator (GCD) of a list of integers A.
def GCD(a, b):
if b == 0:
return a
else:
return GCD(b, a%b)
gcd = reduce(lambda x, y: GCD(x, y), A)
@brettfreer
brettfreer / combinations.py
Created June 5, 2016 10:55
Return all 2 part combinations of a list
from itertools import combinations
def f(A):
for p in combinations(A, 2):
return p[0], p[1]
@brettfreer
brettfreer / linux_user_expire.sh
Created June 3, 2016 00:49
Expire/delete inactive linux user accounts
#!/bin/sh
#
# Expire and delete inactive linux users
#
# check this before running!
exclude_users="keepme|metoo|daemon|adm|lp|sync|shutdown|halt|mail|news|uucp|operator|man|postmaster|smmsp|portage|nobody|sshd|cron|ntp|messagebus|mysql|apache|haldaemon|ftp|postfix|dhcp|ntop|fetchmail|squid|hsqldb|tomcat"
remove_period="548" # 18 months