This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(a) | |
if a.length==0 | |
1 | |
else | |
sum=a.inject(:+) | |
((a.length+1)*(a.length+2)/2)-sum | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(x, y, d) | |
if x>y | |
0 | |
else | |
((y-x)/d.to_f).ceil | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", |
NewerOlder