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 / 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 / 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 / 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')