Skip to content

Instantly share code, notes, and snippets.

@metallurgix
metallurgix / codility1-4.rb
Created December 10, 2014 09:20
Codility-Lesson 2-Challenge 4
def solution(n, a)
max=0
b=Array.new(n,0)
(0..a.length-1).each do |i|
if a[i]>n
b.fill(max)
else
b[a[i]-1]+=1
if b[a[i]-1]>max
max=b[a[i]-1]
@metallurgix
metallurgix / codility2-2.rb
Created December 10, 2014 08:54
Codility-Lesson 2-Challenge 2
def solution(x, a)
sum=(x*(x+1))/2
pos=-1
b=Array.new(x,0)
(0..a.length-1).each do |i|
if b[a[i]-1]==0
b[a[i]-1]=1
sum-=a[i]
if sum==0
pos=i
@metallurgix
metallurgix / codility1-3.rb
Created December 9, 2014 11:35
Codility-Lesson 1-Challenge 3
def solution(a)
if a.length==0
1
else
sum=a.inject(:+)
((a.length+1)*(a.length+2)/2)-sum
end
end
@metallurgix
metallurgix / codility2-1.rb
Created December 9, 2014 10:38
Codility-Lesson 2-Challenge 1
def solution(a)
len=a.length
sum=a.inject(:+)
if sum==(((a.length)*(a.length+1))/2) && (a.uniq.length==len)
1
else
0
end
end
@metallurgix
metallurgix / codility1-2.rb
Created December 8, 2014 18:21
Codility-Lesson 1-Challenge 2
def solution(x, y, d)
if x>y
0
else
((y-x)/d.to_f).ceil
end
end
@metallurgix
metallurgix / codility1-1.rb
Created December 8, 2014 17:59
Codility-Lesson 1-Challenge 1
def solution(a)
if a.empty?
0
elsif a.length==2
a[1]-a[0]
else
right=a.inject(:+)-a[0]
left=a[0]
min=(right-left).abs
(1..a.length-1).each do |i|
@metallurgix
metallurgix / day.c
Created August 11, 2014 22:12
Day of the Week using Date
#include <stdio.h>
char* cal_day(int y, int m, int d)
{
char* days[]={"Sunday","Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday"};
int a=(14-m)/12;
y=y-a;
m=m+12*a-2;
return days[ (d + y + y/4 - y/100 + y/400 + (31*m)/12) % 7];
}
@metallurgix
metallurgix / day.java
Created August 11, 2014 21:44
Day of the week using Date
import java.util.*;
String[] days={"Sunday","Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday"};
Calendar cal = Calendar.getInstance();
// Month ranges from 0-11
cal.set(2014,7,11);
System.out.println(days[cal.get(cal.DAY_OF_WEEK)-1]);
@metallurgix
metallurgix / day.rb
Last active August 29, 2015 14:05
Day of the week using Date
require "date"
days=["Sunday","Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday"]
test_date=Date.new(2014,8,10)
#test_date.wday -> returns 0-6
puts days[test_date.wday]
@metallurgix
metallurgix / morse.rb
Last active August 29, 2015 14:04
Morse Encoding and Decoding
class String
#Decodes Morse String
#A space is assumed to be represented by 2 spaces in encoded string
def decode_morse
result=Array.new
self.gsub!(" "," | ")
morse= {"|"=>" ",".-"=>"A","-..."=>"B","-.-."=>"C","-.."=>"D","."=>"E","..-."=>"F","--."=>"G","...."=>"H",".."=>"I",
".---"=>"J","-.-"=>"K",".-.."=>"L","--"=>"M","-."=>"N","---"=>"O",".--."=>"P","--.-"=>"Q",".-."=>"R",
"..."=>"S","-"=>"T","..-"=>"U","...-"=>"V",".--"=>"W","-..-"=>"X","-.--"=>"Y","--.."=>"Z",