Created
January 3, 2022 10:18
-
-
Save mdpabel/03f36e9f5f03a03cbbc602d762549b13 to your computer and use it in GitHub Desktop.
Hacker-rank-warm-up-challenges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
# | |
""" | |
valley -> sea level(d) to sea level | |
U D D D U D U U | |
+1 -1 -1 -1 +1 -1 +1 +1 | |
+1 0 -1 -2 -1 -2 -1 0 => st 0 to en 0 && st d | |
""" | |
def countingValleys(steps, path): | |
# Write your code here | |
i = 0 | |
flag = 0 | |
count = 0 | |
while(i < steps): | |
if(path[i] =='D' and flag == 0): | |
count += 1 | |
if(path[i] == 'U'): | |
flag += 1 | |
else: | |
flag -= 1 | |
i += 1 | |
return count | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
n = len(c) | |
count = 0 | |
i = 0 | |
while i < n-1: | |
if i+2 >= n or c[i+2] == 1: | |
i +=1 | |
count +=1 | |
else: | |
i +=2 | |
count +=1 | |
return count | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
# | |
""" | |
abcac abc ac abcac | |
8 / 5 | |
""" | |
def repeatedString(s, n): | |
# Write your code here | |
lenS = len(s) | |
total_repeat = math.floor(n / lenS) | |
total_sub = n % lenS | |
count = 0 | |
i = 0 | |
while(i < lenS): | |
if(s[i] == 'a'): | |
count += 1 | |
i += 1 | |
subCount = 0 | |
j = 0 | |
while(j < total_sub): | |
if(s[j] == 'a'): | |
subCount += 1 | |
j += 1 | |
return count * total_repeat + subCount | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
# | |
""" | |
1 2 1 2 1 3 2 | |
1 1 1 2 2 2 3 - sorted | |
arr[i] == arr[i + 1] | |
i = 0; => 1 | |
i = 2 => 0 | |
i = 3 => 1 | |
i = 5 => 0 | |
i = 6 | |
""" | |
def sockMerchant(n, ar): | |
ar.sort() | |
i = 0 | |
count = 0; | |
while i < n -1: | |
if(ar[i] == ar[i+1]): | |
count += 1 | |
i += 2 | |
else: | |
i += 1 | |
return count | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment