Question

Find the expected result of the two list comprehension by hand (show your work): list_1 list_2 [10-thing for thing in range (

2. Use python to, and you may not import any modules. Write a program that will take a list as input and return a list where

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This problem is related to python programming language.

1.

list_1= [10-thing for thing in range(20,1,-1)]

Above is an example of list comprehension, range(20,1,-1) will generate a sequence from 20 to 1(excluding) in reverse order like 20, 19, 18, 17,......, 2. then each number will be subtracted from value 10 hence the elements in the list_1 will be

10-20, 10-19, 10-18, 10-17, 10-16,....., 10-2 which means -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8.

list_2=[val**3 for val in [1, 1, 2, 2, 3, 3, 4, 4] if val!=2]

In this list comprehension, list_2 will have cubes of each element except for the value 2. hence, the elements for the list_2 will be 1**3, 1**3, 3**3, 3**3, 4**3, 4**3 i.e. 1, 1, 27, 27, 64, 64.

2. Please refer the screenshot for the indentation.

Program implementation:
#function to calculate the required list
def neighbour_sum(lst):
#result list
result=[]
#loop iterates through each element of the list
for i in range(len(lst)):
#if element is first element of the list
if(i==0):
result.append(lst[i]+lst[i+1])
#if element is last element of the list
elif(i==len(lst)-1):
result.append(lst[i]+lst[i-1])
#if element is neither first nor last element
else:
result.append(lst[i]+lst[i-1]+lst[i+1])
#returns the result list
return result
  
#function call to the neighbour_sum() with required input
print(neighbour_sum([1,3,5,6,9]))

  Program implementation and code output screenshot:

Spython main.py [4, 9, 14, 20, 15] 1 2 #function to calculate the required list 3- def neighbour_sum(1st): 4 #result list 5 r

Feel free to ask if you have any doubts in the comment section.

HOPE YOU LIKE THE SOLUTION!!!!

Spython main.py [4, 9, 14, 20, 15] 1 2 #function to calculate the required list 3- def neighbour_sum(1st): 4 #result list 5 result=[] 6 #loop iterates through each element of the list 7 for i in range(len(lst)): 8 #if element is first element of the list 9 if(i==0): 10 result.append(1st[i]+1st[i+1]) 11 #if element is last element of the list 12 elif(i==len(1st)-1): 13 result.append(ist[i]+1st[i-1]) 14 #if element is neither first nor last element else: 16 result.append(1st[i]+lst[i-1]+lst[i+1]) 17 #returns the result list 18 return result 19 20 #function call to the neighbour_sum() with required input 21 print(neighbour_sum((1,3,5,6,9])) 15 22

Add a comment
Know the answer?
Add Answer to:
Find the expected result of the two list comprehension by hand (show your work): list_1 list_2...
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
  • Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n...

    Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n as parameters and returns a NEW list which contains the n largest values in the parameter list. The values in the returned list should be in increasing order. The returned list must always be of length n. If the number of values in the original list is less than n, the value None should be repeated at the end of the returned list to...

  • 1. Using list comprehension and a single line of code, flatten a given list of lists....

    1. Using list comprehension and a single line of code, flatten a given list of lists. For example your line of code must give [1, 2, 3, 4, 5, 6] out of [[1, 2], [3, 4], [5, 6]]. 2. Using list comprehension, and without using any imported module, write a Python function to return a list of email addresses in a given phrase of text. Inside your function you must do this with a single line of code. For example,...

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • I need help for this assignment. Thank you!! # 5555555555555555555555555555555555555555555555555 # Returns the longest word (which has six or more # characters and contains the letter, e) from # the...

    I need help for this assignment. Thank you!! # 5555555555555555555555555555555555555555555555555 # Returns the longest word (which has six or more # characters and contains the letter, e) from # the parameter list - 4 marks Os Define the get_longest_e_word() function which is passed a list of strings as a parameter. The function returns the word in the list which has the most characters (i.e., the longest word) BUT only words which have 6 or more characters and contain the letter...

  • def second_index(a_list, number): Accepts a list of integers and an integer, and returns the index of...

    def second_index(a_list, number): Accepts a list of integers and an integer, and returns the index of the SECOND occurrence of the integer in the list. It returns None if the number does not occur two or more times in the list. Example 1: second_index ([2,34,3,45,34,45,3,3), 3) returns 6 Example 2: second_index( [2,34,3,45,34,45,3,3), 45) returns 5 Example 3: second_index ( [2,34,3,45,134,45,3,3), 134) returns None Example 4: second_index( [2,34,3,45, 134,45,3,3), 100) returns None return None def hasEveryLetter(s): #s is a string Returns...

  • 3.5 max number We will pass in a list of numbers your job is to find...

    3.5 max number We will pass in a list of numbers your job is to find the largest number in that list and outputted it’s index , not the actual value # Get our numbers from the command line import sys 4 numbers sys.argv[1].split',') Collagse) Challenges 5 numbers int(i) for i in numbers] 3. 5. Max number # Your code goes here We will pass in a list of numbers. Your job is to find the largest number in that...

  • Define a function called collapse() which takes a list as input. Each element of the list...

    Define a function called collapse() which takes a list as input. Each element of the list will either be an integer, or a list of integers. The function should modify the input list by replacing all of the elements which themselves are lists with the sum of their elements. For example: Test Result vals = [1, 2, 3, 4, 5] collapse(vals) print(vals) [1, 2, 3, 4, 5] vals = [1, [2, 3], 4, 5] collapse(vals) print(vals) [1, 5, 4, 5]...

  • In java, how can I convert this text file format: n=6 m=7 1 2 5 4...

    In java, how can I convert this text file format: n=6 m=7 1 2 5 4 9 5 6 2 3 2 3 4 3 5 6 2 6 4 2 // end of file into this text file format: 1,2,5 1,4,9 1,5,3 2,3,2 3,4,3 5,6,2 6,4,2 This is how to read the first text file: The first line of each file below contains the number of vertices and the number of edges in the graph (in the format "n=XXXX...

  • A study of reading comprehension in children compared three methods of instruction. The three methods of...

    A study of reading comprehension in children compared three methods of instruction. The three methods of instruction are called Basal, DRTA, and Strategies. As is common in such studies, several pretest variables were measured before any instruction was given. One purpose of the pretest was to see if the three groups of children were similar in their comprehension skills. The READING data set described in the Data Appendix gives two pretest measures that were used in this study. Use one-way...

  • en Your Comprehension Sharpen dhe following matching exercise by selecting, from the list p that bess...

    en Your Comprehension Sharpen dhe following matching exercise by selecting, from the list p that bess is correcr a case mix statement ts. For each statement, only one answer f. fraud & LOINC h. nomenclature i. unbundling j. whistle-blower c dinical medical terminology d. compliance e ctiology 1, Standard medical language 2Sysem for maming hing wihin paniadr pup a particular g 3. _ _ System for tests, measurements, and observations Cause of a disease or a condition 5.- Acting in...

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