Skip to content

Instantly share code, notes, and snippets.

@stevenewey
Last active December 16, 2015 23:21
Show Gist options
  • Save stevenewey/5513210 to your computer and use it in GitHub Desktop.
Save stevenewey/5513210 to your computer and use it in GitHub Desktop.
A Python test for potential apprentices
#!/usr/bin/env python
#
# Regenology interview test
#
"""
#
# Try to complete as many of these as you can in the available time. Don't
# worry if you can't finish, and move on to the next task if you get stuck.
# You are free to use the Internet to assist. You can run an interactive
# Python terminal to test things by running the command 'ipython' in the
# terminal.
#
print('You can return information to the user with print')
# 1. Capitalise each word in the string below.
cap_me = 'the quick brown fox jumped over the lazy dog.'
# 2. Calculate the sum of the integers in this list.
sum_me = [20, 7, 42, 16, 93]
# 3. Print the largest number.
find_me = [2, 73, 392, 102, 49, 2931, 7]
# 4. Copy and paste the code below into ipython, observing the result:
### copy from here ###
import turtle
t = turtle.Turtle()
t.forward(10)
t.right(90)
t.forward(10)
t.right(90)
t.forward(20)
t.right(90)
t.forward(20)
t.right(90)
### copy to here ###
#
# Using as little code as you can, try to repeat the expanding pattern
# adding 10 pixels each time until the lines reach 250 pixels.
#
"""
# 5. The program below will ask a user for their date of birth.
# It's not too clever about the user making mistakes, but we won't worry
# about that for now.
#
# You can run this program in a fresh terminal by typing: python pytest.py
import datetime
from dateutil.relativedelta import relativedelta
def get_birthday():
birth_year = int(raw_input('What is your year of birth (full year)? '))
birth_month = int(raw_input('What is your month of birth (1-12)? '))
birth_day = int(raw_input('What is your day of birth (1-31)? '))
return datetime.date(birth_year, birth_month, birth_day)
def get_days():
days = int(
raw_input('How many days into the future shall we go? '))
return days
birthday = None
timedelta = None
while not birthday:
try:
birthday = get_birthday()
except Exception:
continue
age = relativedelta(datetime.date.today(), birthday)
print("You are %d years old at the moment!" % (age.years))
while not timedelta:
try:
timedelta = get_days()
except ValueError:
continue
# Now, complete the program by calculating and returning the user's age in
# years at 'timedelta' days in the future. If you want to get really fancy
# then you can tell them how many years, months and days old they'll be.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment