Question
language is python

Write a function named subsetStrings that has two inputs, a list of strings and an integer n. Your function should use a sing
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello,

PFB my python code. You can change the way of input if required. I have used a method that takes input as space separated characters for the list. I have also added comments to the code for better understanding. Please let me know in case of any queries/modification regarding the solution / in case of any further assistance required.

***************************************** Python Code Starts****************************************************

#function defined for the substring and list operation
#function takes the input list and the number n as input
def list_substring(input_list, input_no):

#define the list to be returned and string to store the substring that will be added to the list  
   new_list = []
   new_string = ''
#traverse through every element of the list  
   for i in input_list:
#calculate the length of the element of the list      
       len_i = len(i)
#if the length is greater than or equal to the input number then display the first n characters from the string.
#strings with length less than the input_no will be skipped      
       if (len_i >= input_no):
#displays the first n characters of the string      
           new_string = i[:input_no]
#add it to the new list that will be returned          
           new_list.append(new_string)
#return the list
   return new_list

#take input list from the user
input_data = input('Enter details separated by space')
#take input number
input_no = input('Enter a number')
#execute the logic only if both the list and number is provided
if(input_no != '' and input_data != ''):
#convert the input to list by splitting the space separated string using split() function  
   list_string = input_data.split()
   new_list = []
#call the function list_substring defined above and pass the input number and input list to the function  
   new_list = list_substring(list_string,int(input_no))
#print the list returned from the function  
   print(new_list)

************************************** Python code ends ***************************************************

Add a comment
Know the answer?
Add Answer to:
language is python Write a function named subsetStrings that has two inputs, a list of strings...
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
  • In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and...

    In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and n2. Using a single list comprehension to make a list of all of the numbers in the range 1..N that are either divisible by n1 or n2, where n1 and n2 can be any positive integers. After creating the list, your code should print out the following 2 lines: “We are printing all numbers from 1 to that are divisible by or ” “These...

  • PYTHON QUESTION PLEASE!! Write a function named problem3 that accepts two strings as the arguments, returns...

    PYTHON QUESTION PLEASE!! Write a function named problem3 that accepts two strings as the arguments, returns the characters that occur in both strings. Test case: the arguments are “apple@123” and “banana@#345”, your program should return “a@3”. Write a function named problem3 that accepts two strings as the arguments, returns the characters that occur in both strings. (20 pts) Test case: the arguments are "apple@123” and “banana@#345”, your program should return "a@3".

  • Write a function named words_in_both that takes two strings as parameters and returns a set of...

    Write a function named words_in_both that takes two strings as parameters and returns a set of the words contained in both strings. You can assume all characters are letters or spaces. Capitalization shouldn't matter: "to", "To", "tO", and "TO" should all count as the same word. The words in the set should be all lower-case. For example, if one string contains "To", and the other string contains "TO", then the set should contain "to". The file must be named: words_in_both.py...

  • Define a function named how_many_substr_of_string(...) which receives two parameters, the first parameters is a list with...

    Define a function named how_many_substr_of_string(...) which receives two parameters, the first parameters is a list with strings (name it  listst) and the second parameter is a single string (name it st). The function should return a number, indicating how many of the strings in the list listst are substrings in the string st As an example, the following code fragment: listst = ["a","bc","dd"] st = "abc" res = how_many_substr_of_string(listst,st) print (res) should produce the output: 2 Language Python

  • python: Write a function named "write_values" that takes a key-value store mapping strings to strings as...

    python: Write a function named "write_values" that takes a key-value store mapping strings to strings as a parameter and writes the values of the input to a file named "persuade.txt" with one element per line. If a file named "persuade.txt" already exists it must be overwritten. The function should not return any value

  • python Write a function named average_list() that uses a string list and outputs the av. len...

    python Write a function named average_list() that uses a string list and outputs the av. len of all the strings in the list with also the amount of strings with a len of ^ average, below, and = to avg.

  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. Write a function...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. Write a function named capital_letter that accepts a string as an argument and checks if each word in the string begins with a capital letter. If so, the function will return true, otherwise, return false. Please see the outcome below: Outcome number 1: Enter a string: Python Is Really Fun! True Sample run number 2: Enter a string: i Love Python False Note: Try to keep this...

  • Write a function named words_in_both that takes two strings as parameters and returns a set of...

    Write a function named words_in_both that takes two strings as parameters and returns a set of the words contained in both strings. You can assume all characters are letters or spaces. Capitalization shouldn't matter: "to", "To", "tO", and "TO" should all count as the same word. The words in the set should be all lower-case. For example, if one string contains "To", and the other string contains "TO", then the set should contain "to". You can use Python's split() funciton,...

  • Python code Write a program that will ask the user to input two strings, assign the...

    Python code Write a program that will ask the user to input two strings, assign the strings to variables m and n If the first or second string is less than 10 characters, the program must output a warning message that the string is too short The program must join the strings m and n with the join function The output must be displayed. Please name the program and provide at least one code comment (10)

  • Define a Python function named getResponse() that takes a string as its only argument and returns...

    Define a Python function named getResponse() that takes a string as its only argument and returns a new string. If the argument ends with a question mark and has an even number of characters, the function should return the string "Yes". If the argument ends with a question mark and contains an odd number of characters, the function should return the string "No". If the argument ends with an exclamation point, the function should return the string "Wow". Otherwise, the...

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