Question

from __future__ import annotations import random from typing import TYPE_CHECKING, List, Any from course import sort_students...

from __future__ import annotations
import random
from typing import TYPE_CHECKING, List, Any
from course import sort_students
if TYPE_CHECKING:
from survey import Survey
from course import Course, Student


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], 3) == [['a', 1, 6.0], [False]]
True
"""
ret = []
tmp = []
for i in range(len(lst)):
if i % n == 0 and i != 0:
tmp.append(lst[i])
if len(tmp) != 0:
ret.append(tmp)
tmp = []
return ret


def windows(lst: List[Any], n: int) -> List[List[Any]]:
"""
Return a list containing windows of <lst> in order. Each window is a list
of size <n> containing the elements with index i through index i+<n> in the
original list where i is the index of window in the returned list.

=== Precondition ===
n <= len(lst)

>>> windows([3, 4, 6, 2, 3], 2) == [[3, 4], [4, 6], [6, 2], [2, 3]]
True
>>> windows(['a', 1, 6.0, False], 3) == [['a', 1, 6.0], [1, 6.0, False]]
True
"""
ret = []
tmp = []
for i in range(n):
tmp.append(lst[i])
ret.append(tmp)
for i in range(len(lst)-n):
tmp = tmp[1:]
tmp.append(lst[i+n])
ret.append(tmp)
return ret

I don't think the codes of these two methods are correct? Can you help me check these two methods and tell me the correct codes using python3.8? thankyou.

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

# The below two codes are working, I have tested them in google colab

def slice_list(lst,n):

  ret=[]

  tmp=[]

  for i in range(len(lst)):

    if i%n == 0 and i!=0:

      #change here

      ret.append(tmp)

      tmp=[]

      #upto here

    tmp.append(lst[i])

  if len(tmp)!=0:

    ret.append(tmp)

  return ret

def windows(lst,n):

  ret=[]

  tmp=[]

  for i in range(n):

    tmp.append(lst[i])

  ret.append(tmp)

  for i in range(len(lst)-n):

    tmp = tmp[1:]

    tmp.append(lst[i+n])

    ret.append(tmp)

  return ret

# Please upvote, if you got the answer :)

Add a comment
Know the answer?
Add Answer to:
from __future__ import annotations import random from typing import TYPE_CHECKING, List, Any from course import sort_students...
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],...

  • this is the function and here's what I did for this function but there is an...

    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 gerer grodpugu def slice_list(1st: List(Any) ni int) -> List[List[Any]]: Return a list containing slices of <st> in order. Each slice is a List of size <n> containing the next <n> elements in <tst>....

  • from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked...

    from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked list. Note that this is considered a "private class", one which is only meant to be used in this module by the LinkedList class, but not by client code. === Attributes === item: The data stored in this node. next: The next node in the list, or None if there are no more nodes. """ item: Any next: Optional[_Node] def __init__(self, item: Any) ->...

  • PYTHON 3: An n x n matrix forms a magic square if the following conditions are...

    PYTHON 3: An n x n matrix forms a magic square if the following conditions are met: 1. The elements of the matrix are numbers 1,2,3, ..., n2 2. The sum of the elements in each row, in each column and in the two diagonals is the same value. Question: Complete the function that tets if the given matrix m forms a magic square. def is_square(m): '''2d-list => bool Return True if m is a square matrix, otherwise return False...

  • can you finish implementing the functions below ********************************************************************************************************** import urllib.request from typing import List, TextIO #...

    can you finish implementing the functions below ********************************************************************************************************** import urllib.request from typing import List, TextIO # Precondition for all functions in this module: Each line of the url # file contains the average monthly temperatures for a year (separated # by spaces) starting with January. The file must also have 3 header # lines. DATA_URL = 'http://robjhyndman.com/tsdldata/data/cryer2.dat' def open_temperature_file(url: str) -> TextIO: '''Open the specified url, read past the three-line header, and return the open file. ''' ... def avg_temp_march(f:...

  • using Python --- from typing import List THREE_BY_THREE = [[1, 2, 1], [4, 6, 5], [7,...

    using Python --- from typing import List THREE_BY_THREE = [[1, 2, 1], [4, 6, 5], [7, 8, 9]] FOUR_BY_FOUR = [[1, 2, 6, 5], [4, 5, 3, 2], [7, 9, 8, 1], [1, 2, 1, 4]] UNIQUE_3X3 = [[1, 2, 3], [9, 8, 7], [4, 5, 6]] UNIQUE_4X4 = [[10, 2, 3, 30], [9, 8, 7, 11], [4, 5, 6, 12], [13, 14, 15, 16]] def find_peak(elevation_map: List[List[int]]) -> List[int]: """Return the cell that is the highest point in the...

  • from typing import List THREE_BY_THREE = [[1, 2, 1], [4, 6, 5], [7, 8, 9]] FOUR_BY_FOUR...

    from typing import List THREE_BY_THREE = [[1, 2, 1], [4, 6, 5], [7, 8, 9]] FOUR_BY_FOUR = [[1, 2, 6, 5], [4, 5, 3, 2], [7, 9, 8, 1], [1, 2, 1, 4]] UNIQUE_3X3 = [[1, 2, 3], [9, 8, 7], [4, 5, 6]] UNIQUE_4X4 = [[10, 2, 3, 30], [9, 8, 7, 11], [4, 5, 6, 12], [13, 14, 15, 16]] def get_average_elevation(elevation_map: List[List[int]]) -> float: """Return the average elevation across all cells in the elevation map elevation_map. Precondition:...

  • Using a sort method and my previous code (put inside of addHours method if possible), how...

    Using a sort method and my previous code (put inside of addHours method if possible), how would I get the list of each employee's total hours to display in order of least amount of hours to greatest. An explanation for each step would also be great. import random #Create function addHours def addHours(lst): #Display added hours of employees print("\nEmployee# Weekly Hours") print("----------------------------") print(" 1 ",sum(lst[0])) print(" 2 ",sum(lst[1])) print(" 3 ",sum(lst[2])) #Create main function def main(): #Create first empty list...

  • #We've written the function, sort_with_bubbles, below. It takes #in one list parameter, lst. However, there are...

    #We've written the function, sort_with_bubbles, below. It takes #in one list parameter, lst. However, there are two problems in #our current code: # - There's a missing line # - There's a semantic error (the code does not raise an # error message, but it does not perform correctly) # #Find and fix these problems! Note that you should only need #to change or add code where explicitly indicated. # #Hint: If you're stuck, use an example input list and...

  • I need to remove the even numbers in the back of the list, any ideas? #f....

    I need to remove the even numbers in the back of the list, any ideas? #f. Demonstrate moving even elements to the front of the list. data = list(ONE_TEN) def evenToFront(data): for i in range(len(data)): if i % 2 == 0: data.insert(0, i) comments too please

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