Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS BW2 - Dan Anderson - CSPT10 / DSPT4 #50

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.DS_Store
65 changes: 65 additions & 0 deletions CountingValleys/countingvalleys.py
Original file line number Diff line number Diff line change
@@ -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()
130 changes: 130 additions & 0 deletions ImplementQueueUsingStacks/implement-queue-using-stacks.py
Original file line number Diff line number Diff line change
@@ -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()

98 changes: 98 additions & 0 deletions JumpingOnTheClouds/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
38 changes: 38 additions & 0 deletions RepeatedString/repeatedstring.py
Original file line number Diff line number Diff line change
@@ -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()
44 changes: 44 additions & 0 deletions SalesByMatch/salesbymatch.py
Original file line number Diff line number Diff line change
@@ -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()
Loading