Skip to content
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
45 changes: 22 additions & 23 deletions challenges.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
this is a list of easy - medium coding challenges in no partiuclar order

---

# 1
Most effecient way to guess a random number in a range:

You are given a range of values, and you must write an algorithm to guess a random number in the range in the most effecient way possible.

After every time you guess, you're told if you're right, too high, or too low.

---

# 2
Write an algorithm to flatten a multi-dimensional array:

```
vec = [[1,2,3], [4,5,6], [7,8,9]]
=> [1,2,3,4,5,6,7,8,9]
```
---

# 3
Write a program that prints the day of the week given a number of days and weeks.

Example: 30 days from Monday is Wednesday.
Expand All @@ -32,7 +31,7 @@ Answer the following questions:
* Bonus: What month and day is it 73 days after October 31st 2018?

---

# 4
Directions:
Given a string, return a new string with the reversed
order of characters
Expand All @@ -46,7 +45,7 @@ reverse('Greetings!') === '!sgniteerG'
```

---

# 5
Directions:
Given a string, return true if the string is a palindrome
or false if it is not. Palindromes are strings that
Expand All @@ -61,7 +60,7 @@ palindrome("abcdefg") === false
```

---

# 6
Directions:

Given a string, return the character that is most
Expand All @@ -75,7 +74,7 @@ maxChar("apple 1231111") === "1"
```

---

# 7
Directions:
Given an array and chunk size, divide the array into many subarrays
where each subarray is of length size
Expand All @@ -91,7 +90,7 @@ chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]
```

---

# 8
Direction:

Given an amount of change, determine the minimum number of coins required to make that change
Expand All @@ -104,7 +103,7 @@ greedy(5) --> 1 `(1 nickle)`
```

---

# 9
Directions:

Given an integer (either positive or negative), reverse the number and keep it's sign.
Expand All @@ -118,7 +117,7 @@ reverseInt(601) --> 106
```

---

# 10
Directions:

Write a function that returns the number of vowels used in a string. Vowels are the characters 'a', 'e'
Expand All @@ -132,32 +131,32 @@ vowels('Why?') --> 0
```

---

# 11
reverse an array in place (in place means do not make a new array in order to reverse given array) without using builtin method for reversal (example not `.reverse` in python)

---

# 12
implement a forEach (a function that works like Array.forEach() -- accepts a callback and invokes it on every element in an array)

---

# 13
Given an array of integers, find the one that appears an odd number of times

---

# 14
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.

Bonus: solve recursively

---

# 15
Write a function that takes in a list of numbers and gives you back a tuple or list with two items: the sum of all negative numbers in the list, and the count of all positive numbers.

For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65]. (There are 10 positive numbers, and the negative numbers add up to -65.)

---

# 16
Implement a function unique_in_order that will take in an argument either a string or an array and return a list of elements without any elements with the same # values next to each other, but preserving the original order.

```
Expand All @@ -166,13 +165,13 @@ unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
unique_in_order([1,2,2,3,3]) == [1,2,3]
```
---

# 17
Given an array of integers as strings and numbers, return the sum of the array values as if all were numbers.

Return your answer as a number.

---

# 18
Complete a method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case).

```
Expand All @@ -182,22 +181,22 @@ Examples
```

---

# 19
Given an array of integers, find the one that appears an odd number of times.

There will always be only one integer that appears an odd number of times.

---

# 20
Complete the solution so that it splits the string into pairs of two characters.
If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_').

---

# 21
given an array and a total, return true if one or more consecutive numbers in the array sum up to that total

---

# 22
Write a function that takes an array as a parameter and returns an array with the elements in a random order.

---
Expand Down
53 changes: 53 additions & 0 deletions solves.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* # 1
Most effecient way to guess a random number in a range:

You are given a range of values, and you must write an algorithm to guess a random number in the range in the most effecient way possible.

After every time you guess, you're told if you're right, too high, or too low. */

const binarysearch = (arr, target) => {
let iterations = 0
// define some indices
let low = 0
let high = arr.length - 1

while (low <= high) {
iterations++
let middle = Math.floor((low + high) / 2)
// check if target is greater than arr at middle index
if (arr[middle] === target) {
return {index: middle, iterations: iterations}
} else if (arr[middle] < target) {
// if true move window to right side
low = middle + 1
} else { // move window to the left side
high = middle - 1
}
}
return {index: -1, iterations: iterations}
}

const arr = [1, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

console.log("# 1. ", binarysearch(arr, 1))


/* # 2
Write an algorithm to flatten a multi-dimensional array: */

const flattener = (arr) => {
let iterations = 0
let newArr = []
// outer loop to iterate through outer array
for (let subArr of arr) {
// inner loop to iterate through each element in subArr
for (let ele of subArr) {
// add each element to new array
newArr.push(ele)
iterations++
}
}
return {newArr: newArr, iterations: iterations}
}

console.log("# 2. ", flattener([[1,2,3], [4,5,6], [7,8,9]]))
147 changes: 147 additions & 0 deletions solves.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
'''
# 1 - Most efficient way to guess a random number in a range:
You are given a range of values, and you must write an algorithm to guess a random number in the range in the most efficient way possible.
After every time you guess, you're told if you're right, too high, or too low.
'''
def binary_search(arr, target):
# start with the lowest index for the search
low = 0
# start with the highest end of the range for our search
high = len(arr) - 1

# iterate through the array while lower index is still less than high index
while low <= high:
# find the mid index of the portion between low & high indices
mid = low + (high - low) // 2
# save guess to variable to check
guess = arr[mid]

# check guess against target
if guess == target:
return mid
# if guess was too low, adjust lower bound to be higher than mid
elif guess < target:
low = mid + 1
# if guess was too high, adjust higher bound to be lower than mid
else:
high = mid - 1

# if we finished iterating without answer, it's not there
return None

arr = [1,2,3,4,5,6,7,8,9]
print("solve 1: ", binary_search(arr, 8))


'''
# 2 - Write an algorithm to flatten a multi-dimensional array:
vec = [[1,2,3], [4,5,6], [7,8,9]] => [1,2,3,4,5,6,7,8,9]
'''
def two_d_flatten(arr):
# start with new empty array
flat_arr = []
# Outer loop to iterate through the outer array
for sub_arr in arr:
# inner loop to iterate through each ele in array
for ele in sub_arr:
# add each ele to empty array
flat_arr.append(ele)

# return new array
return flat_arr

vec = [[1,2,3], [4,5,6], [7,8,9]]
print("solve 2: ", two_d_flatten(vec))


'''
# 3 -- Write a program that prints the day of the week given a number of days and weeks.
For example: 30 days from Monday is Wednesday.

Answer the following:
'''

def day_of_the_week(day, days_later, weeks_later):
# check if weeks_later, then convert it to days change
if weeks_later > 0:
days_later += (7 * weeks_later)
#represent the days of the week as an arr
days = ["sunday","monday", "tuesday", "wednesday", "thursday","friday","saturday"]
# find the index of the day
day_index = days.index(day.lower())
# calculate the new day index
new_day_index = (days_later + day_index) % 7
# return days[new day index]
return days[new_day_index]

# Today is Sunday - what day of the week will it be in 100 days?
print("solve 3.1: ", day_of_the_week("Monday", 100, 0))
# Today is Tuesday - What day of the week will it be in 4 weeks and 2 days?
print("solve 3.2: ", day_of_the_week("TUESDAY", 2, 4))
# Today is Friday - What day of the week will it be in 294 days?
print("solve 3.3: ", day_of_the_week("friday", 294, 0))

# Bonus: What month and day is it 73 days after October 31st 2018
from datetime import datetime, timedelta
def add_days(start_date, days_to_add):
# parse the start date into a datetime object
start_date = datetime.strptime(start_date, "%B %d %Y")
# calculate new date
new_date = start_date + timedelta(days=days_to_add)
# return the new date as a string w/ m, d, y
return new_date.strftime("%m %d, %Y")
print("solve 3.bonus: ", add_days("October 31 2018", 73))

'''
# 4 -- Given a string, return a new string with the reversed order of characters
reverse('apple') === 'leppa'
reverse('hello') === 'olleh'
reverse('Greetings!') === '!sgniteerG'
'''
def reverse(str):
return ''.join(reversed(str))

print("solve 4: ", reverse('Greetings!'))


'''
# 5 -- Given a string, return true if the string is a palindrome or false if it is not. Do include spaces and punctuation in determining if the string is a palindrome.
palindrome("abba") === true
palindrome("abcdefg") === false
'''
def is_palindrome(str):
reversed_str = ''.join(reversed(str))
if str.lower() == reversed_str.lower():
return True
else:
return False

print("solve 5: ", is_palindrome("Abba"))

'''
# 6 -- Given a string, return the character that is most commonly used in the string
maxChar("abcccccccd") === "c"
maxChar("apple 1231111") === "1"
'''
def most_char(str):
char_dict = {}
for char in str:
if char in char_dict:
char_dict[char] += 1
else:
char_dict[char] = 1
# find the key with the max value in char_dict
max_char = max(char_dict, key=char_dict.get)
return max_char

print("solve 6: ", most_char("abbbbbcccccccd"))


'''
# 7 -- Given an array and chunk size, divide the array into many subarrays where each subarray is of length size
chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]
'''
Empty file added whiteboarding.txt
Empty file.