<repo_name>bariiscicek/hackerrank-solutions<file_sep>/between-two-sets/betweenTwoSets.py
import math
import os
import random
import re
import sys

#
# Complete the 'getTotalX' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER_ARRAY a
#  2. INTEGER_ARRAY b
#

def getTotalX(a, b):
    # Write your code here
    if max(a)==min(b):
        return 1
    elif max(a)>min(b):
        return 0
    else:
        out=0;
        for i in range(max(a),min(b)+1):
            status=1
            for j in range(len(a)):
                if i%a[j]!=0:
                    status=0
            for j in range(len(b)):
                if b[j]%i!=0:
                    status=0
            if status==1:
                out=out+1
        return out



if __name__ == '__main__':
    arr=[2,4]
    brr=[16,32,96]

    total = getTotalX(arr, brr)

    print(total)
<file_sep>/climbing-the-leaderboard/climbingTheLeaderboard.py
import sys

# Complete the climbingLeaderboard function below.
def climbingLeaderboard(scores, alice):
    scores=list(dict.fromkeys(scores))
    ind=len(scores)-1
    k=len(alice)
    tres=scores[0]
    out=[]
    for i in range(k):
        status=1
        while status==1:
            if alice[i]>=tres:
                out.append(1)
                status=0
            else:
                if alice[i]<scores[ind]:
                    status=0
                    out.append(ind+2)

                else:
                    ind=ind-1
    return out





if __name__ == '__main__':
    scores=[100,100,50,40,40,20,10]
    alice=[5,25,50,120]

    result = climbingLeaderboard(scores, alice)

    print(result)
<file_sep>/angry-professor/angryProfessor.py
import math
import os
import random
import re
import sys


def angryProfessor(k, a):
    leng=len(a)
    check=0
    for i in range(leng):
        if a[i]<=0:
            check=check+1
    if check>=k:
        out="NO"
    else:
        out="YES"
    return out

if __name__ == '__main__':
    a=[-1,-2,-3,5,6,7]
    k=2
    print(angryProfessor(k,a))
<file_sep>/magic-square-forming/magicSquareForming.py

import math
import os
import random
import re
import sys

def formingMagicSquare(s):
    q = [0,0,0,0,0,0,0,0,0]
    ind=0
    for i in range(3):
        for j in range(3):
            q[ind]=s[i][j]
            ind=ind+1

    sol=[]
    sol.append([2,7,6,9,5,1,4,3,8])
    sol.append([2,9,4,7,5,3,6,1,8])
    sol.append([4,3,8,9,5,1,2,7,6])
    sol.append([4,9,2,3,5,7,8,1,6])
    sol.append([6,1,8,7,5,3,2,9,4])
    sol.append([6,7,2,1,5,9,8,3,4])
    sol.append([8,1,6,3,5,7,4,9,2])
    sol.append([8,3,4,1,5,9,6,7,2])
    minval=100000

    for i in sol:
        dst=0
        for j in range(9):
            dst=dst+abs(q[j]-i[j])
        if dst<minval:
            minval=dst
    return minval







s=[
[1,2,3],
[4,5,6],
[7,8,9]]

result = formingMagicSquare(s)
print(result)
<file_sep>/maximize-it/maximizeIt.py
from ortools.sat.python import cp_model


def maximizeIt(m,lists):
    model=cp_model.CpModel()
    k=model.NewIntVar(0,100,'k')
    x=[]
    for i in range(len(lists)):
        temp=[]
        for j in range(len(lists[i])):
            temp.append(model.NewIntVar(0, 1, 'x'))
        x.append(temp)
    for i in range(len(lists)):
        sum=0
        for j in range(len(lists[i])):
            sum=sum+x[i][j]
        model.Add(sum==1)
    obj=0
    for i in range(len(lists)):

        for j in range(len(lists[i])):
            obj=obj+x[i][j]*lists[i][j]**2
    model.Add(obj-m*k>=0)
    model.Maximize(1*(obj-m*k)+200*k)

    solver = cp_model.CpSolver()
    status = solver.Solve(model)

    return solver.ObjectiveValue()


m=100
lists=[]
lists.append([1,2,3,6,8])
lists.append([5,5,7,6])

print(maximizeIt(m,lists))
<file_sep>/saveprincess2/savePrincess2.py
import random
def find_str(s, char):
    index = 0

    if char in s:
        c = char[0]
        for ch in s:
            if ch == c:
                if s[index:index+len(char)] == char:
                    return index

            index += 1

    return -1


def nextMove(n,r,c,grid):
    for i in range(n):
        check=find_str(grid[i],"m")
        if check >=0:
            index1=i
            index2=check
    for i in range(n):
        check=find_str(grid[i],"p")
        if check >=0:
            indexx1=i
            indexx2=check
    dif1=index1-indexx1
    dif2=index2-indexx2
    if dif1 != 0:
        if dif1<0:
            return "DOWN"
        elif dif1>0:
            return "UP"
    elif dif2 != 0:
        if dif2<0:
            return "RIGHT"
        elif dif2>0:
            return "LEFT"


n=5
r=2
c=3
px=random.randint(0,4)
py=random.randint(0,4)
grid=[]
for i in range(n):
    inp=''
    for j in range(n):
        if i==r and j==c:
            inp=inp+'m'
        elif i==px and j==py:
            inp=inp+'p'
        else:
            inp=inp+'-'
    grid.append(inp)
print(grid)




print(nextMove(n,r,c,grid))
