diff --git a/02-flatten.py b/02-flatten.py new file mode 100644 index 0000000..c940fb3 --- /dev/null +++ b/02-flatten.py @@ -0,0 +1,16 @@ +''' +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 flatten(list): + i = 1 + while i < len(list): + for j in list[i]: + list[0].append(j) + i += 1 + return list[0] + +print(flatten([[1,2,3], [4,5,6], [7,8,9], [10,11], [12, 13, 14] ])) \ No newline at end of file diff --git a/03-days.py b/03-days.py new file mode 100644 index 0000000..03295de --- /dev/null +++ b/03-days.py @@ -0,0 +1,24 @@ +''' + +Write a program that prints the day of the week given a number of days and weeks. + +Example: 30 days from Monday is Wednesday. + +Answer the following questions: + +* Today is Sunday - What day of the week will it be in 100 days? +* Today is Tuesday - What day of the week will it be in 4 weeks and 2 days? +* Today is Friday - What day of the week will it be in 294 days? +* Bonus: What month and day is it 73 days after October 31st 2018? + +''' + + +def weekday(beginning_day, add_days): + weekdays = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"] + index = int(weekdays.index(beginning_day)) + days = add_days % 7 + index2 = (index + days) % 7 + return weekdays[index2] + +print(weekday("saturday", 5)) \ No newline at end of file diff --git a/04-reverse.py b/04-reverse.py new file mode 100644 index 0000000..1af6f58 --- /dev/null +++ b/04-reverse.py @@ -0,0 +1,22 @@ +''' +Directions: +Given a string, return a new string with the reversed +order of characters + +Examples + +reverse('apple') === 'leppa' +reverse('hello') === 'olleh' +reverse('Greetings!') === '!sgniteerG' +''' + +# def reverse(word): +# new_string = "" +# for i in word: +# new_string = i + new_string +# return new_string + +def reverse(string): + return string[::-1] + +print(reverse("hello")) \ No newline at end of file diff --git a/05-palindrome.py b/05-palindrome.py new file mode 100644 index 0000000..4decc64 --- /dev/null +++ b/05-palindrome.py @@ -0,0 +1,27 @@ +''' +Directions: +Given a string, return true if the string is a palindrome +or false if it is not. Palindromes are strings that +form the same word if it is reversed. Do include spaces +and punctuation in determining if the string is a palindrome. + +Examples: + +palindrome("abba") === true +palindrome("abcdefg") === false + +''' + +# def palindrome(string): +# for i in range(len(string)): +# end = int(len(string) - i) - 1 +# if string[i] != string[end]: +# return False +# return True + +def palindrome(string): + return string == string[::-1] + +print(palindrome("tacocat")) +print(palindrome("racecar")) +print(palindrome("abcdefg")) \ No newline at end of file diff --git a/06-most_common.py b/06-most_common.py new file mode 100644 index 0000000..c776ef8 --- /dev/null +++ b/06-most_common.py @@ -0,0 +1,12 @@ +''' +Directions: + +Given a string, return the character that is most +commonly used in the string. + +Examples: + +maxChar("abcccccccd") === "c" +maxChar("apple 1231111") === "1" +''' + diff --git a/07-chunk_subarrays.py b/07-chunk_subarrays.py new file mode 100644 index 0000000..5a90e1e --- /dev/null +++ b/07-chunk_subarrays.py @@ -0,0 +1,44 @@ +''' +Directions: Given an array and chunk size, divide the array into many subarrays where each subarray is of length size + +Examples: + +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]] +''' + + +def chunkify(list, chunk): + # placeholder for new array + new_list = [] + # placeholder sub array + new_sublist = [] + # for loop that iterates over each item in array + for i in range(len(list)): + # if (i + 1) % chunk = 0, + if (i+1) % chunk == 0: + # push current index into array + new_sublist.append(list[i]) + # push subarray into placeholder array + new_list.append(new_sublist) + # start a new subarray + new_sublist = [] + # else + else: + # push current index into array + new_sublist.append(list[i]) + # if placeholder array[len(array)] != [] + if new_sublist != []: + # push subarray into array + new_list.append(new_sublist) + # return array + return(new_list) + +print(chunkify([1, 2, 3, 4], 2)) +print(chunkify([1, 2, 3, 4, 5], 2)) +print(chunkify([1, 2, 3, 4, 5, 6, 7, 8], 3)) +print(chunkify([1, 2, 3, 4, 5], 4)) +print(chunkify([1, 2, 3, 4, 5], 10)) diff --git a/08-coins.py b/08-coins.py new file mode 100644 index 0000000..46048df --- /dev/null +++ b/08-coins.py @@ -0,0 +1,27 @@ +''' +Direction: + +Given an amount of change, determine the minimum number of coins required to make that change + +Examples: + +greedy(65) --> 4 `(2 quarters, 1 dime, 1 nickle)` +greedy(5) --> 1 `(1 nickle)` + +''' + +def greedy(change): + coins = { + 25: 0, + 10: 0, + 5: 0, + 1: 0, + } + remainder = change + for i in coins: + coins[i] = int(remainder / i) + remainder = remainder % i + min_coins = coins[25] + coins[10] + coins[5] + coins[1] + return f"{min_coins}: {coins[25]} quarters, {coins[10]} dimes, {coins[5]} nickels, {coins[1]} pennies" + +print(greedy(65)) \ No newline at end of file