diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..689e5e94 Binary files /dev/null and b/.DS_Store differ diff --git a/algorithms/dynamic_programming/1_factorial/solution/solution.py b/algorithms/dynamic_programming/1_factorial/solution/solution.py index 0eaee264..fa53c9fc 100644 --- a/algorithms/dynamic_programming/1_factorial/solution/solution.py +++ b/algorithms/dynamic_programming/1_factorial/solution/solution.py @@ -1,2 +1,5 @@ def factorial(n): - return \ No newline at end of file + if n <= 1: + return 1 + else: + return n * factorial(n-1) \ No newline at end of file diff --git a/algorithms/dynamic_programming/1_is_palindrome/solution/solution.py b/algorithms/dynamic_programming/1_is_palindrome/solution/solution.py index b068a658..74071108 100644 --- a/algorithms/dynamic_programming/1_is_palindrome/solution/solution.py +++ b/algorithms/dynamic_programming/1_is_palindrome/solution/solution.py @@ -1,6 +1,6 @@ - def is_palindrome(string): if len(string) <= 1: return True else: - return string[0] == string[-1] and is_palindrome(string[1:-1]) + return string[0] == string[-1] + is_palindrome(string[1:-1]) \ No newline at end of file diff --git a/algorithms/dynamic_programming/1_product_sequence_n/solution/solution.py b/algorithms/dynamic_programming/1_product_sequence_n/solution/solution.py index f9e379ed..8ab12897 100644 --- a/algorithms/dynamic_programming/1_product_sequence_n/solution/solution.py +++ b/algorithms/dynamic_programming/1_product_sequence_n/solution/solution.py @@ -1,2 +1,5 @@ def product_sequence_n(n): - return + if n <= 1: + return 1 + else: + return n * product_sequence_n(n-2) diff --git a/algorithms/dynamic_programming/1_sum_first_n_integers/solution/solution.py b/algorithms/dynamic_programming/1_sum_first_n_integers/solution/solution.py index 194623e1..e3f29280 100644 --- a/algorithms/dynamic_programming/1_sum_first_n_integers/solution/solution.py +++ b/algorithms/dynamic_programming/1_sum_first_n_integers/solution/solution.py @@ -1,2 +1,5 @@ def sum_first_n(n): - return + if n == 1: + return 1 + else: + return n + sum_first_n(n-1) diff --git a/algorithms/dynamic_programming/2_Tribonacci/solution/solution.py b/algorithms/dynamic_programming/2_Tribonacci/solution/solution.py index 655f2cbe..1756866d 100644 --- a/algorithms/dynamic_programming/2_Tribonacci/solution/solution.py +++ b/algorithms/dynamic_programming/2_Tribonacci/solution/solution.py @@ -1,2 +1,20 @@ +# def tribonacci(n): +# if n == 0 or n == 1: +# return 0 +# elif n == 2: +# return 1 +# else: +# return tribonacci(n-1) + tribonacci(n-2) +tribonacci(n-3) def tribonacci(n): - return \ No newline at end of file + if n == 0 or n == 1: + return 0 + if n == 2: + return 1 + trib = [-1 for x in range(n+1)] + trib[0] = 0 + trib[1] = 0 + trib[2] = 1 + for x in range(3, n+1): + # trib[x] = trib[x-1] + trib[x-2] + trib[x-3] + trib[x] = trib[x-3] + trib[x-2] + trib[x-1] #to make the bottom-up tabulation make more sense + return trib[x] diff --git a/algorithms/dynamic_programming/2_paths_to_nth_stair/solution/solution.py b/algorithms/dynamic_programming/2_paths_to_nth_stair/solution/solution.py index 60cba7d0..f9a48356 100644 --- a/algorithms/dynamic_programming/2_paths_to_nth_stair/solution/solution.py +++ b/algorithms/dynamic_programming/2_paths_to_nth_stair/solution/solution.py @@ -1,5 +1,10 @@ def paths_nth_stair(n): - return - - - + if n <= 2: + return n + else: + paths = [-1 for x in range(n+1)] + paths[1] = 1 + paths[2] = 2 + for x in range(3, n+1): + paths[x] = paths[x-1] + paths[x-2] + return paths[n] \ No newline at end of file diff --git a/algorithms/dynamic_programming/2_shortest_path_to_1/solution/solution.py b/algorithms/dynamic_programming/2_shortest_path_to_1/solution/solution.py index 2fa2f79d..88bedbf9 100644 --- a/algorithms/dynamic_programming/2_shortest_path_to_1/solution/solution.py +++ b/algorithms/dynamic_programming/2_shortest_path_to_1/solution/solution.py @@ -1,2 +1,22 @@ def shortest_path_to_1(n): - return + if n == 1: + return 0 + elif n == 2 or n == 3: + return 1 + shortest = [-1 for x in range(n+1)] + shortest[1] = 0 + shortest[2] = 1 + shortest[3] = 1 + for x in range(4, n+1): + if shortest[x] == -1: + if x % 3 == 0 and x % 2 == 0: + shortest[x] = 1 + min(shortest[x-1], shortest[x//3], shortest[x//2]) + elif x % 3 == 0: + shortest[x] = 1 + min(shortest[x-1], shortest[x//3]) + elif x % 2 == 0: + shortest[x] = 1 + min(shortest[x-1], shortest[x//2]) + else: + shortest[x] = 1 + shortest[x-1] + return shortest[n] + + diff --git a/algorithms/dynamic_programming/3_longest_common_subsequence/solution/solution.py b/algorithms/dynamic_programming/3_longest_common_subsequence/solution/solution.py index 129b22d8..8d77638b 100644 --- a/algorithms/dynamic_programming/3_longest_common_subsequence/solution/solution.py +++ b/algorithms/dynamic_programming/3_longest_common_subsequence/solution/solution.py @@ -1,2 +1,13 @@ def longest_common_subseq(str1, str2): - return \ No newline at end of file + length_1 = len(str1) + length_2 = len(str2) + Length = [[None]*(length_2 + 1) for i in range(length_1 + 1)] + for i in range(length_1 + 1): + for x in range(length_2 + 1): + if i == 0 or x == 0: + Length[i][x] = 0 + elif str1[i-1] == str2[x-1]: + Length[i][x] = Length[i-1][x-1] + 1 + else: + Length[i][x] = max(Length[i-1][x], Length[i][x-1]) + return Length[length_1][length_2] \ No newline at end of file diff --git a/algorithms/dynamic_programming/3_palindromic_substring/solution/solution.py b/algorithms/dynamic_programming/3_palindromic_substring/solution/solution.py index 785697e6..7f221d07 100644 --- a/algorithms/dynamic_programming/3_palindromic_substring/solution/solution.py +++ b/algorithms/dynamic_programming/3_palindromic_substring/solution/solution.py @@ -1,2 +1,23 @@ def longest_palindrome_substr(str): - return \ No newline at end of file + n = len(str) + if n == 0: + return 0 + table = [[0 for x in range(n)] for y in range(n)] + max_length = 1 + for i in range(n): + table[i][i] = True + start = 0 + for i in range(n-1): + if (str[i] == str[i+1]): + table[i][i+1] = True + start = i + max_length = 2 + for k in range(3, n+1): + for i in range(n - k + 1): + j = i + k - 1 + if (table[i+1][j-1] and str[i] == str[j]): + table[i][j] = True + if k > max_length: + start = i + max_length = k + return max_length \ No newline at end of file diff --git a/algorithms/graph_traversal/1_count_edges_matrix/solution/.model_solution.py b/algorithms/graph_traversal/1_count_edges_matrix/solution/.model_solution.py index 79168448..3f215a5d 100644 --- a/algorithms/graph_traversal/1_count_edges_matrix/solution/.model_solution.py +++ b/algorithms/graph_traversal/1_count_edges_matrix/solution/.model_solution.py @@ -1,2 +1,6 @@ def count_edges(matrix): - return \ No newline at end of file + edge_count = 0 + for i in range(len(matrix)): + for j in range(len(matrix[i])): + edge_count += matrix[i][j] + return edge_count \ No newline at end of file diff --git a/algorithms/graph_traversal/1_count_islands_list/solution/solution.py b/algorithms/graph_traversal/1_count_islands_list/solution/solution.py index 984d95ff..1545b70b 100644 --- a/algorithms/graph_traversal/1_count_islands_list/solution/solution.py +++ b/algorithms/graph_traversal/1_count_islands_list/solution/solution.py @@ -1,2 +1,28 @@ def count_islands(adjacency_list): - return \ No newline at end of file + dictionary_count_edges = dict.fromkeys(adjacency_list.keys(), 0) + for adj_keyNode, edge_list in adjacency_list.items(): + for keyNode in dictionary_count_edges.keys(): + if not adj_keyNode == keyNode: + if keyNode in edge_list: + dictionary_count_edges[keyNode] += 1 + number_of_Islands = 0 + for edgeCount in dictionary_count_edges.values(): + if edgeCount == 0: + number_of_Islands += 1 + return number_of_Islands + +#adjacency_list = { 0: [1], 1: [0, 1, 2], 2: []} + +# dictionary_count_edges -> assigns every key:value pair to 0 + +#dictionary_count_edges = {0:0, 1:0, 2: 0} +#for every key and list in adjacency_list +#0 : [1] +#1 : [0, 1, 2] +#2 : [] + +#dictionary_count_edges = {0:2, 1:1, 2:1} +# for every key in dictionary_count_edges +# 0 -> in 0 and 1 so increment by 2 +# 1 -> in 0 so increment by 1 +# 2 -> in 1 so increment by 1 \ No newline at end of file diff --git a/algorithms/graph_traversal/1_list_to_matrix_representation/solution/solution.py b/algorithms/graph_traversal/1_list_to_matrix_representation/solution/solution.py index 98e903bc..908411a4 100644 --- a/algorithms/graph_traversal/1_list_to_matrix_representation/solution/solution.py +++ b/algorithms/graph_traversal/1_list_to_matrix_representation/solution/solution.py @@ -1 +1,7 @@ -adjacency_matrix = [] +adjacency_matrix = [ + [1, 1, 0, 1, 0], + [0, 0, 0, 1, 1], + [0, 1, 0, 0, 0], + [1, 0, 1, 0, 0], + [0, 0, 0, 0, 0], +] diff --git a/algorithms/graph_traversal/1_matrix_to_list_representation/solution/solution.py b/algorithms/graph_traversal/1_matrix_to_list_representation/solution/solution.py index 6340aed8..f7c80d5c 100644 --- a/algorithms/graph_traversal/1_matrix_to_list_representation/solution/solution.py +++ b/algorithms/graph_traversal/1_matrix_to_list_representation/solution/solution.py @@ -1 +1,6 @@ -adjacency_list = {} \ No newline at end of file +adjacency_list = { + 0: [1,2,3], + 1: [0,3], + 2: [1], + 3: [0,3] +} \ No newline at end of file diff --git a/algorithms/graph_traversal/2_defining_a_graph_list/solution/solution.py b/algorithms/graph_traversal/2_defining_a_graph_list/solution/solution.py index 6e9ac058..ff5f1a77 100644 --- a/algorithms/graph_traversal/2_defining_a_graph_list/solution/solution.py +++ b/algorithms/graph_traversal/2_defining_a_graph_list/solution/solution.py @@ -1 +1,6 @@ -adjacency_list = {} +adjacency_list = { + 0: [], + 1: [2, 3], + 2: [1], + 3: [2] +} diff --git a/algorithms/graph_traversal/2_defining_a_graph_matrix/solution/solution.py b/algorithms/graph_traversal/2_defining_a_graph_matrix/solution/solution.py index 98e903bc..2518a871 100644 --- a/algorithms/graph_traversal/2_defining_a_graph_matrix/solution/solution.py +++ b/algorithms/graph_traversal/2_defining_a_graph_matrix/solution/solution.py @@ -1 +1,5 @@ -adjacency_matrix = [] +adjacency_matrix = [ + [1, 0, 1], + [0, 0, 0], + [1, 0, 1] +] diff --git a/algorithms/graph_traversal/2_most_neighbors/solution/solution.py b/algorithms/graph_traversal/2_most_neighbors/solution/solution.py index 2ada6d4a..944c9227 100644 --- a/algorithms/graph_traversal/2_most_neighbors/solution/solution.py +++ b/algorithms/graph_traversal/2_most_neighbors/solution/solution.py @@ -1,2 +1,11 @@ def most_neighbours(adjacency_list): - return \ No newline at end of file + maximum_len = -1 + maximum_key = -1 + for keyNode, edge_list in adjacency_list.items(): + number_of_neighbours = len(edge_list) + if number_of_neighbours > maximum_len: + maximum_key = keyNode + maximum_len = number_of_neighbours + elif number_of_neighbours == maximum_len and keyNode < maximum_key: + maximum_key = keyNode + return maximum_key \ No newline at end of file diff --git a/algorithms/graph_traversal/2_num_hops_away_list/solution/solution.py b/algorithms/graph_traversal/2_num_hops_away_list/solution/solution.py index 6921aded..7735a3b7 100644 --- a/algorithms/graph_traversal/2_num_hops_away_list/solution/solution.py +++ b/algorithms/graph_traversal/2_num_hops_away_list/solution/solution.py @@ -1,2 +1,14 @@ def hops_away(adjacency_list, node, num_hops): - return \ No newline at end of file + if not node in adjacency_list.keys(): + return [] + elif num_hops == 0: + return [node] + else: + number_hops_away = [] + for neighbour in adjacency_list[node]: + number_hops_away.extend(hops_away(adjacency_list, neighbour, num_hops-1)) + number_hops_away_1 = [] + for n in number_hops_away: + if not n in number_hops_away_1: + number_hops_away_1.append(n) + return number_hops_away_1 \ No newline at end of file diff --git a/algorithms/graph_traversal/3_depth_first_search_matrix/solution/solution.py b/algorithms/graph_traversal/3_depth_first_search_matrix/solution/solution.py index 3d9203e8..ec4a3880 100644 --- a/algorithms/graph_traversal/3_depth_first_search_matrix/solution/solution.py +++ b/algorithms/graph_traversal/3_depth_first_search_matrix/solution/solution.py @@ -1,2 +1,28 @@ def exists_path(matrix, origin, destination): - return \ No newline at end of file + def exists_path_visited(matrix, origin, destination, visited): + if origin in visited: + return False + if not (origin in range(len(matrix)) and + destination in range(len(matrix))): + return False + elif matrix[origin][destination] == 1: + return True + visited.append(origin) + for j in range(len(matrix[origin])): + if matrix[origin][j] == 1 and exists_path_visited(matrix, j, destination, visited): + return True + return False + return exists_path_visited(matrix, origin, destination, []) + +# matrix = [ [0, 1, 0], [0, 0, 1], [0, 1, 0] ] + +# exists_path(matrix, 0, 2) => True + +# exists_path_visited(matrix, 0, 2, []) + +# visited = [0] +# for j = 0, 1, 2 +# matrix, j, destination, visitedlist +# exists_path_visited(matrix, 1, 2, [0]) + +#matrix[1][2] == 1, so we would return True \ No newline at end of file diff --git a/algorithms/graph_traversal/3_num_components/solution/solution.py b/algorithms/graph_traversal/3_num_components/solution/solution.py index 307c1e19..4cafa4b9 100644 --- a/algorithms/graph_traversal/3_num_components/solution/solution.py +++ b/algorithms/graph_traversal/3_num_components/solution/solution.py @@ -1,2 +1,32 @@ +def reachable_dict(matrix): + reachable_dict = {} + for i in range(len(matrix)): + reachable_lst = [] + for j in range(len(matrix[i])): + if matrix[i][j] == 1: + reachable_lst.append(j) + reachable_dict[i] = reachable_lst +#getting every key that's directly reachable and putting it into a list + all_reachable = {} + for i, neighbours in reachable_dict.items(): + all_reachable_lst = [] + for n in neighbours: + all_reachable_lst = all_reachable_lst + reachable_dict[n] + all_reachable_lst = all_reachable_lst + [n] + all_reachable[i] = all_reachable_lst + return all_reachable +#getting every key that's neighbors of the directly reachables and putting it into a list + def num_components(adjacency_matrix): - return \ No newline at end of file + reachableDict = reachable_dict(adjacency_matrix) + num_components = 0 + visited = [False for i in range(len(adjacency_matrix))] + for i in range(len(adjacency_matrix)): + if visited[i] == False: + num_components += 1 + for j in reachableDict[i]: + visited[j] = True + return num_components + +# visited [False, False, False, False] +# visited [] \ No newline at end of file diff --git a/algorithms/greedy_and_divide_and_conquer/1_frog_hops/solution/solution.py b/algorithms/greedy_and_divide_and_conquer/1_frog_hops/solution/solution.py index 1b5d3daa..de410054 100644 --- a/algorithms/greedy_and_divide_and_conquer/1_frog_hops/solution/solution.py +++ b/algorithms/greedy_and_divide_and_conquer/1_frog_hops/solution/solution.py @@ -1,3 +1,14 @@ +def min_hops_helper(max_units, n, river, num_hops): + position = 0 + for i in range(max_units, 0, -1): + if position + i <= n and river[position + i] == 1: + num_hops += 1 + position = position + i + return min_hops_helper(max_units, n-i, river[position:], num_hops) + if position == n: + return num_hops + else: + return -1 + def min_hops(max_units, n, river): - return - \ No newline at end of file + return min_hops_helper(max_units, n, river, 0) \ No newline at end of file diff --git a/algorithms/greedy_and_divide_and_conquer/1_jewel_thief/solution/solution.py b/algorithms/greedy_and_divide_and_conquer/1_jewel_thief/solution/solution.py index ba31302d..db7681c6 100644 --- a/algorithms/greedy_and_divide_and_conquer/1_jewel_thief/solution/solution.py +++ b/algorithms/greedy_and_divide_and_conquer/1_jewel_thief/solution/solution.py @@ -1,3 +1,22 @@ -def steal_jewels(x, jewels): - return +def sort(tuples): + tuples.sort(key = lambda x: x[1]) + tuples.reverse() + return tuples +def steal_jewels(x, jewels): + max_val = 0 + sorted_value_jewels = sort(jewels) + for jewel in sorted_value_jewels: + jewel_weight = jewel[0] + jewel_value = jewel[1] + if x == 0: + return max_val + elif jewel_weight <= x: + max_val += jewel_value + x = x-jewel_weight + else: + # value of 1kg portion of the jewel + unit_val = jewel_value/jewel_weight + max_val += x * unit_val + return max_val + return max_val \ No newline at end of file diff --git a/algorithms/greedy_and_divide_and_conquer/1_make_change/solution/solution.py b/algorithms/greedy_and_divide_and_conquer/1_make_change/solution/solution.py index a7f30ace..372663df 100644 --- a/algorithms/greedy_and_divide_and_conquer/1_make_change/solution/solution.py +++ b/algorithms/greedy_and_divide_and_conquer/1_make_change/solution/solution.py @@ -1,3 +1,16 @@ +def make_change_1(amount, denominations, num_coins): + # if amount is 0 we made all the change and we return num_coins. + if amount == 0: + return num_coins + # itterate through denominations from largest to smallest + for i in range(len(denominations)): + # if the amount is larger or equal to the current coin value we recurse, decreasing the amount by that coin value, and increasing the number of coins by 1. + if amount >= denominations[i]: + return make_change_1(amount-denominations[i], denominations, num_coins+1) + # once we've itterated through all the coins we never hit amount == 0, so we have a remainder we could not make change for and we return -1. + return -1 def make_change(amount, denominations): - return + # Sort denominations in decreasing order + denominations = sorted(denominations, reverse=True) + return make_change_1(amount, denominations, 0) \ No newline at end of file diff --git a/algorithms/greedy_and_divide_and_conquer/2_binary_search/solution/solution.py b/algorithms/greedy_and_divide_and_conquer/2_binary_search/solution/solution.py index 782b41ae..94b9027b 100644 --- a/algorithms/greedy_and_divide_and_conquer/2_binary_search/solution/solution.py +++ b/algorithms/greedy_and_divide_and_conquer/2_binary_search/solution/solution.py @@ -1,2 +1,14 @@ +def binary_search_helper(lst, x, low, high): + if high >= low: + mid = (high + low) // 2 + if lst[mid] == x: + return mid + elif lst[mid] > x: + return binary_search_helper(lst, x, low, mid - 1) + else: + return binary_search_helper(lst, x, mid + 1, high) + else: + return -1 + def binary_search(lst, x): - return \ No newline at end of file + return binary_search_helper(lst, x, 0, len(lst)-1) diff --git a/algorithms/greedy_and_divide_and_conquer/2_make_change_limited_coins/solution/solution.py b/algorithms/greedy_and_divide_and_conquer/2_make_change_limited_coins/solution/solution.py index c1634823..1656d6d0 100644 --- a/algorithms/greedy_and_divide_and_conquer/2_make_change_limited_coins/solution/solution.py +++ b/algorithms/greedy_and_divide_and_conquer/2_make_change_limited_coins/solution/solution.py @@ -1,2 +1,25 @@ +def help_make_change(amount, denominations, denomination_keys, number_of_coins): + # if we have ammount 0, we made all the change and we return num_coins. + if amount == 0: + return number_of_coins + # we itterate through the denomination_keys list by index and the denomination key + for index, keys in enumerate(denomination_keys): + # if amount is greater than the denomination key + if amount >= keys: + # we update the denomination dict taking one away + denominations[keys] -= 1 + # if we now have none of that coin left in our denominations dict + # we update the list by removing that element and only looking at the + # denominations in the rest of the list. + if denominations[keys] == 0: + denomination_keys = denomination_keys[index + 1:] + # we recurse, updating the amount by subtracting the coin we saw, and + # adding one to num_coins + return help_make_change(amount - keys, denominations, denomination_keys, number_of_coins + 1) + # if we itterated through all the coins and still have a remainder, we could not + # make all the change, so we return -1. + return -1 def make_change(amount, denominations): - return \ No newline at end of file + # create a list of denomination values sorted in decreasing order. + denomination_keys = sorted(denominations.keys(), reverse=True) + return help_make_change(amount, denominations, denomination_keys, 0) \ No newline at end of file diff --git a/algorithms/greedy_and_divide_and_conquer/2_min_max/solution/solution.py b/algorithms/greedy_and_divide_and_conquer/2_min_max/solution/solution.py index 3cd23490..560cd607 100644 --- a/algorithms/greedy_and_divide_and_conquer/2_min_max/solution/solution.py +++ b/algorithms/greedy_and_divide_and_conquer/2_min_max/solution/solution.py @@ -1,2 +1,20 @@ +def helper_min_max(lst, low, high): + maximum = lst[high] + minimum = lst[low] + if low == high: + return (minimum, maximum) + elif low + 1 == high: + if lst[low] > lst[high]: + maximum = lst[low] + minimum = lst[high] + else: + maximum = lst[high] + minimum = lst[low] + return (minimum, maximum) + else: + mid = int((low + high) / 2) + min1, max1 = helper_min_max(lst, low, mid) + min2, max2 = helper_min_max(lst, mid + 1, high) + return (min(min1, min2), max(max1, max2)) def min_max(lst): - return \ No newline at end of file + return helper_min_max(lst, 0, len(lst)-1) \ No newline at end of file diff --git a/algorithms/greedy_and_divide_and_conquer/2_square_root/solution/solution.py b/algorithms/greedy_and_divide_and_conquer/2_square_root/solution/solution.py index 05de023f..4beaf8df 100644 --- a/algorithms/greedy_and_divide_and_conquer/2_square_root/solution/solution.py +++ b/algorithms/greedy_and_divide_and_conquer/2_square_root/solution/solution.py @@ -1,2 +1,15 @@ def bin_sqrt(n): - return \ No newline at end of file + if n == 0 or n == 1: + return n + beginning = 1 + end = n + while beginning <= end: + mid = (beginning + end) // 2 + if mid*mid == n: + return mid + if mid*mid < n: + beginning = mid + 1 + answer = mid + else: + end = mid - 1 + return answer \ No newline at end of file diff --git a/algorithms/greedy_and_divide_and_conquer/3_scheduling/solution/solution.py b/algorithms/greedy_and_divide_and_conquer/3_scheduling/solution/solution.py index 24ea4ba8..e9a756e0 100644 --- a/algorithms/greedy_and_divide_and_conquer/3_scheduling/solution/solution.py +++ b/algorithms/greedy_and_divide_and_conquer/3_scheduling/solution/solution.py @@ -1,3 +1,9 @@ - def task_schedule(tasks): - return + tasks.sort(key = lambda x: x[1]) + number_of_tasks = 0 + current_end = 0 + for x in tasks: + if x[0] >= current_end: + current_end = x[1] + number_of_tasks += 1 + return number_of_tasks \ No newline at end of file diff --git a/algorithms/greedy_and_divide_and_conquer/3_work_projects/solution/solution.py b/algorithms/greedy_and_divide_and_conquer/3_work_projects/solution/solution.py index babcb8e7..d0c3475e 100644 --- a/algorithms/greedy_and_divide_and_conquer/3_work_projects/solution/solution.py +++ b/algorithms/greedy_and_divide_and_conquer/3_work_projects/solution/solution.py @@ -1,3 +1,11 @@ - def max_profits(projects): - return + projects.sort(key = lambda x: x[1], reverse=True) + dict = {} + profit = 0 + for x in projects: + for i in range(x[0], 0, -1): + if not i in dict: + dict[i] = x[1] + profit += x[1] + break + return profit \ No newline at end of file diff --git a/algorithms/sorting/1_frequency_array/solution/solution.py b/algorithms/sorting/1_frequency_array/solution/solution.py index e5b85e17..294cb06f 100644 --- a/algorithms/sorting/1_frequency_array/solution/solution.py +++ b/algorithms/sorting/1_frequency_array/solution/solution.py @@ -1,3 +1,9 @@ - def frequency_array(lst): - return + maximum = -1 + for i in lst: + if i > maximum: + maximum = i + frequency_lst = [0 for i in range(maximum + 1)] + for i in range(len(lst)): + frequency_lst[lst[i]] += 1 + return frequency_lst diff --git a/algorithms/sorting/1_merge_sorted/solution/solution.py b/algorithms/sorting/1_merge_sorted/solution/solution.py index f79fd0e4..95a88b9d 100644 --- a/algorithms/sorting/1_merge_sorted/solution/solution.py +++ b/algorithms/sorting/1_merge_sorted/solution/solution.py @@ -1,2 +1,20 @@ +# def merge_sorted(lst1, lst2): + # new_lst = lst1 + lst2 + # new_lst.sort() + # return new_lst +def merge_helper(lst1, lst2, merged_lst): + if len(lst1) == 0: + merged_lst.extend(lst2) + return merged_lst + elif len(lst2) == 0: + merged_lst.extend(lst1) + return merged_lst + elif lst1[0] <= lst2[0]: + merged_lst.append(lst1[0]) + return merge_helper(lst1[1:], lst2, merged_lst) + else: + merged_lst.append(lst2[0]) + return merge_helper(lst1, lst2[1:], merged_lst) def merge_sorted(lst1, lst2): - return \ No newline at end of file + return merge_helper(lst1, lst2, []) + \ No newline at end of file diff --git a/algorithms/sorting/1_min_or_max_heap/solution/solution.py b/algorithms/sorting/1_min_or_max_heap/solution/solution.py index bbac9775..5abb3508 100644 --- a/algorithms/sorting/1_min_or_max_heap/solution/solution.py +++ b/algorithms/sorting/1_min_or_max_heap/solution/solution.py @@ -1,2 +1,63 @@ +def max_heap(heap): + if len(heap) == 0: + return True + for i in range(len(heap)): + root = heap[i] + leftindex = (2*i) + 1 + rightindex = (2*i) + 2 + left = None + right = None + if (2*i)+1 < len(heap): + left = heap[(2*i)+1] + if (2*i)+2 < len(heap): + right = heap[(2*i)+2] + if left == None and right == None: + return True + if left == None: + if right < root: + return max_heap(heap[rightindex:]) + elif right > root: + return False + if right == None: + if left < root: + return max_heap(heap[leftindex:]) + elif left > root: + return False + if left > root or right > root: + return False + return True +def min_heap(heap): + if len(heap) == 0: + return True + for i in range(len(heap)): + root = heap[i] + leftindex = (2*i) + 1 + rightindex = (2*i) + 2 + left = None + right = None + if (2*i)+1 < len(heap): + left = heap[(2*i)+1] + if (2*i)+2 < len(heap): + right = heap[(2*i)+2] + if left == None and right == None: + return True + if left == None: + if right > root: + return min_heap(heap[rightindex:]) + elif right < root: + return False + if right == None: + if left > root: + return min_heap(heap[leftindex:]) + elif left < root: + return False + if left < root or right < root: + return False + return True def min_or_max_heap(heap): - return \ No newline at end of file + if min_heap(heap): + return 'min' + elif max_heap(heap): + return 'max' + else: + return 'neither' \ No newline at end of file diff --git a/algorithms/sorting/2_frequency_array_cumulative/solution/solution.py b/algorithms/sorting/2_frequency_array_cumulative/solution/solution.py index 6cd3c60b..403e3a1d 100644 --- a/algorithms/sorting/2_frequency_array_cumulative/solution/solution.py +++ b/algorithms/sorting/2_frequency_array_cumulative/solution/solution.py @@ -1,3 +1,20 @@ -def frequency_array_cumulative(lst): - return +def frequency_array(lst): + maximum = -1 + for i in lst: + if i > maximum: + maximum = i + frequency_lst = [0 for i in range(maximum + 1)] + for i in range(len(lst)): + frequency_lst[lst[i]] += 1 + return frequency_lst +def frequency_array_cumulative(lst): + frequency_lst = frequency_array(lst) + frequency_length = len(frequency_lst) + frequency_list = [0 for i in range(frequency_length)] + for i in range(frequency_length): + if i == 0: + frequency_list[i] = frequency_lst[i] + else: + frequency_list[i] = frequency_lst[i] + frequency_list[i-1] + return frequency_list \ No newline at end of file diff --git a/algorithms/sorting/2_median_of_medians/solution/solution.py b/algorithms/sorting/2_median_of_medians/solution/solution.py index b1d3c339..09b73f79 100644 --- a/algorithms/sorting/2_median_of_medians/solution/solution.py +++ b/algorithms/sorting/2_median_of_medians/solution/solution.py @@ -1,2 +1,9 @@ def median_of_medians(lst): - return \ No newline at end of file + sublists = [lst[i:i+5] for i in range(0, len(lst), 5)] + medians = [] + for lists in sublists: + medians.append(sorted(lists)[len(lists)//2]) + if len(medians) <= 5: + return sorted(medians)[len(medians)//2] + else: + return median_of_medians(medians) \ No newline at end of file diff --git a/algorithms/sorting/2_sort_linked_list/solution/solution.py b/algorithms/sorting/2_sort_linked_list/solution/solution.py index 75e37545..365accc8 100644 --- a/algorithms/sorting/2_sort_linked_list/solution/solution.py +++ b/algorithms/sorting/2_sort_linked_list/solution/solution.py @@ -2,13 +2,21 @@ class ListNode: def __init__(self, value=None, next=None): self.value = value self.next = next - class LinkedList: def __init__(self, head=None): self.head = head - def sort_ll(self): - ... - - - + current_n = self.head + index = None + if self.head == None: + return None + else: + while current_n != None: + index = current_n.next + while index != None: + if current_n.value > index.value: + x = current_n.value + current_n.value = index.value + index.value = x + index = index.next + current_n = current_n.next \ No newline at end of file diff --git a/algorithms/sorting/2_sort_vectors/solution/solution.py b/algorithms/sorting/2_sort_vectors/solution/solution.py index d64b9657..3b20845e 100644 --- a/algorithms/sorting/2_sort_vectors/solution/solution.py +++ b/algorithms/sorting/2_sort_vectors/solution/solution.py @@ -1,4 +1,14 @@ import math +def magnitude(vector): + return math.sqrt((vector[0][0]-vector[1][0])**2 + (vector[0][1]-vector[1][1])**2) + def sort_vectors(vector_lst): - return + for index in range(1, len(vector_lst)): + current_vector = vector_lst[index] + position = index + while position > 0 and magnitude(vector_lst[position-1]) > magnitude(current_vector): + vector_lst[position] = vector_lst[position-1] + position -= 1 + vector_lst[position] = current_vector + return vector_lst \ No newline at end of file diff --git a/algorithms/sorting/3_quick_sort/solution/solution.py b/algorithms/sorting/3_quick_sort/solution/solution.py index a7495778..a54e3336 100644 --- a/algorithms/sorting/3_quick_sort/solution/solution.py +++ b/algorithms/sorting/3_quick_sort/solution/solution.py @@ -1,6 +1,30 @@ def median_of_medians(lst): - return - -def quick_sort(lst): - return + sublists = [lst[i:i+5] for i in range(0, len(lst), 5)] + medians = [] + for lists in sublists: + medians.append(sorted(lists)[len(lists)//2]) + if len(medians) <= 5: + return sorted(medians)[len(medians)//2] + else: + return median_of_medians(medians) + +def _partition(lst, min_index, max_index): + pivot_index = lst.index(median_of_medians(lst[min_index:max_index+1])) + lst[min_index], lst[pivot_index] = lst[pivot_index], lst[min_index] + pivot_index = min_index + for index in range(min_index, max_index + 1): + if lst[index] < lst[pivot_index]: + lst[pivot_index], lst[index] = lst[index], lst[pivot_index] + lst[pivot_index + 1], lst[index] = lst[index], lst[pivot_index + 1] + pivot_index += 1 + return pivot_index +def quick_sort_helper(lst, min_index, max_index): + if min_index < max_index: + pivot_index = _partition(lst, min_index, max_index) + quick_sort_helper(lst, min_index, pivot_index - 1) + quick_sort_helper(lst, pivot_index + 1, max_index) + +def quick_sort(lst): + quick_sort_helper(lst, 0, len(lst) - 1) + return lst \ No newline at end of file diff --git a/algorithms/sorting/3_sort_students/solution/solution.py b/algorithms/sorting/3_sort_students/solution/solution.py index 18aa8dcc..cdaf6e23 100644 --- a/algorithms/sorting/3_sort_students/solution/solution.py +++ b/algorithms/sorting/3_sort_students/solution/solution.py @@ -3,6 +3,17 @@ def __init__(self, name=None, avg=None, grade=None): self.name = name self.avg = avg self.grade = grade + def __lt__(self, other): + if self.grade < other.grade: + return True + if self.grade == other.grade: + if self.avg < other.avg: + return True + if self.avg == other.avg: + return self.name < other.name + return False + def __repr__(self): + return f'Name: {self.name}, Avg: {self.avg}, Grade: {self.grade}' def sort_students(slst): - return + return sorted(slst) diff --git a/data_structures/binary_trees/1_binary_tree/solution/solution.py b/data_structures/binary_trees/1_binary_tree/solution/solution.py index d4b2fbc9..ebdb7a03 100644 --- a/data_structures/binary_trees/1_binary_tree/solution/solution.py +++ b/data_structures/binary_trees/1_binary_tree/solution/solution.py @@ -1,7 +1,10 @@ #Write your solutions here - - - - - - +class Node: + def __init__(self, key, left=None, right=None): + self.key = key + self.left = left + self.right = right + +left_child = Node(4) +right_child = Node(7) +sample_tree = Node(5, left_child, right_child) \ No newline at end of file diff --git a/data_structures/binary_trees/1_height_of_tree/solution/solution.py b/data_structures/binary_trees/1_height_of_tree/solution/solution.py index 0352cbbf..03e48dd9 100644 --- a/data_structures/binary_trees/1_height_of_tree/solution/solution.py +++ b/data_structures/binary_trees/1_height_of_tree/solution/solution.py @@ -1 +1,18 @@ # Write your solutions here +class Node: + def __init__(self, key, left=None, right=None): + self.key = key + self.left = left + self.right = right + +def height(node): + if node is None: + return 0 + else: + left_height = height(node.left) + right_height = height(node.right) + + if (left_height > right_height): + return left_height+1 + else: + return right_height+1 \ No newline at end of file diff --git a/data_structures/binary_trees/2_inorder_list/solution/solution.py b/data_structures/binary_trees/2_inorder_list/solution/solution.py index c4f40e78..f863d95a 100644 --- a/data_structures/binary_trees/2_inorder_list/solution/solution.py +++ b/data_structures/binary_trees/2_inorder_list/solution/solution.py @@ -1 +1,13 @@ -#Write your solutions here \ No newline at end of file +#Write your solutions here +class Node: + def __init__(self, key, left=None, right=None): + self.key = key + self.left = left + self.right = right +def Inorder(root): + lst = [] + if root: + lst = Inorder(root.left) + lst.append(root.key) + lst += Inorder(root.right) + return lst \ No newline at end of file diff --git a/data_structures/binary_trees/2_postorder_list/solution/solution.py b/data_structures/binary_trees/2_postorder_list/solution/solution.py index 0352cbbf..f39cecc7 100644 --- a/data_structures/binary_trees/2_postorder_list/solution/solution.py +++ b/data_structures/binary_trees/2_postorder_list/solution/solution.py @@ -1 +1,14 @@ # Write your solutions here +class Node: + def __init__(self, key, left=None, right=None): + self.key = key + self.left = left + self.right = right + +def Postorder(root): + lst = [] + if root: + lst = Postorder(root.left) + lst += Postorder(root.right) + lst.append(root.key) + return lst \ No newline at end of file diff --git a/data_structures/binary_trees/2_preorder_list/solution/solution.py b/data_structures/binary_trees/2_preorder_list/solution/solution.py index 0352cbbf..2f01c868 100644 --- a/data_structures/binary_trees/2_preorder_list/solution/solution.py +++ b/data_structures/binary_trees/2_preorder_list/solution/solution.py @@ -1 +1,13 @@ # Write your solutions here +class Node: + def __init__(self, key, left=None, right=None): + self.key = key + self.left = left + self.right = right +def Preorder(root): + lst = [] + if root: + lst.append(root.key) + lst += Preorder(root.left) + lst += Preorder(root.right) + return lst \ No newline at end of file diff --git a/data_structures/binary_trees/2_search_node/solution/solution.py b/data_structures/binary_trees/2_search_node/solution/solution.py index a1fd7b02..b66724c1 100644 --- a/data_structures/binary_trees/2_search_node/solution/solution.py +++ b/data_structures/binary_trees/2_search_node/solution/solution.py @@ -1 +1,16 @@ # Write solutions here +class Node: + def __init__(self, key, left=None, right=None): + self.key = key + self.left = left + self.right = right + +def search(root, key): + if root is None: + return False + elif key == root.key: + return True + elif root.key > key: + return search(root.left, key) + else: + return search(root.right, key) \ No newline at end of file diff --git a/data_structures/binary_trees/3_closest_value/solution/solution.py b/data_structures/binary_trees/3_closest_value/solution/solution.py index 45487fe8..508a9244 100644 --- a/data_structures/binary_trees/3_closest_value/solution/solution.py +++ b/data_structures/binary_trees/3_closest_value/solution/solution.py @@ -5,9 +5,28 @@ def __init__(self, key, left=None, right=None): self.right = right self.key = key +# def closest_value_helper(node, target, previous): +# if node: +# if node.key == target: +# return node.key +# child = node.left if target < node.key else node.right +# closest_child = closest_value_helper(child, target, node.key) +# if abs(target - node.key) < abs(target - closest_child): +# return node.key +# else: +# return closest_child +# return previous -def closest_value(node, target): - return - - +# def closest_value(node, target): +# return closest_value_helper(node, target, node.key) +def closest_value(node, target): + a = node.key + if target < a: + child = node.left + else: + child = node.right + if not child: + return a + b = closest_value(child, target) + return min((a,b), key=lambda x: abs(target - b)) \ No newline at end of file diff --git a/data_structures/binary_trees/3_insert_node/solution/solution.py b/data_structures/binary_trees/3_insert_node/solution/solution.py index 0a3a0368..8b6cd61c 100644 --- a/data_structures/binary_trees/3_insert_node/solution/solution.py +++ b/data_structures/binary_trees/3_insert_node/solution/solution.py @@ -6,10 +6,23 @@ def __init__(self, key, left=None, right=None): self.key = key # Fill in your code here - def insert(self, key): - return - - - + def inorder_nodes(self): + if not self: + return [] + leftNodes = self.left.inorder_nodes() if self.left else [] + rightNodes = self.right.inorder_nodes() if self.right else [] + return [self] + leftNodes + rightNodes + def insert(self, key): + if not self: + return Node(key) + node_lst = self.inorder_nodes() + for node in node_lst: + if not node.left: + node.left = Node(key) + return + elif not node.right: + node.right = Node(key) + return + return \ No newline at end of file diff --git a/data_structures/binary_trees/3_nth_inorder/solution/solution.py b/data_structures/binary_trees/3_nth_inorder/solution/solution.py index 6011d5ed..4f8162dc 100644 --- a/data_structures/binary_trees/3_nth_inorder/solution/solution.py +++ b/data_structures/binary_trees/3_nth_inorder/solution/solution.py @@ -1 +1,20 @@ #Write your solutions here +class Node: + def __init__(self, key, left=None, right=None): + self.key = key + self.left = left + self.right = right + +def Inorder(root): + lst = [] + if root: + lst = Inorder(root.left) + lst.append(root.key) + lst += Inorder(root.right) + return lst +def NthInorder(root, n): + inorder_key = Inorder(root) + if len(inorder_key) > n: + return inorder_key[n-1] + else: + return f'no {n}-th element' \ No newline at end of file diff --git a/data_structures/binary_trees/3_nth_preorder/solution/solution.py b/data_structures/binary_trees/3_nth_preorder/solution/solution.py index a8c5443e..d526af60 100644 --- a/data_structures/binary_trees/3_nth_preorder/solution/solution.py +++ b/data_structures/binary_trees/3_nth_preorder/solution/solution.py @@ -1,3 +1,21 @@ #Write your solutions here +class Node: + def __init__(self, key, left=None, right=None): + self.key = key + self.left = left + self.right = right +def Preorder(root): + lst = [] + if root: + lst.append(root.key) + lst += Preorder(root.left) + lst += Preorder(root.right) + return lst +def NthPreorder(root, n): + preorder_key = Preorder(root) + if len(preorder_key) > n: + return preorder_key[n-1] + else: + return f'no {n}-th element' \ No newline at end of file diff --git a/data_structures/dictionaries_and_arrays/1_insert_element/solution/solution.py b/data_structures/dictionaries_and_arrays/1_insert_element/solution/solution.py index 8a7c9d9a..97505f32 100755 --- a/data_structures/dictionaries_and_arrays/1_insert_element/solution/solution.py +++ b/data_structures/dictionaries_and_arrays/1_insert_element/solution/solution.py @@ -1 +1,4 @@ -# Write your solution here \ No newline at end of file +# Write your solution here +def insert_element(array_num, ins_pos, ins_val): + array_num.insert(ins_pos, ins_val) + return array_num \ No newline at end of file diff --git a/data_structures/dictionaries_and_arrays/1_print_array/solution/solution.py b/data_structures/dictionaries_and_arrays/1_print_array/solution/solution.py index aeb50baa..7b26e86d 100755 --- a/data_structures/dictionaries_and_arrays/1_print_array/solution/solution.py +++ b/data_structures/dictionaries_and_arrays/1_print_array/solution/solution.py @@ -1 +1,4 @@ # Write your solution here +def print_array(array_1): + for element in array_1: + print(element) \ No newline at end of file diff --git a/data_structures/dictionaries_and_arrays/1_print_dictionary/solution/solution.py b/data_structures/dictionaries_and_arrays/1_print_dictionary/solution/solution.py index aeb50baa..51b9b751 100755 --- a/data_structures/dictionaries_and_arrays/1_print_dictionary/solution/solution.py +++ b/data_structures/dictionaries_and_arrays/1_print_dictionary/solution/solution.py @@ -1 +1,4 @@ # Write your solution here +def print_dict(dict_num): + for value in dict_num.values(): + print(value) \ No newline at end of file diff --git a/data_structures/dictionaries_and_arrays/1_update_dict/solution/solution.py b/data_structures/dictionaries_and_arrays/1_update_dict/solution/solution.py index aeb50baa..32315543 100755 --- a/data_structures/dictionaries_and_arrays/1_update_dict/solution/solution.py +++ b/data_structures/dictionaries_and_arrays/1_update_dict/solution/solution.py @@ -1 +1,5 @@ # Write your solution here +def update_dict(key, value): + new_dict = {} + new_dict[key] = value + return new_dict \ No newline at end of file diff --git a/data_structures/dictionaries_and_arrays/2_convert_json/solution/solution.py b/data_structures/dictionaries_and_arrays/2_convert_json/solution/solution.py index aeb50baa..73675b58 100755 --- a/data_structures/dictionaries_and_arrays/2_convert_json/solution/solution.py +++ b/data_structures/dictionaries_and_arrays/2_convert_json/solution/solution.py @@ -1 +1,5 @@ # Write your solution here +import json +def convert(dict): + new_dict = json.dumps(dict) + return new_dict \ No newline at end of file diff --git a/data_structures/dictionaries_and_arrays/2_occurance/solution/solution.py b/data_structures/dictionaries_and_arrays/2_occurance/solution/solution.py index aeb50baa..a1d41e5b 100755 --- a/data_structures/dictionaries_and_arrays/2_occurance/solution/solution.py +++ b/data_structures/dictionaries_and_arrays/2_occurance/solution/solution.py @@ -1 +1,3 @@ # Write your solution here +def occurances(array_num, element): + return array_num.count(element) \ No newline at end of file diff --git a/data_structures/dictionaries_and_arrays/2_reverse_array/solution/solution.py b/data_structures/dictionaries_and_arrays/2_reverse_array/solution/solution.py index aeb50baa..e89dfe25 100755 --- a/data_structures/dictionaries_and_arrays/2_reverse_array/solution/solution.py +++ b/data_structures/dictionaries_and_arrays/2_reverse_array/solution/solution.py @@ -1 +1,3 @@ # Write your solution here +def reverse(array_num): + return array_num[::-1] \ No newline at end of file diff --git a/data_structures/dictionaries_and_arrays/2_sort_values/solution/solution.py b/data_structures/dictionaries_and_arrays/2_sort_values/solution/solution.py index aeb50baa..c4d10351 100755 --- a/data_structures/dictionaries_and_arrays/2_sort_values/solution/solution.py +++ b/data_structures/dictionaries_and_arrays/2_sort_values/solution/solution.py @@ -1 +1,4 @@ # Write your solution here +import operator +def sort_dict(my_dictionary): + return dict(sorted(my_dictionary.items(), key = operator.itemgetter(1))) \ No newline at end of file diff --git a/data_structures/dictionaries_and_arrays/3_difference/solution/solution.py b/data_structures/dictionaries_and_arrays/3_difference/solution/solution.py index aeb50baa..0d8321c9 100755 --- a/data_structures/dictionaries_and_arrays/3_difference/solution/solution.py +++ b/data_structures/dictionaries_and_arrays/3_difference/solution/solution.py @@ -1 +1,5 @@ # Write your solution here +def difference(array_num): + numbers = sorted(array_num) + result = numbers[-1] - numbers[0] + return result \ No newline at end of file diff --git a/data_structures/dictionaries_and_arrays/3_square_keys/solution/solution.py b/data_structures/dictionaries_and_arrays/3_square_keys/solution/solution.py index aeb50baa..0da21ee2 100755 --- a/data_structures/dictionaries_and_arrays/3_square_keys/solution/solution.py +++ b/data_structures/dictionaries_and_arrays/3_square_keys/solution/solution.py @@ -1 +1,6 @@ # Write your solution here +def square_keys(num): + dict = {} + for x in range(1, num+1): + dict[x] = x**2 + return dict \ No newline at end of file diff --git a/data_structures/heaps/1_get_child_nodes/solution/solution.py b/data_structures/heaps/1_get_child_nodes/solution/solution.py index 476c5ab8..c1877f7b 100644 --- a/data_structures/heaps/1_get_child_nodes/solution/solution.py +++ b/data_structures/heaps/1_get_child_nodes/solution/solution.py @@ -1,5 +1,10 @@ # Write your solution here - - - - \ No newline at end of file +def get_child_nodes(heap, index): + left_index = 2*index + 1 #left child of heap[i] + right_index = 2*index + 2 #right child of heap[i] + lst = [] #creating empty list to append index's of heap + if left_index < len(heap): + lst.append(heap[left_index]) + if right_index < len(heap): + lst.append(heap[right_index]) + return lst \ No newline at end of file diff --git a/data_structures/heaps/1_get_parent_node/solution/solution.py b/data_structures/heaps/1_get_parent_node/solution/solution.py index aeb50baa..3c130417 100644 --- a/data_structures/heaps/1_get_parent_node/solution/solution.py +++ b/data_structures/heaps/1_get_parent_node/solution/solution.py @@ -1 +1,8 @@ # Write your solution here +def get_parent_node(heap, index): + parent_index = (index-1) // 2 #parent of heap[i] + if index == 0 and len(heap) > 0: + return heap[0] # root is heap[0] + elif index < len(heap): + return heap[parent_index] + return f'{index} is not a valid index in the heap' \ No newline at end of file diff --git a/data_structures/heaps/1_is_leaf_node/solution/solution.py b/data_structures/heaps/1_is_leaf_node/solution/solution.py index 4dca28ed..225dff78 100644 --- a/data_structures/heaps/1_is_leaf_node/solution/solution.py +++ b/data_structures/heaps/1_is_leaf_node/solution/solution.py @@ -1,6 +1,5 @@ # Write your solution here def is_leaf(heap, index): - return - - - \ No newline at end of file + if index >= (len(heap)//2) and index < len(heap): + return True + return False \ No newline at end of file diff --git a/data_structures/linked_lists/1_delete_end/solution/solution.py b/data_structures/linked_lists/1_delete_end/solution/solution.py index 35fc9320..591cb528 100644 --- a/data_structures/linked_lists/1_delete_end/solution/solution.py +++ b/data_structures/linked_lists/1_delete_end/solution/solution.py @@ -1 +1,18 @@ # Write your answers here +class Node: + def __init__(self, data=None): + self.data = data + self.next = None + # def __str__(self): + # return str(self.data) + +class linkedlist: + def __init__(self, head=None): + self.head = head + def delete_at_end(self): + if self.head is None: + return None + n = self.head + while n.next.next != None: + n = n.next + n.next = None \ No newline at end of file diff --git a/data_structures/linked_lists/1_insert_after/solution/solution.py b/data_structures/linked_lists/1_insert_after/solution/solution.py index 35fc9320..8ee77832 100644 --- a/data_structures/linked_lists/1_insert_after/solution/solution.py +++ b/data_structures/linked_lists/1_insert_after/solution/solution.py @@ -1 +1,40 @@ # Write your answers here +class Node: + def __init__(self, data): + self.data = data + self.next = None + +class linkedList: + def __init__(self, head=None): + self.head = head + def insert_after_item(self, x, data): + current_node = self.head + previous_node = self.head + new_node = Node(data) + while current_node: + if current_node.data == x: + new_node.next = current_node.next + current_node.next = new_node + return + previous_node = current_node + current_node = current_node.next + previous_node.next = new_node + +# 1 -> 2 -> 3 -> 5 +# insert: x=3 data=4 + +# c_n = 1 +# p_n = 1 +# n_n = 4 + +# p_n = c_n = 1 +# c_n = 1+next = 2 + +# p_n = 1+next = 2 +# c_n = 2+next = 3 + +# n_n.next = c_n.next = 5 +# 4 -> 5 + +# c_n.next = n_n +# 1 -> 2 -> 3 -> 4 -> 5 \ No newline at end of file diff --git a/data_structures/linked_lists/1_insert_start/solution/solution.py b/data_structures/linked_lists/1_insert_start/solution/solution.py index aeb50baa..8dc2c33e 100644 --- a/data_structures/linked_lists/1_insert_start/solution/solution.py +++ b/data_structures/linked_lists/1_insert_start/solution/solution.py @@ -1 +1,14 @@ # Write your solution here +class Node: + def __init__(self, data): + self.data = data + self.next = None + +class linkedList: + def __init__(self, head=None): + self.head = head + def insert_at_start(self, data): + Node1 = Node(data) + Node1.next = self.head + self.head = Node1 + diff --git a/data_structures/linked_lists/2_delete_item_value/solution/solution.py b/data_structures/linked_lists/2_delete_item_value/solution/solution.py index aeb50baa..ec72d0cc 100644 --- a/data_structures/linked_lists/2_delete_item_value/solution/solution.py +++ b/data_structures/linked_lists/2_delete_item_value/solution/solution.py @@ -1 +1,35 @@ # Write your solution here +class Node: + def __init__(self,data): + self.data = data + self.next = None + +class linkedList: + def __init__(self, head=None): + self.head = head + def delete_item_by_value(self, x): + current_node = self.head + previous_node = self.head + while current_node: + if current_node.data == x: + next_node = current_node.next + previous_node.next = next_node + return + previous_node = current_node + current_node = current_node.next + return +# 1 -> 2 -> 3 -> 4 +# 1 -> 2 -> 4 +# delete: 3 + +# c_n = 1 +# p_n = 1 + +# p_n = 1 +# c_n = 1+next = 2 + +# p_n = 2 +# c_n = 2+next = 3 + +# n_n = c_n + next = 4 +# p_n.next = 4 diff --git a/data_structures/linked_lists/2_insert_end/.DS_Store b/data_structures/linked_lists/2_insert_end/.DS_Store new file mode 100644 index 00000000..751c1c60 Binary files /dev/null and b/data_structures/linked_lists/2_insert_end/.DS_Store differ diff --git a/data_structures/linked_lists/2_insert_end/solution/solution.py b/data_structures/linked_lists/2_insert_end/solution/solution.py index 0b2305ce..88e65f56 100644 --- a/data_structures/linked_lists/2_insert_end/solution/solution.py +++ b/data_structures/linked_lists/2_insert_end/solution/solution.py @@ -1 +1,17 @@ #Write your Answers here +class Node: + def __init__(self, data): + self.data = data + self.next = None + +class linkedList: + def __init__(self, head=None): + self.head = head + def insert_at_end(self, data): + current_node = self.head + previous_node = self.head + while current_node != None: + previous_node = current_node + current_node = current_node.next + new_node = Node(data) + previous_node.next = new_node \ No newline at end of file diff --git a/data_structures/linked_lists/2_search_item/solution/solution.py b/data_structures/linked_lists/2_search_item/solution/solution.py index a14631d2..ac699d2e 100644 --- a/data_structures/linked_lists/2_search_item/solution/solution.py +++ b/data_structures/linked_lists/2_search_item/solution/solution.py @@ -1 +1,16 @@ # Write your solution here +class Node: + def __init__(self, data=None): + self.data = data + self.next = None +class linkedList: + def __init__(self, head=None): + self.head = head + def search(self, x): + current_node = self.head + while not current_node is None: + if current_node.data == x: + return True + else: + current_node = current_node.next + return False \ No newline at end of file diff --git a/data_structures/linked_lists/3_index_insertion/solution/solution.py b/data_structures/linked_lists/3_index_insertion/solution/solution.py index c4eb69b5..86eca1e1 100644 --- a/data_structures/linked_lists/3_index_insertion/solution/solution.py +++ b/data_structures/linked_lists/3_index_insertion/solution/solution.py @@ -1,2 +1,18 @@ #Write your answers here - +class Node: + def __init__(self, data=None): + self.data = data + self.next = None +class linkedList: + def __init__(self, head=None): + self.head = head + def insert_at_index(self, index, data): + current_index = 1 + current_node = self.head + while current_index < index-1: + current_node = current_node.next + current_index +=1 + next_node = current_node.next + new_node = Node(data) + new_node.next = next_node + current_node.next = new_node \ No newline at end of file diff --git a/data_structures/linked_lists/3_node_traverse/solution/solution.py b/data_structures/linked_lists/3_node_traverse/solution/solution.py index 4932939a..25c6cfce 100644 --- a/data_structures/linked_lists/3_node_traverse/solution/solution.py +++ b/data_structures/linked_lists/3_node_traverse/solution/solution.py @@ -1,3 +1,13 @@ # Write your answers here - - +class Node: + def __init__(self, data=None): + self.data = data + self.next = None +class linkedList: + def __init__(self, head=None): + self.head = head + def traverse(self): + current_node = self.head + while not current_node is None: + print(current_node.data) + current_node = current_node.next \ No newline at end of file diff --git a/data_structures/linked_lists/3_reverse/solution/solution.py b/data_structures/linked_lists/3_reverse/solution/solution.py index 58bdfc7d..746e3ce9 100644 --- a/data_structures/linked_lists/3_reverse/solution/solution.py +++ b/data_structures/linked_lists/3_reverse/solution/solution.py @@ -1,4 +1,18 @@ #Write your answers here +class Node: + def __init__(self, data=None): + self.data = data + self.next = None - - +class linkedList: + def __init__(self, head=None): + self.head = head + def reverse(self): + previous_node = None + current_node = self.head + while current_node: + next = current_node.next + current_node.next = previous_node + previous_node = current_node + current_node = next + self.head = previous_node \ No newline at end of file diff --git a/data_structures/linked_lists/Quiz_Solution.py b/data_structures/linked_lists/Quiz_Solution.py new file mode 100644 index 00000000..8687ea39 --- /dev/null +++ b/data_structures/linked_lists/Quiz_Solution.py @@ -0,0 +1,38 @@ +#Quiz - 2 + +#Question1 +class Tree: + def __init__(self, root_node=None): + self.root_node = root_node + +class TreeNode: + def __init__(self, value, left=None, right=None): + self.value = value + self.left = left + self.right = right + +items = TreeNode(4) +items.left = TreeNode(2) +items.right = TreeNode(6) +items.left.left = TreeNode(1) +items.left.right = TreeNode(3) +items.right.left = TreeNode(5) +items.right.right = TreeNode(7) +my_tree_node = Tree(items) + +#Question2 +class LinkedList: + def __init__(self, head_node=None): + self.head_node = head_node + def remove_tail(self): + if self.head_node is None: + return None + n = self.head_node + while n.next.next != None: + n = n.next + n.next = None + +class ListNode: + def __init__(self, value, next=None): + self.value = value + self.next = next \ No newline at end of file diff --git a/data_structures/stacks_and_queues/1_queue_bool/solution/solution.py b/data_structures/stacks_and_queues/1_queue_bool/solution/solution.py index 103eb323..995faf0d 100644 --- a/data_structures/stacks_and_queues/1_queue_bool/solution/solution.py +++ b/data_structures/stacks_and_queues/1_queue_bool/solution/solution.py @@ -28,6 +28,8 @@ def dequeue(self): # Fill in the code for __bool__ def __bool__(self): - return - - + # return self.head_node != None + if self.head_node is None: + return False + else: + return True \ No newline at end of file diff --git a/data_structures/stacks_and_queues/1_stack_bool/solution/solution.py b/data_structures/stacks_and_queues/1_stack_bool/solution/solution.py index a7a4c309..34ff1426 100644 --- a/data_structures/stacks_and_queues/1_stack_bool/solution/solution.py +++ b/data_structures/stacks_and_queues/1_stack_bool/solution/solution.py @@ -22,4 +22,10 @@ def pop(self): # Fill in the code for __bool__ def __bool__(self): - return \ No newline at end of file + # if self.head_node is None: + # return False + # else: + # return True + if self.head_node: + return True + return False \ No newline at end of file diff --git a/data_structures/stacks_and_queues/2_queue_eq/solution/solution.py b/data_structures/stacks_and_queues/2_queue_eq/solution/solution.py index 7fdf97e8..9201dfdb 100644 --- a/data_structures/stacks_and_queues/2_queue_eq/solution/solution.py +++ b/data_structures/stacks_and_queues/2_queue_eq/solution/solution.py @@ -28,8 +28,25 @@ def dequeue(self): # You may want to solve this magic method first! def __len__(self): - return + current_node = self.head_node + number_nodes = 0 + while current_node: + number_nodes += 1 + current_node = current_node.next + return number_nodes # Fill in the code for __len__ def __eq__(self, other): - return + #return len(self) == len(other) + if len(self) == len(other): + current_node1 = self.head_node + current_node2 = other.head_node + while current_node1: + if current_node1.value != current_node2.value: + return False + else: + current_node1 = current_node1.next + current_node2 = current_node2.next + return True + return False + diff --git a/data_structures/stacks_and_queues/2_queue_len/solution/solution.py b/data_structures/stacks_and_queues/2_queue_len/solution/solution.py index 7242a724..3d6e57d5 100644 --- a/data_structures/stacks_and_queues/2_queue_len/solution/solution.py +++ b/data_structures/stacks_and_queues/2_queue_len/solution/solution.py @@ -29,4 +29,9 @@ def dequeue(self): # Fill in the code for __len__ def __len__(self): - return \ No newline at end of file + current_node = self.head_node + number_nodes = 0 + while current_node: + number_nodes += 1 + current_node = current_node.next + return number_nodes \ No newline at end of file diff --git a/data_structures/stacks_and_queues/2_stack_eq/solution/solution.py b/data_structures/stacks_and_queues/2_stack_eq/solution/solution.py index a9fd5a45..27fe61b6 100644 --- a/data_structures/stacks_and_queues/2_stack_eq/solution/solution.py +++ b/data_structures/stacks_and_queues/2_stack_eq/solution/solution.py @@ -22,8 +22,24 @@ def pop(self): # You may want to solve this magic method first! def __len__(self): - return + current_node = self.head_node + number_nodes = 0 + while current_node: + number_nodes += 1 + current_node = current_node.next + return number_nodes # Fill in the code for __len__ def __eq__(self, other): - return \ No newline at end of file + #return len(self) == len(other) + if len(self) == len(other): + current_node1 = self.head_node + current_node2 = other.head_node + while current_node1: + if current_node1.value != current_node2.value: + return False + else: + current_node1 = current_node1.next + current_node2 = current_node2.next + return True + return False \ No newline at end of file diff --git a/data_structures/stacks_and_queues/2_stack_len/solution/solution.py b/data_structures/stacks_and_queues/2_stack_len/solution/solution.py index 6e8aa6fb..da619bca 100644 --- a/data_structures/stacks_and_queues/2_stack_len/solution/solution.py +++ b/data_structures/stacks_and_queues/2_stack_len/solution/solution.py @@ -22,4 +22,9 @@ def pop(self): # Fill in the code for __len__ def __len__(self): - return \ No newline at end of file + current_node = self.head_node + number_nodes = 0 + while current_node: + number_nodes += 1 + current_node = current_node.next + return number_nodes \ No newline at end of file diff --git a/data_structures/stacks_and_queues/3_backspace_compare/.DS_Store b/data_structures/stacks_and_queues/3_backspace_compare/.DS_Store new file mode 100644 index 00000000..751c1c60 Binary files /dev/null and b/data_structures/stacks_and_queues/3_backspace_compare/.DS_Store differ diff --git a/data_structures/stacks_and_queues/3_backspace_compare/solution/solution.py b/data_structures/stacks_and_queues/3_backspace_compare/solution/solution.py index eaac5bc9..94478da0 100644 --- a/data_structures/stacks_and_queues/3_backspace_compare/solution/solution.py +++ b/data_structures/stacks_and_queues/3_backspace_compare/solution/solution.py @@ -1,2 +1,74 @@ +# def backspace_compare(str1=str, str2=str): +# def New_string(string): +# stack = [] +# for char in string: +# if char != '#': +# stack.append(char) +# elif stack: +# stack.pop() +# return stack +# return New_string(str1) == New_string(str2) + +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +class Stack: + def __init__(self): + self.head_node = None + def push(self, value): + new_head = ListNode(value) + new_head.next = self.head_node + self.head_node = new_head + def pop(self): + if self.head_node: + value = self.head_node.value + self.head_node = self.head_node.next + return value + else: + raise IndexError + # def __bool__(self): + # if self.head_node is None: + # return False + # else: + # return True + def __len__(self): + current_node = self.head_node + number_nodes = 0 + while current_node: + number_nodes += 1 + current_node = current_node.next + return number_nodes + def __eq__(self, other): + if len(self) == len(other): + current_node1 = self.head_node + current_node2 = other.head_node + while current_node1: + if current_node1.value != current_node2.value: + return False + else: + current_node1 = current_node1.next + current_node2 = current_node2.next + return True + return False + def backspace_compare(str1, str2): - return \ No newline at end of file + stack_1 = Stack() + stack_2 = Stack() + for char in str1: + if char == '#': + if stack_1: + stack_1.pop() + else: + stack_1.push(char) + for char in str2: + if char == '#': + if stack_2: + stack_2.pop() + else: + stack_2.push(char) + return stack_1 == stack_2 + +str1 = "a#c" +str2 = "c#d" +print(backspace_compare(str1, str2)) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/1_compare_check/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/1_compare_check/Solution/solution.py index ade03179..63f1d036 100644 --- a/introduction_and_environment/data_types_and_control_flow/1_compare_check/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/1_compare_check/Solution/solution.py @@ -1 +1,6 @@ # Code your solution here +x = 200 +y = 201 + +result = x and y +print(bool(result)) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/1_even_odd/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/1_even_odd/Solution/solution.py index ade03179..9529b438 100644 --- a/introduction_and_environment/data_types_and_control_flow/1_even_odd/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/1_even_odd/Solution/solution.py @@ -1 +1,10 @@ # Code your solution here +number = int(input()) +def Data(number): + if number % 2 == 0: + return Even + else: + return Odd + + +print(Data) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/1_list_max/solution/solution.py b/introduction_and_environment/data_types_and_control_flow/1_list_max/solution/solution.py index e69de29b..1578858a 100644 --- a/introduction_and_environment/data_types_and_control_flow/1_list_max/solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/1_list_max/solution/solution.py @@ -0,0 +1,12 @@ +L = [152, 463, 1112, 1337, -10] +def LIST_MAX1(L): + current_max = L[0] + if L[1] > current_max: + current_max = L[1] + if L[2] > current_max: + current_max = L[2] + if L[3] > current_max: + current_max = L[3] + if L[4] > current_max: + current_max = L[4] + return current_max \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/1_loop_divisibility/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/1_loop_divisibility/Solution/solution.py index cbc031aa..0d7bc458 100644 --- a/introduction_and_environment/data_types_and_control_flow/1_loop_divisibility/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/1_loop_divisibility/Solution/solution.py @@ -1,2 +1,4 @@ # Code your solution here - \ No newline at end of file +for x in range(0, 51): + if x % 5 == 0 or x % 7 == 0: + print(x) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/1_string_convert/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/1_string_convert/Solution/solution.py index ade03179..ffd1a53e 100644 --- a/introduction_and_environment/data_types_and_control_flow/1_string_convert/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/1_string_convert/Solution/solution.py @@ -1 +1,8 @@ # Code your solution here +string = str(input()) +def data(string): + if string.isupper(): + string.lower + else: + string.upper +print(data) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/1_string_length/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/1_string_length/Solution/solution.py index ade03179..62059e37 100644 --- a/introduction_and_environment/data_types_and_control_flow/1_string_length/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/1_string_length/Solution/solution.py @@ -1 +1,4 @@ # Code your solution here +string_1 = str(input()) +data = len(string_1) +print(data) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/1_string_slice/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/1_string_slice/Solution/solution.py index ade03179..2f4be347 100644 --- a/introduction_and_environment/data_types_and_control_flow/1_string_slice/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/1_string_slice/Solution/solution.py @@ -1 +1,5 @@ # Code your solution here +string_value = 'Hello Universe' +data = string_value[0:5] +data_val = data + 'World' +print(data_val) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/2_add_item/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/2_add_item/Solution/solution.py index ade03179..2e0a6d46 100644 --- a/introduction_and_environment/data_types_and_control_flow/2_add_item/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/2_add_item/Solution/solution.py @@ -1 +1,5 @@ # Code your solution here +dict_a = {} +dict_a["key1"] = 2 +dict_val = dict_a +print(dict_val) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/2_append/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/2_append/Solution/solution.py index ade03179..2eef5e26 100644 --- a/introduction_and_environment/data_types_and_control_flow/2_append/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/2_append/Solution/solution.py @@ -1 +1,4 @@ # Code your solution here +list_a = [] +list_b = list_a.append(3) +print(list_b) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/2_except_letter/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/2_except_letter/Solution/solution.py index ade03179..e09d2c29 100644 --- a/introduction_and_environment/data_types_and_control_flow/2_except_letter/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/2_except_letter/Solution/solution.py @@ -1 +1,11 @@ # Code your solution here +string = 'BYTE ACADEMY' +vowels = ['A', 'E', 'I', 'O', 'U'] +def data(x): + ans = False + for characters in string: + if characters != vowels: + ans = True + return ans +print(data) + \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/2_farm_area/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/2_farm_area/Solution/solution.py index ade03179..9b6c3c51 100644 --- a/introduction_and_environment/data_types_and_control_flow/2_farm_area/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/2_farm_area/Solution/solution.py @@ -1 +1,6 @@ -# Code your solution here +# length = len(input()) +# breadth = len(input()) +length = 6 +breadth = 4 +data = length * breadth +print(data) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/2_remove_item/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/2_remove_item/Solution/solution.py index ade03179..ab77c215 100644 --- a/introduction_and_environment/data_types_and_control_flow/2_remove_item/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/2_remove_item/Solution/solution.py @@ -1 +1,5 @@ # Code your solution here +list_1 = [] +lister = list_1.remove() + +print(lister) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/2_sum_list/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/2_sum_list/Solution/solution.py index ade03179..8115ca30 100644 --- a/introduction_and_environment/data_types_and_control_flow/2_sum_list/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/2_sum_list/Solution/solution.py @@ -1 +1,4 @@ # Code your solution here +list_a = [] +total = sum(list_a) +print(total) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/2_vowel_consonant/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/2_vowel_consonant/Solution/solution.py index ade03179..a00455dd 100644 --- a/introduction_and_environment/data_types_and_control_flow/2_vowel_consonant/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/2_vowel_consonant/Solution/solution.py @@ -1 +1,13 @@ # Code your solution here +vowels = ['a', 'e', 'i', 'o', 'u'] +consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'] +letter = input() + +def data(x): + for characters in letter: + if characters == vowels: + return vowel + elif characters == consonants: + return consonant + +print(data) diff --git a/introduction_and_environment/data_types_and_control_flow/3_count_duplicate/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/3_count_duplicate/Solution/solution.py index ade03179..e12307d6 100644 --- a/introduction_and_environment/data_types_and_control_flow/3_count_duplicate/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/3_count_duplicate/Solution/solution.py @@ -1 +1,4 @@ # Code your solution here +list_dup = [] +tot = list_dup.count +print(tot) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/3_grade/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/3_grade/Solution/solution.py index ade03179..c58a31fd 100644 --- a/introduction_and_environment/data_types_and_control_flow/3_grade/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/3_grade/Solution/solution.py @@ -1 +1,15 @@ # Code your solution here +def grade(mark): + if mark >= 90: + return 'A+ GRADE' + elif mark >= 70: + return 'B GRADE' + elif mark >= 50: + return 'C GRADE' + elif mark >= 35: + return 'D GRADE' + else: + return 'FAIL' +# score = int(input("Enter your grade here:")) +mark = 80 +print(grade(mark)) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/3_month_days/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/3_month_days/Solution/solution.py index ade03179..9615947a 100644 --- a/introduction_and_environment/data_types_and_control_flow/3_month_days/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/3_month_days/Solution/solution.py @@ -1 +1,11 @@ # Code your solution here +# month = input('Enter any month:') +month = 'august' +month_dict = {'january':'31', 'february':'28, 29', 'march':'31', 'april':'30', 'may':'31', 'june':'30', 'july':'31', 'august':'31','september':'30', 'october':'31', 'november':'30', 'december':'31'} +def data(month): + if month in month_dict.keys(): + return month_dict[month] + else: + return None + +print(data(month)) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/3_palindrome/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/3_palindrome/Solution/solution.py index ade03179..122aaedc 100644 --- a/introduction_and_environment/data_types_and_control_flow/3_palindrome/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/3_palindrome/Solution/solution.py @@ -1 +1,9 @@ # Code your solution here +# line = input('Enter any string value:') +line = 'malayalam' +if line == line[::-1]: + is_palindrome = True +else: + is_palindrome = False + +print(is_palindrome) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/3_set_difference/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/3_set_difference/Solution/solution.py index ade03179..e13ac0ba 100644 --- a/introduction_and_environment/data_types_and_control_flow/3_set_difference/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/3_set_difference/Solution/solution.py @@ -1 +1,5 @@ # Code your solution here +set_1 = set() +set_2 = set() +result = set_1 - set_2 +print(result) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/3_shape_check/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/3_shape_check/Solution/solution.py index ade03179..379e81de 100644 --- a/introduction_and_environment/data_types_and_control_flow/3_shape_check/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/3_shape_check/Solution/solution.py @@ -1 +1,11 @@ # Code your solution here +# figure = input('Enter any geometric shape value:') +figure = '5' +my_dict = {'3':'Triangle', '4':'Quadrilateral', '5':'Pentagon', '6':'Hexagon', '7':'Heptagon', '8':'Octagon', '9':'Nonagon'} + +def data(figure): + if figure in my_dict: + return my_dict[figure] + else: + return 'None' +print(data(figure)) \ No newline at end of file diff --git a/introduction_and_environment/data_types_and_control_flow/3_string_case/Solution/solution.py b/introduction_and_environment/data_types_and_control_flow/3_string_case/Solution/solution.py index ade03179..0c9227fb 100644 --- a/introduction_and_environment/data_types_and_control_flow/3_string_case/Solution/solution.py +++ b/introduction_and_environment/data_types_and_control_flow/3_string_case/Solution/solution.py @@ -1 +1,8 @@ # Code your solution here +string_val = str(input()) + +upper_data = string_val.upper() +lower_data = string_val.lower() + +print(upper_data) +print(lower_data) \ No newline at end of file diff --git a/introduction_and_environment/git/1_creating_a_repository/Solution/solution.py b/introduction_and_environment/git/1_creating_a_repository/Solution/solution.py index cccdee23..e7abe60c 100644 --- a/introduction_and_environment/git/1_creating_a_repository/Solution/solution.py +++ b/introduction_and_environment/git/1_creating_a_repository/Solution/solution.py @@ -1,2 +1,2 @@ # Code your solution here -answer = '' +answer = 'a' diff --git a/introduction_and_environment/git/1_remote_repository/Solution/solution.py b/introduction_and_environment/git/1_remote_repository/Solution/solution.py index cccdee23..482a5499 100644 --- a/introduction_and_environment/git/1_remote_repository/Solution/solution.py +++ b/introduction_and_environment/git/1_remote_repository/Solution/solution.py @@ -1,2 +1,2 @@ # Code your solution here -answer = '' +answer = 'd' diff --git a/introduction_and_environment/git/1_staging_area/Solution/solution.py b/introduction_and_environment/git/1_staging_area/Solution/solution.py index cccdee23..b8eb8f57 100644 --- a/introduction_and_environment/git/1_staging_area/Solution/solution.py +++ b/introduction_and_environment/git/1_staging_area/Solution/solution.py @@ -1,2 +1,2 @@ # Code your solution here -answer = '' +answer = 'c' diff --git a/introduction_and_environment/git/1_update_code/Solution/solution.py b/introduction_and_environment/git/1_update_code/Solution/solution.py index cccdee23..ca9c8ca8 100644 --- a/introduction_and_environment/git/1_update_code/Solution/solution.py +++ b/introduction_and_environment/git/1_update_code/Solution/solution.py @@ -1,2 +1,2 @@ # Code your solution here -answer = '' +answer = 'b' diff --git a/introduction_and_environment/hello_world/1_arithmetic/solution/solution.py b/introduction_and_environment/hello_world/1_arithmetic/solution/solution.py index 217b5003..5c4e4332 100644 --- a/introduction_and_environment/hello_world/1_arithmetic/solution/solution.py +++ b/introduction_and_environment/hello_world/1_arithmetic/solution/solution.py @@ -1 +1,9 @@ from provided_code import x, y + +SUM = x + y +DIFF = x - y +MULT = x * y +DIV = x / y +POWER = x ** y +QUOTIENT = x // y +REMAINDER = x % y diff --git a/introduction_and_environment/hello_world/1_concantenation/Solution/solution.py b/introduction_and_environment/hello_world/1_concantenation/Solution/solution.py index ade03179..4f39a9d5 100644 --- a/introduction_and_environment/hello_world/1_concantenation/Solution/solution.py +++ b/introduction_and_environment/hello_world/1_concantenation/Solution/solution.py @@ -1 +1,5 @@ # Code your solution here +string_1 = "hello" +string_2 = "world!" +result = string_1 + string_2 +print(result) \ No newline at end of file diff --git a/introduction_and_environment/hello_world/1_name_bindings/solution/solution.py b/introduction_and_environment/hello_world/1_name_bindings/solution/solution.py index ade03179..6b02d8a7 100644 --- a/introduction_and_environment/hello_world/1_name_bindings/solution/solution.py +++ b/introduction_and_environment/hello_world/1_name_bindings/solution/solution.py @@ -1 +1,4 @@ # Code your solution here +x = 1337 +y = "hello world" +z = 13.5 \ No newline at end of file diff --git a/introduction_and_environment/hello_world/1_operators/solution/solution.py b/introduction_and_environment/hello_world/1_operators/solution/solution.py index ade03179..f247d49c 100644 --- a/introduction_and_environment/hello_world/1_operators/solution/solution.py +++ b/introduction_and_environment/hello_world/1_operators/solution/solution.py @@ -1 +1,3 @@ # Code your solution here +x = 11 +y = 10 \ No newline at end of file diff --git a/introduction_and_environment/hello_world/2_capture_display/Solution/solution.py b/introduction_and_environment/hello_world/2_capture_display/Solution/solution.py index ade03179..abc9b361 100644 --- a/introduction_and_environment/hello_world/2_capture_display/Solution/solution.py +++ b/introduction_and_environment/hello_world/2_capture_display/Solution/solution.py @@ -1 +1,7 @@ # Code your solution here +# name = input("Enter your name:") +# age = input("Enter your age:") +name = 'Charlie' +age = 100 +print(name) +print(age) \ No newline at end of file diff --git a/introduction_and_environment/hello_world/2_python_caches/solution/solution.py b/introduction_and_environment/hello_world/2_python_caches/solution/solution.py index 45a087b6..3e9db70e 100644 --- a/introduction_and_environment/hello_world/2_python_caches/solution/solution.py +++ b/introduction_and_environment/hello_world/2_python_caches/solution/solution.py @@ -1,2 +1,2 @@ -LOWER_BOUND = 0 -UPPER_BOUND = 0 +LOWER_BOUND = -5 +UPPER_BOUND = 256 diff --git a/introduction_and_environment/hello_world/2_string_arithmetic/solution/solution.py b/introduction_and_environment/hello_world/2_string_arithmetic/solution/solution.py index ade03179..92feda11 100644 --- a/introduction_and_environment/hello_world/2_string_arithmetic/solution/solution.py +++ b/introduction_and_environment/hello_world/2_string_arithmetic/solution/solution.py @@ -1 +1,6 @@ # Code your solution here +x = 'Hello' +y = 'World!' +z = 3 + +STR_ARITHMETIC = 2 \ No newline at end of file diff --git a/introduction_and_environment/hello_world/2_string_duplication/Solution/solution.py b/introduction_and_environment/hello_world/2_string_duplication/Solution/solution.py index ade03179..123ed16b 100644 --- a/introduction_and_environment/hello_world/2_string_duplication/Solution/solution.py +++ b/introduction_and_environment/hello_world/2_string_duplication/Solution/solution.py @@ -1 +1,5 @@ # Code your solution here +string_1 = 'Charlie' +dup_val = 5 +result = string_1 * dup_val +print(result) \ No newline at end of file diff --git a/introduction_and_environment/hello_world/2_type_check/Solution/solution.py b/introduction_and_environment/hello_world/2_type_check/Solution/solution.py index 0ec5aa42..8e231d8e 100644 --- a/introduction_and_environment/hello_world/2_type_check/Solution/solution.py +++ b/introduction_and_environment/hello_world/2_type_check/Solution/solution.py @@ -1 +1,9 @@ # Code your solution here... +input_1 = input("Store any data type value:") +input_2 = input("Store any other data type value:") + +result_1 = type(input_1) +result_2 = type(input_2) + +print(result_1) +print(result_2) \ No newline at end of file diff --git a/introduction_and_environment/hello_world/3_type_change/Solution/solution.py b/introduction_and_environment/hello_world/3_type_change/Solution/solution.py index ade03179..5e90ae1b 100644 --- a/introduction_and_environment/hello_world/3_type_change/Solution/solution.py +++ b/introduction_and_environment/hello_world/3_type_change/Solution/solution.py @@ -1 +1,18 @@ # Code your solution here +object_1 = 3.2 +<<<<<<< HEAD +<<<<<<< HEAD +<<<<<<< HEAD +object_2 = '2' +======= +object_2 = 2 +>>>>>>> 3b8cef1... Exercises completed +======= +object_2 = '2' +>>>>>>> 3569546... Exercises completed +======= +object_2 = '2' +>>>>>>> 95a635a4ecbbedb34125c1f8cacdc445bd0dbfd0 + +print(str(object_1)) +print(int(object_2)) \ No newline at end of file diff --git a/introduction_and_environment/hello_world/3_user_input/solution/solution.py b/introduction_and_environment/hello_world/3_user_input/solution/solution.py index ade03179..79af3166 100644 --- a/introduction_and_environment/hello_world/3_user_input/solution/solution.py +++ b/introduction_and_environment/hello_world/3_user_input/solution/solution.py @@ -1 +1,7 @@ # Code your solution here +first_num = input("State one number:") +second_num = input("State another number:") +third_num = input("State a third number:") + +result = (first_num * second_num * third_num) / 3 +print(result) \ No newline at end of file diff --git a/introduction_and_environment/introduction_to_programming/1_DNS/solution/solution.py b/introduction_and_environment/introduction_to_programming/1_DNS/solution/solution.py index ee95bbbb..fee1b0e9 100644 --- a/introduction_and_environment/introduction_to_programming/1_DNS/solution/solution.py +++ b/introduction_and_environment/introduction_to_programming/1_DNS/solution/solution.py @@ -1 +1,3 @@ -# Code your solution below \ No newline at end of file +# Code your solution below + +print('Domain Name System') \ No newline at end of file diff --git a/introduction_and_environment/introduction_to_programming/1_architecture/solution/solution.py b/introduction_and_environment/introduction_to_programming/1_architecture/solution/solution.py index abe91ec3..7ef8b2ba 100644 --- a/introduction_and_environment/introduction_to_programming/1_architecture/solution/solution.py +++ b/introduction_and_environment/introduction_to_programming/1_architecture/solution/solution.py @@ -1 +1,2 @@ # Code your solution below +print('a.John Von Neuman') \ No newline at end of file diff --git a/introduction_and_environment/introduction_to_programming/1_bytes/solution/solution.py b/introduction_and_environment/introduction_to_programming/1_bytes/solution/solution.py index abe91ec3..63bab332 100644 --- a/introduction_and_environment/introduction_to_programming/1_bytes/solution/solution.py +++ b/introduction_and_environment/introduction_to_programming/1_bytes/solution/solution.py @@ -1 +1,2 @@ # Code your solution below +print('Gigabyte') \ No newline at end of file diff --git a/introduction_and_environment/introduction_to_programming/1_languages/solution/solution.py b/introduction_and_environment/introduction_to_programming/1_languages/solution/solution.py index ee95bbbb..38b0c3cd 100644 --- a/introduction_and_environment/introduction_to_programming/1_languages/solution/solution.py +++ b/introduction_and_environment/introduction_to_programming/1_languages/solution/solution.py @@ -1 +1,2 @@ -# Code your solution below \ No newline at end of file +# Code your solution below +print('Interpreter') \ No newline at end of file diff --git a/introduction_and_environment/introduction_to_programming/2_ASCII/Solutions/solution.py b/introduction_and_environment/introduction_to_programming/2_ASCII/Solutions/solution.py index e69de29b..117c7cd9 100644 --- a/introduction_and_environment/introduction_to_programming/2_ASCII/Solutions/solution.py +++ b/introduction_and_environment/introduction_to_programming/2_ASCII/Solutions/solution.py @@ -0,0 +1,2 @@ +# print('Hexadecimal') +print('Boolean') \ No newline at end of file diff --git a/introduction_and_environment/introduction_to_programming/2_operators/Solutions/solution.py b/introduction_and_environment/introduction_to_programming/2_operators/Solutions/solution.py index e69de29b..e983d85b 100644 --- a/introduction_and_environment/introduction_to_programming/2_operators/Solutions/solution.py +++ b/introduction_and_environment/introduction_to_programming/2_operators/Solutions/solution.py @@ -0,0 +1,2 @@ +# print('Boolean') +print('Hexadecimal') \ No newline at end of file diff --git a/introduction_and_environment/introduction_to_programming/3_bitwise_operators_1/Solutions/solution.py b/introduction_and_environment/introduction_to_programming/3_bitwise_operators_1/Solutions/solution.py index e69de29b..439e8ebe 100644 --- a/introduction_and_environment/introduction_to_programming/3_bitwise_operators_1/Solutions/solution.py +++ b/introduction_and_environment/introduction_to_programming/3_bitwise_operators_1/Solutions/solution.py @@ -0,0 +1,10 @@ +x = 0 +y = 1 + +Answer_and = x & y +Answer_or = x | y +Answer_not = ~ x + +print(Answer_and) +print(Answer_or) +print(Answer_not) \ No newline at end of file diff --git a/introduction_and_environment/introduction_to_programming/3_bitwise_operators_2/Solutions/solution.py b/introduction_and_environment/introduction_to_programming/3_bitwise_operators_2/Solutions/solution.py index e69de29b..b3ac89c3 100644 --- a/introduction_and_environment/introduction_to_programming/3_bitwise_operators_2/Solutions/solution.py +++ b/introduction_and_environment/introduction_to_programming/3_bitwise_operators_2/Solutions/solution.py @@ -0,0 +1,10 @@ +x = 1 +y = 1 + +Answer_Xor = x ^ y +Answer_right_shift = x >> y +Answer_left_shift = x << y + +print(Answer_Xor) +print(Answer_right_shift) +print(Answer_left_shift) \ No newline at end of file diff --git a/introduction_and_environment/introduction_to_programming/3_logical_operators_1/Solutions/solution.py b/introduction_and_environment/introduction_to_programming/3_logical_operators_1/Solutions/solution.py index e69de29b..c06b7320 100644 --- a/introduction_and_environment/introduction_to_programming/3_logical_operators_1/Solutions/solution.py +++ b/introduction_and_environment/introduction_to_programming/3_logical_operators_1/Solutions/solution.py @@ -0,0 +1,6 @@ +x = True +y = False + +Answer = bool(x and y) + +print(Answer) \ No newline at end of file diff --git a/introduction_and_environment/introduction_to_programming/3_logical_operators_2/Solutions/solution.py b/introduction_and_environment/introduction_to_programming/3_logical_operators_2/Solutions/solution.py index e69de29b..89aa95f9 100644 --- a/introduction_and_environment/introduction_to_programming/3_logical_operators_2/Solutions/solution.py +++ b/introduction_and_environment/introduction_to_programming/3_logical_operators_2/Solutions/solution.py @@ -0,0 +1,5 @@ +x = True +y = False + +Answer = bool(x or y) +print(Answer) \ No newline at end of file diff --git a/introduction_and_environment/unix_and_bash/1_basic_commands/solution/solution.py b/introduction_and_environment/unix_and_bash/1_basic_commands/solution/solution.py index a19ce0a0..0373a93d 100644 --- a/introduction_and_environment/unix_and_bash/1_basic_commands/solution/solution.py +++ b/introduction_and_environment/unix_and_bash/1_basic_commands/solution/solution.py @@ -2,9 +2,9 @@ ONE = 'man' # Assign the correct strings below -TWO = '' -THREE = '' -FOUR = '' -FIVE = '' -SIX = '' -SEVEN = '' +TWO = 'ls' +THREE = 'mkdir' +FOUR = 'touch' +FIVE = 'cp' +SIX = 'mv' +SEVEN = 'rm' \ No newline at end of file diff --git a/introduction_and_environment/unix_and_bash/1_commands_with_options/solution/solution.py b/introduction_and_environment/unix_and_bash/1_commands_with_options/solution/solution.py index fa9fd397..b904358d 100644 --- a/introduction_and_environment/unix_and_bash/1_commands_with_options/solution/solution.py +++ b/introduction_and_environment/unix_and_bash/1_commands_with_options/solution/solution.py @@ -1,9 +1,9 @@ # Assign the correct strings below -ONE = '' -TWO = '' -THREE = '' -FOUR = '' -FIVE = '' -SIX = '' -SEVEN = '' +ONE = 'cd exercises' +TWO = 'pwd' +THREE = 'cat textfile.txt' +FOUR = 'tail textfile.txt' +FIVE = 'ls -a' +SIX = 'head textfile.txt' +SEVEN = 'cp stackdata.json stockdata.json' \ No newline at end of file diff --git a/introduction_and_environment/unix_and_bash/1_commands_with_options_2/solution/solution.py b/introduction_and_environment/unix_and_bash/1_commands_with_options_2/solution/solution.py index fa9fd397..30820b11 100644 --- a/introduction_and_environment/unix_and_bash/1_commands_with_options_2/solution/solution.py +++ b/introduction_and_environment/unix_and_bash/1_commands_with_options_2/solution/solution.py @@ -1,9 +1,9 @@ # Assign the correct strings below -ONE = '' -TWO = '' -THREE = '' -FOUR = '' -FIVE = '' -SIX = '' +ONE = 'grep elephant book.txt' +TWO = 'mv notes.txt newdir' +THREE = 'grep -r string testdir' +FOUR = 'rm empty' +FIVE = 'cd' +SIX = 'rm -r Downloads' SEVEN = '' diff --git a/introduction_and_environment/unix_and_bash/2_bash_operators/solution/solution.py b/introduction_and_environment/unix_and_bash/2_bash_operators/solution/solution.py index f34e86b6..cb28f995 100644 --- a/introduction_and_environment/unix_and_bash/2_bash_operators/solution/solution.py +++ b/introduction_and_environment/unix_and_bash/2_bash_operators/solution/solution.py @@ -1,7 +1,7 @@ # Assign the correct strings below -ONE = '' -TWO = '' -THREE = '' -FOUR = '' -FIVE = '' +ONE = 'mkdir testdir && cd testdir' +TWO = 'cat textfile.txt | grep and' +THREE = 'ls >> contents.txt' +FOUR = 'cd current || mkdir current' +FIVE = 'pwd > currentpath.txt' \ No newline at end of file diff --git a/introduction_and_environment/unix_and_bash/3_star_wars/millenium_falcon/chewbacca.txt b/introduction_and_environment/unix_and_bash/3_star_wars/millenium_falcon/chewbacca.txt new file mode 100644 index 00000000..e69de29b diff --git a/introduction_and_environment/unix_and_bash/3_star_wars/millenium_falcon/han_solo.txt b/introduction_and_environment/unix_and_bash/3_star_wars/millenium_falcon/han_solo.txt new file mode 100644 index 00000000..e69de29b diff --git a/introduction_and_environment/unix_and_bash/3_star_wars/the_rebellion/leia_organa.txt b/introduction_and_environment/unix_and_bash/3_star_wars/the_rebellion/leia_organa.txt new file mode 100644 index 00000000..e69de29b diff --git a/introduction_and_environment/unix_and_bash/3_star_wars/tie_fighter/darth_vader.txt b/introduction_and_environment/unix_and_bash/3_star_wars/tie_fighter/darth_vader.txt new file mode 100644 index 00000000..3df9b92b --- /dev/null +++ b/introduction_and_environment/unix_and_bash/3_star_wars/tie_fighter/darth_vader.txt @@ -0,0 +1 @@ +Darth Vader diff --git a/introduction_and_environment/unix_and_bash/3_star_wars/x_wing/luke_skywalker.txt b/introduction_and_environment/unix_and_bash/3_star_wars/x_wing/luke_skywalker.txt new file mode 100644 index 00000000..e69de29b diff --git a/introduction_to_python/assessment/solution.py b/introduction_to_python/assessment/solution.py new file mode 100644 index 00000000..fa77ab1b --- /dev/null +++ b/introduction_to_python/assessment/solution.py @@ -0,0 +1,23 @@ +# Quiz1 + +#Question1 +def recursive_multiply(x, y): + if x == 0: + return 0 + elif x == 1: + return y + else: + return y + recursive_multiply(x-1, y) + +# Question2 +class Ticket: + counter = 0 + def __init__(self): + self.ticket_number = Ticket.counter + if Ticket.counter == 99: + Ticket.counter = 0 + else: + Ticket.counter += 1 +for ticket in range(0, 100): + my_ticket = Ticket() + print(my_ticket.ticket_number) \ No newline at end of file diff --git a/introduction_to_python/class_protocols/1_print_vector/solution/solution.py b/introduction_to_python/class_protocols/1_print_vector/solution/solution.py index 4be32358..db0a7a40 100644 --- a/introduction_to_python/class_protocols/1_print_vector/solution/solution.py +++ b/introduction_to_python/class_protocols/1_print_vector/solution/solution.py @@ -1,6 +1,8 @@ class Vector3D: def __init__(self, x=0, y=0, z=0): - pass + self.x = x + self.y = y + self.z = z def __str__(self): - return \ No newline at end of file + return f'(x = {str(self.x)}, y = {str(self.y)}, z = {str(self.z)})' \ No newline at end of file diff --git a/introduction_to_python/class_protocols/1_vector_class_constructor/solution/solution.py b/introduction_to_python/class_protocols/1_vector_class_constructor/solution/solution.py index 77118a08..e135d5a4 100644 --- a/introduction_to_python/class_protocols/1_vector_class_constructor/solution/solution.py +++ b/introduction_to_python/class_protocols/1_vector_class_constructor/solution/solution.py @@ -1,2 +1,5 @@ class Vector3D: - pass + def __init__(self, x=0, y=0, z=0): + self.x = x + self.y = y + self.z = z diff --git a/introduction_to_python/class_protocols/2_add_vectors/solution/solution.py b/introduction_to_python/class_protocols/2_add_vectors/solution/solution.py index 468bcf31..2bab55fe 100644 --- a/introduction_to_python/class_protocols/2_add_vectors/solution/solution.py +++ b/introduction_to_python/class_protocols/2_add_vectors/solution/solution.py @@ -1,6 +1,8 @@ class Vector3D: def __init__(self, x=0, y=0, z=0): - pass + self.x = x + self.y = y + self.z = z def __add__(self, other): - return + return Vector3D(self.x + other.x, self.y + other.y, self.z + other.z) diff --git a/introduction_to_python/class_protocols/2_dot_product/solution/solution.py b/introduction_to_python/class_protocols/2_dot_product/solution/solution.py index 60007f82..9523afdd 100644 --- a/introduction_to_python/class_protocols/2_dot_product/solution/solution.py +++ b/introduction_to_python/class_protocols/2_dot_product/solution/solution.py @@ -1,6 +1,8 @@ class Vector3D: def __init__(self, x=0, y=0, z=0): - pass + self.x = x + self.y = y + self.z = z def __mul__(self, other): - return + return (self.x*other.x) + (self.y*other.y) + (self.z*other.z) diff --git a/introduction_to_python/class_protocols/2_equal_comparison/solution/solution.py b/introduction_to_python/class_protocols/2_equal_comparison/solution/solution.py index 82778c46..d5e6398e 100644 --- a/introduction_to_python/class_protocols/2_equal_comparison/solution/solution.py +++ b/introduction_to_python/class_protocols/2_equal_comparison/solution/solution.py @@ -1,6 +1,8 @@ class Vector3D: def __init__(self, x=0, y=0, z=0): - pass + self.x = x + self.y = y + self.z = z def __eq__(self, other): - return + return self.x == other.x and self.y == other.y and self.z == other.z \ No newline at end of file diff --git a/introduction_to_python/class_protocols/2_magnitude/solution/solution.py b/introduction_to_python/class_protocols/2_magnitude/solution/solution.py index afd886cb..6c4f716f 100644 --- a/introduction_to_python/class_protocols/2_magnitude/solution/solution.py +++ b/introduction_to_python/class_protocols/2_magnitude/solution/solution.py @@ -1,7 +1,9 @@ import math class Vector3D: def __init__(self, x=0, y=0, z=0): - pass + self.x = x + self.y = y + self.z = z def magnitude(self): - return + return math.sqrt(self.x**2 + self.y**2 + self.z**2) diff --git a/introduction_to_python/class_protocols/2_sub_vectors/solution/solution.py b/introduction_to_python/class_protocols/2_sub_vectors/solution/solution.py index a44d8c1d..e2354a2c 100644 --- a/introduction_to_python/class_protocols/2_sub_vectors/solution/solution.py +++ b/introduction_to_python/class_protocols/2_sub_vectors/solution/solution.py @@ -1,6 +1,8 @@ class Vector3D: def __init__(self, x=0, y=0, z=0): - pass + self.x = x + self.y = y + self.z = z def __sub__(self, other): - return + return Vector3D(self.x-other.x, self.y-other.y, self.z-other.z) \ No newline at end of file diff --git a/introduction_to_python/class_protocols/3_comparison_lt_gt/solution/solution.py b/introduction_to_python/class_protocols/3_comparison_lt_gt/solution/solution.py index a82925e2..146ee698 100644 --- a/introduction_to_python/class_protocols/3_comparison_lt_gt/solution/solution.py +++ b/introduction_to_python/class_protocols/3_comparison_lt_gt/solution/solution.py @@ -1,13 +1,15 @@ import math class Vector3D: def __init__(self, x=0, y=0, z=0): - pass + self.x = x + self.y = y + self.z = z def magnitude(self): - return + return math.sqrt(self.x**2 + self.y**2 + self.z**2) def __lt__(self, other): - return + return self.magnitude() < other.magnitude() def __gt__(self, other): - return + return self.magnitude() > other.magnitude() diff --git a/introduction_to_python/class_protocols/3_sort_vectors/solution/solution.py b/introduction_to_python/class_protocols/3_sort_vectors/solution/solution.py index 812b5bc6..fed2065f 100644 --- a/introduction_to_python/class_protocols/3_sort_vectors/solution/solution.py +++ b/introduction_to_python/class_protocols/3_sort_vectors/solution/solution.py @@ -1,23 +1,25 @@ import math class Vector3D: def __init__(self, x=0, y=0, z=0): - pass + self.x = x + self.y = y + self.z = z def __str__(self): - return + return f'(x = {str(self.x)}, y = {str(self.y)}, z = {str(self.z)})' def magnitude(self): - return + return math.sqrt(self.x**2 + self.y**2 + self.z**2) def __lt__(self, other): - return + return self.magnitude() < other.magnitude() def __gt__(self, other): - return + return self.magnitude() > other.magnitude() def __eq__(self, other): - return + return self.x == other.x and self.y == other.y and self.z == other.z def sort_vectors(vect_lst): - return \ No newline at end of file + return sorted(vect_lst) \ No newline at end of file diff --git a/introduction_to_python/classes/1_convert_to_upper/solution/solution.py b/introduction_to_python/classes/1_convert_to_upper/solution/solution.py index 29b766c0..5b7f482e 100644 --- a/introduction_to_python/classes/1_convert_to_upper/solution/solution.py +++ b/introduction_to_python/classes/1_convert_to_upper/solution/solution.py @@ -1,3 +1,11 @@ # Write your solution here class MyString(): - ... \ No newline at end of file + def __init__(self): + self.input_string = None + def set_string(self, input_string): + self.input_string = input_string + def upper_string(self): + if self.input_string == None: + return 'undefined' + else: + return self.input_string.upper() \ No newline at end of file diff --git a/introduction_to_python/classes/1_implement_pow/solution/solution.py b/introduction_to_python/classes/1_implement_pow/solution/solution.py index d33cf4ad..afdee239 100644 --- a/introduction_to_python/classes/1_implement_pow/solution/solution.py +++ b/introduction_to_python/classes/1_implement_pow/solution/solution.py @@ -1,3 +1,8 @@ class py_solution: - def pow(self, x, n): - ... \ No newline at end of file + def pow(self, x, n): + if x == 0: + return 0 + elif n == 0: + return 1 + else: + return x*pow(x, n-1) \ No newline at end of file diff --git a/introduction_to_python/classes/1_reverse_string/solution/solution.py b/introduction_to_python/classes/1_reverse_string/solution/solution.py index 34d5e5b4..651a3861 100644 --- a/introduction_to_python/classes/1_reverse_string/solution/solution.py +++ b/introduction_to_python/classes/1_reverse_string/solution/solution.py @@ -1,3 +1,5 @@ class py_solution: def reverse_words(self, str): - return ... \ No newline at end of file + words = str.split() + words = words[::-1] + return ' '.join(words) \ No newline at end of file diff --git a/introduction_to_python/classes/2_roman_to_int/solution/solution.py b/introduction_to_python/classes/2_roman_to_int/solution/solution.py index 066c943d..026544a6 100644 --- a/introduction_to_python/classes/2_roman_to_int/solution/solution.py +++ b/introduction_to_python/classes/2_roman_to_int/solution/solution.py @@ -1,3 +1,10 @@ class py_solution: def roman_to_int(self, s): - return \ No newline at end of file + rom_val = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} + int_val = 0 + for i in range(len(s)): + if i > 0 and rom_val[s[i]] > rom_val[s[i - 1]]: + int_val += rom_val[s[i]] - 2 * rom_val[s[i - 1]] + else: + int_val += rom_val[s[i]] + return int_val \ No newline at end of file diff --git a/introduction_to_python/classes/2_string_parenthesis/solution/solution.py b/introduction_to_python/classes/2_string_parenthesis/solution/solution.py index 9fabcbf8..2a852a63 100644 --- a/introduction_to_python/classes/2_string_parenthesis/solution/solution.py +++ b/introduction_to_python/classes/2_string_parenthesis/solution/solution.py @@ -1,3 +1,9 @@ class py_solution: def is_valid_parenthese(self, str): - return \ No newline at end of file + stack, pchar = [], {"(": ")", "{": "}", "[": "]"} + for parentheses in str: + if parentheses in pchar: + stack.append(parentheses) + elif len(stack) == 0 or pchar[stack.pop()] != parentheses: + return False + return len(stack) == 0 \ No newline at end of file diff --git a/introduction_to_python/classes/2_unique_subsets/solution/solution.py b/introduction_to_python/classes/2_unique_subsets/solution/solution.py index ff5e3a7d..0e50eeb1 100644 --- a/introduction_to_python/classes/2_unique_subsets/solution/solution.py +++ b/introduction_to_python/classes/2_unique_subsets/solution/solution.py @@ -1,3 +1,7 @@ class py_solution: def sub_sets(self, sset): - return + return self.subsets1([], sorted(sset)) + def subsets1(self, current, sset): + if sset: + return self.subsets1(current, sset[1:]) + self.subsets1(current + [sset[0]], sset[1:]) + return [current] \ No newline at end of file diff --git a/introduction_to_python/classes/3_multiple_inheritance/solution/solution.py b/introduction_to_python/classes/3_multiple_inheritance/solution/solution.py index de242a02..302c2ecd 100644 --- a/introduction_to_python/classes/3_multiple_inheritance/solution/solution.py +++ b/introduction_to_python/classes/3_multiple_inheritance/solution/solution.py @@ -1,21 +1,30 @@ class Clock(object): def __init__(self, hrs=0, mins=0, secs=0): - pass + self.hrs = hrs + self.mins = mins + self.secs = secs def __str__(self): - return + return "{0:02d}:{1:02d}:{2:02d}".format(self.hrs, self.mins, self.secs) class Calendar(object): def __init__(self, day=1, month=1, year=2020): - pass + self.day = day + self.month = month + self.year = year def __str__(self): - return + return "{0:02d}/{1:02d}/{2:4d}".format(self.day, self.month, self.year) class CalendarClock(Clock, Calendar): def __init__(self, day, month, year, hrs, mins, secs): - pass + self.day = day + self.month = month + self.year = year + self.hrs = hrs + self.mins = mins + self.secs = secs def __str__(self): - return \ No newline at end of file + return "{0:02d}/{1:02d}/{2:4d}".format(self.day, self.month, self.year) + ', ' + "{0:02d}:{1:02d}:{2:02d}".format(self.hrs, self.mins, self.secs) \ No newline at end of file diff --git a/introduction_to_python/functions/1_default_args/Solution/solution.py b/introduction_to_python/functions/1_default_args/Solution/solution.py index 55bdef79..4c64139d 100644 --- a/introduction_to_python/functions/1_default_args/Solution/solution.py +++ b/introduction_to_python/functions/1_default_args/Solution/solution.py @@ -1,3 +1,3 @@ # Code your solution here def concat_input(val_1="Hello ",val_2="World"): - return \ No newline at end of file + return val_1 + val_2 \ No newline at end of file diff --git a/introduction_to_python/functions/1_range/Solution/solution.py b/introduction_to_python/functions/1_range/Solution/solution.py index c8900ad6..262a8b51 100644 --- a/introduction_to_python/functions/1_range/Solution/solution.py +++ b/introduction_to_python/functions/1_range/Solution/solution.py @@ -1,3 +1,9 @@ -# Code your solution here +number = 99 def range_100(number): - return \ No newline at end of file +# number = int(input('Enter any integer value:')) + if number <= 100: + return 'GREATNESS' + else: + return 'OOPS' + +print(range_100(number)) \ No newline at end of file diff --git a/introduction_to_python/functions/1_return_hello/Solution/solution.py b/introduction_to_python/functions/1_return_hello/Solution/solution.py index 2e0cba9b..b85f1df8 100644 --- a/introduction_to_python/functions/1_return_hello/Solution/solution.py +++ b/introduction_to_python/functions/1_return_hello/Solution/solution.py @@ -1,3 +1,5 @@ -# Code your solution here +# name = input('Enter your name: ') +name = 'Newton' def hello(name): - return \ No newline at end of file + val1 = 'Hello' +' '+ name + return val1 \ No newline at end of file diff --git a/introduction_to_python/functions/2_max_value/Solution/solution.py b/introduction_to_python/functions/2_max_value/Solution/solution.py index fec0f0a6..0f3cf2e3 100644 --- a/introduction_to_python/functions/2_max_value/Solution/solution.py +++ b/introduction_to_python/functions/2_max_value/Solution/solution.py @@ -1,3 +1,3 @@ -# Code your solution here def max_val(a, b, c): - return \ No newline at end of file + max_value = max(a, b, c) + return max_value \ No newline at end of file diff --git a/introduction_to_python/functions/2_shut_down/Solution/solution.py b/introduction_to_python/functions/2_shut_down/Solution/solution.py index 333edc82..5c153f1f 100644 --- a/introduction_to_python/functions/2_shut_down/Solution/solution.py +++ b/introduction_to_python/functions/2_shut_down/Solution/solution.py @@ -1,3 +1,9 @@ -# Code your solution here +# x = bool(input('Enter a boolean value: ')) +x = 'false' def shut_down(x): - return \ No newline at end of file + if x == True: + return 'SHUTDOWN' + elif x == False: + return 'SHUTDOWN ABORTED' + else: + return '.' \ No newline at end of file diff --git a/introduction_to_python/functions/2_variable_argument/Solution/solution.py b/introduction_to_python/functions/2_variable_argument/Solution/solution.py index 0f6921b1..dbbcec4b 100644 --- a/introduction_to_python/functions/2_variable_argument/Solution/solution.py +++ b/introduction_to_python/functions/2_variable_argument/Solution/solution.py @@ -1,5 +1,2 @@ -# Code your solution here def concat_args(*args): - return - - + return ''.join(args) diff --git a/introduction_to_python/functions/3_anonymous/Solution/solution.py b/introduction_to_python/functions/3_anonymous/Solution/solution.py new file mode 100644 index 00000000..f3496960 --- /dev/null +++ b/introduction_to_python/functions/3_anonymous/Solution/solution.py @@ -0,0 +1,3 @@ +# Code your solution here +def f(*args): + return list(map(lambda x: x % 13 == 0)) \ No newline at end of file diff --git a/introduction_to_python/functions/3_count_even/Solution/solution.py b/introduction_to_python/functions/3_count_even/Solution/solution.py index abdfec29..93ea2775 100644 --- a/introduction_to_python/functions/3_count_even/Solution/solution.py +++ b/introduction_to_python/functions/3_count_even/Solution/solution.py @@ -1,3 +1,2 @@ -# Code your solution here def count_even(*args): - return \ No newline at end of file + return len(list(filter(lambda x: x % 2 == 0, args))) \ No newline at end of file diff --git a/introduction_to_python/functions/3_div_by_3_annonymous/Solution/solution.py b/introduction_to_python/functions/3_div_by_3_annonymous/Solution/solution.py index aadbaeb8..043c48b3 100644 --- a/introduction_to_python/functions/3_div_by_3_annonymous/Solution/solution.py +++ b/introduction_to_python/functions/3_div_by_3_annonymous/Solution/solution.py @@ -1,3 +1,3 @@ # Code your solution here def div_by_3(*args): - return \ No newline at end of file + return list(filter(lambda x: x % 3 == 0, args)) \ No newline at end of file diff --git a/introduction_to_python/functions/3_factorial/Solution/solution.py b/introduction_to_python/functions/3_factorial/Solution/solution.py index b733941e..ba7eaaa0 100644 --- a/introduction_to_python/functions/3_factorial/Solution/solution.py +++ b/introduction_to_python/functions/3_factorial/Solution/solution.py @@ -1,3 +1,11 @@ -# Code your solution here def factorial(n): - return \ No newline at end of file + factorial = 1 + if n < 0: + return None + elif None == 0: + return None + # if int(n) >= 1: + else: + for i in range(1, int(n) + 1): + factorial = factorial * i + return factorial \ No newline at end of file diff --git a/introduction_to_python/functions/3_total/Solution/solution.py b/introduction_to_python/functions/3_total/Solution/solution.py index 6162db4b..8a5dba47 100644 --- a/introduction_to_python/functions/3_total/Solution/solution.py +++ b/introduction_to_python/functions/3_total/Solution/solution.py @@ -1,3 +1,8 @@ -# Code your solution here +l = [10, 20, 30, 40, 50, 60] +# l = [1, 2, 3, 4, 5] +# used the list provided in test_solution.py for the test to work def sum_data(lst): - return \ No newline at end of file + summ = 0 + for i in lst: + summ += i + return summ \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/1_addition/Solution/app_solution.py b/introduction_to_python/mvc_architecture/1_addition/Solution/app_solution.py index 1c878f3e..e323b591 100644 --- a/introduction_to_python/mvc_architecture/1_addition/Solution/app_solution.py +++ b/introduction_to_python/mvc_architecture/1_addition/Solution/app_solution.py @@ -1,2 +1,9 @@ import model_solution as model import view_solution as view + +number_1=input(view.capture_number1()) +number_2=input(view.capture_number2()) +result=int(number_1)+int(number_2) +model.store([number_1, number_2, result]) + +print(view.display(result)) \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/1_addition/Solution/model_solution.py b/introduction_to_python/mvc_architecture/1_addition/Solution/model_solution.py index 6536c365..fe065f8a 100644 --- a/introduction_to_python/mvc_architecture/1_addition/Solution/model_solution.py +++ b/introduction_to_python/mvc_architecture/1_addition/Solution/model_solution.py @@ -1,3 +1,3 @@ - +data = [] def store(result): - return + return data.append(result) diff --git a/introduction_to_python/mvc_architecture/1_addition/Solution/view_solution.py b/introduction_to_python/mvc_architecture/1_addition/Solution/view_solution.py index dc8db3eb..3160db8c 100644 --- a/introduction_to_python/mvc_architecture/1_addition/Solution/view_solution.py +++ b/introduction_to_python/mvc_architecture/1_addition/Solution/view_solution.py @@ -1,7 +1,6 @@ def capture_number1(): - return + return f'Enter a number: ' def capture_number2(): - return + return f'Enter another number: ' def display(result): - return - + return f'{result}' \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/1_delete_key/Solution/app_solution.py b/introduction_to_python/mvc_architecture/1_delete_key/Solution/app_solution.py index 1c878f3e..dad38af9 100644 --- a/introduction_to_python/mvc_architecture/1_delete_key/Solution/app_solution.py +++ b/introduction_to_python/mvc_architecture/1_delete_key/Solution/app_solution.py @@ -1,2 +1,8 @@ import model_solution as model import view_solution as view + +movie_list = model.get_list() +del_key = input(view.delete_key()) +model.del_list_key(del_key) +final_list = model.get_list() +print(view.display(final_list)) \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/1_delete_key/Solution/model_solution.py b/introduction_to_python/mvc_architecture/1_delete_key/Solution/model_solution.py index 4c0cb967..4b462042 100644 --- a/introduction_to_python/mvc_architecture/1_delete_key/Solution/model_solution.py +++ b/introduction_to_python/mvc_architecture/1_delete_key/Solution/model_solution.py @@ -1,8 +1,7 @@ movie_list={'1':'World War Z','2':'Inception','3':'Black Mirror','4':'Once Upon A Time In Hollywood','5':'Birdman'} def get_list(): - return + return str(movie_list) def del_list_key(del_key): - pass - + del movie_list[del_key] \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/1_delete_key/Solution/view_solution.py b/introduction_to_python/mvc_architecture/1_delete_key/Solution/view_solution.py index 0593e2e7..692d85c5 100644 --- a/introduction_to_python/mvc_architecture/1_delete_key/Solution/view_solution.py +++ b/introduction_to_python/mvc_architecture/1_delete_key/Solution/view_solution.py @@ -1,5 +1,5 @@ def delete_key(): - return + return f'What key do you want to delete?: ' def display(final_list): - return \ No newline at end of file + return f'{final_list}' \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/1_details/Solution/app_solution.py b/introduction_to_python/mvc_architecture/1_details/Solution/app_solution.py index e71d316e..b2211fc7 100644 --- a/introduction_to_python/mvc_architecture/1_details/Solution/app_solution.py +++ b/introduction_to_python/mvc_architecture/1_details/Solution/app_solution.py @@ -1,3 +1,8 @@ import view_solution as view import model_solution as model +name = input(view.capture_name()) +age = input(view.capture_age()) +model.store(name,age) +info = view.display(name, age) +print(info) \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/1_details/Solution/model_solution.py b/introduction_to_python/mvc_architecture/1_details/Solution/model_solution.py index 20790038..01528e9c 100644 --- a/introduction_to_python/mvc_architecture/1_details/Solution/model_solution.py +++ b/introduction_to_python/mvc_architecture/1_details/Solution/model_solution.py @@ -1,3 +1,3 @@ data=[] def store(name,age): - return \ No newline at end of file + data.append((name, age)) \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/1_details/Solution/view_solution.py b/introduction_to_python/mvc_architecture/1_details/Solution/view_solution.py index 45fae5aa..9d1db399 100644 --- a/introduction_to_python/mvc_architecture/1_details/Solution/view_solution.py +++ b/introduction_to_python/mvc_architecture/1_details/Solution/view_solution.py @@ -1,8 +1,8 @@ def capture_name(): - return + return f'Enter your name: ' def capture_age(): - return + return f'Enter your age: ' def display(name,age): - return \ No newline at end of file + return f'{name}, {age}' \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/1_update_list/Solution/app_solution.py b/introduction_to_python/mvc_architecture/1_update_list/Solution/app_solution.py index 5e0d8984..c5296a58 100644 --- a/introduction_to_python/mvc_architecture/1_update_list/Solution/app_solution.py +++ b/introduction_to_python/mvc_architecture/1_update_list/Solution/app_solution.py @@ -1,2 +1,10 @@ import view_solution as view -import model_solution as model \ No newline at end of file +import model_solution as model + +fruits = model.get_store() +show = view.show_fruits(fruits) +print(show) +fruit = input(view.ask_fruit()) +model.update_fruit(fruit) +data = view.show_fruits(fruits) +print(data) \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/1_update_list/Solution/model_solution.py b/introduction_to_python/mvc_architecture/1_update_list/Solution/model_solution.py index e6b44e57..72807d6a 100644 --- a/introduction_to_python/mvc_architecture/1_update_list/Solution/model_solution.py +++ b/introduction_to_python/mvc_architecture/1_update_list/Solution/model_solution.py @@ -4,4 +4,6 @@ def get_store(): return fruits def update_fruit(fruit): - return \ No newline at end of file + # fruits.append(fruit) + for index, value in enumerate(fruits): + return index,value \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/1_update_list/Solution/view_solution.py b/introduction_to_python/mvc_architecture/1_update_list/Solution/view_solution.py index 69a4f56b..8d1ae771 100644 --- a/introduction_to_python/mvc_architecture/1_update_list/Solution/view_solution.py +++ b/introduction_to_python/mvc_architecture/1_update_list/Solution/view_solution.py @@ -1,6 +1,6 @@ def ask_fruit(): - return + return f'Enter a fruit: ' def show_fruits(fruits): - return + return f'{fruits}' diff --git a/introduction_to_python/mvc_architecture/2_books/Solution/app_solution.py b/introduction_to_python/mvc_architecture/2_books/Solution/app_solution.py index 8e7eefbc..9952d3bf 100644 --- a/introduction_to_python/mvc_architecture/2_books/Solution/app_solution.py +++ b/introduction_to_python/mvc_architecture/2_books/Solution/app_solution.py @@ -1,3 +1,10 @@ import model_solution as model import view_solution as view +book_list = model.get_list() +show = view.show_list(book_list) +print(show) +book = input(view.retrive_book()) +author = model.retrive_book(book) +data = view.display(book, author) +print(data) \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/2_books/Solution/model_solution.py b/introduction_to_python/mvc_architecture/2_books/Solution/model_solution.py index 178d7c44..958e8b6f 100644 --- a/introduction_to_python/mvc_architecture/2_books/Solution/model_solution.py +++ b/introduction_to_python/mvc_architecture/2_books/Solution/model_solution.py @@ -1,15 +1,13 @@ import json book_author_list={"books":{"A Tale of Two Cities":'Charles Dickens',"Lord Of The Rings":'J.R.R. Tolkien',"Dark Psychology":'Steven Turner',"Rich Dad Poor Dad":'Robert T Kiyosaki'}} - +book_list = json.dumps(book_author_list) +# json.dumps() is a method in which it turns a python object into a JSON string def get_list(): - return + return str(book_list) def retrive_book(book): - return - - - - - - - + x = json.loads(book_list) + books = x['books'] + result = books.get(book) + return result +# json.loads() is a method in which it parses a JSON string \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/2_books/Solution/view_solution.py b/introduction_to_python/mvc_architecture/2_books/Solution/view_solution.py index 543c48b6..396197e8 100644 --- a/introduction_to_python/mvc_architecture/2_books/Solution/view_solution.py +++ b/introduction_to_python/mvc_architecture/2_books/Solution/view_solution.py @@ -1,8 +1,8 @@ def show_list(book_list): - return + return f'{book_list}' def retrive_book(): - return + return f'Enter the name of the book you want: ' def display(book,author): - return \ No newline at end of file + return f'{book}{author}' \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/2_file_write/Solution/app_solution.py b/introduction_to_python/mvc_architecture/2_file_write/Solution/app_solution.py index 78cd8bf5..ff2f44be 100644 --- a/introduction_to_python/mvc_architecture/2_file_write/Solution/app_solution.py +++ b/introduction_to_python/mvc_architecture/2_file_write/Solution/app_solution.py @@ -1,2 +1,10 @@ import view_solution as view import model_solution as model + +file_name = input(view.ask_fileName()) +model.create(file_name) +content = input(view.ask_content()) +model.write(file_name, content) +data = model.read(file_name) +result = view.display(data) +print(result) \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/2_file_write/Solution/model_solution.py b/introduction_to_python/mvc_architecture/2_file_write/Solution/model_solution.py index 56fa05a6..49b389ca 100644 --- a/introduction_to_python/mvc_architecture/2_file_write/Solution/model_solution.py +++ b/introduction_to_python/mvc_architecture/2_file_write/Solution/model_solution.py @@ -1,8 +1,11 @@ def create(file_name): - return + open(f'{file_name}.txt', 'w') def write(file,content): - return + with open(f'{file}.txt', 'w') as file_writer: + file_writer.write(content) def read(file): - return \ No newline at end of file + with open(f'{file}.txt', 'r') as file_reader: + x = file_reader.read() + return x \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/2_file_write/Solution/view_solution.py b/introduction_to_python/mvc_architecture/2_file_write/Solution/view_solution.py index 4ceef4e3..49791107 100644 --- a/introduction_to_python/mvc_architecture/2_file_write/Solution/view_solution.py +++ b/introduction_to_python/mvc_architecture/2_file_write/Solution/view_solution.py @@ -1,10 +1,8 @@ def ask_fileName(): - return + return f'What is the name of the file you are searching for?: ' def ask_content(): - return + return f'What content are you searching for?: ' def display(data): - return - - + return f'{data}' \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/2_file_write/filename_x.txt b/introduction_to_python/mvc_architecture/2_file_write/filename_x.txt new file mode 100644 index 00000000..0ae5e8d3 --- /dev/null +++ b/introduction_to_python/mvc_architecture/2_file_write/filename_x.txt @@ -0,0 +1 @@ +filename_y \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/2_update_item_value/Solution/app_solution.py b/introduction_to_python/mvc_architecture/2_update_item_value/Solution/app_solution.py index 1c878f3e..70752a3d 100644 --- a/introduction_to_python/mvc_architecture/2_update_item_value/Solution/app_solution.py +++ b/introduction_to_python/mvc_architecture/2_update_item_value/Solution/app_solution.py @@ -1,2 +1,9 @@ import model_solution as model import view_solution as view + +program_list = model.get_list +key = input(view.update_key()) +value = input(view.enter_value()) +new_list = model.update_list_key(key, value) +result = view.display(new_list) +print(result) \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/2_update_item_value/Solution/model_solution.py b/introduction_to_python/mvc_architecture/2_update_item_value/Solution/model_solution.py index f0cf1614..4a2c500c 100644 --- a/introduction_to_python/mvc_architecture/2_update_item_value/Solution/model_solution.py +++ b/introduction_to_python/mvc_architecture/2_update_item_value/Solution/model_solution.py @@ -1,9 +1,8 @@ program_list={'1':'Perl','2':'C','3':'C++','4':'Java','5':'Python'} def get_list(): - return + return str(program_list) def update_list_key(key,value): - return - - + for key, value in enumerate(program_list): + return key, value \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/2_update_item_value/Solution/view_solution.py b/introduction_to_python/mvc_architecture/2_update_item_value/Solution/view_solution.py index c4309d62..ca8afa83 100644 --- a/introduction_to_python/mvc_architecture/2_update_item_value/Solution/view_solution.py +++ b/introduction_to_python/mvc_architecture/2_update_item_value/Solution/view_solution.py @@ -1,11 +1,11 @@ def show_list(program_list): - return + return f'{program_list}' def update_key(): - return + return f'Enter a key: ' def enter_value(): - return + return f'Enter a value: ' def display(new_list): - return \ No newline at end of file + return f'{new_list}' \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/3_json_match/Solution/app_solution.py b/introduction_to_python/mvc_architecture/3_json_match/Solution/app_solution.py index 1c878f3e..5426550c 100644 --- a/introduction_to_python/mvc_architecture/3_json_match/Solution/app_solution.py +++ b/introduction_to_python/mvc_architecture/3_json_match/Solution/app_solution.py @@ -1,2 +1,13 @@ import model_solution as model import view_solution as view + +data_list = model.get_list() +show = view.show_list(data_list) +print(show) +sport = input(view.retrive_sport()) +country = input(view.retrive_country()) +data_a = model.retrive_match(sport, country) +# data_b = model.retrive_match(country) +# result = data_a + ' ' + data_b +result = data_a +print(result) \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/3_json_match/Solution/model_solution.py b/introduction_to_python/mvc_architecture/3_json_match/Solution/model_solution.py index 511f0fa9..2e452fd9 100644 --- a/introduction_to_python/mvc_architecture/3_json_match/Solution/model_solution.py +++ b/introduction_to_python/mvc_architecture/3_json_match/Solution/model_solution.py @@ -3,14 +3,10 @@ data_match=json.dumps(match_data) def get_list(): - return + return str(data_match) def retrive_match(sport,country): - return - - - - - - - + x = json.loads(data_match) + sport_country = x['sport', 'country'] + result = sport_country.get(sport, country) + return result \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/3_json_match/Solution/view_solution.py b/introduction_to_python/mvc_architecture/3_json_match/Solution/view_solution.py index 69aade0d..35f1e8d6 100644 --- a/introduction_to_python/mvc_architecture/3_json_match/Solution/view_solution.py +++ b/introduction_to_python/mvc_architecture/3_json_match/Solution/view_solution.py @@ -1,11 +1,11 @@ def show_list(data_list): - return + return f'{data_list}' def retrive_sport(): - return + return f'Which sport are you looking for?: ' def retrive_country(): - return + return f'Which country are you looking for?: ' def display(sport,country): - return \ No newline at end of file + return f'{sport} {country}' \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/3_json_update/Solution/app_solution.py b/introduction_to_python/mvc_architecture/3_json_update/Solution/app_solution.py index 3dd36bc4..d94fa6a9 100644 --- a/introduction_to_python/mvc_architecture/3_json_update/Solution/app_solution.py +++ b/introduction_to_python/mvc_architecture/3_json_update/Solution/app_solution.py @@ -1,4 +1,11 @@ import model_solution as model import view_solution as view - +data_list = model.get_list() +show = view.show_list(data_list) +print(show) +sport = input(view.retrive_sport()) +country = input(view.retrive_country()) +data_a = model.retrive_match(sport, country) +result = data_a +print(result) \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/3_json_update/Solution/model_solution.py b/introduction_to_python/mvc_architecture/3_json_update/Solution/model_solution.py index d3b7a8a5..f564c6d0 100644 --- a/introduction_to_python/mvc_architecture/3_json_update/Solution/model_solution.py +++ b/introduction_to_python/mvc_architecture/3_json_update/Solution/model_solution.py @@ -3,13 +3,12 @@ data_match=json.dumps(match_data) def get_list(): - return + return str(data_match) def retrive_match(sport,country): - return - - - - - - + x = json.loads(data_match) + sport_lst=x['sports'] + sport_lst.append(sport) + country_lst=x['country'] + country_lst.append(country) + return x \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/3_json_update/Solution/view_solution.py b/introduction_to_python/mvc_architecture/3_json_update/Solution/view_solution.py index ceff9d64..41a2d1de 100644 --- a/introduction_to_python/mvc_architecture/3_json_update/Solution/view_solution.py +++ b/introduction_to_python/mvc_architecture/3_json_update/Solution/view_solution.py @@ -1,11 +1,11 @@ def show_list(data_list): - return + return f'{data_list}' def retrive_sport(): - return + return f'Which sport would you like to retrieve?: ' def retrive_country(): - return + return f'Which country would you like to retrieve?: ' def display(data): - return \ No newline at end of file + return f'{data}' \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/3_sql_connection/Solution/app_solution.py b/introduction_to_python/mvc_architecture/3_sql_connection/Solution/app_solution.py index 78cd8bf5..c123dabe 100644 --- a/introduction_to_python/mvc_architecture/3_sql_connection/Solution/app_solution.py +++ b/introduction_to_python/mvc_architecture/3_sql_connection/Solution/app_solution.py @@ -1,2 +1,8 @@ import view_solution as view import model_solution as model + +model.create() +result = model.query() +for i in range(len(result)): + data = view.display(result[i][0], result[i][1]) +print(data) \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/3_sql_connection/Solution/model_solution.py b/introduction_to_python/mvc_architecture/3_sql_connection/Solution/model_solution.py index 0b0a809d..d790d02c 100644 --- a/introduction_to_python/mvc_architecture/3_sql_connection/Solution/model_solution.py +++ b/introduction_to_python/mvc_architecture/3_sql_connection/Solution/model_solution.py @@ -2,7 +2,20 @@ def create(): - return - -def query(): + connection = sqlite3.connect('db.db') + cursor = connection.cursor() + query = 'CREATE TABLE IF NOT EXISTS student(student_id integer , student_name text);' + cursor.execute(query) return + +def query(): + connection=sqlite3.connect('db.db') + cursor=connection.cursor() + query="INSERT INTO student VALUES(1,'abc');" + cursor.execute(query) + query="INSERT INTO student VALUES(2,'xyz');" + cursor.execute(query) + query="SELECT * FROM student;" + cursor.execute(query) + data=cursor.fetchall() + return data \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/3_sql_connection/Solution/view_solution.py b/introduction_to_python/mvc_architecture/3_sql_connection/Solution/view_solution.py index 8afea60b..4f6194c3 100644 --- a/introduction_to_python/mvc_architecture/3_sql_connection/Solution/view_solution.py +++ b/introduction_to_python/mvc_architecture/3_sql_connection/Solution/view_solution.py @@ -1,5 +1,3 @@ def display(result_1,result_2): - return - - + return f'{result_1} {result_2}' \ No newline at end of file diff --git a/introduction_to_python/mvc_architecture/3_sql_connection/db.db b/introduction_to_python/mvc_architecture/3_sql_connection/db.db new file mode 100644 index 00000000..ccbf333e Binary files /dev/null and b/introduction_to_python/mvc_architecture/3_sql_connection/db.db differ diff --git a/introduction_to_python/recursive_functions/1_array_sum/solution/solution.py b/introduction_to_python/recursive_functions/1_array_sum/solution/solution.py index 8fd0f657..98755e88 100644 --- a/introduction_to_python/recursive_functions/1_array_sum/solution/solution.py +++ b/introduction_to_python/recursive_functions/1_array_sum/solution/solution.py @@ -1,3 +1,6 @@ # Write your solution here def sum_array(num_list): - return \ No newline at end of file + if num_list == []: + return 0 + else: + return num_list.pop() + sum_array(num_list) \ No newline at end of file diff --git a/introduction_to_python/recursive_functions/1_array_sum/solutions/solution.py b/introduction_to_python/recursive_functions/1_array_sum/solutions/solution.py new file mode 100644 index 00000000..f99e96e7 --- /dev/null +++ b/introduction_to_python/recursive_functions/1_array_sum/solutions/solution.py @@ -0,0 +1,3 @@ +# Write your solution here +def sum_array(num_list): + return 0 if num_list == [] else num_list[0] + sum_array(num_list[1:]) \ No newline at end of file diff --git a/introduction_to_python/recursive_functions/1_count_down/solution/solution.py b/introduction_to_python/recursive_functions/1_count_down/solution/solution.py index 309e8ed7..b6ce9f00 100644 --- a/introduction_to_python/recursive_functions/1_count_down/solution/solution.py +++ b/introduction_to_python/recursive_functions/1_count_down/solution/solution.py @@ -1,3 +1,5 @@ # Write your solution here def count_down_from(num): - return \ No newline at end of file + if num >= 1: + print(num) + count_down_from(num-1) \ No newline at end of file diff --git a/introduction_to_python/recursive_functions/1_count_down/solutions/solution.py b/introduction_to_python/recursive_functions/1_count_down/solutions/solution.py new file mode 100644 index 00000000..4342bf27 --- /dev/null +++ b/introduction_to_python/recursive_functions/1_count_down/solutions/solution.py @@ -0,0 +1,5 @@ +# Write your solution here +def count_down_from(num): + return num + if num >= 1: + count_down_from(num - 1) \ No newline at end of file diff --git a/introduction_to_python/recursive_functions/1_find_power/solution/solution.py b/introduction_to_python/recursive_functions/1_find_power/solution/solution.py index 7aa17747..fcc11c10 100644 --- a/introduction_to_python/recursive_functions/1_find_power/solution/solution.py +++ b/introduction_to_python/recursive_functions/1_find_power/solution/solution.py @@ -1,3 +1,8 @@ # Write your solution here def power(a, b): - return \ No newline at end of file + if a == 0: + return 0 + elif b == 0: + return 1 + else: + return a*power(a,b-1) \ No newline at end of file diff --git a/introduction_to_python/recursive_functions/1_reverse_string/solution/solution.py b/introduction_to_python/recursive_functions/1_reverse_string/solution/solution.py index 4b4c9b1d..445f3475 100644 --- a/introduction_to_python/recursive_functions/1_reverse_string/solution/solution.py +++ b/introduction_to_python/recursive_functions/1_reverse_string/solution/solution.py @@ -1,3 +1,6 @@ # Write your solution here def reverse(string): - return \ No newline at end of file + if string == '': + return '' + else: + return reverse(string[1:]) + string[0] \ No newline at end of file diff --git a/introduction_to_python/recursive_functions/2_count_ways/solution/solution.py b/introduction_to_python/recursive_functions/2_count_ways/solution/solution.py index 4921b2c4..66d9207c 100644 --- a/introduction_to_python/recursive_functions/2_count_ways/solution/solution.py +++ b/introduction_to_python/recursive_functions/2_count_ways/solution/solution.py @@ -1,3 +1,8 @@ # Write your solution here +def stairs(n): + if n <= 1: + return n + else: + return stairs(n - 1) + stairs(n - 2) def count_ways(steps): - return + return stairs(steps + 1) diff --git a/introduction_to_python/recursive_functions/2_fibonacci/solution/solution.py b/introduction_to_python/recursive_functions/2_fibonacci/solution/solution.py index 129c4332..e44b28ad 100644 --- a/introduction_to_python/recursive_functions/2_fibonacci/solution/solution.py +++ b/introduction_to_python/recursive_functions/2_fibonacci/solution/solution.py @@ -1,3 +1,8 @@ # Write your solution here def fibonacci(n): - return + if n == 0: + return 0 + elif n == 1: + return 1 + else: + return (fibonacci(n-1) + fibonacci(n-2)) diff --git a/introduction_to_python/recursive_functions/2_find_factorial/solution/solution.py b/introduction_to_python/recursive_functions/2_find_factorial/solution/solution.py index 41e8caef..894cbb9f 100644 --- a/introduction_to_python/recursive_functions/2_find_factorial/solution/solution.py +++ b/introduction_to_python/recursive_functions/2_find_factorial/solution/solution.py @@ -1,3 +1,6 @@ # Write your solution here def factorial_recursive(n): - return \ No newline at end of file + if n <= 1: + return 1 + else: + return n * factorial_recursive(n-1) \ No newline at end of file diff --git a/introduction_to_python/recursive_functions/3_merge_sort/solution/solution.py b/introduction_to_python/recursive_functions/3_merge_sort/solution/solution.py index a66e3e81..d3e47862 100644 --- a/introduction_to_python/recursive_functions/3_merge_sort/solution/solution.py +++ b/introduction_to_python/recursive_functions/3_merge_sort/solution/solution.py @@ -1,3 +1,28 @@ # Write your solution here def custom_sort(num_list): - return \ No newline at end of file + if len(num_list)>1: + mid = len(num_list) // 2 + left = num_list[:mid] + right = num_list[mid:] + custom_sort(left) + custom_sort(right) + a = b = c = 0 + + while a < len(left) and b < len(right): + if left[a] < right[b]: + num_list[c] = left[a] + a += 1 + c += 1 + else: + num_list[c] = right[b] + b += 1 + c += 1 + while a < len(left): + num_list[c] = left[a] + a += 1 + c += 1 + while b < len(right): + num_list[c] = right[b] + b += 1 + c += 1 + return num_list \ No newline at end of file diff --git a/introduction_to_python/recursive_functions/3_towers_of_hanoi/solution/solution.py b/introduction_to_python/recursive_functions/3_towers_of_hanoi/solution/solution.py index b2ab499c..68d0b4c2 100644 --- a/introduction_to_python/recursive_functions/3_towers_of_hanoi/solution/solution.py +++ b/introduction_to_python/recursive_functions/3_towers_of_hanoi/solution/solution.py @@ -1,3 +1,10 @@ # Write your solution here def tower_of_hanoi(n , from_rod, to_rod, aux_rod): - return + if n <= 1: + return n + else: + counter = tower_of_hanoi(n-1, from_rod, aux_rod, to_rod) + to_rod.append(from_rod.pop()) + counter += 1 + counter += tower_of_hanoi(n-1, aux_rod, to_rod, from_rod) + return counter