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
16 changes: 16 additions & 0 deletions 02-flatten.py
Original file line number Diff line number Diff line change
@@ -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] ]))
24 changes: 24 additions & 0 deletions 03-days.py
Original file line number Diff line number Diff line change
@@ -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))
22 changes: 22 additions & 0 deletions 04-reverse.py
Original file line number Diff line number Diff line change
@@ -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"))
27 changes: 27 additions & 0 deletions 05-palindrome.py
Original file line number Diff line number Diff line change
@@ -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"))
12 changes: 12 additions & 0 deletions 06-most_common.py
Original file line number Diff line number Diff line change
@@ -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"
'''

44 changes: 44 additions & 0 deletions 07-chunk_subarrays.py
Original file line number Diff line number Diff line change
@@ -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))
27 changes: 27 additions & 0 deletions 08-coins.py
Original file line number Diff line number Diff line change
@@ -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))