giulio98/xlcost-single-prompt huggingface dataset:
# rotate digits of a given number by k | function to find the count of digits in n ; stores count of digits in n ; calculate the count of digits in n ; update digit ; update n ; function to rotate the digits of n by k ; stores count of digits in n ; update k so that only need to handle left rotation ; stores first k digits of n ; remove first k digits of n ; stores count of digits in left_no ; append left_no to the right of digits of n ; 
def numberOfDigit(N):
    digit = 0
    while (N > 0):
        digit += 1
        N //= 10
    return digit


def rotateNumberByK(N, K):
    X = numberOfDigit(N)
    K = ((K % X) + X) % X
    left_no = N // pow(10, X - K)
    N = N % pow(10, X - K)
    left_digit = numberOfDigit(left_no)
    N = N * pow(10, left_digit) + left_no
    print(N)


if __name__ == '__main__':
    N, K = 12345, 7
    rotateNumberByK(N, K)

# Expected output:
34512
# count rotations required to sort given array in non | function to count minimum anti - clockwise rotations required to sort the array in non - increasing order ; stores count of arr [ i + 1 ] > arr [ i ] ; store last index of arr [ i + 1 ] > arr [ i ] ; traverse the given array ; if the adjacent elements are in increasing order ; increment count ; update index ; prthe result according to the following conditions ; otherwise , it is not possible to sort the array ; 
def minMovesToSort(arr, N):
    count = 0
    index = 0
    for i in range(N - 1):
        if (arr[i] < arr[i + 1]):
            count += 1
            index = i
    if (count == 0):
        print("0")
    elif (count == N - 1):
        print(N - 1)
    elif (count == 1 and arr[0] <= arr[N - 1]):
        print(index + 1)
    else:
        print("-1")


arr = [2, 1, 5, 4, 2]
N = len(arr)
minMovesToSort(arr, N)

# Expected output:
2
# maximize sum of diagonal of a matrix by rotating all rows or all columns | python3 program to implement the above approach ; function to find maximum sum of diagonal elements of matrix by rotating either rows or columns ; stores maximum diagonal sum of elements of matrix by rotating rows or columns ; rotate all the columns by an integer in the range [ 0 , n - 1 ] ; stores sum of diagonal elements of the matrix ; calculate sum of diagonal elements of the matrix ; update curr ; update maxdiagonalsum ; rotate all the rows by an integer in the range [ 0 , n - 1 ] ; stores sum of diagonal elements of the matrix ; calculate sum of diagonal elements of the matrix ; update curr ; update maxdiagonalsum ; 

import sys
N = 3


def findMaximumDiagonalSumOMatrixf(A):
    maxDiagonalSum = -sys .maxsize - 1
    for i in range(N):
        curr = 0
        for j in range(N):
            curr += A[j][(i + j) % N]
        maxDiagonalSum = max(maxDiagonalSum, curr)
    for i in range(N):
        curr = 0
        for j in range(N):
            curr += A[(i + j) % N][j]
        maxDiagonalSum = max(maxDiagonalSum, curr)
    return maxDiagonalSum


if __name__ == "__main__":
    mat = [[1, 1, 2], [2, 1, 2], [1, 2, 2]]
    print(findMaximumDiagonalSumOMatrixf(mat))

# Expected output:
6
# queries to find maximum sum contiguous subarrays of given length in a rotating array | function to calculate the maximum sum of length k ; calculating the max sum for the first k elements ; find subarray with maximum sum ; update the sum ; return maximum sum ; function to calculate gcd of the two numbers n1 and n2 ; base case ; recursively find the gcd ; function to rotate the array by y ; for handling k >= n ; dividing the array into number of sets ; rotate the array by y ; update arr [ j ] ; return the rotated array ; function that performs the queries on the given array ; traverse each query ; if query of type x = 1 ; print the array ; if query of type x = 2 ; 
def MaxSum(arr, n, k):
    i, max_sum = 0, 0
    sum = 0
    while i < k:
        sum += arr[i]
        i += 1
    max_sum = sum
    while (i < n):
        sum = sum - arr[i - k] + arr[i]
        if (max_sum < sum):
            max_sum = sum
        i += 1
    return max_sum


def gcd(n1, n2):
    if (n2 == 0):
        return n1
    else:
        return gcd(n2, n1 % n2)


def RotateArr(arr, n, d):
    i = 0
    j = 0
    d = d % n
    no_of_sets = gcd(d, n)
    for i in range(no_of_sets):
        temp = arr[i]
        j = i
        while (True):
            k = j + d
            if (k >= n):
                k = k - n
            if (k == i):
                break
            arr[j] = arr[k]
            j = k
        arr[j] = temp
    return arr


def performQuery(arr, Q, q):
    N = len(arr)
    for i in range(q):
        if (Q[i][0] == 1):
            arr = RotateArr(arr, N, Q[i][1])
            for t in arr:
                print(t, end=" ")
            print()
        else:
            print(MaxSum(arr, N, Q[i][1]))


if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5]
    q = 5
    Q = [[1, 2], [2, 3], [1, 3], [1, 1], [2, 4]]
    performQuery(arr, Q, q)

# Expected output:
3 4 5 1 2 
12
1 2 3 4 5 
2 3 4 5 1 
14
# minimize characters to be changed to make the left and right rotation of a string same | function to find the minimum characters to be removed from the string ; initialize answer by n ; if length is even ; frequency array for odd and even indices ; store the frequency of the characters at even and odd indices ; stores the most occuring frequency for even and odd indices ; update the answer ; if length is odd ; stores the frequency of the characters of the string ; stores the most occuring characterin the string ; update the answer ; 
def getMinimumRemoval(str):
    n = len(str)
    ans = n
    if (n % 2 == 0):
        freqEven = {}
        freqOdd = {}
        for ch in range(ord('a'), ord('z') + 1):
            freqEven[chr(ch)] = 0
            freqOdd[chr(ch)] = 0
        for i in range(n):
            if (i % 2 == 0):
                if str[i] in freqEven:
                    freqEven[str[i]] += 1
            else:
                if str[i] in freqOdd:
                    freqOdd[str[i]] += 1
        evenMax = 0
        oddMax = 0
        for ch in range(ord('a'), ord('z') + 1):
            evenMax = max(evenMax, freqEven[chr(ch)])
            oddMax = max(oddMax, freqOdd[chr(ch)])
        ans = ans - evenMax - oddMax
    else:
        freq = {}
        for ch in range('a', 'z'):
            freq[chr(ch)] = 0
        for i in range(n):
            if str[i] in freq:
                freq[str[i]] += 1
        strMax = 0
        for ch in range('a', 'z'):
            strMax = max(strMax, freq[chr(ch)])
        ans = ans - strMax
    return ans


str = "geeksgeeks"
print(getMinimumRemoval(str))

# Expected output:
6
# longest subsequence of a number having same left and right rotation | python3 program to implement the above approach ; function to find the longest subsequence having equal left and right rotation ; length of the string ; iterate for all possible combinations of a two - digit numbers ; check for alternate occurrence of current combination ; increment the current value ; increment the current value ; if alternating sequence is obtained of odd length ; reduce to even length ; update answer to store the maximum ; return the answer ; 

import sys


def findAltSubSeq(s):
    n = len(s)
    ans = -sys .maxsize - 1
    for i in range(10):
        for j in range(10):
            cur, f = 0, 0
            for k in range(n):
                if (f == 0 and ord(s[k]) - ord('0') == i):
                    f = 1
                    cur += 1
                elif (f == 1 and ord(s[k]) - ord('0') == j):
                    f = 0
                    cur += 1
            if i != j and cur % 2 == 1:
                cur -= 1
            ans = max(cur, ans)
    return ans


s = "100210601"
print(findAltSubSeq(s))

# Expected output:
4
# rotate matrix by 45 degrees | function to rotate matrix by 45 degree ; counter variable ; iterate [ 0 , m ] ; iterate [ 0 , n ] ; diagonal elements condition ; appending the diagonal elements ; printing reversed diagonal elements ; dimensions of matrix ; 
def matrix(n, m, li):
    ctr = 0
    while (ctr < 2 * n - 1):
        print(" " * abs(n - ctr - 1), end="")
        lst = []
        for i in range(m):
            for j in range(n):
                if i + j == ctr:
                    lst .append(li[i][j])
        lst .reverse()
        print(*lst)
        ctr += 1


n = 8
m = n
li = [
    [
        4, 5, 6, 9, 8, 7, 1, 4], [
            1, 5, 9, 7, 5, 3, 1, 6], [
                7, 5, 3, 1, 5, 9, 8, 0], [
                    6, 5, 4, 7, 8, 9, 3, 7], [
                        3, 5, 6, 4, 8, 9, 2, 1], [
                            3, 1, 6, 4, 7, 9, 5, 0], [
                                8, 0, 7, 2, 3, 1, 0, 8], [
                                    7, 5, 3, 1, 5, 9, 8, 5]]
matrix(n, m, li)

# Expected output:
       4
      1 5
     7 5 6
    6 5 9 9
   3 5 3 7 8
  3 5 4 1 5 7
 8 1 6 7 5 3 1
7 0 6 4 8 9 1 4
 5 7 4 8 9 8 6
  3 2 7 9 3 0
   1 3 9 2 7
    5 1 5 1
     9 0 0
      8 8
       5
# mth element after k right rotations of an array | function to return mth element of array after k right rotations ; the array comes to original state after n rotations ; if k is greater or equal to m ; mth element after k right rotations is ( n - k ) + ( m - 1 ) th element of the array ; otherwise ; ( m - k - 1 ) th element of the array ; return the result ; 
def getFirstElement(a, N, K, M):
    K %= N
    if (K >= M):
        index = (N - K) + (M - 1)
    else:
        index = (M - K - 1)
    result = a[index]
    return result


if __name__ == "__main__":
    a = [1, 2, 3, 4, 5]
    N = len(a)
    K, M = 3, 2
    print(getFirstElement(a, N, K, M))

# Expected output:
4
# find the mth element of the array after k left rotations | function to return mth element of array after k left rotations ; the array comes to original state after n rotations ; mth element after k left rotations is ( k + m - 1 ) % n th element of the original array ; return the result ; 
def getFirstElement(a, N, K, M):
    K %= N
    index = (K + M - 1) % N
    result = a[index]
    return result


if __name__ == '__main__':
    a = [3, 4, 5, 23]
    N = len(a)
    K = 2
    M = 1
    print(getFirstElement(a, N, K, M))

# Expected output:
5
# rotate all odd numbers right and all even numbers left in an array of 1 to n | function to left rotate ; function to right rotate ; function to rotate the array ; 
def left_rotate(arr):
    last = arr[1]
    for i in range(3, len(arr), 2):
        arr[i - 2] = arr[i]
    arr[len(arr) - 1] = last


def right_rotate(arr):
    start = arr[len(arr) - 2]
    for i in range(len(arr) - 4, -1, -2):
        arr[i + 2] = arr[i]
    arr[0] = start


def rotate(arr):
    left_rotate(arr)
    right_rotate(arr)
    for i in range(len(arr)):
        print(arr[i], end=" ")


arr = [1, 2, 3, 4, 5, 6]
rotate(arr)

# Expected output:
5 4 1 6 3 2
# maximize count of corresponding same elements in given permutations using cyclic rotations | python3 program for the above approach ; function to maximize the matching pairs between two permutation using left and right rotation ; left array store distance of element from left side and right array store distance of element from right side ; map to store index of elements ; idx1 is index of element in first permutation idx2 is index of element in second permutation ; if element if present on same index on both permutations then distance is zero ; calculate distance from left and right side ; calculate distance from left and right side ; maps to store frequencies of elements present in left and right arrays ; find maximum frequency ; return the result ; 

from collections import defaultdict


def maximumMatchingPairs(perm1, perm2, n):
    left = [0] * n
    right = [0] * n
    mp1 = {}
    mp2 = {}
    for i in range(n):
        mp1[perm1[i]] = i
    for j in range(n):
        mp2[perm2[j]] = j
    for i in range(n):
        idx2 = mp2[perm1[i]]
        idx1 = i
        if (idx1 == idx2):
            left[i] = 0
            right[i] = 0
        elif (idx1 < idx2):
            left[i] = (n - (idx2 - idx1))
            right[i] = (idx2 - idx1)
        else:
            left[i] = (idx1 - idx2)
            right[i] = (n - (idx1 - idx2))
    freq1 = defaultdict(int)
    freq2 = defaultdict(int)
    for i in range(n):
        freq1[left[i]] += 1
        freq2[right[i]] += 1
    ans = 0
    for i in range(n):
        ans = max(ans, max(freq1[left[i]], freq2[right[i]]))
    return ans


if __name__ == "__main__":
    P1 = [5, 4, 3, 2, 1]
    P2 = [1, 2, 3, 4, 5]
    n = len(P1)
    print(maximumMatchingPairs(P1, P2, n))

# Expected output:
1
# count of rotations required to generate a sorted array | function to return the count of rotations ; find the smallest element ; return its index ; if array is not rotated at all ; 
def countRotation(arr, n):
    for i in range(1, n):
        if (arr[i] < arr[i - 1]):
            return i
    return 0


if __name__ == "__main__":
    arr1 = [4, 5, 1, 2, 3]
    n = len(arr1)
    print(countRotation(arr1, n))

# Expected output:
2
# count of rotations required to generate a sorted array | function to return the count of rotations ; if array is not rotated ; check if current element is greater than the next element ; the next element is the smallest ; check if current element is smaller than it 's previous  element ; current element is the smallest ; check if current element is greater than lower bound ; the sequence is increasing so far search for smallest element on the right subarray ; smallest element lies on the left subarray ; search for the smallest element on both subarrays ; 
def countRotation(arr, low, high):
    if (low > high):
        return 0
    mid = low + (high - low) // 2
    if (mid < high and arr[mid] > arr[mid + 1]):
        return mid + 1
    if (mid > low and arr[mid] < arr[mid - 1]):
        return mid
    if (arr[mid] > arr[low]):
        return countRotation(arr, mid + 1, high)
    if (arr[mid] < arr[high]):
        return countRotation(arr, low, mid - 1)
    else:
        rightIndex = countRotation(arr, mid + 1, high)
        leftIndex = countRotation(arr, low, mid - 1)
        if (rightIndex == 0):
            return leftIndex
        return rightIndex


if __name__ == '__main__':
    arr1 = [4, 5, 1, 2, 3]
    N = len(arr1)
    print(countRotation(arr1, 0, N - 1))

# Expected output:
2
# range sum queries for anticlockwise rotations of array by k indices | function to execute the queries ; construct a new array of size 2 * n to store prefix sum of every index ; copy elements to the new array ; calculate the prefix sum for every index ; set start pointer as 0 ; query to perform anticlockwise rotation ; query to answer range sum ; if pointing to 1 st index ; display the sum upto start + r ; subtract sum upto start + l - 1 from sum upto start + r ; 
def rotatedSumQuery(arr, n, query, Q):
    prefix = [0] * (2 * n)
    for i in range(n):
        prefix[i] = arr[i]
        prefix[i + n] = arr[i]
    for i in range(1, 2 * n):
        prefix[i] += prefix[i - 1]
    start = 0
    for q in range(Q):
        if (query[q][0] == 1):
            k = query[q][1]
            start = (start + k) % n
        elif (query[q][0] == 2):
            L = query[q][1]
            R = query[q][2]
            if (start + L == 0):
                print(prefix[start + R])
            else:
                print(prefix[start + R] - prefix[start + L - 1])


arr = [1, 2, 3, 4, 5, 6]
Q = 5
query = [[2, 1, 3], [1, 3], [2, 0, 3], [1, 4], [2, 3, 5]]
n = len(arr)
rotatedSumQuery(arr, n, query, Q)

# Expected output:
9
16
12
# check if a string can be formed from another string by at most x circular clockwise shifts | function to check that the string s1 can be converted to s2 by clockwise circular shift of all characters of str1 atmost x times ; check for all characters of the strings whether the difference between their ascii values is less than x or not ; if both characters are the same ; condition to check if the difference less than 0 then find the circular shift by adding 26 to it ; if difference between their ascii values exceeds x ; 
def isConversionPossible(s1, s2, x):
    n = len(s1)
    s1 = list(s1)
    s2 = list(s2)
    for i in range(n):
        diff = ord(s2[i]) - ord(s1[i])
        if diff == 0:
            continue
        if diff < 0:
            diff = diff + 26
        if diff > x:
            return False
    return True


if __name__ == "__main__":
    s1 = "you"
    s2 = "ara"
    x = 6
    result = isConversionPossible(s1, s2, x)
    if result:
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# count rotations which are divisible by 10 | function to return the count of all rotations which are divisible by 10. ; loop to iterate through the number ; if the last digit is 0 , then increment the count ; 
def countRotation(n):
    count = 0
    while n > 0:
        digit = n % 10
        if (digit % 2 == 0):
            count = count + 1
        n = int(n / 10)
    return count


if __name__ == "__main__":
    n = 10203
    print(countRotation(n))

# Expected output:
3
# check if it is possible to make array increasing or decreasing by rotating the array | python3 implementation of the approach ; function that returns true if the array can be made increasing or decreasing after rotating it in any direction ; if size of the array is less than 3 ; check if the array is already decreasing ; if the array is already decreasing ; check if the array is already increasing ; if the array is already increasing ; find the indices of the minimum and the maximum value ; check if we can make array increasing ; if the array is increasing upto max index and minimum element is right to maximum ; check if array increasing again or not ; check if we can make array decreasing ; if the array is decreasing upto min index and minimum element is left to maximum ; check if array decreasing again or not ; if it is not possible to make the array increasing or decreasing ; 

import sys


def isPossible(a, n):
    if (n <= 2):
        return True
    flag = 0
    for i in range(n - 2):
        if (not (a[i] > a[i + 1] and a[i + 1] > a[i + 2])):
            flag = 1
            break
    if (flag == 0):
        return True
    flag = 0
    for i in range(n - 2):
        if (not (a[i] < a[i + 1] and a[i + 1] < a[i + 2])):
            flag = 1
            break
    if (flag == 0):
        return True
    val1 = sys .maxsize
    mini = -1
    val2 = -sys .maxsize
    maxi = -1
    for i in range(n):
        if (a[i] < val1):
            mini = i
            val1 = a[i]
        if (a[i] > val2):
            maxi = i
            val2 = a[i]
    flag = 1
    for i in range(maxi):
        if (a[i] > a[i + 1]):
            flag = 0
            break
    if (flag == 1 and maxi + 1 == mini):
        flag = 1
        for i in range(mini, n - 1):
            if (a[i] > a[i + 1]):
                flag = 0
                break
        if (flag == 1):
            return True
    flag = 1
    for i in range(mini):
        if (a[i] < a[i + 1]):
            flag = 0
            break
    if (flag == 1 and maxi - 1 == mini):
        flag = 1
        for i in range(maxi, n - 1):
            if (a[i] < a[i + 1]):
                flag = 0
                break
        if (flag == 1):
            return True
    return False


a = [4, 5, 6, 2, 3]
n = len(a)
if (isPossible(a, n)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# count number of rotated strings which have more number of vowels in the first half than second half | function to return the count of rotated strings which have more number of vowels in the first half than the second half ; create a new string ; pre array to store count of all vowels ; compute the prefix array ; to store the required answer ; find all rotated strings ; right and left index of the string ; x1 stores the number of vowels in the rotated string ; left stores the number of vowels in the first half of rotated string ; right stores the number of vowels in the second half of rotated string ; if the count of vowels in the first half is greater than the count in the second half ; return the required answer ; 
def cntRotations(s, n):
    str = s + s
    pre = [0] * (2 * n)
    for i in range(2 * n):
        if (i != 0):
            pre[i] += pre[i - 1]
        if (str[i] == 'a' or str[i] == 'e' or str[i]
                == 'i' or str[i] == 'o' or str[i] == 'u'):
            pre[i] += 1
    ans = 0
    for i in range(n - 1, 2 * n - 1, 1):
        r = i
        l = i - n
        x1 = pre[r]
        if (l >= 0):
            x1 -= pre[l]
        r = (int)(i - n / 2)
        left = pre[r]
        if (l >= 0):
            left -= pre[l]
        right = x1 - left
        if (left > right):
            ans += 1
    return ans


s = "abecidft"
n = len(s)
print(cntRotations(s, n))

# Expected output:
4
# count number of rotated strings which have more number of vowels in the first half than second half | function to return the count of rotated strings which have more number of vowels in the first half than the second half ; compute the number of vowels in first - half ; compute the number of vowels in second - half ; check if first - half has more vowels ; check for all possible rotations ; return the answer ; 
def cntRotations(s, n):
    lh, rh, ans = 0, 0, 0
    for i in range(n // 2):
        if (s[i] == 'a' or s[i] == 'e' or s[i] ==
                'i' or s[i] == 'o' or s[i] == 'u'):
            lh += 1
    for i in range(n // 2, n):
        if (s[i] == 'a' or s[i] == 'e' or s[i] ==
                'i' or s[i] == 'o' or s[i] == 'u'):
            rh += 1
    if (lh > rh):
        ans += 1
    for i in range(1, n):
        if (s[i - 1] == 'a' or s[i - 1] == 'e' or s[i - 1]
                == 'i' or s[i - 1] == 'o' or s[i - 1] == 'u'):
            rh += 1
            lh -= 1
        if (s[(i - 1 + n // 2) %
              n] == 'a' or s[(i - 1 + n // 2) %
                             n] == 'e' or s[(i - 1 + n // 2) %
                                            n] == 'i' or s[(i - 1 + n // 2) %
                                                           n] == 'o' or s[(i - 1 + n // 2) %
                                                                          n] == 'u'):
            rh -= 1
            lh += 1
        if (lh > rh):
            ans += 1
    return ans


if __name__ == "__main__":
    s = "abecidft"
    n = len(s)
    print(cntRotations(s, n))

# Expected output:
4
# queries for rotation and kth character of the given string in constant time | python3 implementation of the approach ; function to perform the required queries on the given string ; pointer pointing to the current starting character of the string ; for every query ; if the query is to rotate the string ; update the pointer pointing to the starting character of the string ; index of the kth character in the current rotation of the string ; print the kth character ; 

size = 2


def performQueries(string, n, queries, q):
    ptr = 0
    for i in range(q):
        if (queries[i][0] == 1):
            ptr = (ptr + queries[i][1]) % n
        else:
            k = queries[i][1]
            index = (ptr + k - 1) % n
            print(string[index])


if __name__ == "__main__":
    string = "abcdefgh"
    n = len(string)
    queries = [[1, 2], [2, 2], [1, 4], [2, 7]]
    q = len(queries)
    performQueries(string, n, queries, q)

# Expected output:
d
e