Question

Write a Python file containing the following functions. Also turn in the output from testing the...

  1. Write a Python file containing the following functions. Also turn in the output from testing the functions. All arguments to the functions may be hard-coded.
    1. Function 1 takes a list of strings as a parameter and returns a list of strings consisting of all the strings in the original list that have identical consecutive letters. For example:

fun1 ([llama, good, cat, abba, abab, 112, dog])

returns [llama, good, abba, 112]

  1. Function 2 takes an integer (n), representing the number of toggle light switches in a hall. Pressing the switch changes the state of the light from off to on or from on to off. Assume that someone went down the hall n times and for all j, on the jth trip down the hall pressed only those switches whose position number was divisible by j.

Determine the final state of the last bulb (position n) after all trips down the hall are completed. The function should return True for on and False for off.

For example: n is 3 returns False

n is 4 returns True.

  1. Function 3 takes an integer (n) and returns the how many numbers less than or equal to n have a return value of True from function 2.
  1. Function 4 takes a list of mixed types and returns the number of lists in the list. For example:

fun4 ([1, [A, [2]], B, 3, C, [4, [D, 5]]])

returns the value 4 – for [A [2]], [2], [4, [D, 5]] and [D, 5]   

  1. Function 5 takes a list of anything and removes the odd position elements from the list (counting from 0). For example:

fun5 ([A, [A, B], C, D, [A, B]])

returns [A, C, [A, B]]

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Question # b and # c is confusing I am unable to provide help on it. Rest all the remaining function has been implemented.

If you could add more clarity to question #b and #c I will do it, no need to post another question for this. Sorry for the inconvenience.


Thank You !

===========================================================================

# a
def fun1(strings):
    consecutive_letter_word_list = []
    for word in strings:

        for i in range(0, len(word) - 1):
            if word[i] == word[i + 1]:
                consecutive_letter_word_list.append(word)
                break

    return consecutive_letter_word_list


print(fun1(['llama', 'good', 'cat', 'abba', 'abab', '112', 'dog']))


# d

def fun4(types):
    count = 0
    for element in types:
        if isinstance(element, list):
            count += 1
            for inner_element in element:
                if isinstance(inner_element, list):
                    count += 1
    return count


c = fun4([1, ['A', [2]], 'B', 3, 'C', [4, ['D', 5]]])
print(c)


# e
def func5(arr):
    count_to_be_deleted = (len(arr) ) // 2
    index=1
    while count_to_be_deleted > 0:
        del arr[index]
        count_to_be_deleted-=1
        index+=1


arr=['A', ['A', 'B'], 'C', 'D', ['A', 'B']]
print('Before deleting: ',arr)
func5(arr)
print('After deleting: ',arr)

=========================================================================

I 1: Proj def funl (strings) : consecutive_letter_word_list = [] for word in strings: for i in range (0, len (word) - 1): if

def func5 (arr) : count_to_be_deleted = (len (arr) ) // 2 index=1 while count_to_be_deleted > 0: del arr[index] count_to_be_d

Add a comment
Know the answer?
Add Answer to:
Write a Python file containing the following functions. Also turn in the output from testing the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please place all of the following functions (defun …) into a SINGLE .lsp file As such,...

    Please place all of the following functions (defun …) into a SINGLE .lsp file As such, be sure to use the EXACT SAME function names and parameter numbers and orders I provide ​ Write a function ONEFIB that takes a single parameter n and returns the nth Fibonacci number. Write a function ALLFIB that takes a single parameter n and returns a list of the first n Fibonacci numbers. Do not worry about efficiency here. HINT: You can use ONEFIB...

  • Write C++ Code in single source file (.cpp file) containing following user defined functions: 1. IsEven-...

    Write C++ Code in single source file (.cpp file) containing following user defined functions: 1. IsEven- takes an integer input (one argument of type int) and return true or false. 2. IsPositive- takes a float input (one argument of type double) and return a Boolean value. The function returns the true if its argument is positive and false if its argument is zero or negative. 3. IsPrime- takes an integer input and return true or false. 4. NumOfDigits- takes an...

  • In PYTHON! Exercise 3: Lists and Functions In this exercise we will explore the use of...

    In PYTHON! Exercise 3: Lists and Functions In this exercise we will explore the use of lists and functions with multiple inputs and multiple outputs. Your task is to implement the separate_and_sort function. This function takes two input parameters: 1. A list containing strings and integers in any order. 2. An optional integer parameter size which controls how many items in the list the function will process. If the length of the list is smaller than the value of size,...

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • Define the functions in Python 3.8 1. Write a function most frequent n that takes a...

    Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...

  • IN PYTHON: Write a function that takes, as an argument, a positive integer n, and returns...

    IN PYTHON: Write a function that takes, as an argument, a positive integer n, and returns a LIST consisting of all of the digits of n (as integers) in the same order. Name this function intToList(n). For example, intToList(123) should return the list [1,2,3].

  • Instructions We will carry on working with functions! Once more, all input and output has been...

    Instructions We will carry on working with functions! Once more, all input and output has been taken care of for you. All you need to do is finish off the function tableMin that finds the minimum value within a table (2-D list). Details Input Input has been handled for you the list has been populated. It reads in N as well as the data needed to fill an NxN integer table. Processing Complete the tableMin function. Note that you have...

  • Write a Python program (remove_first_char.py) that removes the first character from each item in a list...

    Write a Python program (remove_first_char.py) that removes the first character from each item in a list of words. Sometimes, we come across an issue in which we require to delete the first character from each string, that we might have added by mistake and we need to extend this to the whole list. Having shorthands (like this program) to perform this particular job is always a plus. Your program should contain two functions: (1) remove_first(list): This function takes in a...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • (Must be written in Python) You've been asked to write several functions. Here they are: yourName()...

    (Must be written in Python) You've been asked to write several functions. Here they are: yourName() -- this function takes no parameters. It returns a string containing YOUR name. For example, if Homer Simpson wrote this function it would return "Homer Simpson" quadster(m) -- this function takes a value m that you pass it then returns m times 4. For example, if you passed quadster the value 7, it would return 28. isRratedMovieOK(age) -- this function takes a value age...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT