Function

homework
python

#1
  1. Write a function named is_three_digit_number that accepts a single parameter number. This function returns True if number is three digits, False otherwise. You can assume number is always positive.
    Test cases:
    0 -> False
    10 -> False
    100 -> True
    999 -> True
    500 -> True
    20 -> False

  2. Write a function named get_three_digit_numbers that accepts a single parameter, number_list. This function returns a list containing all of the three digit numbers in number_list. You will not receive full credit if this function does not include a call to the is_three_digit_number function.
    Test cases:
    [200, 300, 500] -> [200, 300, 500]
    [400, 999, 988, 20] -> [400, 999, 988]
    [400, 999, 988, 203] -> [400, 999, 988, 203]
    [20, 300] -> [300]
    [30, 40, 50] -> []

  3. Write a function named get_shortest_word that accepts a single parameter, word_list. This function returns the word with the shortest length (smallest # of characters) in word_list.
    Test cases:
    [‘go’, ‘java’, ‘python’, ‘fortran’] -> ‘go’
    [‘dora’, ‘the’, ‘explorer’] -> ‘the’
    [‘kid’, ‘a’] -> ‘a’

  4. Write a function named index_of_number that accepts two parameters, number_list and target_number. The function returns the index of target_number in number_list or -1 if target_number is not contained in number_list.
    Test cases:
    [80, 90, 10], 10 -> 2
    [80, 90, 10], 50 -> -1
    [6, 9, 2], 6 -> 0
    [6, 9, 2], 5 -> -1

  5. Write a function named get_first_word_containing that accepts two parameters, word_list and character. The function returns the first word in word_list that contains character. If no words in word_list contain character, then return None.
    Test cases:
    [‘boat’, ‘board’, ‘water’], ‘w’ -> ‘water’
    [‘tomorrow’, ‘friday’, ‘chair’, ‘keyboard’], ‘c’ -> ‘chair’
    [‘today’, ‘monitor’, ‘computer’, ‘mouse’], ‘z’ -> None

Bonus) Write a function named is_palindrome that accepts a single parameter named string. This function returns True if string is a palindrome and False otherwise. A palindrome is a string that is the same forwards and backwards.
Test cases:
‘madam’ -> True
’mom’ -> True
’dog’ -> False
’pillow’ -> False
’racecar’ -> True
’senses’ -> False
’m’ -> True


#2

This challenge took about 30 minutes to complete. If I’m your teacher it would not impress me much that you have not even attempted the problems and opted instead to reach for help from an online community. Do you expect to learn anything?

Below find my solutions (cheat code) but your teacher will smell a rat if you use any of them. I suspect you have not mastered many of the concepts I applied. Create your own solutions for the best learning experience and the take away of your own memory of how you found them and the muscle memory your fingers gained by typing and debugging code.

1.
def is_three_digit_number(number):
    return number in range(100, 1000)
2.
def get_three_digit_numbers(numbers_list):
    return numbers_list.filter(lambda x: is_three_digit_number(x))
3.
def get_shortest_word(word_list):
    lens = list(map(lambda x: len(x), word_list))
    return word_list[lens.index(min(lens))]
4.
def index_of_number(number_list, target_number):
    return -1 if target_number not in number_list else number_list.index(target_number)
5.
def get_first_word_containing(word_list, character):
    words = list(filter(lambda x: character in x, word_list))
    return None if len(words) < 1 else words[0]
6.
def is_palindrome(string):
    return string == string[::-1]

https://repl.it/NikI


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.