Question

In: Computer Science

Question 12 PYTHON: Write a function named first_last that takes a single parameter, string_list (a list...

Question 12 PYTHON:

Write a function named first_last that takes a single parameter, string_list (a list of strings). The
function first_last should return a list of the strings in string_list that are not empty and that begin
and end with the same letter.

For example, the following would be correct input and output for the function first_last.

response = ['to', 'that', 'I', 'say', '', 'hurrah']
print(first_last(response))
['that', 'I', 'hurrah']

Question 13 (20 points)

Write a function named number_luck. The function number_luck takes two parameters:

1. lucky, a list of lucky numbers between 2 and 12, inclusive
2. unlucky, a list of unlucky numbers between 2 and 12, inclusive

Every number is either lucky, unlucky or boring (neither lucky nor unlucky).

The function number_luck should

1. ask the user for a number in the range 2 to 12 (you may assume that the user provides valid
input)
2. print a message echoing the user’s number and stating whether it is lucky, unlucky or boring
3. return an integer that is the user’s number

For example, the following would be correct input and output.

>>> a_num = number_luck([7, 11], [2, 3, 12])
Give me a number from 2 to 12: 7
7 is lucky. You win!
>>> print(a_num)
7

Solutions

Expert Solution

Python code:

def first_last(string_list):
    l=[]
    for i in string_list:
        if len(i)==0 or i[0]!=i[-1]:
            pass
        elif(i[0]==i[-1]):
            l.append(i)
        else:
            pass
    return l
  
def number_luck(lucky,unlucky):
    n=int(input("Give me a number in the range of 2 to 12 inclusive : "))
    if n in lucky :
        return "lucky"
    elif n in unlucky:
        return "unlucky"
    else:
        return "neither lucky nor unlucky"

print("Question12")
response = ['to', 'that', 'I', 'say', '', 'hurrah']
print("Initial list : ",response)
print("After callng first_last function : ",first_last(response))
print()
print("Question13")
a_num = number_luck([7, 11], [2, 3, 12])
print(a_num)

Execution screenshots:
Note:please like the answer if you are satisfied with it. Thank you in advance.       


Related Solutions

Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and...
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and returns a new list containing each string in the title-case version. Any strings that have less than 5 characters needs to be expanded with the appropriate number of space characters to make them exactly 5 characters long. For example, consider the following list: words = ['Right', 'SAID', 'jO'] The new list would be: ['Right', 'Said ', 'Jo '] Since the second element only contains...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "w" replaced with the character "v" My code: function replacement(word){ var str=word; var n=str.replace("w","v"); return n; } Syntax Error: function replacement incorrect on input Not sure how to fix? Can't use a loop for answer
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It returns a list which duplicated elements remains and each duplicated element is followed by a number which shows how many times it appears. All elements in return list should be in the same order as their appearance in the original list. For example, given the input [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘d’, ‘a’,‘e’], the function would return [‘a’, 3, ‘b’, 2]. Another example, ['abc',...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
IN SCHEME Write a function subtract which takes a list of numbers as a parameter and...
IN SCHEME Write a function subtract which takes a list of numbers as a parameter and returns a list of the differences. So, a list containing the second minus the first, third minus the second, etc. (subtract '(3 4 10 14 5)) '(1 6 4 -9)
Can someone do this python program? Write a function that takes a number as a parameter...
Can someone do this python program? Write a function that takes a number as a parameter and then prints out all of the factors for that number.
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT