Question

5. You have the following list stored in variable myList [ 3, 7, -1, 2, -10, 6, 8) i. Write a program that creates a new list

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

i)


def changeMyList(myList):
    #this function removes all negetives from myList
    modifiedMyList=[]       #resultant modified list of myList
    for item in myList:             #iterate over MyList
        if item>=0:                  #if element is not negetive the append to result
            modifiedMyList.append(item)
    #return result
    return modifiedMyList

#test-1
myList= [1,2,-3,-4,5,-6]

print("before:",myList,'\tAfter:',changeMyList(myList))

#test-2
myList= [0,3,4,5,-6]
print("before:",myList,'\tAfter:',changeMyList(myList))

before: [1, 2, -3, -4, 5, -6] After: [1, 2, 5] before: [0, 3, 4, 5, -6] After: [0, 3, 4, 5] def changeMyList(myList): #this faabove screenshot is for reference

ii).


def changeMyListModified(myList):
    #this function removes all negetives from myList and add zero instead of negetive numbers
    modifiedMyList=[]       #resultant modified list of myList
    for item in myList:             #iterate over MyList
        if item>=0:                  #if element is not negetive the append to result
            modifiedMyList.append(item)
        else:
            modifiedMyList.append(0)    #for negetive append zero to result
    #return result
    return modifiedMyList

#test-1
myList= [3,7,-1,2,-10,6,8]

print("before:",myList,'\tAfter:',changeMyListModified(myList))

#test-2
myList= [0,3,4,5,-6]
print("before:",myList,'\tAfter:',changeMyListModified(myList))

1 before: (3, 7, -1, 2, -10, 6, 8] After: [3, 7, 0, 2, 0, 6, 8] before: [0, 3, 4, 5, -6] After: [0, 3, 4, 5, 0] 4 2- def chanabove screen shot is for reference which includes code and ouput(right side)

logic used:

create an empty resultant list , iterate over myList and check each element . If element is greater or equal to zero then add to result in problem i , if element is greater than zero then append same number to resultant list else append zero to resultant list (this is in problem 2.)

note: in the programme changeMyListModified and changeMyList function is used to performe specific modification

Add a comment
Know the answer?
Add Answer to:
require python 5. You have the following list stored in variable myList [ 3, 7, -1,...
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
  • 3. Write Python statements that will do the following: a) Input a string from the user....

    3. Write Python statements that will do the following: a) Input a string from the user. *** Print meaningful messages along with your output.* b) Print the length of the String. Example input: The sky is blue. Correct output: The length of the string is 16 Incorrect output: 16 c) Print the first character of the string. d) Print the last character of the string. 4. Save the program. Double-check the left edge for syntax errors or warning symbols before...

  • 1) Which of the following is NOT true about a Python variable? a) A Python variable...

    1) Which of the following is NOT true about a Python variable? a) A Python variable must have a value b) A Python variable can be deleted c) The lifetime of a Python variable is the whole duration of a program execution.   d) A Python variable can have the value None.        2) Given the code segment:        What is the result of executing the code segment? a) Syntax error b) Runtime error c) No error      d) Logic error 3) What...

  • True or False: Python programs require two or more modules. 1. 2. The keyword while means the same thing as While in Python. 3. In Python, _MyVar15 is a valid variable name. 4. To make your progr...

    True or False: Python programs require two or more modules. 1. 2. The keyword while means the same thing as While in Python. 3. In Python, _MyVar15 is a valid variable name. 4. To make your program more secure, use obscure variable names such as xz14dEE 5. Indenting code that should be executed when an if statement evaluates as true makes your program easier to read, but the indentation is not necessary. 6. A dictionary object contains zero or more...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • IN PYTHON Develop the program that will allow you to obtain an ordered list of whole...

    IN PYTHON Develop the program that will allow you to obtain an ordered list of whole numbers from least to greatest from two lists of whole numbers. The individual values ​​of the input lists are captured by the user one by one and then both lists will be joined and then sorted in ascending order (from lowest to highest value). Your program should allow the capture of the entire values ​​of the lists without making use of messages (legends) to...

  • List Python

    A car park management system uses a list to represent which spaces in a car park are currently occupied and which are currently unoccupied. An empty space is represented by a 0 and a full space by a 1. Then the state of the car park would be represented by the following list: [1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0] The designers of the car park management system require a program that will accept a list such as the one above (although of course the length will not always be 11) and calculate what percentage of the spaces are currently occupied, rounded to the nearest percent. In the example above 7 spaces are occupied and there are 11 altogether, so the percentage occupied is: (7 / 11) x 100 = 63.63… = 64% to the nearest whole number. i.What are the admissible inputs of the problem? ii.What is the output of this problem? iii.Write three tests for this problem. The inputs should be different from the example given in the scenario description above. At least one of your tests must be a borderline case. Present the tests in a table, with a column for each input and output, and an extra column with a brief explanation of why you selected each test iv.Decompose the problem into sub-problems. Use the > notation and state in brackets the type of the problem and of each sub-problem v. Choose the patterns for the subproblem types you identified, instantiate the patterns into an algorithm, and translate the algorithm into code.This part of the question involves writing Python code. Write your code in this file and then submit the completed code file as part of the answer to this part of the question. Note: Although Python has a built-in function to find the sum of a list, please do not use it to compute how many 1s there are in the list. We want you to write your own code , because the purpose of this question is to practice following the problem solving approach described there, which will equip you to solve problems in other cases for which there is no built-in Python function. A Python program which is not preceded by a corresponding decomposition will gain at most half marks. Again, the reason for this is that we want you to follow a systematic problem-solving approach and not dive straight into code. Please therefore make sure that you have supplied an answer to iv. If you find it difficult to get the decomposition quite right at first there is no reason why you cannot review it when you have written the code. The main thing is to have a clear idea of what the solution needs to do (the decomposition) that ties in correctly with how it does it (the program). Make sure you use appropriate variable names and comments and that you have tested your code with the inputs of your test table. (Keep each of your test inputs in your code, but precede all of the test inputs, except for one, with the # symbol. Recall that # signals a comment, which is ignored by the Python interpreter.)

  • Comma Code (Using Python Language) Say you have a list value like this: spam = [...

    Comma Code (Using Python Language) Say you have a list value like this: spam = [ 'apples' , 'bananas' , 'tofu', 'cats' ] Write a function that takes a list value as an argument and returns a string will all the items separated by comma and space, with and inserted before the last item. For example, passing the previous spam list to the function would return 'apples, bananas, tofu and cats'. but your function should be able to work with...

  • Write a python program and pseudocode The Program Using Windows Notepad (or similar program), create a...

    Write a python program and pseudocode The Program Using Windows Notepad (or similar program), create a file called Numbers.txt in your the same folder as your Python program. This file should contain a list on floating point numbers, one on each line. The number should be greater than zero but less than one million. Your program should read the following and display: The number of numbers in the file (i.e. count them) (counter) The maximum number in the file (maximum)...

  • ** Language Used : Python ** PART 2 : Create a list of unique words This...

    ** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...

  • Please use python and show the code you used. Question 2 5 pts from sympy import...

    Please use python and show the code you used. Question 2 5 pts from sympy import sieve sieve.extend_to_no(999) primes - set (sieve._list) The code above creates a set of prime numbers, stored in the variable primes. Check whether or not the following list of numbers is prime, by checking if it is in the set primes. Keep the sequence of True/False values in the order given, and express that sequence as a 14-digit binary number. Enter the equivalent decimal integer...

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