Skip to content

Instantly share code, notes, and snippets.

@kmandreza
kmandreza / Flashcards
Created February 7, 2013 22:02
Ruby Flashcards 1: Single Deck Let's build a simple flashcard game in Ruby with a command-line interface. Here is an example of one possible implementation: $ ruby flashcards.rb Welcome to Ruby Flash Cards. To play, just enter the correct term for each definition. Ready? Go! Definition A file format in which values are delimited by commas. Guess…
class Flashcard #data structure
attr_accessor :term, :definition
def initialize(term,definition)
@term=term
@definition=definition
end
end
@kmandreza
kmandreza / Pseudocode
Created January 12, 2013 04:28
Using pseudocode, write a simple hangman game. The point of pseudocode is to represent the logical structureof a program without getting bogged down in the syntax of a particular language. For example, a program to select the largest integer from an array of integers might be written like this in pseudocode initialize max_so_far to the first ele…
# generate a word at random and store it in a variable
# display the length of the word to the user
# correct_guesses is less than the length of the word
# prompt the user to guess a letter
# if the guess is correct increment correct_guesses by 1
# if the guess is incorrect increment incorrect_guesses by 1
# and draw the next part of the hangman
# if the incorrect_guesses is greater than 8, tell the user
# they lost and exit the program
# if correct_guesses is equal to the length of the word, tell the user they won
@kmandreza
kmandreza / Create a method to pad an array
Created January 4, 2013 03:06
Implement Array#pad and Array#pad!. Each method accepts a minimum size (non-negative integer) and an optional pad value as arguments. If the array's length is less than the minimum size, Array#pad should return a new array padded with the pad value up to the minimum size. For example, ruby [1,2,3].pad(5) should return [1,2,3,nil,nil] And ruby [1…
class Array
def pad!(min_size, value = nil)
x = min_size - self.count #take the argument and subtract the count from it.
x.times do
self << value
end# I want to add the value x number of times to the array
self
end
@kmandreza
kmandreza / gist:6747571
Last active March 11, 2016 23:15
Let's Play 2
Exercise 1 Deaf Grandma
We're going to model something a little silly: an interaction between you and your imaginary deaf grandma. She exhibits the following inexplicable behavior:
1.She can only hear you if you shout at her.
2.If you say something but don't shout, she'll shout right back: "HUH?! SPEAK UP, SONNY!"
3.If you do shout you're also out of luck, because she'll misunderstand you and shout back "NO, NOT SINCE 1983!"
4.She won't let you leave the room unless you say, politely, "I love ya, Grandma, but I've got to go." She may be deaf, but she can smell rude a mile away.
How should these behaviors map to code?
@kmandreza
kmandreza / gist:6747393
Last active December 24, 2015 04:59
Sudoku
PART 1
By the end of the next two challenges you'll have a fully-functioning Sudoku solver that you can run from the command line.
Sudoku is a logic-based, combinatorial number-placement puzzle. The objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 sub-grids that compose the grid (also called "boxes") contains all of the digits from 1 to 9.
The person who created the puzzle provides a partial solution so that some squares already have numbers. Typically, there are enough initial numbers to guarantee a unique solution.
Unsolved Solved Sudoku
@kmandreza
kmandreza / gist:6654798
Created September 21, 2013 22:26
LET'S PLAY
# Exercise 1
Define three local variables: home_address, home_city, and home_state. Define and assign them the values of your own personal home address, city, and state, respectively.
# Exercise 2
Explain the difference between the following lines of code in plain English. "Plain English" means use language Edward, your non-programming second cousin from Ames, IA, would understand.
If any of the lines are invalid, explain why.
first_name = "Khara"
@kmandreza
kmandreza / flashcard.rb
Last active December 12, 2015 08:49
flashcard and sherif's notes
class Flashcard #data structure
attr_accessor :term, :definition
def initialize(term,definition)
@term=term
@definition=definition
end
end
@kmandreza
kmandreza / gist:4743420
Last active December 12, 2015 08:19
Ruby Todos 1.0: Core Features
require_relative 'view'
require_relative 'FileReadWrite'
class Parser
TODO_FILE = 'Todolist.txt'
def initialize(user_input)
@filerw = FileReadWrite.new(TODO_FILE)
@list = get_list
@viewer = Viewer.new
@kmandreza
kmandreza / todo.rb
Last active December 12, 2015 08:19
To Do
class FileReadWrite
def initialize(filename)
@filename = filename
end
def read_file
File.open(@filename).readlines
end
@kmandreza
kmandreza / jsonyaml.rb
Last active December 12, 2015 06:09
json yaml
require 'csv'
require 'date'
require 'yaml'
require 'json'
FILENAME = "people.csv"
NEW_FILE = "new_people.csv"
YAML_FILE = "people.yaml"
JSON_FILE = "people.json"