diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..f54aedba Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..9bea4330 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +.DS_Store diff --git a/CountingValleys/countingvalleys.py b/CountingValleys/countingvalleys.py new file mode 100644 index 00000000..2468381d --- /dev/null +++ b/CountingValleys/countingvalleys.py @@ -0,0 +1,65 @@ +#!/bin/python3 +# https://www.hackerrank.com/challenges/counting-valleys/problem + +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): + elev = 0 + ctr_val = 0 + flg_start = True + flg_inval = False + + # Iterate through the path string + for stp in path: + # What kind of step are we taking? + if stp == "U": + adj = 1 + if stp == "D": + adj = -1 + + # Are we at sea level? + if elev == 0: + # Have just started? + if flg_start: + # yes: adjust the elevation + elev = elev + adj + flg_start = False + continue + + # Are we coming out of a valley? + if adj == 1 and elev == -1: # stepping up to sea level + ctr_val = ctr_val + 1 + elev = elev + adj + continue + + # Adjust the elevation given the step type ("up" or "down") + elev = elev + adj + + return ctr_val + + +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() diff --git a/ImplementQueueUsingStacks/implement-queue-using-stacks.py b/ImplementQueueUsingStacks/implement-queue-using-stacks.py new file mode 100644 index 00000000..8fabb2bf --- /dev/null +++ b/ImplementQueueUsingStacks/implement-queue-using-stacks.py @@ -0,0 +1,130 @@ + +def foo(): + return + +class MyQueue: + + def __init__(self): + """ + Initialize your data structure here. + """ + self.stack_A = list() + self.stack_B = list() + + def stack_A_push(self, val): + """ + push value onto stack A + """ + if val != None: + self.stack_A.insert(0, val) + + def stack_A_pop(self): + """ + pop and return value from stack A + """ + if len(self.stack_A) == 0: + return None + + ret_elm = self.stack_A[0] + del self.stack_A[0] + return ret_elm + + def stack_A_peek(self): + """ + return value from stack A (but don't pop) + """ + if len(self.stack_A) == 0: + return None + + ret_elm = self.stack_A[0] + return ret_elm + + def stack_B_push(self, val): + """ + push value onto stack B + """ + if val != None: + self.stack_B.insert(0, val) + + def stack_B_pop(self): + """ + pop and return value from stack A + """ + if len(self.stack_B) == 0: + return None + + ret_elm = self.stack_B[0] + del self.stack_B[0] + return ret_elm + + def stack_A2stack_B(self): + """ + pops all stack A elements and + pushes them onto stack B + """ + tmp = self.stack_A_pop() + while tmp != None: + self.stack_B_push(tmp) + tmp = self.stack_A_pop() + + return + + def stack_B2stack_A(self): + """ + pops all stack B elements and + pushes them onto stack A + """ + tmp = self.stack_B_pop() + while tmp != None: + self.stack_A_push(tmp) + tmp = self.stack_B_pop() + + return + + def push(self, x): + """ + Push element x to the back of queue. + """ + # Push all Stack A elements onto Stack B + self.stack_A2stack_B() + # Push x onto Stack A + self.stack_A_push(x) + # Push all Stack B elements onto Stack A + self.stack_B2stack_A() + + return + + def pop(self): + """ + Removes the element from in front of queue and returns that element. + """ + return self.stack_A_pop() + + def peek(self): + """ + Get the front element. + """ + return self.stack_A_peek() + + def empty(self): + """ + Returns whether the queue is empty. + """ + if len(self.stack_A) == 0: + return True + + return False + + +my_queue = MyQueue() +my_queue.push(1) +is_e = my_queue.empty() +my_queue.push(2) +is_e = my_queue.empty() +my_queue.pop() +is_e = my_queue.empty() +my_queue.pop() +is_e = my_queue.empty() + +quit() + diff --git a/JumpingOnTheClouds/main.go b/JumpingOnTheClouds/main.go new file mode 100644 index 00000000..2f67d851 --- /dev/null +++ b/JumpingOnTheClouds/main.go @@ -0,0 +1,98 @@ +// https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem +package main + +import ( + "bufio" + "fmt" + "io" + "os" + "strconv" + "strings" +) + +// Complete the jumpingOnClouds function below. +func jumpingOnClouds(c []int32) int32 { + cntHops := 0 + cldNextNext := 0 + idx := 0 + + // Iterate through the + for idx < len(c) { + fmt.Println("Hop:", idx) + + // Are we on the last cloud? + if idx == len(c)-1 { + // yes: done processing + break + } + + // Determine next two cloud indexes (numbers) + cldNextNext = -999 + if idx < len(c)-2 { + // Determine the next-next cloud if it exists + cldNextNext = idx + 2 + } + + // Is next-next cloud a cumulus cloud? + if cldNextNext != -999 && c[cldNextNext] == 0 { + // yes: the next-next cloud exists and is "safe" + cntHops = cntHops + 1 // increase cloud hop by 1 + idx = cldNextNext + continue + } + + // Only option is to jump on the next cloud (assume "safe") + cntHops = cntHops + 1 // increase cloud hop by 1 + idx = idx + 1 // shift to the next-next cloud + } + + // return the number of hops + return int32(cntHops) +} + +func main() { + reader := bufio.NewReaderSize(os.Stdin, 1024*1024) + + stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) + checkError(err) + + defer stdout.Close() + + writer := bufio.NewWriterSize(stdout, 1024*1024) + + nTemp, err := strconv.ParseInt(readLine(reader), 10, 64) + checkError(err) + n := int32(nTemp) + + cTemp := strings.Split(readLine(reader), " ") + + var c []int32 + + for i := 0; i < int(n); i++ { + cItemTemp, err := strconv.ParseInt(cTemp[i], 10, 64) + checkError(err) + cItem := int32(cItemTemp) + c = append(c, cItem) + } + + result := jumpingOnClouds(c) + + fmt.Fprintf(writer, "%d\n", result) + + writer.Flush() +} + +func readLine(reader *bufio.Reader) string { + str, _, err := reader.ReadLine() + if err == io.EOF { + return "" + } + + return strings.TrimRight(string(str), "\r\n") +} + +func checkError(err error) { + if err != nil { + panic(err) + } +} diff --git a/RepeatedString/repeatedstring.py b/RepeatedString/repeatedstring.py new file mode 100644 index 00000000..ad12313d --- /dev/null +++ b/RepeatedString/repeatedstring.py @@ -0,0 +1,38 @@ +#!/bin/python3 +# https://www.hackerrank.com/challenges/repeated-string/problem + +import math +import os +import random +import re +import sys + +# Complete the repeatedString function below. +def repeatedString(s, n): + num_a_str = s.count("a") # of times "a" is found in s + num_whole_str = int(n/len(s)) # of times s is repeated in a repeating + # string of length n + + num_rem_chrs = int(n%len(s)) # of characters in the string "remander" + num_a_rem = s[:num_rem_chrs].count("a") # of times "a" is found in remainder + # substring of s + + # Return the total number of "a"'s found which is equal to: + # 1. # of a's in the set of repeating strings + # 2. # of a's in the remainder substring + total_num_a = num_a_str*num_whole_str + num_a_rem + + return total_num_a + +if __name__ == '__main__': + fptr = open(os.environ['OUTPUT_PATH'], 'w') + + s = input() + + n = int(input()) + + result = repeatedString(s, n) + + fptr.write(str(result) + '\n') + + fptr.close() diff --git a/SalesByMatch/salesbymatch.py b/SalesByMatch/salesbymatch.py new file mode 100644 index 00000000..42d0cc72 --- /dev/null +++ b/SalesByMatch/salesbymatch.py @@ -0,0 +1,44 @@ +#!/bin/python3 +# https://www.hackerrank.com/challenges/sock-merchant/problem + +import math +import os +import random +import re +import sys + +# Complete the sockMerchant function below. +def sockMerchant(n, ar): + mp_sock = {} + ctr_pair = 0 + + # Iterate through the passed array + for clr in ar: + # Is this sock color in our working map? + if clr not in mp_sock.keys(): + # No: add the sock color as a key + mp_sock[clr] = 1 + continue + + # Color is already in the map - we have now found a sock pair + # Increase the pair count + ctr_pair = ctr_pair + 1 + + # Delete the current color key - so we can identify the next pair + del mp_sock[clr] + + # Done iterating... return the current counter value + return ctr_pair + +if __name__ == '__main__': + fptr = open(os.environ['OUTPUT_PATH'], 'w') + + n = int(input()) + + ar = list(map(int, input().rstrip().split())) + + result = sockMerchant(n, ar) + + fptr.write(str(result) + '\n') + + fptr.close() diff --git a/TwoSum/twosum.py b/TwoSum/twosum.py new file mode 100644 index 00000000..100c193c --- /dev/null +++ b/TwoSum/twosum.py @@ -0,0 +1,54 @@ +# https://leetcode.com/problems/two-sum +class Solution: + def twoSum(self, nums, target): + # Create working variables + wrk_dict = {} + + # Iterate through the list of integers + for idx, val in enumerate(nums): + # Insert value into the working dict + + # Is the current value already in our dict? + if val not in wrk_dict: + # no: insert the value associate with a list + # containing the value's index in the list + wrk_dict[val] = [idx] + continue + + # Current value already exists, append this index + wrk_dict[val].append(idx) + + # Iterate through the dictionary's keys + for ky in wrk_dict.keys(): + # Calculate the difference between the target and current key + diff = target - ky + + # Is the difference equal to the key (e.g. ky + ky = target)? + if diff == ky: + # yes: must have dupes, return multiple indices associated with + # this list value + if len(wrk_dict[ky]) == 1: + # the difference is equal to the key but only value found... keep processing + continue + + # return first two index values + return wrk_dict[ky][0:2] + + # Is the diff in the dict? + if diff in wrk_dict: + # yes: return a list that includes the indices of the + # two keys + ret_lst = [wrk_dict[ky][0]] + ret_lst.append(wrk_dict[diff][0]) + return ret_lst + + # Finished iterating: no solution found + return [] + + +my_solv = Solution() + +rslt = my_solv.twoSum([3,2,4], 6) +print(rslt) + + \ No newline at end of file