Coding challenges in Python !
Learn to code in python with these quick and easy challenges
Challenge #1
Given a number, write a function to output its reverse digits. (e.g. given 123 the answer is 321) Make sure that if it is a negative number you keep the negative in the front (-123 becomes -321)
Steps to solving this problem:
- Check to see if it is negative.
- Turn the number to a string — if negative, only turn the numerical characters into a string.
- Reverse the string using
reversed
- Join the reversed list using
‘’.join(string)
- Turn the string into an integer using
int()
function - If it was negative multiply by -1
- Return the number
My solution
def reverse_number(n):
if str(n)[0] == '-':
n = -1*int(''.join(reversed(str(n)[1:])))
else:
n = int(''.join(reversed(str(n))))
return n
Challenge #2
Determine whether the numbers are in ascending order. An array is said to be in ascending order if there are no two adjacent integers where the left integer exceeds the right integer in value.
Steps to a solution:
- Sort the incoming array
- Check to see if the sorted array is the same as the original array
- If they are the same, return True, otherwise return false
My solution:
def in_asc_order(arr):
return sorted(arr) == arrarr = [1,2,5,4]
in_asc_order(arr) #returns Falsearr = [1,2,4]
in_asc_order(arr) #returns True
Challenge #3
Given a sorted array of distinct integers, write a function index_equals_value
that returns the lowest index for which array[index] == index
. Return -1
if there is no such index.
Steps to a solution:
- Given an array, go through each value in the array, and keep track of what index each is at using
enumerate
- If the value is equal to the index: return that value
- Keep checking until you get to the end
- If none of the values are the same, return -1
My solution
def index_equals_value(arr):
for index, value in enumerate(arr):
if index == value:
return index
return -1
Challenge #4
You are given a sentence, and want to shift each letter by 2 in alphabet to create a secret code. The sentence you want to encode is the lazy dog jumped over the quick brown fox
and the output should be ’vjg ncba fqi lworgf qxgt vjg swkem dtqyp hqz’
Steps to a solution:
- Create a dictionary mapping each letter to its number in the alphabet
- Create a dictionary mapping each number to its letter in the alphabet
- Go through each letter in the sentence and find the corresponding number, add 2 and then find the new corresponding letter
- Make sure to take care of the edge cases so that if you get the letter z, it maps to b… ect
- Print the encoded string
My Solution:
import string
Number2Letter = {}
Letter2Number ={}# Make both dictionaries
for i,letter in enumerate(string.ascii_lowercase):
Number2Letter[i] = letter
Letter2Number[letter] = i# Encode the string!
s2 = ''
for letter in 'the lazy dog jumped over the quick brown fox':
if letter in Letter2Number:
current_letter_number = Letter2Number[letter] + 2 # take care of the edge cases
if current_letter_number == 26:
current_letter_number = 0
elif current_letter_number == 27:
current_letter_number = 1 s2 += Number2Letter[current_letter_number] else: s2+=letter
How would you decode?