giulio98/xlcost-single-prompt huggingface dataset:
# check if a string can be obtained by rotating another string d places | function to reverse an array from left index to right index ( both inclusive ) ; function that returns true if str1 can be made equal to str2 by rotating either d places to the left or to the right ; left rotation string will contain the string rotated anti - clockwise right rotation string will contain the string rotated clockwise ; copying the str1 string to left rotation string and right rotation string ; rotating the string d positions to the left ; rotating the string d positions to the right ; compairing the rotated strings ; if cannot be made equal with left rotation ; if cannot be made equal with right rotation ; if both or any one of the rotations of str1 were equal to str2 ; 
def ReverseArray(arr, left, right):
    while (left < right):
        temp = arr[left]
        arr[left] = arr[right]
        arr[right] = temp
        left += 1
        right -= 1


def RotateAndCheck(str1, str2, d):
    if (len(str1) != len(str2)):
        return False
    left_rot_str1 = []
    right_rot_str1 = []
    left_flag = True
    right_flag = True
    str1_size = len(str1)
    for i in range(str1_size):
        left_rot_str1 .append(str1[i])
        right_rot_str1 .append(str1[i])
    ReverseArray(left_rot_str1, 0, d - 1)
    ReverseArray(left_rot_str1, d, str1_size - 1)
    ReverseArray(left_rot_str1, 0, str1_size - 1)
    ReverseArray(right_rot_str1, 0, str1_size - d - 1)
    ReverseArray(right_rot_str1, str1_size - d, str1_size - 1)
    ReverseArray(right_rot_str1, 0, str1_size - 1)
    for i in range(str1_size):
        if (left_rot_str1[i] != str2[i]):
            left_flag = False
        if (right_rot_str1[i] != str2[i]):
            right_flag = False
    if (left_flag or right_flag):
        return True
    return False


if __name__ == "__main__":
    str1 = list("abcdefg")
    str2 = list("cdefgab")
    d = 2
    d = d % len(str1)
    if (RotateAndCheck(str1, str2, d)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# count rotations of n which are odd and even | function to count of all rotations which are odd and even ; 
def countOddRotations(n):
    odd_count = 0
    even_count = 0
    while n != 0:
        digit = n % 10
        if digit % 2 == 0:
            odd_count += 1
        else:
            even_count += 1
        n = n // 10
    print("Odd =", odd_count)
    print("Even =", even_count)


n = 1234
countOddRotations(n)

# Expected output:
Odd = 2
Even = 2
# generate all rotations of a number | function to return the count of digit of n ; function to print the left shift numbers ; formula to calculate left shift from previous number ; update the original number ; 
def numberofDigits(n):
    cnt = 0
    while n > 0:
        cnt += 1
        n //= 10
    return cnt


def cal(num):
    digit = numberofDigits(num)
    powTen = pow(10, digit - 1)
    for i in range(digit - 1):
        firstDigit = num // powTen
        left = (num * 10 + firstDigit - (firstDigit * powTen * 10))
        print(left, end=" ")
        num = left


num = 1445
cal(num)

# Expected output:
4451 4514 5144
# check whether all the rotations of a given number is greater than or equal to the given number or not | python3 implementation of the approach ; splitting the number at index i and adding to the front ; checking if the value is greater than or equal to the given value ; 
def CheckKCycles(n, s):
    ff = True
    for i in range(1, n):
        x = int(s[i:] + s[0:i])
        if (x >= int(s)):
            continue
        ff = False
        break
    if (ff):
        print("Yes")
    else:
        print("No")


n = 3
s = "123"
CheckKCycles(n, s)

# Expected output:
Yes
# generating numbers that are divisor of their right | python program to generating numbers that are divisor of their right - rotations ; function to check if n is a divisor of its right - rotation ; function to generate m - digit numbers which are divisor of their right - rotation ; 

from math import log10


def rightRotationDivisor(N):
    lastDigit = N % 10
    rightRotation = (lastDigit * 10 ** int(log10(N)) + N // 10)
    return rightRotation % N == 0


def generateNumbers(m):
    for i in range(10 ** (m - 1), 10 ** m):
        if rightRotationDivisor(i):
            print(i)


m = 3
generateNumbers(m)

# Expected output:
111
222
333
444
555
666
777
888
999
# generating numbers that are divisor of their right | python program to generating numbers that are divisor of their right - rotations ; function to generate m - digit numbers which are divisor of their right - rotation ; 

from math import log10


def generateNumbers(m):
    numbers = []
    for y in range(1, 10):
        k_max = ((10 ** (m - 2) * (10 * y + 1)) // (10 ** (m - 1) + y))
        for k in range(1, k_max + 1):
            x = ((y * (10 ** (m - 1) - k)) // (10 * k - 1))
            if ((y * (10 ** (m - 1) - k)) % (10 * k - 1) == 0):
                numbers .append(10 * x + y)
    for n in sorted(numbers):
        print(n)


m = 3
generateNumbers(m)

# Expected output:
111
222
333
444
555
666
777
888
999
# check if an array is sorted and rotated | python3 program to check if an array is sorted and rotated clockwise ; function to check if an array is sorted and rotated clockwise ; find the minimum element and it 's index ; check if all elements before minindex are in increasing order ; check if all elements after minindex are in increasing order ; check if last element of the array is smaller than the element just before the element at minindex starting element of the array for arrays like [ 3 , 4 , 6 , 1 , 2 , 5 ] - not sorted circular array ; 

import sys


def checkIfSortRotated(arr, n):
    minEle = sys .maxsize
    maxEle = -sys .maxsize - 1
    minIndex = -1
    for i in range(n):
        if arr[i] < minEle:
            minEle = arr[i]
            minIndex = i
    flag1 = 1
    for i in range(1, minIndex):
        if arr[i] < arr[i - 1]:
            flag1 = 0
            break
    flag2 = 2
    for i in range(minIndex + 1, n):
        if arr[i] < arr[i - 1]:
            flag2 = 0
            break
    if (flag1 and flag2 and arr[n - 1] < arr[0]):
        print("YES")
    else:
        print("NO")


arr = [3, 4, 5, 1, 2]
n = len(arr)
checkIfSortRotated(arr, n)

# Expected output:
YES
# rotate a matrix by 90 degree in clockwise direction without using any extra space | python3 implementation of above approach ; function to rotate the matrix 90 degree clockwise ; printing the matrix on the basis of observations made on indices . ; 

N = 4


def rotate90Clockwise(arr):
    global N
    for j in range(N):
        for i in range(N - 1, -1, -1):
            print(arr[i][j], end=" ")
        print()


arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
rotate90Clockwise(arr)

# Expected output:
13 9 5 1 
14 10 6 2 
15 11 7 3 
16 12 8 4
# elements that occurred only once in the array | function to find the elements that appeared only once in the array ; sort the array ; check for first element ; check for all the elements if it is different its adjacent elements ; check for the last element ; 
def occurredOnce(arr, n):
    arr .sort()
    if arr[0] != arr[1]:
        print(arr[0], end=" ")
    for i in range(1, n - 1):
        if (arr[i] != arr[i + 1] and arr[i] != arr[i - 1]):
            print(arr[i], end=" ")
    if arr[n - 2] != arr[n - 1]:
        print(arr[n - 1], end=" ")


if __name__ == "__main__":
    arr = [7, 7, 8, 8, 9, 1, 1, 4, 2, 2]
    n = len(arr)
    occurredOnce(arr, n)

# Expected output:
4 9
# elements that occurred only once in the array | python3 implementation to find elements that appeared only once ; function to find the elements that appeared only once in the array ; store all the elements in the map with their occurrence ; traverse the map and print all the elements with occurrence 1 ; 

import math as mt


def occurredOnce(arr, n):
    mp = dict()
    for i in range(n):
        if arr[i] in mp .keys():
            mp[arr[i]] += 1
        else:
            mp[arr[i]] = 1
    for it in mp:
        if mp[it] == 1:
            print(it, end=" ")


arr = [7, 7, 8, 8, 9, 1, 1, 4, 2, 2]
n = len(arr)
occurredOnce(arr, n)

# Expected output:
9 4
# elements that occurred only once in the array | function to find the elements that appeared only once in the array ; check if the first and last element is equal if yes , remove those elements ; start traversing the remaining elements ; check if current element is equal to the element at immediate previous index if yes , check the same for next element ; else print the current element ; check for the last element ; 
def occurredOnce(arr, n):
    i = 1
    len = n
    if arr[0] == arr[len - 1]:
        i = 2
        len -= 1
    while i < n:
        if arr[i] == arr[i - 1]:
            i += 1
        else:
            print(arr[i - 1], end=" ")
        i += 1
    if (arr[n - 1] != arr[0] and arr[n - 1] != arr[n - 2]):
        print(arr[n - 1])


if __name__ == "__main__":
    arr = [7, 7, 8, 8, 9, 1, 1, 4, 2, 2]
    n = len(arr)
    occurredOnce(arr, n)

# Expected output:
9 4
# check if strings are rotations of each other or not | set 2 | python program to check if two strings are rotations of each other ; create lps [ ] that will hold the longest prefix suffix values for pattern ; length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; the loop calculates lps [ i ] for i = 1 to n - 1 ; match from that rotating point ; 
def isRotation(a: str, b: str) -> bool:
    n = len(a)
    m = len(b)
    if (n != m):
        return False
    lps = [0 for _ in range(n)]
    length = 0
    i = 1
    lps[0] = 0
    while (i < n):
        if (a[i] == b[length]):
            length += 1
            lps[i] = length
            i += 1
        else:
            if (length == 0):
                lps[i] = 0
                i += 1
            else:
                length = lps[length - 1]
    i = 0
    for k in range(lps[n - 1], m):
        if (b[k] != a[i]):
            return False
        i += 1
    return True


if __name__ == "__main__":
    s1 = "ABACD"
    s2 = "CDABA"
    print("1"if isRotation(s1, s2)else "0")

# Expected output:
1
# count rotations divisible by 8 | function to count of all rotations divisible by 8 ; for single digit number ; for two - digit numbers ( considering all pairs ) ; first pair ; second pair ; considering all three - digit sequences ; considering the number formed by the last digit and the first two digits ; considering the number formed by the last two digits and the first digit ; required count of rotations ; 
def countRotationsDivBy8(n):
    l = len(n)
    count = 0
    if (l == 1):
        oneDigit = int(n[0])
        if (oneDigit % 8 == 0):
            return 1
        return 0
    if (l == 2):
        first = int(n[0]) * 10 + int(n[1])
        second = int(n[1]) * 10 + int(n[0])
        if (first % 8 == 0):
            count += 1
        if (second % 8 == 0):
            count += 1
        return count
    threeDigit = 0
    for i in range(0, (l - 2)):
        threeDigit = (int(n[i]) * 100 + int(n[i + 1]) * 10 + int(n[i + 2]))
        if (threeDigit % 8 == 0):
            count += 1
    threeDigit = (int(n[l - 1]) * 100 + int(n[0]) * 10 + int(n[1]))
    if (threeDigit % 8 == 0):
        count += 1
    threeDigit = (int(n[l - 2]) * 100 + int(n[l - 1]) * 10 + int(n[0]))
    if (threeDigit % 8 == 0):
        count += 1
    return count


if __name__ == '__main__':
    n = "43262488612"
    print("Rotations:", countRotationsDivBy8(n))

# Expected output:
Rotations: 4
# minimum move to end operations to make all strings equal | python 3 program to make all strings same using move to end operations . ; returns minimum number of moves to end operations to make all strings same . ; consider s [ i ] as target string and count rotations required to make all other strings same as str [ i ] . ; find function returns the index where we found arr [ i ] which is actually count of move - to - front operations . ; if any two strings are not rotations of each other , we can 't make them same. ; 

import sys


def minimunMoves(arr, n):
    ans = sys .maxsize
    for i in range(n):
        curr_count = 0
        for j in range(n):
            tmp = arr[j] + arr[j]
            index = tmp .find(arr[i])
            if (index == len(arr[i])):
                return -1
            curr_count += index
        ans = min(curr_count, ans)
    return ans


if __name__ == "__main__":
    arr = ["xzzwo", "zwoxz", "zzwox", "xzzwo"]
    n = len(arr)
    print(minimunMoves(arr, n))

# Expected output:
5
# check if two numbers are bit rotations of each other or not | function to check if two numbers are equal after bit rotation ; x64 has concatenation of x with itself . ; comapring only last 32 bits ; right shift by 1 unit ; 
def isRotation(x, y):
    x64 = x | (x << 32)
    while (x64 >= y):
        if ((x64) == y):
            return True
        x64 >>= 1
    return False


if __name__ == "__main__":
    x = 122
    y = 2147483678
    if (isRotation(x, y) == False):
        print("yes")
    else:
        print("no")

# Expected output:
yes
# count rotations divisible by 4 | returns count of all rotations divisible by 4 ; for single digit number ; at - least 2 digit number ( considering all pairs ) ; considering the number formed by the pair of last digit and 1 st digit ; 
def countRotations(n):
    l = len(n)
    if (l == 1):
        oneDigit = (int)(n[0])
        if (oneDigit % 4 == 0):
            return 1
        return 0
    count = 0
    for i in range(0, l - 1):
        twoDigit = (int)(n[i]) * 10 + (int)(n[i + 1])
        if (twoDigit % 4 == 0):
            count = count + 1
    twoDigit = (int)(n[l - 1]) * 10 + (int)(n[0])
    if (twoDigit % 4 == 0):
        count = count + 1
    return count


n = "4834"
print("Rotations: ", countRotations(n))

# Expected output:
Rotations:  2
# check if a string can be obtained by rotating another string 2 places | function to check if string2 is obtained by string 1 ; initialize string as anti - clockwise rotation ; initialize string as clock wise rotation ; check if any of them is equal to string1 ; 
def isRotated(str1, str2):
    if (len(str1) != len(str2)):
        return False
    if (len(str1) < 2):
        return str1 == str2
    clock_rot = ""
    anticlock_rot = ""
    l = len(str2)
    anticlock_rot = (anticlock_rot + str2[l - 2:] + str2[0:l - 2])
    clock_rot = clock_rot + str2[2:] + str2[0:2]
    return (str1 == clock_rot or str1 == anticlock_rot)


if __name__ == "__main__":
    str1 = "geeks"
    str2 = "eksge"
if isRotated(str1, str2):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# lexicographically minimum string rotation | set 1 | this function return lexicographically minimum rotation of str ; find length of given string ; create an array of strings to store all rotations ; create a concatenation of string with itself ; one by one store all rotations of str in array . a rotation is obtained by getting a substring of concat ; sort all rotations ; return the first rotation from the sorted array ; 
def minLexRotation(str_):
    n = len(str_)
    arr = [0] * n
    concat = str_ + str_
    for i in range(n):
        arr[i] = concat[i:n + i]
    arr .sort()
    return arr[0]


print(minLexRotation("GEEKSFORGEEKS"))
print(minLexRotation("GEEKSQUIZ"))
print(minLexRotation("BCABDADAB"))

# Expected output:
EEKSFORGEEKSG
EEKSQUIZG
ABBCABDAD
# lucky alive person in a circle | code solution to sword puzzle | node structure ; function to find the luckiest person ; create a single node circular linked list . ; starting from first soldier . ; condition for evaluating the existence of single soldier who is not killed . ; deleting soldier from the circular list who is killed in the fight . ; returning the luckiest soldier who remains alive . ; 

class Node:
    def __init__(self, data):
        self .data = data
        self .next = None


def newNode(data):
    node = Node(data)
    return node


def alivesol(Num):
    if (Num == 1):
        return 1
    last = newNode(1)
    last .next = last
    for i in range(2, Num + 1):
        temp = newNode(i)
        temp .next = last .next
        last .next = temp
        last = temp
    curr = last .next
    temp = None
    while (curr .next != curr):
        temp = curr
        curr = curr .next
        temp .next = curr .next
        del curr
        temp = temp .next
        curr = temp
    res = temp .data
    del temp
    return res


if __name__ == '__main__':
    N = 100
    print(alivesol(N))

# Expected output:
73
# lowest common ancestor of the deepest leaves of a binary tree | node of a binary tree ; function to find the depth of the binary tree ; if root is not null ; left recursive subtree ; right recursive subtree ; returns the maximum depth ; function to perform the depth first search on the binary tree ; if root is null ; if curr is equal to depth ; left recursive subtree ; right recursive subtree ; if left and right are not null ; return left , if left is not null otherwise return right ; function to find the lca of the deepest nodes of the binary tree ; if root is null ; stores the deepest depth of the binary tree ; return the lca of the nodes at level depth ; 

class Node:
    def __init__(self, d):
        self .data = d
        self .left = None
        self .right = None


def finddepth(root):
    if (not root):
        return 0
    left = finddepth(root .left)
    right = finddepth(root .right)
    return 1 + max(left, right)


def dfs(root, curr, depth):
    if (not root):
        return None
    if (curr == depth):
        return root
    left = dfs(root .left, curr + 1, depth)
    right = dfs(root .right, curr + 1, depth)
    if (left is not None and right is not None):
        return root
    return left if left else right


def lcaOfDeepestLeaves(root):
    if (not root):
        return None
    depth = finddepth(root) - 1
    return dfs(root, 0, depth)


if __name__ == '__main__':
    root = Node(1)
    root .left = Node(2)
    root .right = Node(3)
    root .left .left = Node(4)
    root .left .right = Node(5)
    root .right .left = Node(6)
    root .right .right = Node(7)
    root .right .left .left = Node(8)
    root .right .left .right = Node(9)
    print(lcaOfDeepestLeaves(root).data)

# Expected output:
6
# check if two nodes are on same path in a tree | set 2 | function to filter the return values ; utility function to check if nodes are on same path or not ; condition to check if any vertex is equal to given two vertex or not ; check if the current position has 1 ; recursive call ; return lca ; function to check if nodes lies on same path or not ; 
def filter(x, y, z):
    if (x != -1 and y != -1):
        return z
    return y if x == -1 else x


def samePathUtil(mtrx, vrtx, v1, v2, i):
    ans = -1
    if (i == v1 or i == v2):
        return i
    for j in range(0, vrtx):
        if (mtrx[i][j] == 1):
            ans = filter(ans, samePathUtil(mtrx, vrtx, v1, v2, j), i)
    return ans


def isVertexAtSamePath(mtrx, vrtx, v1, v2, i):
    lca = samePathUtil(mtrx, vrtx, v1 - 1, v2 - 1, i)
    if (lca == v1 - 1 or lca == v2 - 1):
        return True
    return False


vrtx = 7
edge = 6
mtrx = [
    [
        0, 1, 1, 1, 0, 0, 0], [
            0, 0, 0, 0, 1, 0, 0], [
                0, 0, 0, 0, 0, 1, 0], [
                    0, 0, 0, 0, 0, 0, 1], [
                        0, 0, 0, 0, 0, 0, 0], [
                            0, 0, 0, 0, 0, 0, 0], [
                                0, 0, 0, 0, 0, 0, 0]]
v1 = 1
v2 = 5
if (isVertexAtSamePath(mtrx, vrtx, v1, v2, 0)):
    print("Yes")
else:
    print("No")

# Expected output:
Yes
# lowest common ancestor for a set of nodes in a rooted tree | python program to find the lca in a rooted tree for a given set of nodes ; set time 1 initially ; case for root node ; in - time for node ; out - time for the node ; level [ i ] -- > level of node i ; t_in [ i ] -- > in - time of node i ; t_out [ i ] -- > out - time of node i ; fill the level , in - time and out - time of all nodes ; to find minimum in - time among all nodes in lca set ; to find maximum in - time among all nodes in lca set ; node with same minimum and maximum out time is lca for the set ; take the minimum level as level of lca ; if i - th node is at a higher level than that of the minimum among the nodes of the given set ; compare in - time , out - time and level of i - th node to the respective extremes among all nodes of the given set ; 

from typing import List
from sys import maxsize
INT_MAX = maxsize
INT_MIN = -maxsize
T = 1


def dfs(node: int,
        parent: int,
        g: List[List[int]],
        level: List[int],
        t_in: List[int],
        t_out: List[int]) -> None:
    global T
    if (parent == -1):
        level[node] = 1
    else:
        level[node] = level[parent] + 1
    t_in[node] = T
    for i in g[node]:
        if (i != parent):
            T += 1
            dfs(i, node, g, level, t_in, t_out)
    T += 1
    t_out[node] = T


def findLCA(n: int, g: List[List[int]], v: List[int]) -> int:
    level = [0 for _ in range(n + 1)]
    t_in = [0 for _ in range(n + 1)]
    t_out = [0 for _ in range(n + 1)]
    dfs(1, -1, g, level, t_in, t_out)
    mint = INT_MAX
    maxt = INT_MIN
    minv = -1
    maxv = -1
    for i in v:
        if (t_in[i] < mint):
            mint = t_in[i]
            minv = i
        if (t_out[i] > maxt):
            maxt = t_out[i]
            maxv = i
    if (minv == maxv):
        return minv
    lev = min(level[minv], level[maxv])
    node = 0
    l = INT_MIN
    for i in range(n):
        if (level[i] > lev):
            continue
        if (t_in[i] <= mint and t_out[i] >= maxt and level[i] > l):
            node = i
            l = level[i]
    return node


if __name__ == "__main__":
    n = 10
    g = [[]for _ in range(n + 1)]
    g[1].append(2)
    g[2].append(1)
    g[1].append(3)
    g[3].append(1)
    g[1].append(4)
    g[4].append(1)
    g[2].append(5)
    g[5].append(2)
    g[2].append(6)
    g[6].append(2)
    g[3].append(7)
    g[7].append(3)
    g[4].append(10)
    g[10].append(4)
    g[8].append(7)
    g[7].append(8)
    g[9].append(7)
    g[7].append(9)
    v = [7, 3, 8]
    print(findLCA(n, g, v))

# Expected output:
3