Python Challenges

Python Challenges


Easy

Calculate the difference between the maximum and minimum of numbers in a list.

Write a function that gets a list of numbers (as a list) from the user and calculates the difference between the max and min of the numbers. For ex., a list of numbers like so.. [ -1,20,34,-25,12,90 ] . The minimum number in this list is -25 and the maximum is 90. The difference between the minimum and maximum is 90 – (-25 ) = 115.
code
def diff (num) :
    return max(num) - min(num)

case insensitive equality of strings

Write a function that compares two strings and returns True if they are equal. However, make sure you do a case insensitive comparison. For example, the following strings are equal if you ignore the case.

name_1 = “Ajay”
name_2 = “ajay”
code
def compare (str1, str2) :
    if str1.lower() == str2.lower():
        return True
    else : 
        return False

Check if a sentence contains spaces

Write a function that takes in a string and returns True if the string has spaces. A space could be a blank space or a tab ( \t ) character.
code
def compare (str1, str2) :
def isspace ( sentence) :
    if " " in sentence or "\t" in sentence : 
        return True
    else :
        return False

Check if a string is contained in the keys or values of a dictionary

Write a function that takes in a string/number/float as the first argument and a dictionary as the second argument. The function should verify if the first argument is there as a key or value in the dictionary. If yes, return the corresponding key value pair.
code
def checkKeyValue(key, dictionary) :
    keys = dictionary.keys()
    values = dictionary.values()
    
    if key in keys : 
        return key, dictionary[key]
    if key in values : 
        return list(keys)[list(values).index(key)] , key

# Try it with a simple dictionary like this
dictionary = {"India"             : 1.3, 
              "China"             : 1.4,
              "United States"     : 0.3,
              "Vietnam"           : 0.1 }

checkKeyValue(1.3,dictionary)

Find the number of digits in a number

Write a function that takes in a number and returns the number of digits in a number. For example, if you pass in the number 12345 should return 5.
code
def findDigits(num) :
    
    count = 0
    while num > 0 :
        num = num // 10
        count = count + 1
    
    return count

Reverse a list ( without using the reversed() or .reverse() functions

Write a function that takes in a list and returns a reversed list. Doing this using the standard reversed() function or list.reverse() is a one-liner. However, try to do it without using these functions. This requires you to essentially navigate the list element by element in reverse order to re-build it.
code
def findDigits(num) :
    
def reverse(li):
    
    i = len(li)
    new_list = []
    
    while i > 0 :
        new_list.append(li[i-1])
        i = i - 1
    
    return new_list

# if you could use the built-in functions, this would be much easier. 
# like 
numbers.reverse() 
# OR get a reverse iterator using 
reversed(numbers)

valid zip codes

Write a function that takes in a zip code and returns if if it a valid zip code. Assume US zip codes in the following formats.

Format 1 –
– Should only contains 5 numbers

Format 2 –
– Should look like this XXXXX-XXXX where X is always a number
code
def us_zip_code ( zip_code ) :
    
    # assume zip_code is a string
    
    if zip_code.isnumeric() and len(zip_code) == 5 :
        return True
    
    if zip_code[0:5].isnumeric() and zip_code[5] == "-" and zip_code[6:10].isnumeric() and len(zip_code) == 10:
        return True
    
    return False

Medium

%d bloggers like this: