giulio98/xlcost-single-prompt huggingface dataset:
# check if frequency of character in one string is a factor or multiple of frequency of same character in other string | python3 implementation of above approach ; function that checks if the frequency of character are a factor or multiple of each other ; map store frequency of each character ; if any frequency is 0 , then continue as condition is satisfied ; if factor or multiple , then condition satisfied ; if condition not satisfied ; 

from collections import defaultdict


def multipleOrFactor(s1, s2):
    m1 = defaultdict(lambda: 0)
    m2 = defaultdict(lambda: 0)
    for i in range(0, len(s1)):
        m1[s1[i]] += 1
    for i in range(0, len(s2)):
        m2[s2[i]] += 1
    for it in m1:
        if it not in m2:
            continue
        if (m2[it] % m1[it] == 0 or m1[it] % m2[it] == 0):
            continue
        else:
            return False
    return True


if __name__ == "__main__":
    s1 = "geeksforgeeks"
    s2 = "geeks"
    if multipleOrFactor(s1, s2):
        print("YES")
    else:
        print("NO")

# Expected output:
YES
# remove even frequency characters from the string | function that removes the characters which have even frequencies in the string ; create a map to store the frequency of each character ; to store the new string ; remove the characters which have even frequencies ; if the character has even frequency then skip ; else concatenate the character to the new string ; display the modified string ; 
def solve(s):
    m = dict()
    for i in range(len(s)):
        if s[i] in m:
            m[s[i]] = m[s[i]] + 1
        else:
            m[s[i]] = 1
    new_string = ""
    for i in range(len(s)):
        if m[s[i]] % 2 == 0:
            continue
        new_string = new_string + s[i]
    print(new_string)


if __name__ == '__main__':
    s = "aabbbddeeecc"
    solve(s)

# Expected output:
bbbeee
# sum of all subsequences of a number | returns numeric value of a subsequence of s . the subsequence to be picked is decided using bit pattern of num ( we pick all thosedigits for which there is a set bit in num ) ; initialize the result ; till n != 0 ; if i - th bit is set then add this number ; right shift i ; function to find combined sum of all individual subsequence sum ; length of string ; stores the combined ; 2 ^ n - 1 subsequences ; loop for all subsequences ; returns the combined sum ; 
def findSubSequence(s, num):
    res = 0
    i = 0
    while (num):
        if (num & 1):
            res += ord(s[i]) - ord('0')
        i += 1
        num = num >> 1
    return res


def combinedSum(s):
    n = len(s)
    c_sum = 0
    ran = (1 << n) - 1
    for i in range(ran + 1):
        c_sum += findSubSequence(s, i)
    return c_sum


if __name__ == "__main__":
    s = "123"
    print(combinedSum(s))

# Expected output:
24
# longest subsequence where each character occurs at least k times | python program to find the subsequence with each character occurring at least k times in string s ; function to find the subsequence ; taking an extra array to keep record for character count in s ; counting occurrences of all characters in str [ ] ; printing characters with count >= k in same order as they appear in str . ; 

MAX_CHAR = 26


def findSubsequence(stri, k):
    a = [0] * MAX_CHAR
    for i in range(len(stri)):
        a[ord(stri[i]) - ord('a')] += 1
    for i in range(len(stri)):
        if a[ord(stri[i]) - ord('a')] >= k:
            print(stri[i], end='')


k = 2
findSubsequence("geeksforgeeks", k)

# Expected output:
geeksgeeks
# google case of a given sentence | python3 program to convert a sentence to google case . ; empty strings ; convert input string to upper case ; checki if character is not a space and adding it to string w ; converting first character to lower case and subsequent initial letter of another word to lower case ; 
def convert(str):
    w = ""
    z = ""
    str = str .upper() + " "
    for i in range(len(str)):
        ch = str[i]
        if (ch != ' '):
            w = w + ch
        else:
            z = (z + (w[0]).lower() + w[1:len(w)] + " ")
            w = ""
    return z


if __name__ == '__main__':
    str = "I got intern at geeksforgeeks"
    print(convert(str))

# Expected output:
i gOT iNTERN aT gEEKSFORGEEKS
# encrypt string with product of number of vowels and consonants in substring of size k | isvowel ( ) is a function that returns true for a vowel and false otherwise . ; function to encrypt the string ; cv to count vowel cc to count consonants ; counting prefix count of vowel and prefix count of consonants ; generating the encrypted string . ; 
def isVowel(c):
    return (c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u')


def encryptString(s, n, k):
    cv = [0 for i in range(n)]
    cc = [0 for i in range(n)]
    if (isVowel(s[0])):
        cv[0] = 1
    else:
        cc[0] = 1
    for i in range(1, n):
        cv[i] = cv[i - 1] + isVowel(s[i])
        cc[i] = cc[i - 1] + (isVowel(s[i]) == False)
    ans = ""
    prod = 0
    prod = cc[k - 1] * cv[k - 1]
    ans += str(prod)
    for i in range(k, len(s)):
        prod = ((cc[i] - cc[i - k]) * (cv[i] - cv[i - k]))
        ans += str(prod)
    return ans


if __name__ == '__main__':
    s = "hello"
    n = len(s)
    k = 2
    print(encryptString(s, n, k))

# Expected output:
1101
# count occurrences of a word in string | python program to count the number of occurrence of a word in the given string ; split the string by spaces in a ; search for pattern in a ; if match found increase count ; 
def countOccurrences(str, word):
    a = str .split(" ")
    count = 0
    for i in range(0, len(a)):
        if (word == a[i]):
            count = count + 1
    return count


str = "GeeksforGeeks A computer science portal for geeks "
word = "portal"
print(countOccurrences(str, word))

# Expected output:
1
# program to find the initials of a name . | python3 program to print initials of a name ; split the string using ' space ' and print the first character of every word ; 
def printInitials(name):
    if (len(name) == 0):
        return
    words = name .split(" ")
    for word in words:
        print(word[0].upper(), end=" ")


if __name__ == '__main__':
    name = "prabhat kumar singh"
    printInitials(name)

# Expected output:
P K S
# permute a string by changing case | function to generate permutations ; number of permutations is 2 ^ n ; converting string to lower case ; using all subsequences and permuting them ; if j - th bit is set , we convert it to upper case ; printing current combination ; 
def permute(inp):
    n = len(inp)
    mx = 1 << n
    inp = inp .lower()
    for i in range(mx):
        combination = [k for k in inp]
        for j in range(n):
            if (((i >> j) & 1) == 1):
                combination[j] = inp[j].upper()
        temp = ""
        for i in combination:
            temp += i
        print(temp,)


permute("ABC")

# Expected output:
abc
Abc
aBc
ABc
abC
AbC
aBC
ABC
# print the string after the specified character has occurred given no . of times | function to print the string ; if given count is 0 print the given string and return ; start traversing the string ; increment occ if current char is equal to given character ; break the loop if given character has been occurred given no . of times ; print the string after the occurrence of given character given no . of times ; otherwise string is empty ; 
def printString(str, ch, count):
    occ, i = 0, 0
    if (count == 0):
        print(str)
    for i in range(len(str)):
        if (str[i] == ch):
            occ += 1
        if (occ == count):
            break
    if (i < len(str) - 1):
        print(str[i + 1:len(str) - i + 2])
    else:
        print("Empty string")


if __name__ == '__main__':
    str = "geeks for geeks"
    printString(str, 'e', 2)

# Expected output:
ks for geeks
# reverse vowels in a given string | utility function to check for vowel ; function to reverse order of vowels ; start two indexes from two corners and move toward each other ; swapping ; 
def isVowel(c):
    return (c == 'a' or c == 'A' or c == 'e' or c == 'E' or c ==
            'i' or c == 'I' or c == 'o' or c == 'O' or c == 'u' or c == 'U')


def reverseVowel(str):
    i = 0
    j = len(str) - 1
    while (i < j):
        if not isVowel(str[i]):
            i += 1
            continue
        if (not isVowel(str[j])):
            j -= 1
            continue
        str[i], str[j] = str[j], str[i]
        i += 1
        j -= 1
    return str


if __name__ == "__main__":
    str = "hello world"
    print(*reverseVowel(list(str)), sep="")

# Expected output:
hollo werld
# minimum number of palindromic subsequences to be removed to empty a binary string | a function to check if a string str is palindrome ; start from leftmost and rightmost corners of str ; keep comparing characters while they are same ; returns count of minimum palindromic subseuqnces to be removed to make string empty ; if string is empty ; if string is palindrome ; if string is not palindrome ; 
def isPalindrome(str):
    l = 0
    h = len(str) - 1
    while (h > l):
        if (str[l] != str[h]):
            return 0
        l = l + 1
        h = h - 1
    return 1


def minRemovals(str):
    if (str[0] == ''):
        return 0
    if (isPalindrome(str)):
        return 1
    return 2


print(minRemovals("010010"))
print(minRemovals("0100101"))

# Expected output:
1
2
# find the value of xxxx ... . . ( n times ) % m where n is large | iterative function to calculate ( x ^ y ) % p in o ( log y ) ; initialize result ; update x if it is >= p ; if y is odd , multiply x with result ; y must be even now y = y 2 ; function to return xxx ... . . ( n times ) % m ; return the mod by m of smaller numbers ; creating a string of n x 's ; converting the string to int and calculating the modulo ; checking the parity of n ; dividing the number into equal half ; utilizing the formula for even n ; dividing the number into equal half ; utilizing the formula for odd n ; 
def power(x, y, p):
    res = 1
    x = x % p
    while (y > 0):
        if (y and 1):
            res = (res * x) % p
        y = y >> 1
        x = (x * x) % p
    return res


def findModuloByM(X, N, M):
    if (N < 6):
        temp = chr(48 + X) * N
        res = int(temp) % M
        return res
    if (N % 2 == 0):
        half = findModuloByM(X, N // 2, M) % M
        res = (half * power(10, N // 2, M) + half) % M
        return res
    else:
        half = findModuloByM(X, N // 2, M) % M
        res = (half * power(10, N // 2 + 1, M) + half * 10 + X) % M
        return res


if __name__ == "__main__":
    X = 6
    N = 14
    M = 9
    print(findModuloByM(X, N, M))

# Expected output:
3
# check if two circles intersect such that the third circle passes through their points of intersections and centers | python3 program for the above approach ; structure of the circle ; utility function to check if given circles satisfy required criteria ; stores the distance between the centres of c1 and c2 ; stores the status if the given given criteria is satisfied or not ; if c1c2 is less than the sum of the radii of the first 2 circles ; if c3 is the midpoint of the centres at c1 and c2 ; mark flag true ; return flag ; function to check if the given circles satisfy required criteria ; check for the current combination of circles ; check for the next combination ; 

from math import sqrt


class circle:
    def __init__(self, a, b, c):
        self .x = a
        self .y = b
        self .r = c


def check(C):
    C1C2 = sqrt((C[1].x - C[0].x) * (C[1].x - C[0].x) +
                (C[1].y - C[0].y) * (C[1].y - C[0].y))
    flag = 0
    if (C1C2 < (C[0].r + C[1].r)):
        if ((C[0].x + C[1].x) == 2 * C[2].x and (C[0].y + C[1].y) == 2 * C[2].y):
            flag = 1
    return flag


def IsFairTriplet(c):
    f = False
    f |= check(c)
    for i in range(2):
        c[0], c[2] = c[2], c[0]
        f |= check(c)
    return f


if __name__ == '__main__':
    C = [circle(0, 0, 0)for i in range(3)]
    C[0] = circle(0, 0, 8)
    C[1] = circle(0, 10, 6)
    C[2] = circle(0, 5, 5)
    if (IsFairTriplet(C)):
        print("Yes")
    else:
        print("No")

# Expected output:
Yes
# program to find the eccentricity of a hyperbola | python3 program for the above approach ; function to find the eccentricity of a hyperbola ; stores the squared ratio of major axis to minor axis ; increment r by 1 ; return the square root of r ; 

import math


def eccHyperbola(A, B):
    r = B * B / A * A
    r += 1
    return math .sqrt(r)


if __name__ == "__main__":
    A = 3.0
    B = 2.0
    print(eccHyperbola(A, B))

# Expected output:
2.23606797749979
# calculate area of a cyclic quadrilateral with given side lengths | python3 program for the above approach ; function to find the area of cyclic quadrilateral ; stores the value of half of the perimeter ; stores area of cyclic quadrilateral ; return the resultant area ; 

from math import sqrt


def calculateArea(A, B, C, D):
    S = (A + B + C + D) // 2
    area = sqrt((S - A) * (S - B) * (S - C) * (S - D))
    return area


if __name__ == '__main__':
    A = 10
    B = 15
    C = 20
    D = 25
    print(round(calculateArea(A, B, C, D), 3))

# Expected output:
273.861
# calculate ratio of area of a triangle inscribed in an ellipse and the triangle formed by corresponding points on auxiliary circle | function to calculate ratio of a triangle inscribed in an ellipse to the triangle on the auxiliary circle ; stores the ratio of the semi - major to semi - minor axes ; print the ratio ; 
def triangleArea(a, b):
    ratio = b / a
    print(ratio)


if __name__ == "__main__":
    a = 1
    b = 2
    triangleArea(a, b)

# Expected output:
2.0
# program to find the excenters of a triangle | python3 program for the above approach ; function to calculate the distance between a pair of points ; function to calculate the coordinates of the excenters of a triangle ; length of the sides of the triangle ; stores the coordinates of the excenters of the triangle ; for i1 ; for i2 ; for i3 ; print the excenters of the triangle ; 

from math import sqrt


def distance(m, n, p, q):
    return (sqrt(pow(n - m, 2) + pow(q - p, 2) * 1.0))


def Excenters(x1, y1, x2, y2, x3, y3):
    a = distance(x2, x3, y2, y3)
    b = distance(x3, x1, y3, y1)
    c = distance(x1, x2, y1, y2)
    excenter = [[0, 0]for i in range(4)]
    excenter[1][0] = ((-(a * x1) + (b * x2) + (c * x3)) // (-a + b + c))
    excenter[1][1] = ((-(a * y1) + (b * y2) + (c * y3)) // (-a + b + c))
    excenter[2][0] = (((a * x1) - (b * x2) + (c * x3)) // (a - b + c))
    excenter[2][1] = (((a * y1) - (b * y2) + (c * y3)) // (a - b + c))
    excenter[3][0] = (((a * x1) + (b * x2) - (c * x3)) // (a + b - c))
    excenter[3][1] = (((a * y1) + (b * y2) - (c * y3)) // (a + b - c))
    for i in range(1, 4):
        print(int(excenter[i][0]), int(excenter[i][1]))


if __name__ == '__main__':
    x1 = 0
    x2 = 3
    x3 = 0
    y1 = 0
    y2 = 0
    y3 = 4
    Excenters(x1, y1, x2, y2, x3, y3)

# Expected output:
6 6
-3 3
2 -2
# program to find height of a trapezoid | python3 program to implement the above approach ; function to calculate height of the trapezoid ; apply heron 's formula ; calculate the area ; calculate height of trapezoid ; prthe height ; 

import math


def findHeight(p1, p2, b, c):
    a = max(p1, p2) - min(p1, p2)
    s = (a + b + c) // 2
    area = math .sqrt(s * (s - a) * (s - b) * (s - c))
    height = (area * 2) / a
    print("Height is: ", height)


p1 = 25
p2 = 10
a = 14
b = 13
findHeight(p1, p2, a, b)

# Expected output:
Height is:  11.2
# icositetragonal number | function to find icositetragonal number ; formula to calculate nth icositetragonal number ; 
def Icositetragonal_num(n):
    return (22 * n * n - 20 * n) / 2


n = 3
print(int(Icositetragonal_num(n)))
n = 10
print(int(Icositetragonal_num(n)))

# Expected output:
69
1000
# area of circle inscribed in a isosceles trapezoid | function to find area of circle inscribed in a trapezoid having non - parallel sides m , n ; radius of circle by the formula i . e . root ( m * n ) / 2 area of circle = ( 3.141 ) * ( r * * 2 ) ; 
def area_of_circle(m, n):
    square_of_radius = (m * n) / 4
    area = (3.141 * square_of_radius)
    return area


if __name__ == '__main__':
    n = 10
    m = 30
    print(area_of_circle(m, n))

# Expected output:
235.575
# area of equilateral triangle inscribed in a circle of radius r | function to find the area of equilateral triangle inscribed in a circle of radius r ; base and height of equilateral triangle ; area using base and height ; 
def area(R):
    base = 1.732 * R
    height = (3 / 2) * R
    area = ((1 / 2) * base * height)
    return area


if __name__ == '__main__':
    R = 7
    print(area(R))

# Expected output:
63.651
# area of largest circle that can be inscribed in a semicircle | function to find the area of the circle ; radius cannot be negative ; area of the largest circle ; 
def circlearea(R):
    if (R < 0):
        return -1
    a = (3.14 * R * R) / 4
    return a


if __name__ == "__main__":
    R = 2
    print(circlearea(R))

# Expected output:
3.14
# number of pairs of lines having integer intersection points | count number of pairs of lines having eger ersection po ; initialize arrays to store counts ; count number of odd and even pi ; count number of odd and even qi ; return the count of pairs ; 
def countPairs(P, Q, N, M):
    A = [0] * 2
    B = [0] * 2
    for i in range(N):
        A[P[i] % 2] += 1
    for i in range(M):
        B[Q[i] % 2] += 1
    return (A[0] * B[0] + A[1] * B[1])


P = [1, 3, 2]
Q = [3, 0]
N = len(P)
M = len(Q)
print(countPairs(P, Q, N, M))

# Expected output:
3
# maximum number of line intersections formed through intersection of n planes | function to count maximum number of intersections possible ; 
def countIntersections(n):
    return n * (n - 1) // 2


n = 3
print(countIntersections(n))

# Expected output:
3
# area of a regular pentagram | python3 implementation of the approach ; function to return the area of triangle bcd ; using golden ratio ; calculate area of triangle bcd ; return area of all 5 triangles are the same ; function to return the area of regular pentagon ; calculate the area of regular pentagon using above formula ; return area of regular pentagon ; function to return the area of pentagram ; area of a pentagram is equal to the area of regular pentagon and five times the area of triangle ; 

import math
PI = 3.14159


def areaOfTriangle(d):
    c = 1.618 * d
    s = (d + c + c) / 2
    area = math .sqrt(s * (s - c) * (s - c) * (s - d))
    return 5 * area


def areaOfRegPentagon(d):
    global PI
    cal = 4 * math .tan(PI / 5)
    area = (5 * d * d) / cal
    return area


def areaOfPentagram(d):
    return areaOfRegPentagon(d) + areaOfTriangle(d)


d = 5
print(areaOfPentagram(d))

# Expected output:
139.18735993498925
# angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given | python 3 program to find the angle subtended by the chord to the centre of the circle when the angle subtended by another equal chord of a congruent circle is given ; 
def anglequichord(z):
    print("The angle is ", z, " degrees")


if __name__ == "__main__":
    z = 48
    anglequichord(z)

# Expected output:
The angle is  48  degrees
# program to print ascii value of all digits of a given number | function to convert digits of n to respective ascii values ; 
def convertToASCII(N):
    num = str(N)
    i = 0
    for ch in num:
        print(ch, "(", ord(ch), ")")


N = 36
convertToASCII(N)

# Expected output:
3 ( 51 )
6 ( 54 )
# a product array puzzle | set 3 | python 3 program for the above approach ; function to form product array with o ( n ) time and o ( 1 ) space ; stores the product of array ; stores the count of zeros ; traverse the array ; if arr [ i ] is not zero ; if arr [ i ] is zero then increment count of z by 1 ; stores the absolute value of the product ; if z is equal to 1 ; if arr [ i ] is not zero ; else ; if count of 0 s at least 2 ; assign arr [ i ] = 0 ; store absolute value of arr [ i ] ; find the value of a / b ; if arr [ i ] and product both are less than zero ; if arr [ i ] and product both are greater than zero ; else ; traverse the array arr [ ] ; 

import math


def productExceptSelf(arr, N):
    product = 1
    z = 0
    for i in range(N):
        if (arr[i] != 0):
            product *= arr[i]
        if (arr[i] == 0):
            z += 1
    a = abs(product)
    for i in range(N):
        if (z == 1):
            if (arr[i] != 0):
                arr[i] = 0
            else:
                arr[i] = product
            continue
        elif (z > 1):
            arr[i] = 0
            continue
        b = abs(arr[i])
        curr = round(math .exp(math .log(a) - math .log(b)))
        if (arr[i] < 0 and product < 0):
            arr[i] = curr
        elif (arr[i] > 0 and product > 0):
            arr[i] = curr
        else:
            arr[i] = -1 * curr
    for i in range(N):
        print(arr[i], end=" ")


arr = [10, 3, 5, 6, 2]
N = len(arr)
productExceptSelf(arr, N)

# Expected output:
180 600 360 300 900
# count subarrays made up of single | function to count of subarrays made up of single digit integers only ; stores count of subarrays ; stores the count of consecutive single digit numbers in the array ; traverse the array ; increment size of block by 1 ; increment res by count ; assign count = 0 ; 
def singleDigitSubarrayCount(arr, N):
    res = 0
    count = 0
    for i in range(N):
        if (arr[i] <= 9):
            count += 1
            res += count
        else:
            count = 0
    print(res)


if __name__ == '__main__':
    arr = [0, 1, 14, 2, 5]
    N = len(arr)
    singleDigitSubarrayCount(arr, N)

# Expected output:
6
# count integers up to n which can be represented as sum of two or more consecutive numbers | function to check if the number n can be expressed as sum of 2 or more consecutive numbers or not ; function to count integers in the range [ 1 , n ] that can be expressed as sum of 2 or more consecutive numbers ; stores the required count ; 
def isPossible(N):
    return ((N & (N - 1)) and N)


def countElements(N):
    count = 0
    for i in range(1, N + 1):
        if (isPossible(i)):
            count += 1
    print(count)


if __name__ == '__main__':
    N = 15
    countElements(N)

# Expected output:
11
# count integers up to n which can be represented as sum of two or more consecutive numbers | function to count integers in the range [ 1 , n ] that can be expressed as sum of 2 or more consecutive numbers ; count powers of 2 up to n ; increment count ; update current power of 2 ; 
def countElements(N):
    Cur_Ele = 1
    Count = 0
    while (Cur_Ele <= N):
        Count += 1
        Cur_Ele = Cur_Ele * 2
    print(N - Count)


if __name__ == '__main__':
    N = 15
    countElements(N)

# Expected output:
11
# maximum difference between a pair of adjacent elements by excluding every element once | python 3 program for the above approach ; function to calculate maximum difference between adjacent elements excluding every array element once ; compute maximum adjacent difference for whole array ; store the maximum between arr_max and curr_max ; append the result into a vector ; print the result ; 

import sys


def maxAdjacent(arr, N):
    res = []
    arr_max = -sys .maxsize - 1
    for i in range(1, N):
        arr_max = max(arr_max, abs(arr[i - 1] - arr[i]))
    for i in range(1, N - 1):
        curr_max = abs(arr[i - 1] - arr[i + 1])
        ans = max(curr_max, arr_max)
        res .append(ans)
    for x in res:
        print(x, end=" ")
    print()


if __name__ == "__main__":
    arr = [1, 3, 4, 7, 8]
    N = len(arr)
    maxAdjacent(arr, N)

# Expected output:
3 4 4