Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am nifo on github.
  • I am nifo (https://keybase.io/nifo) on keybase.
  • I have a public key ASCbWLqw8Sh3fMOU6GBupKnKYnJE-kbXnfHG0N0aHTsuewo

To claim this, I am signing this object:

@nifo
nifo / username.py
Created November 5, 2013 09:30
A consultant once said, "Its hard to decide the username for new users. When using the first 3 letter from the firstname and surname. What about 'Bo Ek'". I figured how hard would that be, just repeat the last character . Like "boo, ekk". Turns out, in python, it was only 1 line.
def create_user_name(first_name, last_name, length=3):
"""The first 'length' (default 3) letters of first name and last name. Fills
out with last letter in the names if less than specified length. """
return first_name.ljust(length, first_name[-1])[:length] + \
last_name.ljust(length, last_name[-1])[:length]
# Examples
print create_user_name('bo', 'ek') # booekk
print create_user_name('niklas', 'forsstrom') # nikfor
print create_user_name('bo', 'ahl') # booahl
@nifo
nifo / rplsls.py
Last active December 27, 2015 11:09
A text version of the game rock, paper, scissors, lizard, spock. Idea from a stackoverflow question, he woundered how he could make his code more beautiful. I enjoyed making this little game one afternoon. stackoverflow Q: http://stackoverflow.com/questions/19584234/a-more-python-efficient-way-to-write-a-code/19592121
from random import randint
# ex. scissors and lizard is beaten by rock
beaten_by = {'rock': ['scissors', 'lizard'],
'paper': ['rock', 'spock'],
'scissors': ['paper', 'lizard'],
'lizard': ['spock', 'paper'],
'spock': ['scissors', 'rock']}
def rplsls_game():
@nifo
nifo / passwords.py
Last active December 27, 2015 10:59
A couple of methods used for building a neat password. passwords is a generator of random passwords using a specfic set of characters and a min and max length. valid_password is just one validation, you can certainly make up more requirements for what should pass as a valid password. This one is used to specify how many digits, lowercase and upp…
def password_generator(min_len= 10, max_len=18,
chars='abcdefghjkmnpqrstauvz123456789ABCDEFGHIJKLMNOPQRSTUVXYZ!#$@'):
"Generator of random passwords using the character set 'chars'"
from random import randint
length = randint(min_len,max_len)
while True:
yield ''.join(choice(chars) for i in range(length))
def valid_password(password, digits=0, lower_case=0, upper_case=0):
@nifo
nifo / personummer.py
Created November 5, 2013 08:25
How to validate a swedish, so called, personnummer. In other countries it might be called social security number.
def valid_pnummer(pnr):
"""Returns True if input is a valid personnummer.
Can be with or without "-" and 19~
Requirement from the swedish tax agency (2013-08-05):
http://www.skatteverket.se/privat/folkbokforing/omfolkbokforing/personnumretsuppbyggnad.4.18e1b10334ebe8bc80001502.html
Kontrollsiffran
Fjärde siffran i födelsenumret är en kontrollsiffra. Den räknas ut
maskinellt med ledning av födelsetiden och födelsenumret.
@nifo
nifo / anagrams.py
Last active December 27, 2015 10:59
A way to get all anagrams from a file. A python version of: http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html and see multimaps.
def anagrams(file_path, min_group_size=8):
"Gets all anagrams for the file at file_path. Print outs the result if "
"there are more than min_group_size (default 8) words."
"A python version of:"
"http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html and see multimaps."
m = {}
with open(file_path) as file:
for line in file: