Question

Lists in python have 2 methods: s.pop([i]) where i is the position in the list to...

Lists in python have 2 methods: s.pop([i]) where i is the position in the list to be “popped”, and s.remove(x) that are somewhat similar. (Check the notes for a description of what they do.) (10 points total)

  1. What are 2 important differences between these 2 functions? (2 points)

We looked at a program that implemented the Sieve of Eratosthenes using a list. The program below implements the same algorithm, but uses pop() and remove to do it. The basic idea is to locate the “next” prime, remove it from x and add it tp primes, and then remove all of its multiples from x. The program terminates when x is empty. (8 points)

Fill in the blanks (b-d).

x=[]

for i in range(2,100):

    x.append(i)

print(x)

primes=[]

while ______b______:

    k=x._____c__________

    primes.append(k)

    i=1

    while i*k<=99:

        if i*k in x:

            x.____d______(e)

        i+=1

print(primes)


Answer:

a=

b=

c=

d=

e=

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

Difference between pop() and remove() functions :

pop():

its remove element from list by index.

if you want to remove element of ith index then pop(i)

if you did not pass anything in pop function then it remove last element from list

remove():

it removes element from list by value

if you want to remove j value from list then you can write like this remove(j)

S.No Pop() Remove()
1. It removes element by index it removes element by value
2. when you did not pass anything to function, it removes last element of list when you did not pass anything to function, it gives error

x=[]
for i in range(2,100):
x.append(i)
print(x)
primes=[]
while x:
k=x.pop(0)
primes.append(k)
i=1
while i*k<=99:
if i*k in x:
x.remove(i*k)
i+=1
print(primes)

In [10]: 6 1 X=[] 2 for i in range(2,100): 3 X.append(i) 4 print(x) 5 primes=[] while x: 7 k=x.pop() 8 primes.append(k) i=1 1

answers:

b=x

c=x.pop(0)

d=remove

e=i*k

Add a comment
Know the answer?
Add Answer to:
Lists in python have 2 methods: s.pop([i]) where i is the position in the list to...
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
  • PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than...

    PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than 1 that is evenly divisible only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: Create a list with all elements initialized to 1 (true). List elements with prime indexes will remain 1. All other elements will eventually be set to zero. Starting with list element 2, every time a list element is found...

  • Hello, would like help with the following question please: 2 This question is on formal methods and software verification (i) Explain why a formal specification of a program is more effective if it i...

    Hello, would like help with the following question please: 2 This question is on formal methods and software verification (i) Explain why a formal specification of a program is more effective if it is considerably shorter than the program that implements the [6 marks] specification (ii) Given the following program x = 0; while (m !- 9) x x + 100; here m and x are integer variables (a) Identify the program input variable or variables (b) Identify the program...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice {       private static final int[]...

    import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice {       private static final int[] arr = new int[100000];    public static void main(String[] args) {        for(int i=0; i<100000; i++)            arr[i] = i;               //TODO comment out this line        LinkedList<Integer> list = new LinkedList<Integer>();               //TODO uncomment this line        //List<Integer> list = new ArrayList<Integer>();               //TODO change the rest of the...

  • Something is preventing this python code from running properly. Can you please go through it and...

    Something is preventing this python code from running properly. Can you please go through it and improve it so it can work. The specifications are below the code. Thanks list1=[] list2=[] def add_player(): d={} name=input("Enter name of the player:") d["name"]=name position=input ("Enter a position:") if position in Pos: d["position"]=position at_bats=int(input("Enter AB:")) d["at_bats"] = at_bats hits= int(input("Enter H:")) d["hits"] = hits d["AVG"]= hits/at_bats list1.append(d) def display(): if len(list1)==0: print("{:15} {:8} {:8} {:8} {:8}".format("Player", "Pos", "AB", "H", "AVG")) print("ORIGINAL TEAM") for x...

  • Can I have the answer for the Questions in Python 3 CSC1015F Assignment 4: Control (if,...

    Can I have the answer for the Questions in Python 3 CSC1015F Assignment 4: Control (if, for, while) Assignment instructions This assignment e s con Python programs that use isputandoutput statements, and 12-lea control flow w a nts, whila'stament, and statement that perform INote that you don't have to retuition in the order in which they are presented on the assignments You should do what works best for you! Question 1 mark) write a program called growth as their tenteramentation...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I...

    I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

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