Skip to content

Instantly share code, notes, and snippets.

View ph3nx's full-sized avatar

ph3nx ph3nx

View GitHub Profile
@ph3nx
ph3nx / array_sorting.rb
Created January 19, 2014 20:45
Some ways to sort arrays in Ruby. Copy and paste the code examples in the irb console to try them.
array = [3,7,1,9,2,12,34,0]
# sort in ascending order
array.sort
# => [0, 1, 2, 3, 7, 9, 12, 34]
# sort in descending order
array.sort.reverse
# => [34, 12, 9, 7, 3, 2, 1, 0]
@ph3nx
ph3nx / combined_comparison_operator.rb
Created January 19, 2014 17:55
The combined comparison operator is used to compare two objects in Ruby. It returns 0 if they are the same. 1 if the first operand is greater and -1 if it's less.
a1 = "A Test"
a2 = "A Boom"
a1 <=> a2
# => 1
b1 = "2"
b2 = "9"
b1 <=> b2
@ph3nx
ph3nx / splat_arguments.rb
Created January 19, 2014 16:42
Example Ruby method with a splat argument. Splat arguments are preceded by a *. You use them when you don't know how many arguments there will be.
def greet message, *people
people.each { |p| puts "#{message}, #{p}!" }
end
greet "Hello", "Tony", "Ann", "BadMan"
@ph3nx
ph3nx / english_2_pig_latin.rb
Created January 18, 2014 18:51
Ruby program with functions that translate english to Pig Latin and back to English. English is translated to Pig Latin by taking the first letter of every word, moving it to the end of the word and adding ‘ay’. “The quick brown fox” becomes “Hetay uickqay rownbay oxfay”.
def english_2_pig_latin string
string.split(/ /).each do |word|
word << word[0] << "ay "
print word[1..word.length]
end
end
@ph3nx
ph3nx / list_to_frame.rb
Created January 18, 2014 18:32
This Ruby program is a ruby function / method that prints a list / array in a rectangular frame of stars.
def list_to_frame list=[]
list = ["Hello", "World", "in", "a", "frame"] if list.empty?
longest = 0
list.each do |element|
longest = element.length if longest < element.length
end
(0..longest+3).each do
@ph3nx
ph3nx / sum_or_product.rb
Last active January 3, 2016 16:39
Ruby program that asks the user for a number num and computes either the sum or the product of all numbers starting at 1 until this number.
def sum_or_product
print 'Enter any number bigger 1: '
num = gets.to_i
print 'sum [s] or product [p]? '
opt = gets.chomp
if opt == 's'
(1..num).inject :+
elsif opt == 'p'
@ph3nx
ph3nx / sum_to.rb
Last active January 3, 2016 16:29
Ruby program that asks the user for a number and prints the sum of the numbers 1 to the number he chose.
def sum_to
print 'Enter any number greater than 1: '
input = gets.to_i
sum = 0
for a in 1..input do
sum += a
end
@ph3nx
ph3nx / rand_str.rb
Created January 17, 2014 12:40
Generate random strings in Ruby. You can use this method for instance to generate random subdomains.
def rand_str length
('a'..'z').to_a.shuffle[0..length].join
end
@ph3nx
ph3nx / Dice.java
Last active January 3, 2016 05:39
Die Klasse Dice stellt einen Würfel dar. Bei der Erzeugung eines Objekts wird die Anzahl an Würfen eingegeben, die durchgeführt werden sollen. Mit Hilfe der Methode zeigeStatistik() erhält man eine Übersicht wie oft jede Zahl gewürfelt wurde sowie den Durchschnitt, der normal wäre.
public class Dice {
private int anzahl;
private int grenze = 6;
private int[] zaehler = new int[6];
Dice(int wuerfeAnzahl){
anzahl = wuerfeAnzahl;
@ph3nx
ph3nx / square_numbers.java
Created January 14, 2014 12:14
Die Methode quadratzahlen(int n) erzeugt Anzahl n Quadratzahlen und gibt diese addiert, sowie deren Durchschnitt aus.
public class Arrays {
public static void main(String[] args) {
int[] numbers = new int[5];
numbers[0] = 5;
int l = numbers.length;