Skip to content

Instantly share code, notes, and snippets.

View antonnifo's full-sized avatar
🎯
Focusing: Make Hard Things Happen.

Mwangi Anthony antonnifo

🎯
Focusing: Make Hard Things Happen.
View GitHub Profile
@antonnifo
antonnifo / number_to_ordinal.py
Created October 25, 2018 11:20
This is a function that takes a number and return it as a string with the correct ordinal indicator suffix (in English). For example, 1 turns into "1st".
def numbertoordinal(number):
number_string = str(number)
SUFFIXES = {1: '1st', 2: '2nd', 3: '3rd'}
# Checking for 11-14 because those are akward
if int(number_string[-2:]) > 10 and int(number_string[-2:]) < 14:
return number_string + 'th'
else:
suffix = SUFFIXES.get(int(number_string[-1:]), number_string + 'th')
@antonnifo
antonnifo / sort.py
Last active January 15, 2019 09:42
Given all_states is a list of the states in a Country, sort the states by the length of their names, in descending order. What state is in the fifth position of the sorted states
def myFunc(e):
return len(e)
states = ["Abia", "Adamawa", "Anambra", "Akwa Ibom", "Bauchi", "Bayelsa", "Benue", "Borno",
"Cross River", "Delta", "Ebonyi", "Enugu", "Edo", "Ekiti", "Gombe", "Imo", "Jigawa",
"Kaduna", "Kano", "Katsina", "Kebbi", "Kogi", "Kwara", "Lagos", "Nasarawa", "Niger",
"Ogun", "Ondo", "Osun", "Oyo", "Plateau", "Rivers", "Sokoto", "Taraba", "Yobe", "Zamfara"]
states.sort(reverse=True,key=myFunc)
print(states[5])
@antonnifo
antonnifo / main.py
Created January 19, 2019 08:45
Given two numbers X and Y, write a function that: 1 returns even numbers between X and Y, if X is greater than Y else it returns odd numbers between x and y For instance, take the integers 10 and 2 . the function would return all the even numbers between 2 and 10.
def number_game(x,y):
if x > y:
return [n for n in range(y,x) if n%2==0]
elif y==x:
return []
else:
return [n for n in range(x,y) if n%2!=0]
@antonnifo
antonnifo / main.py
Created January 19, 2019 08:53
Given a non-negative integer, return an array or a list of the individual digits in order.
def digitize(n):
return [int(s) for s in str(n)][::1]
print(digitize(123))
@antonnifo
antonnifo / main.py
Created January 19, 2019 08:55
Write a function my_sort which takes in a list of numbers (integers). The function should return a list of sorted numbers such that odd numbers come first and even numbers come last.
def my_sort(numbers):
odd = [n for n in numbers if n % 2 != 0]
even = [n for n in numbers if n % 2 == 0]
return sorted(odd) + sorted(even)
print( my_sort([60, 25, 88]))
@antonnifo
antonnifo / power.py
Created January 19, 2019 08:57
Write a function named power that accepts two arguments, a and b and calculates a raised to the power b.
def power(a,b):
if b == 0:
return 1
else:
return eval(((str(a)+"*")*b)[:-1])
@antonnifo
antonnifo / denomations.py
Created January 19, 2019 08:58
Write a function that counts how many different ways you can make change for an amount of money, given an array of coin denominations.
def resolve(n, coins, k):
if k < 0 or n < 0:
return 0
if n == 0:
return 1
count = resolve(n, coins, k - 1) + resolve(n - coins[k], coins, k)
@antonnifo
antonnifo / ordinal.py
Created January 19, 2019 09:00
Finish the function numberToOrdinal, which should take a number and return it as a string with the correct ordinal indicator suffix (in English). For example, 1 turns into "1st"
def numbertoordinal(number):
if number < 0 or number > 10001:
return None
if number < 20: #determining suffix for < 20
if number < 10:
if number == 0:
suffix =''
elif number == 1:
suffix = 'st'
@antonnifo
antonnifo / pipenv_cheat_sheet.md
Created May 14, 2019 14:47 — forked from bradtraversy/pipenv_cheat_sheet.md
Pipenv cheat sheet for common commands

Pipenv Cheat Sheet

Install pipenv

pip3 install pipenv

Activate

pipenv shell
@antonnifo
antonnifo / django_deploy.md
Last active February 3, 2024 09:20 — forked from bradtraversy/django_deploy.md
Django Deployment - Digital Ocean

Django Deployment to Ubuntu 18.04

In this guide I will go through all the steps to create a VPS, secure it and deploy a Django application. This is a summarized document from this digital ocean doc

Any commands with "$" at the beginning run on your local machine and any "#" run when logged into the server

Create A Digital Ocean Droplet

Use this link and get $10 free. Just select the $5 plan unless this a production app.