From 4c20a58a8fab41bbf6d1087bd5ff5d6684ab292d Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Tue, 16 May 2023 18:15:58 -0700 Subject: [PATCH 1/4] adds solve for prob 1 --- challenges.md | 44 ++++++++++++++++++++++---------------------- solves.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 solves.py diff --git a/challenges.md b/challenges.md index 0f9db02..6dfb0d3 100644 --- a/challenges.md +++ b/challenges.md @@ -3,7 +3,7 @@ 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. @@ -11,7 +11,7 @@ You are given a range of values, and you must write an algorithm to guess a rand 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: ``` @@ -19,7 +19,7 @@ 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. @@ -32,7 +32,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 @@ -46,7 +46,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 @@ -61,7 +61,7 @@ palindrome("abcdefg") === false ``` --- - +# 6 Directions: Given a string, return the character that is most @@ -75,7 +75,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 @@ -91,7 +91,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 @@ -104,7 +104,7 @@ greedy(5) --> 1 `(1 nickle)` ``` --- - +# 9 Directions: Given an integer (either positive or negative), reverse the number and keep it's sign. @@ -118,7 +118,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' @@ -132,32 +132,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. ``` @@ -166,13 +166,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). ``` @@ -182,22 +182,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. --- diff --git a/solves.py b/solves.py new file mode 100644 index 0000000..e99069d --- /dev/null +++ b/solves.py @@ -0,0 +1,42 @@ +''' +# 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 + + +''' +# 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 + # Outer loop to iterate through the outer array + # inner loop to iterate through each ele in array + # add each ele to empty array + # return new array \ No newline at end of file From 85e456708a5c798982b1cf762e9650a20c732675 Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Tue, 16 May 2023 21:20:18 -0700 Subject: [PATCH 2/4] adds solve for prob 3 --- solves.py | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/solves.py b/solves.py index e99069d..33977d9 100644 --- a/solves.py +++ b/solves.py @@ -29,6 +29,9 @@ def binary_search(arr, target): # 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: @@ -36,7 +39,45 @@ def binary_search(arr, target): ''' 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 - # return new array \ No newline at end of file + 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("sovle 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("sovle 3.2: ", day_of_the_week("TUESDAY", 2, 4)) +# Today is Friday - What day of the week will it be in 294 days? +print("sovle 3.3: ", day_of_the_week("friday", 294, 0)) +# Bonus: What month and day is it 73 days after October 31st 2018 \ No newline at end of file From 3b3181836c635cfba3ed248a0139e9b7a65f26e3 Mon Sep 17 00:00:00 2001 From: Heiner000 Date: Tue, 16 May 2023 22:18:45 -0700 Subject: [PATCH 3/4] adds solve # 6 --- solves.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/solves.py b/solves.py index 33977d9..d59d0fa 100644 --- a/solves.py +++ b/solves.py @@ -75,9 +75,73 @@ def day_of_the_week(day, days_later, weeks_later): return days[new_day_index] # Today is Sunday - what day of the week will it be in 100 days? -print("sovle 3.1: ", day_of_the_week("Monday", 100, 0)) +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("sovle 3.2: ", day_of_the_week("TUESDAY", 2, 4)) +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("sovle 3.3: ", day_of_the_week("friday", 294, 0)) -# Bonus: What month and day is it 73 days after October 31st 2018 \ No newline at end of file +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]] +''' \ No newline at end of file From 518f3db985ec39c89d14ebf269950a41669571b6 Mon Sep 17 00:00:00 2001 From: Garrett Heiner <64627881+Heiner000@users.noreply.github.com> Date: Thu, 1 Jun 2023 21:21:03 -0700 Subject: [PATCH 4/4] starts javascript solves --- challenges.md | 1 - solves.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++ whiteboarding.txt | 0 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 solves.js create mode 100644 whiteboarding.txt diff --git a/challenges.md b/challenges.md index 6dfb0d3..292adbc 100644 --- a/challenges.md +++ b/challenges.md @@ -13,7 +13,6 @@ 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] diff --git a/solves.js b/solves.js new file mode 100644 index 0000000..c42d790 --- /dev/null +++ b/solves.js @@ -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]])) \ No newline at end of file diff --git a/whiteboarding.txt b/whiteboarding.txt new file mode 100644 index 0000000..e69de29