Skip to content

Instantly share code, notes, and snippets.

View troyleach's full-sized avatar
💭
😃

Troy troyleach

💭
😃
View GitHub Profile
@troyleach
troyleach / rspec_rails.md
Last active June 27, 2021 17:46
Implanmentaiton of rspec in a typical rails app
@troyleach
troyleach / smallest.rb
Created June 3, 2015 13:57
Find the smallest number and MOVE it to the front - 2
# find the smalledst number in a given array of numbers
# then MOVE that number to the front of the array
# return the that same array
#RSpec
describe 'smallest_number_move_to_the_front' do
let(:numbers) { [24,6,8,3,234,6,7,65] }
let(:numbers2) { [24,6,8,234,6,7,65,734,-1,322,324,43213] }
it "method is defined" do
@troyleach
troyleach / gist:41c86113e2816994bd2b
Last active August 29, 2015 14:22
find the smallest number and MOVE it to the front of the array
# given an array of numbers find the smallest number.
# return the same array with the lowest number at the front
#RSpec
describe 'smallest_number_move_to_the_front' do
let(:numbers) { [24,6,8,3,234,6,7,65] }
let(:numbers2) { [24,6,8,234,6,7,65,734,-1,322,324,43213] }
it "method is defined" do
expect(defined? smallest_number_move_to_the_front).to eq 'method'
@troyleach
troyleach / finddup.rb
Last active August 29, 2015 14:22
Return the INDEX of the first duplicate number in an array of number
# Hello Marc, so I just couldn't let our challenge go. after I left I kept thinking
# about the solution that I provided and the fact that it wouldn't work. So tonight
# I decided to code it and write some test for it and I wanted to share it with you.
# If I copied the array and then deleted the elements the program would always return 0
# because that is where the duplicate number would live at the time it got compaired.
# I would love some feedback and would also love to see how you might solve this.
def find_duplicate_retrun_index(arr)
arrCopy = arr
arr.each_with_index do |value1, index1|
@troyleach
troyleach / gist:e96ffebcc9498e71ea5d
Created March 28, 2015 05:16
count the frequency of given letters
// Write a function charFreq() that takes a string and builds a frequency listing of the characters contained in it.
// Represent the frequency listing as a Javascript object.
// Try it with something like charFreq("abbabcbdbabdbdbabababcbcbab").
// I got a lot help from stack on this one. I had no idea how to make a object from a string.
// and in doing research for that I found a solution to something similar to this. But I do understand
// how every line of code works and why!
function charFreq(source) {
var frequency = {};
@troyleach
troyleach / gist:9e5bfe6b421b094fb1da
Created March 27, 2015 22:24
find words greater than i
// Write a function filterLongWords() that takes an array of words and an integer i and returns the array of words
// that are longer than i. (if I understand what they are asking for)..
// also, why when I use << - this syntax doesn't work but the .push does?? I thought they both would work?
var i = 5
var wordsGreaterThan = []
function filterLongWords(source) {
var sentence = source.split(/\s/);
for ( var j = 0; j < sentence.length; j++) {
@troyleach
troyleach / gist:5aa193515b9425faeef8
Created March 27, 2015 22:02
Find the longest word in a phase
// Write a function findLongestWord() that takes an array of words and returns the length of the longest one.
// woops, didn't read it.... I took a phase turned it into a array...
var longestWord = "";
function findLongestWord(phase) {
var sentence = phase.split(/\s/);
for ( var i = 0; i < sentence.length; i++ ) {
if (sentence[i].length > longestWord.length) {
longestWord = longestWord.replace(longestWord, sentence[i]);
@troyleach
troyleach / gist:6ea3e0ea50c2ae5d57b1
Last active August 29, 2015 14:17
Translate English to Swedish
// Represent a small bilingual lexicon as a Javascript object in the following fashion {"merry":"god", "christmas":"jul", "and":"och", "happy":gott", "new":"nytt", "year":"år"} and use it to translate your Christmas cards from English into Swedish.
var greetings = {
"merry": "god",
"christmas": "jul",
"and": "och",
"happy": "gott",
"new": "nytt",
"year": "ar"
};
// Sure seems like a lot of code just to reverse a string!
// Define a function reverse() that computes the reversal of a string. For example, reverse("jag testar")
// should return the string "ratset gaj".
var new_str = ""
function reverse(string) {
for (var i = string.length - 1; i >= 0; i--) {
new_str += string[i];
}
return new_str;
}
@troyleach
troyleach / gist:dbf9e3625eb0c39975be
Created March 27, 2015 16:25
JS funcation - code review
// Write a function translate() that will translate a text into "rövarspråket".
// That is, double every consonant and place an occurrence of "o" in between.
// For example, translate("this is fun") should return the string "tothohisos isos fofunon".
var character = []
function translate(sentence) {
var new_str = sentence.split("");
for (var i = 0; i < new_str.length; i++) {