Grace Jeung
2 min readJul 15, 2023

HackerRank Interview Preparation Kit | Warm-up

1. Sales by Match

Solution:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'sockMerchant' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER n
# 2. INTEGER_ARRAY ar
#

def sockMerchant(n, ar):
# Write your code here
pair =0
indep_ar = set(ar)
for i in indep_ar:
pair += ar.count(i)//2
return pair

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

n = int(input().strip())

ar = list(map(int, input().rstrip().split()))

result = sockMerchant(n, ar)

fptr.write(str(result) + '\n')

fptr.close()

2. Counting Valleys

Solution:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'countingValleys' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER steps
# 2. STRING path
#

def countingValleys(steps, path):
# Write your code here
valley = 0
seaL = 0
for i in range (0,steps):
if (path[i] == 'U'):
seaL += 1
elif (path[i] == 'D'):
seaL -= 1
if (seaL == 0 and path[i] == 'U'):
valley += 1
return (valley)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

steps = int(input().strip())

path = input()

result = countingValleys(steps, path)

fptr.write(str(result) + '\n')

fptr.close()

3. Jumping on the Clouds

Solution:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'jumpingOnClouds' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY c as parameter.
#

def jumpingOnClouds(c):
# Write your code here
jump=0
start=0
while start<len(c):
if start+2<len(c) and c[start+2]==0:
start+=2
jump+=1
else:
start+=1
jump+=1
return jump-1

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

n = int(input().strip())

c = list(map(int, input().rstrip().split()))

result = jumpingOnClouds(c)

fptr.write(str(result) + '\n')

fptr.close()

4. Repeated String

Solution:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'repeatedString' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts following parameters:
# 1. STRING s
# 2. LONG_INTEGER n
#

def repeatedString(s, n):
# Write your code here
return s.count('a') * int(n / len(s)) + s[:n % len(s)].count('a')


if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

s = input()

n = int(input().strip())

result = repeatedString(s, n)

fptr.write(str(result) + '\n')

fptr.close()
Grace Jeung
Grace Jeung

No responses yet