Skip to content

Instantly share code, notes, and snippets.

View AnielaMW's full-sized avatar
☺️
Looking for my next challenge.

A. Wolkonowski AnielaMW

☺️
Looking for my next challenge.
  • RedArgyle
  • Upstate New York
View GitHub Profile
@AnielaMW
AnielaMW / p_and_p.rb
Last active March 11, 2019 22:08
Pride and Prejudice Ruby Logic
puts "Pride and Prejudice Logic"
# It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.
class Man
attr_reader :truth
def initialize (name, single, fortune)
@fortune = fortune
@single = single
@AnielaMW
AnielaMW / heap_sort.rb
Last active July 19, 2019 06:26
Heap Sort Challenge
require 'pry'
puts "Heap Sort"
# 1. In def heap_sort, we first build a max heap.
# 2. Then, we call a loop: while the array length is greater than 1, swap root element with the last element, reduce the length by 1 (since we have sorted one item), and re-build the heap so that it satisfies the max-heap condition.
# 3. At the end, we remake the array into its original size and revert the root back to index [0].
# 4. The def heapify method will sift the elements until they’re in their rightful place.
# 5. parent indicates where to start sifting and limit tells how far down the heap to sift.
# 6. Set parent node as the root. While the child index is less than or equal to the limit (indicates that root has at least one child), we increment child by 1 to get it’s sibling IF the child is less than limit and the value of child is smaller than value of it’s sibling. The loop terminates if value of root is greater than value of child (since root must always be greater than children in a max heap). Otherwise, swap the
@AnielaMW
AnielaMW / selection_sort.rb
Last active March 6, 2019 22:13
Selection Sort Challenge
require 'pry'
puts "Selection Sort"
# 1. Set n equal to array.length — 1: this represents how many times you need to do the comparisons. Loop though n.times.
# 2. Set a min_index value equal to your initial i index (should be the first element in array).
# 3. Create a second (nested) loop starting at the second element until n using variable j.
# 4. Compare the value of element at index j with value of element of min_index. If value of element at index j is less than value of element of min_index, index j becomes the new min_index. Set min_index = j if this is the case. Exit the inner loop.
# 5. If value of new min_index is not equal to value of element at i then swap value of element at i index with value of element at min_index.
# 6. Lastly, return the manipulated array.
@AnielaMW
AnielaMW / insertion_sort.rb
Last active March 6, 2019 21:45
Insertion Sort Challenge
require 'pry'
puts "Insertion Sort"
# 1. First, we iterate through all elements of the array with (array.length).times do. j represents the index of the item in the array.
# 2. Then we set the if/else checks to run only if j > 0 (element is not the first item, which has index 0).
# 3. The if/else checks compares the previous element with the current element; if previous element is larger than current element, swap previous with current.
# 4. If previous element is not larger than current element, the if/else terminates.
# 5. The j counter is decremented by 1.
@AnielaMW
AnielaMW / bubble_sort.rb
Last active March 6, 2019 21:21
Bubble Sort Challenge
require 'pry'
puts "Bubble Sort"
# 1. bubble_sort takes in a single array parameter.
# 2. If the array size is 1 or 0, return the array; by default,
# an empty array or array with one element is sorted.
# 3. Create the swap variable and set it to true by default.
# 4. Create a while loop that will run as long as swap is true.
# 5. Set swap = false since immediately after the beginning of your loop,
# there have been no swaps.
@AnielaMW
AnielaMW / quick_sort.rb
Last active March 6, 2019 22:25
Quick Sort Challenge in Ruby
require 'pry'
puts "Quick Sort"
# 1. First, our checks to see if array.length <= 1.
# 2. Pick a pivot at random. Ruby’s delete_at method will delete the item at the specified index, which in this case would be a rand index in the range of array.length. We’re saving the value of that item to pivot.
# 3. Create a new left and right subarray.
# 4. Loop through every element in the array and compare it to the pivot. If the value is less than pivot, add element to the left subarray. If value is greater than pivot, add element to the right subarray.
def quick_sort(arr)
return arr if arr.length <= 1
@AnielaMW
AnielaMW / merge_sort.rb
Last active July 19, 2019 06:26
Merge Sort-Ruby
require 'pry'
puts "Merge Sort"
# 1. Check the input array length. If it is 0 or 1, return the array (already sorted!)
# 2. If array length is greater than 1, then we want to define a mid-point, picked by choosing array.length / 2 and call a floor method so the number always rounds down.
# 3. Use the midpoint to divide the array into halves, a left and right. I set my left array to start with first element and end at element mid — 1. My right array starts at mid and ends at the last element. (Note: Ruby ranges .. are inclusive of the last element, whereas ... are exclusive of last element).
# 4. Note that not only do I set left and right subarrays equal to the lengths above, but they’re set to the merge_sort version of those subarrays. That means that merge_sort will be called upon these subarrays, which means picking a new midpoint, dividing the subarray into halves, and set left and right subarrays that will call merge_sort again.
# 5. Lastly, I have a call to the merge method to combine the left and right
@AnielaMW
AnielaMW / stair_challenge.rb
Created February 12, 2019 00:35
Stairs Challenge_Ruby
puts "Stairs Challenge"
# Given
# x = the number of steps to the top of a stair
# and
# n = is the maximum stride to be taken
# Solve for the total number of ways to reach the top step.
# Assumptions
# There will always be at least one way.
# 0 is not a valid stride.
@AnielaMW
AnielaMW / first_duplicate.rb
Created March 14, 2018 15:18
First Duplicate Challenge
require 'pry'
def first_duplicate1(a)
counts = []
a.each_with_index do |value, i|
return value if counts[value]
counts[value] = true
binding.pry
end
-1
@AnielaMW
AnielaMW / App.js
Created March 10, 2018 05:21
React Marathon - Launch Academy
import React from 'react';
import PlaylistCollection from './PlaylistCollection';
import SongCollection from './SongCollection';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedPlaylistId: props.data.selectedPlaylistId
};