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 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 return the following:

18 -> 32

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

12 -> 20 -> 32 -> 44

Solutions

Expert Solution

class ListNode:

    """ Models a single node in a singly-linked list. Has no methods, other

        than the constructor.

    """

    def __init__(self, val):

        """ Constructs the object; caller must pass a value, which will be

            stored in the 'val' field.

        """

        self.val = val

        self.next = None

    def __str__(self):

        vals = []

        objs = set()

        curr = self

        while curr is not None:

            curr_str = str(curr.val)

            if curr in objs:

                vals.append(

                    "{} -> ... (to infinity and beyond)".format(curr_str))

                break

            else:

                vals.append(curr_str)

                objs.add(curr)

            curr = curr.next

        return " -> ".join(vals)


def accordion_n(head,skip_amt):

    if skip_amt<0:

        print("INVALID... skip amount should be non-negative")

        

    elif skip_amt==0:

        return head

    

    else:

        op=[]

        curr_skip=0

        for i in range(len(head)):

            if curr_skip==2:

                op.append(head[i])

                curr_skip=0

            else:

                curr_skip+=1

        return " -> ".join(op)


if __name__=="__main__":

    n=int(input("Enter number of nodes in linked list: "))

    l=[]

    a=int(input())

    l.append(ListNode(a))

    for i in range(1,n):

        a=int(input())

        l.append(ListNode(a))

        l[i-1].next=l[i]

    

    x=str(l[0])

    y = x.split(" -> ")

    

    #Test Case 1

    skip_amt=int(input("Enter skip amount: "))

    print("Given List:",str(l[0]))

    print("Skip amount:",skip_amt)

    ans=accordion_n(y,skip_amt)

    print("Output List:",ans)

#SAMPLE OUTPUT

PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION


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 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...
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...
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,...
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...
IN PYTHON Create a function called biochild.  The function has as parameters the number m...
IN PYTHON Create a function called biochild.  The function has as parameters the number m and the lists biomother and biofather.  The biomother and biofather lists contain 0’s and 1’s.  For example: biomother = [1,0,0,1,0,1] and biofather = [1,1,1,0,0,1]  Both lists have the same length n.  The 0's and 1's represent bits of information (remember that a bit is 0 or 1).  The function has to generate a new list (child).  The child...
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints...
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints a message with information about the total amount owed and how much the tip was. As a reminder, the tip amounts are 10%, 15% and 20% for stingy, regular, and generous customers. And the tax amount should be 7%. The total amount is calculated as the sum of two amounts: check_amount = base_cost*1.07 tip_amount = tip_percentage*check_amount To receive full credit, you must use string...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list of elements and interval is an integer larger than 0. Imagine that the elements in items were arranged in a circle. Including the first element, count off the elements in items up to the interval position and then remove that element from the circle. From that position, begin counting the positions of the elements again and remove the element that has the next interval...
Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT