Question

In: Computer Science

In python I want to create a singular function that takes two parameters 'head; and 'skip'....

In python I want to create a singular function that takes two parameters 'head; and 'skip'.

Head is a linked list. Skip is a non negative value. If skip is zero it should return the linked list unchanged. The skip amount determines the amount to skip over. I want to change the linked list accordingly and then return the linked list with the modifications, not a list.

If you have a linked list 11 -> 12 -> 18 -> 20 -> 24 -> 32 -> 38 -> 44

and skip =2, then the linked list you return should be the following:

18 -> 32

If skip = 1, then the linked list you return should be the following:

12 -> 20 -> 32 -> 44

Solutions

Expert Solution


class Node: 
        def __init__(self, data): 
                self.data = data 
                self.next = None 

class LinkedList: 

        def __init__(self): 
                self.head = None

        def push(self, new_data): 
                new_node = Node(new_data) 
                new_node.next = self.head 
                self.head = new_node 

        
        def getNth(self, index): 
                current = self.head # Initialise temp 
                count = 0 # Index of current node 

                while (current): 
                        if (count == index): 
                                return current.data 
                        count += 1
                        current = current.next

                assert(false) 
                return 0; 


if __name__=='__main__': 

        llist = LinkedList() 

        print("length of linked list")
        length = int(input())
        print("enter data for linked list")
        for _ in range(length):
                llist.push(int(input())); 

        print('skips:')
        skip = int(input())+1

        indexes = list(range(0,length,skip))
        indexes.pop(0)
        
        for n in reversed(indexes):
                print(llist.getNth(n-1),"->",end='') 
        print("completed")

Approach:

1. We create a linkedlist taking input
2. We create a second list with indexes that need to be printed
3. We print each of those indexes in reverse order to get the required list.

Alternatively, you can save this newly created linkedlist by simply creating a new object and pushing the data there.


Related Solutions

In python I want to create a singular function that takes two parameters 'head; and 'skip'....
In python I want to create a singular function that takes two parameters 'head; and 'skip'. Head is a linked list. Skip is a non negative value. If skip is zero it should return head unchanged. The skip amount determines the amount to skip over. I want to change the linked list accordingly. If you have a linked list 11 -> 12 -> 18 -> 20 -> 24 -> 32 -> 38 -> 44 and skip =2, then you should...
In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
python practice! 1. Create a function that takes a user choice and one number as parameters...
python practice! 1. Create a function that takes a user choice and one number as parameters and returns the operation result. -Square: print the number square -Sqrt: print the square root of the number -Reverse: reverse the sign of the number (pos or neg) and print it Note: Detect invalid choices and throw an error message – Number can be anything. 2. Create a function that takes a user choice and two numbers (start and end) as parameters. For example,...
(Python) I want to use a function called level() that takes a dictionary. Here is a...
(Python) I want to use a function called level() that takes a dictionary. Here is a dictionary with people's job and skill level. dict1 = {'Jame': {'Cleaning': 5, 'Tutoring': 2, 'Baking': 1},Pam': {'Plumbing': 2, 'Cleaning': 5}) like if I called level(dict1), the output will return {'Pam', 'James'} It finds the people's average skill level like for Pam is (2+5)/2=3.5 and sorted descending. How do I do that and how do I do it in only one return statement(using comprehension or...
Challenge 1 – 2D Array Maker Create a function that takes in two parameters and returns...
Challenge 1 – 2D Array Maker Create a function that takes in two parameters and returns a created two-dimension array. var twoD = Make2D(<width>, <height>); Challenge 2 – Fill 2D Array Create a function that takes a single parameter and fills the 2D array with that parameter Use Cases: Fill2D(<twoD array>, <fill value>); //fills twoD twoD = fill2D(<twoD array>, <fill value>); //fills twoD, but also returns reference Challenge 3 – Make 2D Array Write a function that takes 3 parameters...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT