diff --git a/Python/FindParityOutlier.py b/Python/FindParityOutlier.py new file mode 100644 index 0000000..206ca11 --- /dev/null +++ b/Python/FindParityOutlier.py @@ -0,0 +1,40 @@ +''' +You are given an array (which will have a length of at least 3, but +could be very large) containing integers. The array is either entirely +comprised of odd integers or entirely comprised of even integers except +for a single integer N. Write a method that takes the array as an +argument and returns this "outlier" N. + +Examples: +[2, 4, 0, 100, 4, 11, 2602, 36] +Should return: 11 (the only odd number) + +[160, 3, 1719, 19, 11, 13, -21] +Should return: 160 (the only even number) + +''' + + +def find_outlier(integers): + probe = integers[0] % 2 + integers[1] % 2 + integers[2] % 2 + # Odd + if probe == 2 or probe == 3: + for x in integers: + if x % 2 == 0: + return x + # Even + else: + for x in integers: + if x % 2 == 1: + return x + +# Another solution +# def find_outlier(int): +# odds = [x for x in int if x % 2 != 0] +# evens = [x for x in int if x % 2 == 0] +# return odds[0] if len(odds) < len(evens) else evens[0] + + +if __name__ == "__main__": + print(find_outlier([2, 4, 0, 100, 4, 11, 2602, 36])) + print(find_outlier([160, 3, 1719, 19, 11, 13, -21])) diff --git a/Python/FriendOrFoe.py b/Python/FriendOrFoe.py new file mode 100644 index 0000000..da95670 --- /dev/null +++ b/Python/FriendOrFoe.py @@ -0,0 +1,27 @@ +''' +Make a program that filters a list of strings and returns a list +with only your friends name in it. +If a name has exactly 4 letters in it, you can be sure that it has +to be a friend of yours! Otherwise, you can be sure they're not... + +Example: +Input = ["Ryan", "Kieran", "Jason", "Yous"] +Output = ["Ryan", "Yous"] + +''' + + +def friend(x): + friendslist = [] + for name in x: + if len(name) == 4: + friendslist.append(name) + return friendslist + +# Another Solution +# def friend(x): +# return [f for f in x if len(f) == 4] + + +if __name__ == "__main__": + print(friend(["Ryan", "Kieran", "Jason", "Yous"])) diff --git a/Python/PersistentBugger.py b/Python/PersistentBugger.py new file mode 100644 index 0000000..99e9402 --- /dev/null +++ b/Python/PersistentBugger.py @@ -0,0 +1,36 @@ +''' +Write a function, persistence, that takes in a positive +parameter num and returns its multiplicative persistence, +which is the number of times you must multiply the digits +in num until you reach a single digit. + +Example: +persistence(39) => 3 # Because 3*9 = 27, 2*7 = 14, 1*4=4 + # and 4 has only one digit. + +persistence(999) => 4 # Because 9*9*9 = 729, 7*2*9 = 126, + # 1*2*6 = 12, and finally 1*2 = 2. + +persistence(4) => 0 # Because 4 is already a one-digit number. + +''' + +# Apdapted from https://www.codewars.com/kata/reviews/55c7dba2ea4fa879c4000015/groups/57f2bc8e69e09cb01c0000c4 + + +def persistence(n): + n = str(n) + count = 0 + while len(n) > 1: + sum = 1 + for x in n: + sum *= int(x) + n = str(sum) + count += 1 + return count + + +if __name__ == "__main__": + print(persistence(39)) + print(persistence(999)) + print(persistence(4)) diff --git a/Python/UniqueInOrder.py b/Python/UniqueInOrder.py new file mode 100644 index 0000000..94b614d --- /dev/null +++ b/Python/UniqueInOrder.py @@ -0,0 +1,42 @@ +''' +Implement the function unique_in_order which takes as argument a sequence and +returns a list of items without any elements with the same value next to each +other and preserving the original order of elements. + +Examples: +unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B'] +unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D'] +unique_in_order([1,2,2,3,3]) == [1,2,3] + +''' + + +def unique_in_order(iterable): + # Stores our uniques to be returned as the final result + uniques = [] + # Loop through iterable + for x in iterable: + if x in uniques: + if x != pastUnique: + uniques.append(x) + pastUnique = x + else: + uniques.append(x) + pastUnique = x + return uniques + +# Another implementation that is better +# def unique_in_order(iterable): +# result = [] +# prev = None +# for char in iterable[0:]: +# if char != prev: +# result.append(char) +# prev = char +# return result + + +if __name__ == "__main__": + print(unique_in_order('AAAABBBCCDAABBB')) + print(unique_in_order('ABBCcAD')) + print(unique_in_order([1, 2, 2, 3, 3])) diff --git a/Python/linked lists/copyListWithRandomPointers.py b/Python/linked lists/copyListWithRandomPointers.py new file mode 100644 index 0000000..daf6d2f --- /dev/null +++ b/Python/linked lists/copyListWithRandomPointers.py @@ -0,0 +1,29 @@ +# https://leetcode.com/problems/copy-list-with-random-pointer/ + +class Node: + def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None): + self.val = int(x) + self.next = next + self.random = random + + +def copyRandomList(head: Node) -> Node: + if not head: + return None + curr = head + original_to_clone = {} + while curr: + original_to_clone[curr] = Node(curr.val) + curr = curr.next + + for k, v in original_to_clone.items(): + if k.next: + v.next = original_to_clone[k.next] + else: + v.next = None + if k.random: + v.random = original_to_clone[k.random] + else: + v.random = None + + return original_to_clone[head] \ No newline at end of file diff --git a/Python/meetingRoomsTwo.py b/Python/meetingRoomsTwo.py new file mode 100644 index 0000000..5d52ba0 --- /dev/null +++ b/Python/meetingRoomsTwo.py @@ -0,0 +1,21 @@ +# https://leetcode.com/problems/meeting-rooms-ii/ + +# space: O(n), time: O(nlogn) +import heapq + + +def minMeetingRooms(intervals: list[list[int]]) -> int: + if len(intervals) == 1: + return 1 + answer = 1 + sorted_intervals = sorted(intervals, key=lambda x: x[0]) + # add the first interval's end time to the heap + end_times_min_heap = [sorted_intervals[0][1]] + for interval in sorted_intervals[1:]: + min_end_time = heapq.nsmallest(1, end_times_min_heap)[0] + if interval[0] < min_end_time: + heapq.heappush(end_times_min_heap, interval[1]) + answer += 1 + else: + heapq.heapreplace(end_times_min_heap, interval[1]) + return answer \ No newline at end of file diff --git a/Ruby/MinutesAfterMidnight.rb b/Ruby/MinutesAfterMidnight.rb new file mode 100644 index 0000000..7233cb4 --- /dev/null +++ b/Ruby/MinutesAfterMidnight.rb @@ -0,0 +1,29 @@ +=begin +Time after midnight test +Not associated with a LeetCode test +Takes in a String for time in the format 1:36 PM and outputs how many minutes that time is after midnight +Dr. Schrum was talking about this problem in class so I tried it. +I believe this works but it could be wrong +=end + +# Author = Nolan Winsman + + +# @param [String] time +# @return [Integer] +def minutes_after_midnight (time) + # adds a leading zero to make the formating easier for times like 1:23 PM + time = "0" + time if time.length == 7 + return 0 if time == "12:00 AM" + return 12 * 60 if time == "12:00 PM" + hours = time[0,2].to_i # .to_i casts to integer + minutes = time[3,4].to_i + total_minutes = (hours * 60) + minutes + total_minutes += (12 * 60) if time[6] == 'P' + return total_minutes +end + +puts "Enter a time in the format 11:23 AM" +input = gets.chomp +puts minutes_after_midnight input + diff --git a/Ruby/TwoSum.rb b/Ruby/TwoSum.rb new file mode 100644 index 0000000..00ac9e7 --- /dev/null +++ b/Ruby/TwoSum.rb @@ -0,0 +1,24 @@ +# I used this video to help me finish the code +# https://www.youtube.com/watch?v=4BS8b86ACJo + +=begin +Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. + +You may assume that each input would have exactly one solution, and you may not use the same element twice. + +You can return the answer in any order. +=end + + + +# @param {Integer[]} nums +# @param {Integer} target +# @return {Integer[]} +def two_sum(nums, target) + dict = {} + nums.each_with_index do |n,i| + return [dict[n], i] if dict.key?(n) + diff = target - n + dict[diff] = i + end +end \ No newline at end of file