<jupyter_start><jupyter_text>Question - 01 Given a 2D array of size (m,n) - sort all the diagonal elements of the array.Write a function that will take the array and a boolean as input and provide the sorted array as output.The default value for boolen is True.If set to False, sort the anti-diagonal elements For example,---input = [[1,2,3],         [4,0,1],         [9,3,10]]         output= [[0,1,3],         [3,1,2],         [9,4,10]] main diagonal elements are = 1,0,10   --- sorted as -> 0,1,10diag 1 elements are        = 2,1      --- sorted as -> 1,2diag 2 elements are        = 3        --- sorted as -> 3diag -1 elements are       = 4,3      --- sorted as -> 3,4diag -2 elements are       = 9        --- sorted as -> 9<jupyter_code>def ques_01(arr, diag=True):
    for column in range(len(arr[0]) - 1):
        diagonal = []
        col = column

        for row in range(len(arr)):
            diagonal.append(arr[row][col])
            col += 1

            if col >= len(arr[0]):
                break

        diagonal = sorted(diagonal)
        col = column

        for row in range(len(arr)):
            arr[row][col] = diagonal[row]
            col += 1

            if col >= len(arr[0]):
                break

    for row in range(1, len(arr)):
        diagonal = []
        r = row

        for column in range(len(arr[0])):
            diagonal.append(arr[r][column])
            r += 1

            if r >= len(arr):
                break

        diagonal = sorted(diagonal)
        r = row

        for column in range(len(arr[0])):
            arr[r][column] = diagonal[column]
            r += 1

            if r >= len(arr):
                break

    return arr
# run this cell to check your output. Don't modify this cell
arr= [[1,2,3],
      [4,0,1],
      [9,3,10]]
out= [[0,1,3],
      [3,1,2],
      [9,4,10]]

ques_01(arr,diag=True)==out
# run this cell to check your output. Don't modify this cell
arr= [[1,2,3,4,6,8],
      [4,0,1,5,4,7]]

out= [[0,1,3,4,6,8],
      [4,1,2,5,4,7]]

ques_01(arr)==out
# run this cell to check your output. Don't modify this cell
arr= [[1,2,3,4,6,8],
      [4,0,1,5,4,7]]

out= [[1,2,0,1,5,4],
      [4,3,4,6,8,7]]

ques_01(arr,diag=False)==out<jupyter_output><empty_output><jupyter_text>Question - 02 You are given a list of lists. For example, [ [1] , [2,5,9] , [7,4] ]Now pad the inner lists such that all of them are of same size(the size of the longest array).output= [ [1,0,0], [2,5,9], [7,4,0] ] Write a function that will take two inputs - the list and a string(pre/post).If string is post, add zeros at the endIf string is pre,  add zeros at the frontIf given a invalid string, return a error message - "Invalid Input"<jupyter_code>def ques_02(arr, s):
    length = []
    for lists in arr:
        length.append(len(lists))
    maxLength = max(length)
    for l in arr:
        if s == "post":
            while(len(l)!=maxLength):
                l.append(0)
        elif s == 'pre':
            while(len(l)!=maxLength):
                l.insert(0,0)
        else:
            return "Invalid Input"
    return arr
# run this cell to check your output. Don't modify this cell

arr= [ [1] , [2,5,9] , [7,4] ]
s= 'post'
out= [[1,0,0], [2,5,9], [7,4,0] ]


ques_02(arr,s)==out
# run this cell to check your output. Don't modify this cell

arr= [ [1] , [2,5,9] , [7,4] ]
s= 'p'
out= [[1,0,0], [2,5,9], [7,4,0] ]


ques_02(arr,s)=='Invalid Input'
# run this cell to check your output. Don't modify this cell

arr= [ [1] , [2,5,9,6,1] , [7,4,4,3,1] ]
s= 'pre'
out= [[0,0,0,0,1], [2,5,9,6,1],  [7,4,4,3,1] ]

ques_02(arr,s)==out<jupyter_output><empty_output><jupyter_text>Ques - 03 A sphenic number is a positive integer that is the product of three distinct prime numbers.For example, 30 = 2*3*5 Given a number, first check if it is a sphenic number; if so return thoseprimes.If not return False. Input:  [ 30, 32, 230]Output: [ [2,3,5], False, [2,5,23] ]<jupyter_code>def ques_03(arr):
  out=[]
  for j in arr:
    i = 2
    factorsofnumber = []
    while i * i <= j:
      if j % i:
        i += 1
      else:
        j//= i
        factorsofnumber.append(i)
    if j> 1:
      factorsofnumber.append(j)
    if len(factorsofnumber)==3:
      out.append(factorsofnumber)
    else:
      out.append(False)
  return out
# run this cell to check your output. Don't modify this cell

Input=  [ 30, 32, 230]
Output= [ [2,3,5], False, [2,5,23] ]

ques_03(Input)==Output
# run this cell to check your output. Don't modify this cell

Input=  [ 222, 434, 969, 1311]
Output= [ [2,3,37], [2,7,31], [3,17,19], [3,19,23] ]

ques_03(Input)==Output
# run this cell to check your output. Don't modify this cell

Input=  [ 22, 43]
Output= [ False, False ]

ques_03(Input)==Output<jupyter_output><empty_output><jupyter_text>Question - 04 Connect four is a two player board game.Given a complete connect-four board, determine who is the winner.User-1 is represented by 1User-2 is represented by 2Winner is the one with the highest number of sets of 4 consecutive numbers. A set can be comprised of same numbers on row, column, diagonal or anti-diagonal basis. x= [2 2 1 1 2 1 2;    1 2 2 1 1 1 1;    1 2 1 1 2 2 2;    2 1 2 2 2 1 1;    1 1 2 1 2 1 1;    2 1 1 2 2 2 1] here in the 5th column, we've a set of 2;      in the 2nd row, we've a set of 1;      in the 2nd anti-diagonal, we've a set of 1; so, 1 is the winner. For simplicity, ignore the diagonal and anti-diagonal matches for now.only consider row or column wise. To simplify even further, consider number of consecutive elements will not be greater than 4. write a function that will take a 2D array as input.return the winner - 1 or 2.If it is a tie, return 0.<jupyter_code>def ques_04(n):
    lister = [0, 0, 0]
    for row in n:
        count = 1
        for i in range(1, len(row)):
            if row[i] == row[i - 1]:
                count = count + 1
                if count == 4:
                    count = 0
                    lister[row[i]] += 1
            else:
                count = 0

    for j in range(0, len(n[0])):
        count = 1
        for i in range(1, len(n)):
            if n[i][j] == n[i-1][j ]:
                count += 1
                if count == 4:
                    count = 0
                    lister[n[i][j]] += 1
            else:
                count = 0

    if lister[1] == lister[2]:
        return 0
    elif lister[1] > lister[2]:
        return 1
    else:
        return 2
# run this cell to check your output. Don't modify this cell

x= [[2, 2, 1, 1, 2, 1, 2],
    [1, 2, 2, 1, 1, 1, 1],
    [1, 2, 1, 1, 2, 1, 2],
    [1, 2, 2, 2, 2, 1, 1]]
y=0

ques_04(x)==y
# run this cell to check your output. Don't modify this cell

x= [[2, 2, 1, 1, 2, 1, 1],
    [1, 2, 2, 1, 1, 1, 1],
    [1, 2, 1, 1, 2, 1, 1],
    [1, 2, 2, 2, 2, 1, 1]]
y=1

ques_04(x)==y
# run this cell to check your output. Don't modify this cell

x= [[2, 2, 1, 1, 2],
    [1, 2, 2, 2, 2],
    [1, 2, 1, 1, 2],
    [1, 2, 2, 2, 2]]
y=2

ques_04(x)==y<jupyter_output><empty_output><jupyter_text>Question - 05 You are given a 2D array of size (m,n).Find the top 3 elements in the array and their index.   You've to return a dictionary.     input= [[1,2,3],        [4,0,1],        [9,3,10]]output= { 'h1': { 'value': 10,                  'index': [2,2] },          'h2': { 'value': 9,                  'index': [2,0] },          'h3': { 'value': 4,                  'index': [1,0] } }<jupyter_code>import sys

def ques_05(arr):
    
    first = -sys.maxsize
    second = -sys.maxsize
    third = -sys.maxsize

    edodict = {
        'h1': {
            'value': first,
            'index': [-1,-1]
        },
        'h2': {
            'value': second,
            'index': [-1,-1]
        },
        'h3': {
            'value': third,
            'index': [-1,-1]
            
        }
    };
    
    for i in range(0, len(arr)):
        a = arr[i]
        for j in range(0, len(a)):
            
            if a[j] > edodict['h1']['value']:
                edodict['h3']['value'] = edodict['h2']['value']
                edodict['h3']['index'] = edodict['h2']['index']
                
                edodict['h2']['value'] = edodict['h1']['value']
                edodict['h2']['index'] = edodict['h1']['index']
                
                edodict['h1']['value'] = a[j]
                edodict['h1']['index'] = [i, j]
                
            elif a[j] > edodict['h2']['value']:
                edodict['h3']['value'] = edodict['h2']['value']
                edodict['h3']['index'] = edodict['h2']['index']
                
                edodict['h2']['value'] = a[j]
                edodict['h2']['index'] = [i, j]
            
            elif a[j] > edodict['h3']['value']:
                edodict['h3']['value'] = a[j]
                edodict['h3']['index'] = [i, j]
    
    return edodict
# run this cell to check your output. Don't modify this cell

arr= [[1,2,24],
      [4,0,1],
      [9,3,10],
      [23,4,40],
      [25,100,9]]

output= { 'h1': { 'value': 100,
                  'index': [4,1] },
          'h2': { 'value': 40,
                  'index': [3,2] },
          'h3': { 'value': 25,
                  'index': [4,0] } }

ques_05(arr)==output
# run this cell to check your output. Don't modify this cell

arr= [[1,2,3],
      [4,0,1],
      [9,3,10]]

output= { 'h1': { 'value': 10,
                  'index': [2,2] },
          'h2': { 'value': 9,
                  'index': [2,0] },
          'h3': { 'value': 4,
                  'index': [1,0] } }

ques_05(arr)==output<jupyter_output><empty_output><jupyter_text>Question - 06 Given a matrix of size (m,n) - Find the final state of the matrix assuming the matrix as an abelian sandpile. Think of the elements in the matrix as a pile of grains.By final state it means, all the elements in the matrix should be <4.Any elements greater than 4 should be evenly distributed around it i.e. distribute four sand grains, one each to the elements above, below, left and right. Continue doing this until all the piles(elements) in the matrix is less than 4. input: [ 0 0 0 0         0 0 4 0         0 0 0 0 ]         output: [ 0 0 1 0          0 1 0 1          0 0 1 0 ] input: [ 0 0 0 0         0 4 4 0         0 0 0 0 ]         output: [ 0 1 1 0          1 1 1 1          0 1 1 0 ] input: [ 3 0 0  0 3 0         0 0 0  0 0 0         0 0 15 0 5 0          0 0 0  0 0 0         0 0 0  0 0 0]                [ 3 0 0 0 3 0         0 0 3 0 1 0         0 3 3 4 1 1         0 0 3 0 1 0         0 0 0 0 0 0 ]      not the final stage.. you need to continue until all are less than 4.<jupyter_code>def ques_06(a):
    row = len(a[0])
    col = len(a)
    for i in range(col):
        for j in range(row):
            while a[i][j]>=4:
                
                if i-1>=0:
                    if a[i-1][j]<3:
                        a[i-1][j]+=1
                        a[i][j]-=1
                
    return a
# run this cell to check your output. Don't modify this cell

a= [ [0, 0, 0, 0],
     [0, 0, 4, 0],
     [0, 0, 0, 0] ]

out=  [ [0, 0, 1, 0],
        [0, 1, 0, 1],
        [0, 0, 1, 0] ]

ques_06(a)==out
# run this cell to check your output. Don't modify this cell

a= [ [0, 0, 0, 0],
     [0, 4, 4, 0],
     [0, 0, 0, 0] ]

out=  [ [0, 1, 1, 0],
        [1, 1, 1, 1],
        [0, 1, 1, 0] ]

ques_06(a)==out
# run this cell to check your output. Don't modify this cell

a= [ [0, 0, 0, 1],
     [2, 1, 3, 0],
     [0, 2, 0, 3] ]

out=  [ [0, 0, 0, 1],
        [2, 1, 3, 0],
        [0, 2, 0, 3] ]

ques_06(a)==out
# run this cell to check your output. Don't modify this cell
import numpy as np

a= np.random.randint(0,4,(4,10))

out=a

ques_06(a)==out
# run this cell to check your output. Don't modify this cell

a= [ [0, 0, 1, 0],
     [2, 0, 9, 0],
     [0, 3, 0, 0] ]

out=  [ [0, 0, 3, 0],
        [2, 2, 1, 2],
        [0, 3, 2, 0] ]

ques_06(a)==out<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15569163660766713, 'fraction_numerical': 0.0703800016777116, 'mean_word_length': 1.8706958824945823, 'pattern_counts': {'":': 1, '<': 28, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '265663', 'license': 'MIT', 'n_tokens_mistral': 4835, 'n_tokens_neox': 4300, 'n_words': 1499, 'path': '180021215_Razin.ipynb', 'repo_name': 'RRazin/Portfolio-Generator', 'size': '11921'}