Skip to content

Instantly share code, notes, and snippets.

#SQL notes

  • SELECT within SELECT
    • select each country in Europe and show their population in percentage of population of Germany
    SELECT name, CONCAT(ROUND(population/(SELECT population FROM world WHERE NAME = 'Germany')*100), '%') 
    FROM world
    WHERE continent = 'Europe'
  • ALL to run through a database list made with SELECT
require 'Benchmark'
class Sudoku
attr_accessor :puzzle
def initialize
@puzzle = ''
@game = Array.new(9)
@game.map! { Array.new(9) }
9.times do |i|
@KevinSia
KevinSia / index.html
Last active July 23, 2016 14:58 — forked from anonymous/index.html
JS Bin// source http://jsbin.com/xihegu
<!DOCTYPE html>
<html>
<head>
<script src="https://fb.me/react-15.1.0.js"></script>
<script src="https://fb.me/react-dom-15.1.0.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>

SAPPHIRE EMERALD DIAMOND PEARL

RUBY!

  • Fun fact

  • Script

    • A script is a text file containing commands/codes/instructions that you write and pass to the a program to run.
  • Terminal

    • Also known as shell
  • The shell is a program that takes keyboard commands and passes them to the operating system to carry out.

def triangle_up(num, char)
1.upto(num) { |i| puts char * i }
end
def triangle_down(num, char)
num.downto(1) { |i| puts char * i }
end
def deaf_aunty
conversation_done = false
until conversation_done
puts 'Say something to aunty'
reply = gets.chomp
if reply == reply.upcase
puts 'NO, WE CAN\'T DO THAT!'
elsif reply == 'I love ya, aunty, but I\'ve got to go'
def to_roman(num)
str = ''
# Your code here
if num >= 1000
str += 'M' * (num / 1000)
num %= 1000
end
if num == 900
str += 'CM'
def canonical(word)
word.downcase.chars.sort
end
def is_anagram?(word1, word2)
canonical(word1) == canonical(word2)
end
puts "One word"
word1 = gets.chomp
# Your code here to sort the array
def dictionary_sort(array)
return if array.length <= 1
array.sort { |word1, word2| word1.downcase <=> word2.downcase }
end
# writing `array.sort` is equivalent to writing `array.sort { |i, j| i <=> j }`
# .sort decides which item should be in the front by doing `<=> on each pair of items
# if the block gives -1, i goes to the front and j goes to the back
# if the block gives 1, j goes to the front and i goes to the back
@KevinSia
KevinSia / shuffle.rb
Last active September 21, 2017 07:50
# Fisher-Yates shuffle
# the wiki shows two examples
# https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
# pencil-and-paper method
# 1. Duplicate the array and shuffle the duplicate instead (we call the duplicate `input_array`)
# 2. Generate a random number between 0 and (length of the array - 1) (we call the number `random_position`)
# 3. Take an element at position `random_position` in the `input_array`, and put into another array
# 4. Repeat 2-3 until `input_array` has nothing left