Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Sml: Test your function using pairNleft (2,[1, 2, 3, 4, 5]); and pairNright (

ID: 3748690 • Letter: I

Question

In Sml: Test your function using

pairNleft (2,[1, 2, 3, 4, 5]);

and

pairNright (2,[1, 2, 3, 4, 5]);

6. pair left and pairNright - 25% These functions takes a 2-tuple (e.g. (N, L)) where the first element is an integer (N) and the second is a list (L). The idea is to produce a result in which the elements of the original list have been collected into lists each containing n elements (where N is the integer argument). The leftover elements (if there is any) are included as a tuple with less than N elements. Thus the type of each of these is: int 'a list -'a list list. The difference between the two functions is how they handle the left-over elements of the list when the integer doesn't divide the length of the list evenly. pairNleft treats the initial elements of the list as the extras, thus the result starts with a list of between 1 and N elements and all the remaining elements of the result list contain N elements. pairNright does the opposite: the final group contains between 1 and N elements and all the rest contain N elements Note: these functions are not required to be tail-recursive. Examples: > pairNleft (2,[1, 2, 3, 4, 5]) > pairNright (2,1, 2, 3, 4, 5]) > pairNleft (3,[1, 2, 3, 4, 5]) , 2),13,4, 5]] > pairNright (3,11,2, 3, 4, 51) , 2, 3, [4, 5]]1 > pairNright (3,11)(this may give a warning since the type can't be inferred) t [1

Explanation / Answer

def pairNleft(n,lst):
   #Declaring the List
   res = []
   #size of the given list
   size = len(lst)
   excess = size % n
   index = 0
   if excess > 0:
       res.append(lst[:excess])
       index = excess
   #Index should be in the range of size
   while index < size:
       #Appending the sublist
       res.append(lst[index:index+n])
       index += n
   return res
def pairNright(n,lst):
   #Declaring the List
   res = []
   #size of the given list
   size = len(lst)

   excess = size % n
   index = 0
   #Index should be in the range of size
   while (index + n) < size:
   #Appending the sublist
       res.append(lst[index:index+n])
       index += n
   if excess > 0:
       res.append(lst[-excess:])
   return res

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote