Skip to content

Instantly share code, notes, and snippets.

View ph3nx's full-sized avatar

ph3nx ph3nx

View GitHub Profile
@ph3nx
ph3nx / random_string.php
Last active July 27, 2016 11:26
This code snipped shows you how to generate random strings in PHP.
<?php
function random_string($length) {
$LETTERS = 'abcdefghijklmnopqrstuvwxyz';
$random_string = '';
for ($i=0; $i < $length; $i++) {
$random_string = $random_string.$LETTERS[rand(0, strlen($LETTERS)-1)];
}
@ph3nx
ph3nx / rails_request_headers.erb
Last active January 3, 2016 04:19
Two code examples on how to display all request headers in Ruby on Rails.
<% request.header do |k, v| %>
"<%= k %>" => <%= v %><br>
<% end %>
<% for header in request.env.select {|k,v| k.match("^HTTP.*")} %>
"<%=header[0].split('_',2)[1]%>" => <%=header[1]%><br>
<% end %>
@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;
@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 / timestamp.rb
Created January 16, 2014 08:36
Handy method to get the current unix time stamp in Ruby. Unix time, or POSIX time, is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.
def timestamp
Time.now.to_i
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 / 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 / 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 / multiplication_table.rb
Created January 18, 2014 14:20
Ruby program that prints a multiplication table for numbers up to your input. For example 12 x 12 fields.
def tbl max
for y in (1..max) do
for x in (1..max) do
sol = y*x
if sol < 10
print "#{sol} "
elsif sol < 100
print "#{sol} "
else
print "#{sol} "
@ph3nx
ph3nx / prime_numbers.rb
Last active July 17, 2021 07:16
Ruby program / method that prints out all prime numbers until a given maximum.
def prime_numbers max
for i in (2..max) do
for j in (2..i) do
break if i%j == 0
end
p "#{i} is a prime number." if i == j
end
end
require 'prime'