Skip to content

Commit 7294b1e

Browse files
author
Patrick Curl
committed
Added all my solutions from CodeEval for resume : about.patrickcurl.com
0 parents  commit 7294b1e

33 files changed

+1062
-0
lines changed

arrayAbsurdity.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# ARRAY ABSURDITY
2+
# CHALLENGE DESCRIPTION:
3+
4+
# Imagine we have an immutable array of size N which we know to be filled with integers ranging from 0 to N-2, inclusive. Suppose we know that the array contains exactly one duplicated entry and that duplicate appears exactly twice. Find the duplicated entry. (For bonus points, ensure your solution has constant space and time proportional to N)
5+
6+
# INPUT SAMPLE:
7+
8+
# Your program should accept as its first argument a path to a filename. Each line in this file is one test case. Ignore all empty lines. Each line begins with a positive integer(N) i.e. the size of the array, then a semicolon followed by a comma separated list of positive numbers ranging from 0 to N-2, inclusive. i.e eg.
9+
10+
# 5;0,1,2,3,0
11+
# 20;0,1,10,3,2,4,5,7,6,8,11,9,15,12,13,4,16,18,17,14
12+
# OUTPUT SAMPLE:
13+
14+
# Print out the duplicated entry, each one on a new line eg
15+
16+
# 0
17+
# 4
18+
# Submit your solution in a file (some file name).(py2| c| cpp| java| rb| pl| php| tcl| clj| js| scala| cs| m| py3| hs| go| bash| lua) or use the online editor.
19+
20+
File.open(ARGV[0]).each_line do |line|
21+
if line.include? "\n" then
22+
line = line.gsub(/[\n]/, '')
23+
end
24+
a = line.split(";")
25+
26+
a = a.drop(1)
27+
a = a[0].to_s
28+
a = a.split(",")
29+
#print a
30+
a = a.collect{|s| s.to_i}
31+
dupe = 0
32+
a.each do |x|
33+
if a.count(x) > 1 then
34+
dupe = x.to_i
35+
end
36+
end
37+
38+
print dupe unless line.empty?
39+
print "\n" unless line.empty?
40+
41+
end

beautifulStrings.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# BEAUTIFUL STRINGS
2+
# CHALLENGE DESCRIPTION:
3+
4+
# Credits: This problem appeared in the Facebook Hacker Cup 2013 Hackathon.
5+
6+
# When John was a little kid he didn't have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could... he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.
7+
8+
# Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it. The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn't care about whether letters are uppercase or lowercase, so that doesn't affect the beauty of a letter. (Uppercase 'F' is exactly as beautiful as lowercase 'f', for example.)
9+
10+
# You're a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?
11+
12+
# INPUT SAMPLE:
13+
14+
# Your program should accept as its first argument a path to a filename. Each line in this file has a sentence. E.g.
15+
16+
# ABbCcc
17+
# Good luck in the Facebook Hacker Cup this year!
18+
# Ignore punctuation, please :)
19+
# Sometimes test cases are hard to make up.
20+
# So I just go consult Professor Dalves
21+
# OUTPUT SAMPLE:
22+
23+
# Print out the maximum beauty for the string. E.g.
24+
25+
# 152
26+
# 754
27+
# 491
28+
# 729
29+
# 646
30+
# Submit your solution in a file (some file name).(py2| c| cpp| java| rb| pl| php| tcl| clj| js| scala| cs| m| py3| hs| go| bash| lua) or use the online editor.
31+
32+
def frequency(a)
33+
a.group_by do |e|
34+
e
35+
end.map do |key, values|
36+
[values.size]
37+
end
38+
end
39+
40+
41+
File.open(ARGV[0]).each_line do |line|
42+
a = line.downcase.gsub(/[^a-z]/, '').split('')
43+
b = frequency(a).sort_by(&:last).reverse.flatten
44+
count = 26
45+
total = 0
46+
b.each do |f|
47+
total = total + (f*count)
48+
count -= 1
49+
end
50+
print total
51+
print "\n"
52+
end

calculateDistance.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# CALCULATE DISTANCE
2+
# CHALLENGE DESCRIPTION:
3+
4+
# You have coordinates of 2 points and need to find the distance between them.
5+
6+
# INPUT SAMPLE:
7+
8+
# Your program should accept as its first argument a path to a filename. Input example is the following
9+
10+
# (25, 4) (1, -6)
11+
# (47, 43) (-25, -11)
12+
# All numbers in input are integers between -100 and 100.
13+
14+
# OUTPUT SAMPLE:
15+
16+
# Print results in the following way.
17+
18+
# 26
19+
# 90
20+
# You don't need to round the results you receive.
21+
# They must be integer numbers between -100 and 100.
22+
23+
# Submit your solution in a file (some file name).(py2| c| cpp| java| rb| pl| php| tcl| clj| js| scala| cs| m| py3| hs| go| bash| lua) or use the online editor.
24+
25+
File.open(ARGV[0]).each_line do |line|
26+
a = line.gsub(/[(),\n]/, "").split(" ").map(&:to_i)
27+
ans = Math.sqrt(((a[2]-a[0])**2)+((a[3]-a[1])**2))
28+
print ans.to_i
29+
print "\n"
30+
end

climbingStairs.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# CLIMBING STAIRS
2+
# CHALLENGE DESCRIPTION:
3+
4+
# You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
5+
6+
# INPUT SAMPLE:
7+
8+
# Your program should accept as its first argument a path to a filename. Each line in this file contains a positive integer which is the total number of stairs.
9+
# Ignore all empty lines. E.g.
10+
11+
# 10
12+
# 20
13+
# OUTPUT SAMPLE:
14+
15+
# Print out the number of ways to climb to the top of the staircase. E.g.
16+
17+
# 89
18+
# 10946
19+
# Submit your solution in a file (some file name).(py2| c| cpp| java| rb| pl| php| tcl| clj| js| scala| cs| m| py3| hs| go| bash| lua) or use the online editor.
20+
def fib(n)
21+
gRatio = 1.61803398875
22+
23+
if n == 0
24+
return 0
25+
elsif n == 1
26+
return 1
27+
else
28+
num = (((gRatio)**n) - ((1 - gRatio) ** n)) / Math.sqrt(5)
29+
return num.to_i
30+
end
31+
end
32+
File.open(ARGV[0]).each_line do |line|
33+
puts fib(line.to_i + 1)
34+
end

countingPrimes.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# COUNTING PRIMES
2+
# CHALLENGE DESCRIPTION:
3+
4+
# Given two integers N and M, count the number of prime numbers between N and M (both inclusive)
5+
6+
# INPUT SAMPLE:
7+
8+
# Your program should accept as its first argument a path to a filename. Each line in this file contains two comma separated positive integers. E.g.
9+
10+
# 2,10
11+
# 20,30
12+
# OUTPUT SAMPLE:
13+
14+
# Print out the number of primes between N and M (both inclusive)
15+
16+
# 4
17+
# 2
18+
# Submit your solution in a file (some file name).(py2| c| cpp| java| rb| pl| php| tcl| clj| js| scala| cs| m| py3| hs| go| bash| lua) or use the online editor.
19+
20+
require "prime"
21+
File.open(ARGV[0]).each_line do |line|
22+
a = line.strip.split(',')
23+
a = a.collect{|b| b.to_i}
24+
primes = 0
25+
(a.first..a.last).each do |x|
26+
if x.prime? == true then
27+
primes +=1
28+
end
29+
end
30+
print primes
31+
print "\n"
32+
end

decimalToBinary.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# DECIMAL TO BINARY
2+
# CHALLENGE DESCRIPTION:
3+
4+
# Given a decimal (base 10) number, print out its binary representation.
5+
6+
# INPUT SAMPLE:
7+
8+
# Your program should accept as its first argument a path to a filename containing whole decimal numbers greater or equal to 0, one per line.
9+
# Ignore all empty lines. E.g.
10+
11+
# 2
12+
# 10
13+
# 67
14+
# OUTPUT SAMPLE:
15+
16+
# Print the binary representation, one per line. E.g.
17+
18+
# 10
19+
# 1010
20+
# 1000011
21+
# Submit your solution in a file (some file name).(py2| c| cpp| java| rb| pl| php| tcl| clj| js| scala| cs| m| py3| hs| go| bash| lua) or use the online editor.
22+
23+
File.open(ARGV[0]).each_line do |line|
24+
a = line.to_i.to_s(2)
25+
print a
26+
print "\n"
27+
end

evenNumbers.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# EVEN NUMBERS
2+
# CHALLENGE DESCRIPTION:
3+
4+
# Write a program which checks input numbers and determines whether a number is even or not.
5+
6+
# INPUT SAMPLE:
7+
8+
# Your program should accept as its first argument a path to a filename. Input example is the following
9+
10+
# 701
11+
# 4123
12+
# 2936
13+
# OUTPUT SAMPLE:
14+
15+
# Print 1 if the number is even or 0 if the number is odd.
16+
17+
# 0
18+
# 0
19+
# 1
20+
# All numbers in input are integers > 0 and < 5000.
21+
22+
# Submit your solution in a file (some file name).(py2| c| cpp| java| rb| pl| php| tcl| clj| js| scala| cs| m| py3| hs| go| bash| lua) or use the online editor.
23+
24+
File.open(ARGV[0]).each_line do |line|
25+
if line.to_i.even?
26+
print 1
27+
else
28+
print 0
29+
end
30+
print "\n"
31+
end

fibSeries.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# FIBONACCI SERIES
2+
# CHALLENGE DESCRIPTION:
3+
4+
# The Fibonacci series is defined as: F(0) = 0; F(1) = 1; F(n) = F(n-1) + F(n-2) when n>1. Given a positive integer 'n', print out the F(n).
5+
6+
# INPUT SAMPLE:
7+
8+
# The first argument will be a path to a filename containing a positive integer, one per line. E.g.
9+
10+
# 5
11+
# 12
12+
# OUTPUT SAMPLE:
13+
14+
# Print to stdout, the fibonacci number, F(n). E.g.
15+
16+
# 5
17+
# 144
18+
# Submit your solution in a file (some file name).(py2| c| cpp| java| rb| pl| php| tcl| clj| js| scala| cs| m| py3| hs| go| bash| lua) or use the online editor.
19+
20+
def fib(n)
21+
gRatio = 1.61803398875
22+
num = n.to_f
23+
if num == 0
24+
return 0
25+
elsif num == 1
26+
return 1
27+
else
28+
myNum = (((gRatio) ** num) - ((1 - gRatio) ** num)) / Math.sqrt(5)
29+
return myNum.to_i
30+
end
31+
end
32+
33+
File.open(ARGV[0]).each_line do |line|
34+
puts fib(line)
35+
end

fileSize.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# FILE SIZE
2+
# CHALLENGE DESCRIPTION:
3+
4+
# Print the size of a file in bytes.
5+
6+
# INPUT:
7+
8+
# The first argument to your program has the path to the file you need to check the size of.
9+
10+
# OUTPUT SAMPLE:
11+
12+
# Print the size of the file in bytes. E.g.
13+
14+
# 55
15+
# Submit your solution in a file (some file name).(py2| c| cpp| java| rb| pl| php| tcl| clj| js| scala| cs| m| py3| hs| go| bash| lua) or use the online editor.
16+
17+
print File.size?(ARGV[0])

findAWriter.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# FIND A WRITER
2+
# CHALLENGE DESCRIPTION:
3+
4+
# You have a set of rows with names of famous writers encoded inside. Each row is divided into 2 parts by pipe char (|). The first part has a writer's name. The second part is a "key" to generate a name.
5+
6+
# Your goal is to go through each number in the key (numbers are separated by space) left-to-right. Each number represents a position in the 1st part of a row. This way you collect a writer's name which you have to output.
7+
8+
# INPUT SAMPLE:
9+
10+
# Your program should accept as its first argument a path to a filename. Input example is the following
11+
12+
# osSE5Gu0Vi8WRq93UvkYZCjaOKeNJfTyH6tzDQbxFm4M1ndXIPh27wBA rLclpg| 3 35 27 62 51 27 46 57 26 10 46 63 57 45 15 43 53
13+
14+
# 3Kucdq9bfCEgZGF2nwx8UpzQJyHiOm0hoaYP6ST1WM7Nks5XjrR4IltBeDLV vA| 2 26 33 55 34 50 33 61 44 28 46 32 28 30 3 50 34 61 40 7 1 31
15+
# This input had 2 rows.
16+
17+
# OUTPUT SAMPLE:
18+
19+
# Print results in the following way.
20+
21+
# Stephen King 1947
22+
# Kyotaro Nishimura 1930
23+
# Submit your solution in a file (some file name).(py2| c| cpp| java| rb| pl| php| tcl| clj| js| scala| cs| m| py3| hs| go| bash| lua) or use the online editor.
24+
25+
File.open(ARGV[0]).each_line do |line|
26+
if line.include? "\n" then
27+
line = line.gsub(/[\n]/, '')
28+
end
29+
30+
a = line.split('|')
31+
encoded = a[0].split('')
32+
key = a[1].split(" ")
33+
name = []
34+
key.each do |x|
35+
name.push(encoded[x.to_i - 1])
36+
end
37+
print name.join('')
38+
print "\n"
39+
end

0 commit comments

Comments
 (0)