giulio98/xlcost-single-prompt huggingface dataset:
# count negative numbers in a column | python implementation of naive method to count of negative numbers in m [ n ] [ m ] ; follow the path shown using arrows above ; no more negative numbers in this row ; 
def countNegative(M, n, m):
    count = 0
    for i in range(n):
        for j in range(m):
            if M[i][j] < 0:
                count += 1
            else:
                break
    return count


M = [[-3, -2, -1, 1], [-2, 2, 3, 4], [4, 5, 7, 8]]
print(countNegative(M, 3, 4))

# Expected output:
4
# count negative numbers in a column | function to count negative number ; initialize result ; start with top right corner ; follow the path shown using arrows above ; j is the index of the last negative number in this row . so there must be ( j + 1 ) ; negative numbers in this row . ; move to the left and see if we can find a negative number there ; 
def countNegative(M, n, m):
    count = 0
    i = 0
    j = m - 1
    while j >= 0 and i < n:
        if M[i][j] < 0:
            count += (j + 1)
            i += 1
        else:
            j -= 1
    return count


M = [[-3, -2, -1, 1], [-2, 2, 3, 4], [4, 5, 7, 8]]
print(countNegative(M, 3, 4))

# Expected output:
4
# count negative numbers in a column | recursive binary search to get last negative value in a row between a start and an end ; base case ; get the mid for binary search ; if current element is negative ; if it is the rightmost negative element in the current row ; check in the right half of the array ; check in the left half of the array ; function to return the count of negative numbers in the given matrix ; initialize result ; to store the index of the rightmost negative element in the row under consideration ; iterate over all rows of the matrix ; if the first element of the current row is positive then there will be no negatives in the matrix below or after it ; run binary search only until the index of last negative integer in the above row ; 
def getLastNegativeIndex(array, start, end, n):
    if (start == end):
        return start
    mid = start + (end - start) // 2
    if (array[mid] < 0):
        if (mid + 1 < n and array[mid + 1] >= 0):
            return mid
        return getLastNegativeIndex(array, mid + 1, end, n)
    else:
        return getLastNegativeIndex(array, start, mid - 1, n)


def countNegative(M, n, m):
    count = 0
    nextEnd = m - 1
    for i in range(n):
        if (M[i][0] >= 0):
            break
        nextEnd = getLastNegativeIndex(M[i], 0, nextEnd, 4)
        count += nextEnd + 1
    return count


M = [[-3, -2, -1, 1], [-2, 2, 3, 4], [4, 5, 7, 8]]
r = 3
c = 4
print(countNegative(M, r, c))

# Expected output:
4
# find a specific pair in matrix | a naive method to find maximum value of mat [ d ] [ e ] - mat [ a ] [ b ] such that d > a and e > b ; the function returns maximum value a ( d , e ) - a ( a , b ) over all choices of indexes such that both d > a and e > b . ; stores maximum value ; consider all possible pairs mat [ a ] [ b ] and mat [ d ] [ e ] ; 

N = 5


def findMaxValue(mat):
    maxValue = 0
    for a in range(N - 1):
        for b in range(N - 1):
            for d in range(a + 1, N):
                for e in range(b + 1, N):
                    if maxValue < int(mat[d][e] - mat[a][b]):
                        maxValue = int(mat[d][e] - mat[a][b])
    return maxValue


mat = [[1, 2, -1, -4, -20], [-8, -3, 4, 2, 1],
       [3, 8, 6, 1, 3], [-4, -1, 1, 7, -6], [0, -4, 10, -5, 1]]
print("Maximum Value is " + str(findMaxValue(mat)))

# Expected output:
Maximum Value is 18
# find a specific pair in matrix | an efficient method to find maximum value of mat [ d ] - ma [ a ] [ b ] such that c > a and d > b ; the function returns maximum value a ( c , d ) - a ( a , b ) over all choices of indexes such that both c > a and d > b . ; stores maximum value ; maxarr [ i ] [ j ] stores max of elements in matrix from ( i , j ) to ( n - 1 , n - 1 ) ; last element of maxarr will be same 's as of the input matrix ; preprocess last row initialize max ; preprocess last column initialize max ; preprocess rest of the matrix from bottom ; update maxvalue ; set maxarr ( i , j ) ; 

import sys
N = 5


def findMaxValue(mat):
    maxValue = -sys .maxsize - 1
    maxArr = [[0 for x in range(N)]for y in range(N)]
    maxArr[N - 1][N - 1] = mat[N - 1][N - 1]
    maxv = mat[N - 1][N - 1]
    for j in range(N - 2, -1, -1):
        if (mat[N - 1][j] > maxv):
            maxv = mat[N - 1][j]
        maxArr[N - 1][j] = maxv
    maxv = mat[N - 1][N - 1]
    for i in range(N - 2, -1, -1):
        if (mat[i][N - 1] > maxv):
            maxv = mat[i][N - 1]
        maxArr[i][N - 1] = maxv
    for i in range(N - 2, -1, -1):
        for j in range(N - 2, -1, -1):
            if (maxArr[i + 1][j + 1] - mat[i][j] > maxValue):
                maxValue = (maxArr[i + 1][j + 1] - mat[i][j])
            maxArr[i][j] = max(mat[i][j], max(
                maxArr[i][j + 1], maxArr[i + 1][j]))
    return maxValue


mat = [[1, 2, -1, -4, -20], [-8, -3, 4, 2, 1],
       [3, 8, 6, 1, 3], [-4, -1, 1, 7, -6], [0, -4, 10, -5, 1]]
print("Maximum Value is", findMaxValue(mat))

# Expected output:
Maximum Value is 18
# print all elements in sorted order from row and column wise sorted matrix | python 3 program to print all elements in sorted order from row and column wise sorted matrix ; a utility function to youngify a young tableau . this is different from standard youngify . it assumes that the value at mat [ 0 ] [ 0 ] is infinite . ; find the values at down and right sides of mat [ i ] [ j ] ; if mat [ i ] [ j ] is the down right corner element , return ; move the smaller of two values ( downval and rightval ) to mat [ i ] [ j ] and recur for smaller value ; a utility function to extract minimum element from young tableau ; this function uses extractmin ( ) to print elements in sorted order ; 

import sys
INF = sys .maxsize
N = 4


def youngify(mat, i, j):
    downVal = mat[i + 1][j]if (i + 1 < N)else INF
    rightVal = mat[i][j + 1]if (j + 1 < N)else INF
    if (downVal == INF and rightVal == INF):
        return
    if (downVal < rightVal):
        mat[i][j] = downVal
        mat[i + 1][j] = INF
        youngify(mat, i + 1, j)
    else:
        mat[i][j] = rightVal
        mat[i][j + 1] = INF
        youngify(mat, i, j + 1)


def extractMin(mat):
    ret = mat[0][0]
    mat[0][0] = INF
    youngify(mat, 0, 0)
    return ret


def printSorted(mat):
    print("Elements of matrix in sorted order n")
    i = 0
    while i < N * N:
        print(extractMin(mat), end=" ")
        i += 1


if __name__ == "__main__":
    mat = [[10, 20, 30, 40], [15, 25, 35, 45],
           [27, 29, 37, 48], [32, 33, 39, 50]]
    printSorted(mat)

# Expected output:
Elements of matrix in sorted order n
10 15 20 25 27 29 30 32 33 35 37 39 40 45 48 50
# given an n x n square matrix , find sum of all sub | size k x k size of given matrix ; a simple function to find sum of all sub - squares of size k x k in a given square matrix of size n x n ; k must be smaller than or equal to n ; row number of first cell in current sub - square of size k x k ; column of first cell in current sub - square of size k x k ; calculate and print sum of current sub - square ; line separator for sub - squares starting with next row ; 

n = 5


def printSumSimple(mat, k):
    if (k > n):
        return
    for i in range(n - k + 1):
        for j in range(n - k + 1):
            sum = 0
            for p in range(i, k + i):
                for q in range(j, k + j):
                    sum += mat[p][q]
            print(sum, end=" ")
        print()


if __name__ == "__main__":
    mat = [[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [
        3, 3, 3, 3, 3], [4, 4, 4, 4, 4], [5, 5, 5, 5, 5]]
    k = 3
    printSumSimple(mat, k)

# Expected output:
18 18 18 
27 27 27 
36 36 36
# given an n x n square matrix , find sum of all sub | size of given matrix ; a o ( n ^ 2 ) function to find sum of all sub - squares of size k x k in a given square matrix of size n x n ; k must be smaller than or equal to n ; 1 : preprocessing to store sums of all strips of size k x 1 ; go column by column ; calculate sum of first k x 1 rectangle in this column ; calculate sum of remaining rectangles ; 2 : calculate sum of sub - squares using stripsum [ ] [ ] ; calculate and prsum of first subsquare in this row ; calculate sum of remaining squares in current row by removing the leftmost strip of previous sub - square and adding a new strip ; 

n = 5


def printSumTricky(mat, k):
    global n
    if k > n:
        return
    stripSum = [[None] * n for i in range(n)]
    for j in range(n):
        Sum = 0
        for i in range(k):
            Sum += mat[i][j]
        stripSum[0][j] = Sum
        for i in range(1, n - k + 1):
            Sum += (mat[i + k - 1][j] - mat[i - 1][j])
            stripSum[i][j] = Sum
    for i in range(n - k + 1):
        Sum = 0
        for j in range(k):
            Sum += stripSum[i][j]
        print(Sum, end=" ")
        for j in range(1, n - k + 1):
            Sum += (stripSum[i][j + k - 1] - stripSum[i][j - 1])
            print(Sum, end=" ")
        print()


mat = [[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [
    3, 3, 3, 3, 3], [4, 4, 4, 4, 4], [5, 5, 5, 5, 5]]
k = 3
printSumTricky(mat, k)

# Expected output:
18 18 18 
27 27 27 
36 36 36
# program to find transpose of a matrix | python3 program to find transpose of a matrix ; this function stores transpose of a [ ] [ ] in b [ ] [ ] ; 

M = 3
N = 4


def transpose(A, B):
    for i in range(N):
        for j in range(M):
            B[i][j] = A[j][i]


A = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]
B = [[0 for x in range(M)]for y in range(N)]
transpose(A, B)
print("Result matrix is")
for i in range(N):
    for j in range(M):
        print(B[i][j], " ", end='')
    print()

# Expected output:
Result matrix is
1  2  3  
1  2  3  
1  2  3  
1  2  3
# program to find transpose of a matrix | python3 program to find transpose of a matrix ; finds transpose of a [ ] [ ] in - place ; 

N = 4


def transpose(A):
    for i in range(N):
        for j in range(i + 1, N):
            A[i][j], A[j][i] = A[j][i], A[i][j]


A = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]
transpose(A)
print("Modified matrix is")
for i in range(N):
    for j in range(N):
        print(A[i][j], " ", end='')
    print()

# Expected output:
Modified matrix is
1  2  3  4  
1  2  3  4  
1  2  3  4  
1  2  3  4
# number of paths with exactly k coins | a naive recursive python program to count paths with exactly ' k ' coins ; recursive function to count paths with sum k from ( 0 , 0 ) to ( m , n ) ; base cases ; ( m , n ) can be reached either through ( m - 1 , n ) or through ( m , n - 1 ) ; a wrapper over pathcountrec ( ) ; 

R = 3
C = 3


def pathCountRec(mat, m, n, k):
    if m < 0 or n < 0:
        return 0
    elif m == 0 and n == 0:
        return k == mat[m][n]
    return (
        pathCountRec(
            mat,
            m -
            1,
            n,
            k -
            mat[m][n]) +
        pathCountRec(
            mat,
            m,
            n -
            1,
            k -
            mat[m][n]))


def pathCount(mat, k):
    return pathCountRec(mat, R - 1, C - 1, k)


k = 12
mat = [[1, 2, 3], [4, 6, 5], [3, 2, 1]]
print(pathCount(mat, k))

# Expected output:
2
# number of paths with exactly k coins | a dynamic programming based python3 program to count paths with exactly ' k ' coins ; base cases ; if this subproblem is already solved ; ( m , n ) can be reached either through ( m - 1 , n ) or through ( m , n - 1 ) ; a wrapper over pathcountdprecdp ( ) ; 

R = 3
C = 3
MAX_K = 1000


def pathCountDPRecDP(mat, m, n, k):
    if m < 0 or n < 0:
        return 0
    elif m == 0 and n == 0:
        return k == mat[m][n]
    if (dp[m][n][k] != -1):
        return dp[m][n][k]
    dp[m][n][k] = (
        pathCountDPRecDP(
            mat,
            m -
            1,
            n,
            k -
            mat[m][n]) +
        pathCountDPRecDP(
            mat,
            m,
            n -
            1,
            k -
            mat[m][n]))
    return dp[m][n][k]


def pathCountDP(mat, k):
    return pathCountDPRecDP(mat, R - 1, C - 1, k)


k = 12
dp = [[[-1 for col in range(MAX_K)]for col in range(C)]for row in range(R)]
mat = [[1, 2, 3], [4, 6, 5], [3, 2, 1]]
print(pathCountDP(mat, k))

# Expected output:
2
# bubble sort | an optimized version of bubble sort ; traverse the array from 0 to n - i - 1. swap if the element found is greater than the next element ; if no two elements were swapped by inner loop , then break ; 
def bubbleSort(arr):
    n = len(arr)
    for i in range(n):
        swapped = False
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
                swapped = True
        if not swapped:
            break


arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print("Sorted array :")
for i in range(len(arr)):
    print("%d" % arr[i], end=" ")

# Expected output:
Sorted array :
11 12 22 25 34 64 90
# find k closest elements to a given value | function to find the cross over point ( the point before which elements aresmaller than or equal to x and afterwhich greater than x ) ; base cases x is greater than all ; x is smaller than all ; find the middle point ; if x is same as middle element , then return mid ; if x is greater than arr [ mid ] , then either arr [ mid + 1 ] is ceiling of x or ceiling lies in arr [ mid + 1. . . high ] ; this function prints k closest elements to x in arr [ ] . n is the number of elements in arr [ ] ; find the crossover point ; right index to search ; to keep track of count of elements already printed ; if x is present in arr [ ] , then reduce left index . assumption : all elements in arr [ ] are distinct ; compare elements on left and right of crossover point to find the k closest elements ; if there are no more elements on right side , then print left elements ; if there are no more elements on left side , then print right elements ; 
def findCrossOver(arr, low, high, x):
    if (arr[high] <= x):
        return high
    if (arr[low] > x):
        return low
    mid = (low + high) // 2
    if (arr[mid] <= x and arr[mid + 1] > x):
        return mid
    if (arr[mid] < x):
        return findCrossOver(arr, mid + 1, high, x)
    return findCrossOver(arr, low, mid - 1, x)


def printKclosest(arr, x, k, n):
    l = findCrossOver(arr, 0, n - 1, x)
    r = l + 1
    count = 0
    if (arr[l] == x):
        l -= 1
    while (l >= 0 and r < n and count < k):
        if (x - arr[l] < arr[r] - x):
            print(arr[l], end=" ")
            l -= 1
        else:
            print(arr[r], end=" ")
            r += 1
        count += 1
    while (count < k and l >= 0):
        print(arr[l], end=" ")
        l -= 1
        count += 1
    while (count < k and r < n):
        print(arr[r], end=" ")
        r += 1
        count += 1


if __name__ == "__main__":
    arr = [12, 16, 22, 30, 35, 39, 42, 45, 48, 50, 53, 55, 56]
    n = len(arr)
    x = 35
    k = 4
    printKclosest(arr, x, 4, n)

# Expected output:
39 30 42 45
# coin change | dp | returns the count of ways we can sum s [ 0. . . m - 1 ] coins to get sum n ; if n is 0 then there is 1 solution ( do not include any coin ) ; if n is less than 0 then no solution exists ; if there are no coins and n is greater than 0 , then no solution exist ; count is sum of solutions ( i ) including s [ m - 1 ] ( ii ) excluding s [ m - 1 ] ; 
def count(S, m, n):
    if (n == 0):
        return 1
    if (n < 0):
        return 0
    if (m <= 0 and n >= 1):
        return 0
    return count(S, m - 1, n) + count(S, m, n - S[m - 1])


arr = [1, 2, 3]
m = len(arr)
print(count(arr, m, 4))

# Expected output:
4
# coin change | dp | dynamic programming python implementation of coin change problem ; table [ i ] will be storing the number of solutions for value i . we need n + 1 rows as the table is constructed in bottom up manner using the base case ( n = 0 ) initialize all table values as 0 ; base case ( if given value is 0 ) ; pick all coins one by one and update the table [ ] values after the index greater than or equal to the value of the picked coin ; 
def count(S, m, n):
    table = [0 for k in range(n + 1)]
    table[0] = 1
    for i in range(0, m):
        for j in range(S[i], n + 1):
            table[j] += table[j - S[i]]
    return table[n]


arr = [1, 2, 3]
m = len(arr)
n = 4
x = count(arr, m, n)
print(x)

# Expected output:
4
# matrix chain multiplication | dp | python program using memoization ; function for matrix chain multiplication ; 

import sys
dp = [[-1 for i in range(100)]for j in range(100)]


def matrixChainMemoised(p, i, j):
    if (i == j):
        return 0
    if (dp[i][j] != -1):
        return dp[i][j]
    dp[i][j] = sys .maxsize
    for k in range(i, j):
        dp[i][j] = min(dp[i][j], matrixChainMemoised(p, i, k) +
                       matrixChainMemoised(p, k + 1, j) + p[i - 1] * p[k] * p[j])
    return dp[i][j]


def MatrixChainOrder(p, n):
    i = 1
    j = n - 1
    return matrixChainMemoised(p, i, j)


arr = [1, 2, 3, 4]
n = len(arr)
print("Minimum number of multiplications is", MatrixChainOrder(arr, n))

# Expected output:
Minimum number of multiplications is 18
# matrix chain multiplication | dp | dynamic programming python implementation of matrix chain multiplication . see the cormen book for details of the following algorithm ; matrix ai has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; for simplicity of the program , one extra row and one extra column are allocated in m [ ] [ ] . 0 th row and 0 th column of m [ ] [ ] are not used ; cost is zero when multiplying one matrix . ; l is chain length . ; q = cost / scalar multiplications ; 

import sys


def MatrixChainOrder(p, n):
    m = [[0 for x in range(n)]for x in range(n)]
    for i in range(1, n):
        m[i][i] = 0
    for L in range(2, n):
        for i in range(1, n - L + 1):
            j = i + L - 1
            m[i][j] = sys .maxsize
            for k in range(i, j):
                q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]
                if q < m[i][j]:
                    m[i][j] = q
    return m[1][n - 1]


arr = [1, 2, 3, 4]
size = len(arr)
print("Minimum number of multiplications is " +
      str(MatrixChainOrder(arr, size)))

# Expected output:
Minimum number of multiplications is 18
# cutting a rod | dp | a naive recursive solution for rod cutting problem ; a utility function to get the maximum of two integers ; returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; recursively cut the rod in different pieces and compare different configurations ; 

import sys


def max(a, b):
    return a if (a > b)else b


def cutRod(price, n):
    if (n <= 0):
        return 0
    max_val = -sys .maxsize - 1
    for i in range(0, n):
        max_val = max(max_val, price[i] + cutRod(price, n - i - 1))
    return max_val


arr = [1, 5, 8, 9, 10, 17, 17, 20]
size = len(arr)
print("Maximum Obtainable Value is", cutRod(arr, size))

# Expected output:
Maximum Obtainable Value is 22
# multiply two integers without using multiplication , division and bitwise operators , and no loops | function to multiply two numbers x and y ; 0 multiplied with anything gives 0 ; add x one by one ; the case where y is negative ; 
def multiply(x, y):
    if (y == 0):
        return 0
    if (y > 0):
        return (x + multiply(x, y - 1))
    if (y < 0):
        return -multiply(x, -y)


print(multiply(5, -11))

# Expected output:
-55
# sieve of eratosthenes | python program to print all primes smaller than or equal to n using sieve of eratosthenes ; create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . a value in prime [ i ] will finally be false if i is not a prime , else true . ; if prime [ p ] is not changed , then it is a prime ; update all multiples of p ; print all prime numbers ; 
def SieveOfEratosthenes(n):
    prime = [True for i in range(n + 1)]
    p = 2
    while (p * p <= n):
        if (prime[p]):
            for i in range(p * p, n + 1, p):
                prime[i] = False
        p += 1
    for p in range(2, n + 1):
        if prime[p]:
            print(p,)


if __name__ == '__main__':
    n = 30
    print("Following are the prime numbers smaller",)
    print("than or equal to", n)
    SieveOfEratosthenes(n)

# Expected output:
Following are the prime numbers smaller
than or equal to 30
2
3
5
7
11
13
17
19
23
29
# pascal 's triangle | binomialcoeff ; a simple o ( n ^ 3 ) program for pascal ' s ▁ triangle ▁ function ▁ to ▁ print ▁ first ▁ n ▁ lines ▁ of ▁ pascal ' s triangle ; iterate through every line and print entries in it ; every line has number of integers equal to line number ; 
def binomialCoeff(n, k):
    res = 1
    if (k > n - k):
        k = n - k
    for i in range(0, k):
        res = res * (n - i)
        res = res // (i + 1)
    return res


def printPascal(n):
    for line in range(0, n):
        for i in range(0, line + 1):
            print(binomialCoeff(line, i), " ", end="")
        print()


n = 7
printPascal(n)

# Expected output:
1  
1  1  
1  2  1  
1  3  3  1  
1  4  6  4  1  
1  5  10  10  5  1  
1  6  15  20  15  6  1
# pascal 's triangle | a o ( n ^ 2 ) time and o ( n ^ 2 ) extra space method for pascal 's triangle ; an auxiliary array to store generated pascal triangle values ; iterate through every line and print integer ( s ) in it ; every line has number of integers equal to line number ; first and last values in every row are 1 ; other values are sum of values just above and left of above ; 
def printPascal(n: int):
    arr = [[0 for x in range(n)]for y in range(n)]
    for line in range(0, n):
        for i in range(0, line + 1):
            if (i == 0 or i == line):
                arr[line][i] = 1
                print(arr[line][i], end=" ")
            else:
                arr[line][i] = (arr[line - 1][i - 1] + arr[line - 1][i])
                print(arr[line][i], end=" ")
        print("",  end="")


n = 5
printPascal(n)

# Expected output:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
# pascal 's triangle | python3 program for pascal ' s ▁ triangle ▁ a ▁ o ( n ^ 2 ) ▁ time ▁ and ▁ o ( 1 ) ▁ extra ▁ space ▁ method ▁ for ▁ pascal ' s triangle pascal function ; used to represent c ( line , i ) ; the first value in a line is always 1 ; 
def printPascal(n):
    for line in range(1, n + 1):
        C = 1
        for i in range(1, line + 1):
            print(C, end=" ")
            C = int(C * (line - i) / i)
        print("")


n = 5
printPascal(n)

# Expected output:
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1
# add two numbers without using arithmetic operators | python3 program to add two numbers without using arithmetic operator ; iterate till there is no carry ; carry now contains common set bits of x and y ; sum of bits of x and y where at least one of the bits is not set ; carry is shifted by one so that adding it to x gives the required sum ; 
def Add(x, y):
    while (y != 0):
        carry = x & y
        x = x ^ y
        y = carry << 1
    return x


print(Add(15, 32))

# Expected output:
47
# compute modulus division by a power | this function will return n % d . d must be one of : 1 , 2 , 4 , 8 , 16 , 32 , ... ; 
def getModulo(n, d):
    return (n & (d - 1))


n = 6
d = 4
print(n, "moduo", d, "is", getModulo(n, d))

# Expected output:
6 moduo 4 is 2
# Count set bits in an integer | Function to get no of set bits in binary representation of positive integer n ; Program to test function countSetBits
def countSetBits(n):
    count = 0
    while (n):
        count += n & 1
        n >>= 1
    return count


i = 9
print(countSetBits(i))

# Expected output:
2
# count set bits in an integer | recursive function to count set bits ; base case ; get value from user ; 
def countSetBits(n):
    if (n == 0):
        return 0
    else:
        return 1 + countSetBits(n & (n - 1))


n = 9
print(countSetBits(n))

# Expected output:
2