Question

gerer grodpugu def slice_list(1st: List(Any) ni int) -> List[List[Any]]: Return a list containing slices of <st> in order. Ea

this is the function and here's what I did for this function but there is an error showing saying nonetype does not have type len() because math.ceil function (we cannot import any math..) for this function in python, if you could help me with this code.
thanks

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

from typing import Any,List

def slice_list(lst: List[Any], n:int)->List[List[Any]]:
    '''
    Return a list containing slices of <lst> in order.

    >>> slice_list(['a',1,6.0,False],3)==[['a', 1, 6.0], [False]]
    True
    >>> slice_list([3,4,6,2,3],2)==[[3,4],[6,2],[3]]
    True
    '''
    #if lst is None of n is invalid, returning None
    if lst is None or n<=0:
        return None
    #creating a list to store the results
    group=[]
    #starting with index=0
    index=0
    #looping as long as index+n is a valid index
    while (index+n)<len(lst):
        #fetching sublist between indices index and index+n-1 and appending to group
        group.append(lst[index:index+n])
        #advancing index by n
        index+=n
    #if index is still a valid index, adding rest of the elements between index and end of lst to group
    if index<len(lst):
        group.append(lst[index:])
    #returning the list of slices
    return group

Add a comment
Know the answer?
Add Answer to:
this is the function and here's what I did for this function but there is an...
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
  • def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of <lst> in...

    def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of <lst> in order. Each slice is a list of size <n> containing the next <n> elements in <lst>. The last slice may contain fewer than <n> elements in order to make sure that the returned list contains all elements in <lst>. === Precondition === n <= len(lst) >>> slice_list([3, 4, 6, 2, 3], 2) == [[3, 4], [6, 2], [3]] True >>> slice_list(['a', 1, 6.0, False],...

  • Python recursive function: Given an ordered list L. A permutation of L is a rearrangement of...

    Python recursive function: Given an ordered list L. A permutation of L is a rearrangement of its elements in some order. For example (1,3, 2) and (3, 2, 1) are two different permutations of L=(1,2,3). Implement the following function: def permutations (lst, low, high) The function is given a list 1st of integers, and two indices: low and high (lows high), which indicate the range of indices that need to be considered The function should return a list containing all...

  • BlockPy: #33.8) Maximum Odd Use the Min/Max pattern to write a function maximum_odd that finds the...

    BlockPy: #33.8) Maximum Odd Use the Min/Max pattern to write a function maximum_odd that finds the highest odd value in the list (consumes a list of numbers and returns a number). Use the provided helper function is_odd (that consumes a single number and returns a boolean indicating whether it is true or false). Do not change the helper function is odd. Call your maximum_odd function on your favorite list of numbers Console Feedback: Instructor Feedback You cannot use the builtin...

  • this is part of my code im not sure how to do last 2 function. i...

    this is part of my code im not sure how to do last 2 function. i think above last 2 functions need for making function please help me how to do it. #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include "list.h" typedef struct node { ElemType val; struct node *next; } NODE; struct list_struct { NODE *front; NODE *back; }; int lst_remove_first(LIST *l, ElemType x) { NODE *p; NODE *tmp; if(l->front == NULL) return 0; if(l->front->val == x) {    lst_pop_front(l);...

  • C++ The Function's Specification You will be writing a function with this specification: void quicksort void*...

    C++ The Function's Specification You will be writing a function with this specification: void quicksort void* base size t n, size t bytes, int compar (const void* const void*) Precondition base is a pointer to the first component of an array with at least n elements The component type of the array may be any type at all, and the parameter bytes must be the number of bytes in each component of the array. The fourth parameter compar, must be...

  • I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this func...

    I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this function is to calculate the similarity measure between one data segment and the pattern. The first parameter 'data_segment' is a list of (float) values, and the second parameter 'pattern' is also a list of (float) values. The function calculates the similarity measure between the given data segment and pattern and returns the calculated similarity value (float), as described earlier in this assignment outline. The...

  • // I need help with the following questions. Please use java programming ECLIPSE language to solve...

    // I need help with the following questions. Please use java programming ECLIPSE language to solve the questions. YOU ONLY NEED TO DIRECTLY COPY IT IN YOUR ECLIPSE APPLICATION AND RUN IT. I NEED THOSE PART WHICH IS SAYS --> "TO BE COMPLETED" I NEED HELP WITH [GET*] AND [REPLACE ALL] AND [ADD INT DOUBLE] PLEASE. import java.util.ArrayList; public class CustomArrayList { //instance variables public int[] data; //data.length gives the capacity public int nItems; //nItems gives items currently in the...

  • C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below...

    C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...

  • urgent help with python. can somone code this please and show output QUESTION: There are a variety of games possi...

    urgent help with python. can somone code this please and show output QUESTION: There are a variety of games possible with a deck of cards. A deck of cards has four different suits, i.e. spades (*), clubs (*), diamonds (), hearts (), and thirteen values for each suit. Now write a function that uses list comprehension to create a deck of cards. Each element in the list will be a card, which is represented by a list containing the suit...

  • package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic *...

    package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic * collection to store elements where indexes represent priorities and the * priorities can change in several ways. * * This collection class uses an Object[] data structure to store elements. * * @param <E> The type of all elements stored in this collection */ // You will have an error until you have have all methods // specified in interface PriorityList included inside this...

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