Skip to content

Commit 96600ae

Browse files
committed
New local drive
Hmmm…something happened while installing Google Drive in OS X
1 parent 91ad6d6 commit 96600ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1534
-1511
lines changed

.byebug_history

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
exit
2+
s
3+
line
4+
s
5+
key_length
6+
s
7+
set_header
8+
s
9+
header
10+
n
11+
s
12+
header
13+
s
14+
n
15+
exit
16+
_n
17+
_x
18+
s
19+
exit
20+
n
21+
s
22+
x
23+
s

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# codeval
2-
CodeEval stuff
1+
# codeval
2+
CodeEval stuff

bitpositions.rb

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
=begin
2-
BIT POSITIONS
3-
CHALLENGE DESCRIPTION:
4-
Given a number n and two integers p1,p2 determine if the bits in position p1
5-
and p2 are the same or not. Positions p1 and p2 are 1 based.
6-
INPUT SAMPLE:
7-
The first argument will be a path to a filename containing a comma separated
8-
list of 3 integers, one list per line. E.g.
9-
86,2,3
10-
125,1,2
11-
OUTPUT SAMPLE:
12-
Print to stdout, 'true'(lowercase) if the bits are the same, else 'false'(lowercase). E.g.
13-
true
14-
false
15-
=end
16-
17-
lines = File.readlines(ARGV[0])
18-
19-
lines.each do |line|
20-
num_strs = line.chomp.split(",")
21-
n = num_strs.first
22-
p1, p2 = num_strs[1..2]
23-
n_bin_as_string = n.to_i.to_s(2)
24-
n_bin_as_string.reverse!
25-
puts n_bin_as_string[p1.to_i - 1] == n_bin_as_string[p2.to_i - 1]
26-
end
1+
=begin
2+
BIT POSITIONS
3+
CHALLENGE DESCRIPTION:
4+
Given a number n and two integers p1,p2 determine if the bits in position p1
5+
and p2 are the same or not. Positions p1 and p2 are 1 based.
6+
INPUT SAMPLE:
7+
The first argument will be a path to a filename containing a comma separated
8+
list of 3 integers, one list per line. E.g.
9+
86,2,3
10+
125,1,2
11+
OUTPUT SAMPLE:
12+
Print to stdout, 'true'(lowercase) if the bits are the same, else 'false'(lowercase). E.g.
13+
true
14+
false
15+
=end
16+
17+
lines = File.readlines(ARGV[0])
18+
19+
lines.each do |line|
20+
num_strs = line.chomp.split(",")
21+
n = num_strs.first
22+
p1, p2 = num_strs[1..2]
23+
n_bin_as_string = n.to_i.to_s(2)
24+
n_bin_as_string.reverse!
25+
puts n_bin_as_string[p1.to_i - 1] == n_bin_as_string[p2.to_i - 1]
26+
end

bptest.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
86,2,3
2-
125,1,2
1+
86,2,3
2+
125,1,2

cdtest.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
2 0 6 3 1 6 3 1 6 3 1
2-
3 4 8 0 11 9 7 2 5 6 10 1 49 49 49 49
3-
1 2 3 1 2 3 1 2 3
1+
2 0 6 3 1 6 3 1 6 3 1
2+
3 4 8 0 11 9 7 2 5 6 10 1 49 49 49 49
3+
1 2 3 1 2 3 1 2 3

cycledetection.rb

+46-46
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
=begin
2-
INPUT SAMPLE:
3-
Your program should accept as its first argument a path to a filename containing a sequence of numbers (space delimited). The file can have multiple such lines. E.g
4-
2 0 6 3 1 6 3 1 6 3 1
5-
3 4 8 0 11 9 7 2 5 6 10 1 49 49 49 49
6-
1 2 3 1 2 3 1 2 3
7-
OUTPUT SAMPLE:
8-
Print to stdout the first cycle you find in each sequence. Ensure that there are no trailing empty spaces on each line you print. E.g.
9-
6 3 1
10-
49
11-
1 2 3
12-
The cycle detection problem is explained more widely on wiki
13-
Constrains:
14-
The elements of the sequence are integers in range [0, 99]
15-
The length of the sequence is in range [0, 50]
16-
=end
17-
18-
lines = File.readlines(ARGV[0])
19-
20-
lines.each do |line|
21-
numbers = line.split(" ").map(&:to_i)
22-
found_cycle = false
23-
cycle_length = 1
24-
while (!found_cycle) && (cycle_length < numbers.length)
25-
tortoise_index = 0
26-
hare_index = tortoise_index + cycle_length
27-
while (hare_index < numbers.length - cycle_length - 1)
28-
if (numbers[tortoise_index] == numbers[hare_index]) && !found_cycle
29-
found_cycle = true
30-
cycle = numbers[tortoise_index..(hare_index - 1)]
31-
cycle_start_index = tortoise_index
32-
while cycle_start_index < (numbers.length - cycle_length)
33-
found_cycle = false if numbers[cycle_start_index] != numbers[cycle_start_index + cycle_length]
34-
found_cycle = false if numbers[cycle_start_index..(cycle_start_index + cycle_length - 1)] != cycle
35-
cycle_start_index += cycle_length
36-
end
37-
puts cycle.map(&:to_s).join(" ") if found_cycle
38-
end
39-
tortoise_index +=1
40-
hare_index += 1
41-
end
42-
cycle_length +=1
43-
end
44-
end
45-
46-
__END__
1+
=begin
2+
INPUT SAMPLE:
3+
Your program should accept as its first argument a path to a filename containing a sequence of numbers (space delimited). The file can have multiple such lines. E.g
4+
2 0 6 3 1 6 3 1 6 3 1
5+
3 4 8 0 11 9 7 2 5 6 10 1 49 49 49 49
6+
1 2 3 1 2 3 1 2 3
7+
OUTPUT SAMPLE:
8+
Print to stdout the first cycle you find in each sequence. Ensure that there are no trailing empty spaces on each line you print. E.g.
9+
6 3 1
10+
49
11+
1 2 3
12+
The cycle detection problem is explained more widely on wiki
13+
Constrains:
14+
The elements of the sequence are integers in range [0, 99]
15+
The length of the sequence is in range [0, 50]
16+
=end
17+
18+
lines = File.readlines(ARGV[0])
19+
20+
lines.each do |line|
21+
numbers = line.split(" ").map(&:to_i)
22+
found_cycle = false
23+
cycle_length = 1
24+
while (!found_cycle) && (cycle_length < numbers.length)
25+
tortoise_index = 0
26+
hare_index = tortoise_index + cycle_length
27+
while (hare_index < numbers.length - cycle_length - 1)
28+
if (numbers[tortoise_index] == numbers[hare_index]) && !found_cycle
29+
found_cycle = true
30+
cycle = numbers[tortoise_index..(hare_index - 1)]
31+
cycle_start_index = tortoise_index
32+
while cycle_start_index < (numbers.length - cycle_length)
33+
found_cycle = false if numbers[cycle_start_index] != numbers[cycle_start_index + cycle_length]
34+
found_cycle = false if numbers[cycle_start_index..(cycle_start_index + cycle_length - 1)] != cycle
35+
cycle_start_index += cycle_length
36+
end
37+
puts cycle.map(&:to_s).join(" ") if found_cycle
38+
end
39+
tortoise_index +=1
40+
hare_index += 1
41+
end
42+
cycle_length +=1
43+
end
44+
end
45+
46+
__END__

dectobin.rb

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
=begin
2-
DECIMAL TO BINARY
3-
CHALLENGE DESCRIPTION:
4-
You are given a decimal (base 10) number, print its binary representation.
5-
INPUT SAMPLE:
6-
Your program should accept as its first argument a path to a filename
7-
containing decimal numbers greater or equal to 0, one per line.
8-
Ignore all empty lines.
9-
For example:
10-
2
11-
10
12-
67
13-
OUTPUT SAMPLE:
14-
Print the binary representation, one per line.
15-
For example:
16-
10
17-
1010
18-
1000011
19-
=end
20-
21-
lines = File.readlines(ARGV[0])
22-
23-
lines.each do |line|
24-
puts line.chomp.to_i.to_s(2)
25-
end
1+
=begin
2+
DECIMAL TO BINARY
3+
CHALLENGE DESCRIPTION:
4+
You are given a decimal (base 10) number, print its binary representation.
5+
INPUT SAMPLE:
6+
Your program should accept as its first argument a path to a filename
7+
containing decimal numbers greater or equal to 0, one per line.
8+
Ignore all empty lines.
9+
For example:
10+
2
11+
10
12+
67
13+
OUTPUT SAMPLE:
14+
Print the binary representation, one per line.
15+
For example:
16+
10
17+
1010
18+
1000011
19+
=end
20+
21+
lines = File.readlines(ARGV[0])
22+
23+
lines.each do |line|
24+
puts line.chomp.to_i.to_s(2)
25+
end

disemvowel.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
function disemvowel(str) {
2-
var output = "";
3-
for (var n = 0;n < str.length; n++) {
4-
if (!"aeiou".includes(str.charAt(n))) {
5-
output += str.charAt(n);
6-
}
7-
}
8-
return output;
9-
}
1+
function disemvowel(str) {
2+
var output = "";
3+
for (var n = 0;n < str.length; n++) {
4+
if (!"aeiou".includes(str.charAt(n))) {
5+
output += str.charAt(n);
6+
}
7+
}
8+
return output;
9+
}

doublesquares.rb

+49-49
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
1-
=begin
2-
DOUBLE SQUARES
3-
CHALLENGE DESCRIPTION:
4-
Credits: This challenge appeared in the Facebook Hacker Cup 2011.
5-
A double-square number is an integer X which can be expressed as the sum of
6-
two perfect squares. For example, 10 is a double-square because 10 = 3^2 +
7-
1^2. Your task in this problem is, given X, determine the number of ways in
8-
which it can be written as the sum of two squares.
9-
For example, 10 can only be written as 3^2 + 1^2 (we don't count 1^2 + 3^2 as
10-
being different). On the other hand, 25 can be written as 5^2 + 0^2 or as 4^2
11-
+ 3^2.
12-
NOTE: Do NOT attempt a brute force approach. It will not work. The following
13-
constraints hold:
14-
0 <= X <= 2147483647
15-
1 <= N <= 100
16-
INPUT SAMPLE:
17-
Your program should accept as its first argument a path to a filename. You
18-
should first read an integer N, the number of test cases. The next N lines
19-
will contain N values of X.
20-
5
21-
10
22-
25
23-
3
24-
0
25-
1
26-
OUTPUT SAMPLE:
27-
E.g.
28-
1
29-
2
30-
0
31-
1
32-
1
33-
=end
34-
35-
lines = File.readlines(ARGV[0])
36-
37-
N = lines[0].chomp.to_i
38-
39-
lines[1..N].each do |line|
40-
x = line.chomp.to_i
41-
count = 0
42-
(0..Math.sqrt(x).to_i).each do |n|
43-
root = Math.sqrt(x - n**2)
44-
count +=1 if root == root.to_i.to_f
45-
end
46-
count /= 2
47-
count +=1 if x == 0
48-
puts count
49-
end
1+
=begin
2+
DOUBLE SQUARES
3+
CHALLENGE DESCRIPTION:
4+
Credits: This challenge appeared in the Facebook Hacker Cup 2011.
5+
A double-square number is an integer X which can be expressed as the sum of
6+
two perfect squares. For example, 10 is a double-square because 10 = 3^2 +
7+
1^2. Your task in this problem is, given X, determine the number of ways in
8+
which it can be written as the sum of two squares.
9+
For example, 10 can only be written as 3^2 + 1^2 (we don't count 1^2 + 3^2 as
10+
being different). On the other hand, 25 can be written as 5^2 + 0^2 or as 4^2
11+
+ 3^2.
12+
NOTE: Do NOT attempt a brute force approach. It will not work. The following
13+
constraints hold:
14+
0 <= X <= 2147483647
15+
1 <= N <= 100
16+
INPUT SAMPLE:
17+
Your program should accept as its first argument a path to a filename. You
18+
should first read an integer N, the number of test cases. The next N lines
19+
will contain N values of X.
20+
5
21+
10
22+
25
23+
3
24+
0
25+
1
26+
OUTPUT SAMPLE:
27+
E.g.
28+
1
29+
2
30+
0
31+
1
32+
1
33+
=end
34+
35+
lines = File.readlines(ARGV[0])
36+
37+
N = lines[0].chomp.to_i
38+
39+
lines[1..N].each do |line|
40+
x = line.chomp.to_i
41+
count = 0
42+
(0..Math.sqrt(x).to_i).each do |n|
43+
root = Math.sqrt(x - n**2)
44+
count +=1 if root == root.to_i.to_f
45+
end
46+
count /= 2
47+
count +=1 if x == 0
48+
puts count
49+
end

dstest.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
5
2-
10
3-
25
4-
3
5-
0
6-
1
1+
5
2+
10
3+
25
4+
3
5+
0
6+
1

dtbtest.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
2
2-
10
3-
67
1+
2
2+
10
3+
67

endianness.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
=begin
2-
Print to stdout the endianness, wheather it is LittleEndian or BigEndian.
3-
=end
4-
5-
if [1].pack("I") == [1].pack("N")
6-
puts "BigEndian"
7-
else
8-
puts "LittleEndian"
9-
end
1+
=begin
2+
Print to stdout the endianness, wheather it is LittleEndian or BigEndian.
3+
=end
4+
5+
if [1].pack("I") == [1].pack("N")
6+
puts "BigEndian"
7+
else
8+
puts "LittleEndian"
9+
end

filesize.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
puts File.size(ARGV[0])
1+
puts File.size(ARGV[0])

0 commit comments

Comments
 (0)