Coding challenges in Python !

Elliott Saslow
Future Vision
Published in
3 min readAug 20, 2018

--

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:

  1. Check to see if it is negative.
  2. Turn the number to a string — if negative, only turn the numerical characters into a string.
  3. Reverse the string using reversed
  4. Join the reversed list using ‘’.join(string)
  5. Turn the string into an integer using int() function
  6. If it was negative multiply by -1
  7. 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:

  1. Sort the incoming array
  2. Check to see if the sorted array is the same as the original array
  3. If they are the same, return True, otherwise return false

My solution:

def in_asc_order(arr):
return sorted(arr) == arr
arr = [1,2,5,4]
in_asc_order(arr) #returns False
arr = [1,2,4]
in_asc_order(arr) #returns True

Challenge #3

Given a sorted array of distinct integers, write a function index_equals_valuethat returns the lowest index for which array[index] == index. Return -1 if there is no such index.

Steps to a solution:

  1. Given an array, go through each value in the array, and keep track of what index each is at using enumerate
  2. If the value is equal to the index: return that value
  3. Keep checking until you get to the end
  4. 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:

  1. Create a dictionary mapping each letter to its number in the alphabet
  2. Create a dictionary mapping each number to its letter in the alphabet
  3. Go through each letter in the sentence and find the corresponding number, add 2 and then find the new corresponding letter
  4. Make sure to take care of the edge cases so that if you get the letter z, it maps to b… ect
  5. 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?

--

--