-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit with a few challenges done in ruby and swift
- Loading branch information
Jonathan McAllister
committed
Jun 8, 2017
0 parents
commit 7959f7f
Showing
32 changed files
with
1,384 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
rep for Hackerrank coding challenges | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
6 | ||
31415926535897932384626433832795 | ||
1 | ||
3 | ||
10 | ||
5 | ||
3 |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/ruby | ||
|
||
n = gets.strip.to_i | ||
unsorted = [] | ||
n.times { unsorted << gets.strip.to_i } | ||
puts unsorted.sort | ||
# your code goes here |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Foundation; | ||
var n = Int(readLine()!)! | ||
var unsorted = [String]() | ||
for _ in 0..<n { unsorted.append(readLine()!) } | ||
var sorted = unsorted.sorted{ (left,right) -> Bool in | ||
if left.utf8.count == right.utf8.count { return left < right } | ||
return left.utf8.count < right.utf8.count | ||
} | ||
print("\(sorted.joined(separator: "\n"))") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
4 3 | ||
1 2 3 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
10 4 | ||
2 5 3 6 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
11 2 | ||
4 3 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
11 3 | ||
4 3 5 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
15 24 | ||
49 22 45 6 11 20 30 10 46 8 32 48 2 41 43 5 39 16 28 44 14 4 27 36 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
5 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/ruby | ||
|
||
def get_ways(n,c,m) | ||
|
||
return 1 if n == 0 | ||
return 0 if n < 0 || m <=0 | ||
|
||
@mem_ways ||= [] | ||
@mem_ways[n] ||= [] | ||
|
||
if !!@mem_ways[n][m] | ||
return @mem_ways[n][m] | ||
else | ||
@mem_ways[n][m] = get_ways(n,c,m-1) + get_ways(n - c[m-1],c,m) | ||
end | ||
end | ||
|
||
n, m = gets.strip.split(' ').map(&:to_i) | ||
c = gets.strip.split(' ').map(&:to_i) | ||
|
||
# Print the number of ways of making change for 'n' units using coins having the values given by 'c' | ||
ways = get_ways(n,c,m) | ||
puts ways |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# how to get the list of ways | ||
|
||
#!/bin/ruby | ||
|
||
def get_ways(n,c,m) | ||
|
||
return 1 if n == 0 | ||
return 0 if n < 0 || m <=0 | ||
|
||
@vals ||= [] | ||
@mem_ways ||= [] | ||
@mem_ways[n] ||= [] | ||
|
||
if !!@mem_ways[n][m] | ||
return @mem_ways[n][m] | ||
else | ||
tmp = get_ways(n,c,m-1) | ||
tmp1 = get_ways(n - c[m-1],c,m) | ||
@vals << c[m-1] if tmp1 == 1 | ||
@vals << c[m-1] if tmp1 == 1 | ||
@mem_ways[n][m] = tmp + tmp1 | ||
end | ||
end | ||
|
||
n, m = gets.strip.split(' ').map(&:to_i) | ||
c = gets.strip.split(' ').map(&:to_i) | ||
|
||
# Print the number of ways of making change for 'n' units using coins having the values given by 'c' | ||
ways = get_ways(n,c,m) | ||
puts ways | ||
p @vals |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
2 | ||
4 | ||
2 2 3 7 | ||
5 | ||
2 2 3 9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'matrix' | ||
t = gets.to_i | ||
t.times do | ||
@counter = nil | ||
q = gets.to_i | ||
folks = Matrix[gets.split(" ").map(&:to_i).sort] | ||
targets = folks.first.downto(folks.first-4) | ||
targets.each do |target| | ||
least = Matrix[Array.new(q, target)] | ||
diff = folks - least | ||
min = diff.map { |folk| folk/5 + (folk%5)/2 + (folk%5)%2 }.inject(&:+) | ||
@counter ||= min | ||
@counter = @counter < min ? @counter : min | ||
end | ||
puts @counter | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/ruby | ||
def fibonacci(n) | ||
return n if n < 2 | ||
fib = [0,1] | ||
(2..n).each { fib = [fib.last, fib.first + fib.last] } | ||
fib.last | ||
end | ||
|
||
n = gets.to_i | ||
puts fibonacci(n) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
5 3 | ||
0 1 | ||
2 3 | ||
0 4 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
4 1 | ||
0 2 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
10 7 | ||
0 2 | ||
1 8 | ||
1 4 | ||
2 8 | ||
2 6 | ||
3 5 | ||
6 9 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/bin/ruby | ||
n, p = gets.split.map(&:to_i) | ||
|
||
@nodes = {} | ||
@graph_sizes = [] | ||
|
||
def graph_size(size, node, nodes) | ||
node[:edges].each do |edge_id| | ||
next if !!nodes[edge_id][:graph_id] | ||
nodes[edge_id][:graph_id] = node[:graph_id] | ||
size = graph_size(size+1, nodes[edge_id], nodes) | ||
end | ||
size | ||
end | ||
|
||
p.times do | ||
a,b = gets.split.map(&:to_i) | ||
|
||
@nodes[a] ||= { id: a, edges: [] } | ||
@nodes[a][:edges] << b | ||
|
||
@nodes[b] ||= { id: b, edges: [] } | ||
@nodes[b][:edges] << a | ||
end | ||
|
||
@nodes.each do |id, node| | ||
next if !!node[:graph_id] | ||
node[:graph_id] = id | ||
@graph_sizes << graph_size(1, node, @nodes) | ||
end | ||
|
||
single_nodes = (n - @nodes.length) | ||
@count = 0 | ||
@graph_sizes.length.times do |i| | ||
graph_size = @graph_sizes.shift | ||
remaining_nodes = @graph_sizes.inject(:+).to_i + single_nodes | ||
@count += graph_size * remaining_nodes | ||
end | ||
|
||
@count += (single_nodes - 1).downto(1).inject(:+) if single_nodes > 1 | ||
|
||
puts @count |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Foundation; | ||
var line = readLine()!.components(separatedBy: [" "]) | ||
let n = Int(line[0])! | ||
let p = Int(line[1])! | ||
|
||
class Node { | ||
var id : Int! | ||
var edges: [Int] | ||
var graph_id: Int | ||
|
||
init(id: Int) { | ||
self.id = id | ||
self.edges = [] | ||
self.graph_id = -1 | ||
} | ||
} | ||
|
||
var nodes = [Int: Node]() | ||
var graph_sizes = [Int]() | ||
|
||
func graph_size(size: inout Int, node: Node, nodes: inout [Int: Node]) -> Int { | ||
for edge_id in node.edges { | ||
if(nodes[edge_id]?.graph_id != -1){ | ||
continue | ||
} | ||
nodes[edge_id]?.graph_id = node.graph_id | ||
size+=1 | ||
size = graph_size(size: &size, node: nodes[edge_id]!, nodes: &nodes) | ||
} | ||
return size | ||
} | ||
|
||
for _ in 0..<p { | ||
var line = readLine()!.components(separatedBy: [" "]) | ||
var a = Int(line[0])! | ||
var b = Int(line[1])! | ||
nodes[a] = nodes[a] ?? Node(id: a) | ||
nodes[a]?.edges.append(b) | ||
nodes[b] = nodes[b] ?? Node(id: b) | ||
nodes[b]?.edges.append(a) | ||
} | ||
|
||
for (id,node) in nodes { | ||
if(node.graph_id != -1) { continue } | ||
node.graph_id = id | ||
var size = 1 | ||
var gs = graph_size(size: &size, node: node, nodes: &nodes) | ||
graph_sizes.append(gs) | ||
} | ||
|
||
var single_nodes = (n - nodes.count) | ||
var count = 0 | ||
for _ in 0..<graph_sizes.count { | ||
var gs = graph_sizes.removeFirst() | ||
var remaining_nodes = graph_sizes.reduce(0,+) + single_nodes | ||
count += gs * remaining_nodes | ||
} | ||
|
||
if(single_nodes > 1) { for i in 1..<single_nodes { count += i }} | ||
|
||
print("\(count)") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/bin/ruby | ||
n, p = gets.split.map(&:to_i) | ||
|
||
@astronauts = {} | ||
@countries = {} | ||
|
||
def build_graph(country, astronaut, astronauts) | ||
astronaut[:countrymen].each do |ac| | ||
next if !!astronauts[ac][:country_id] | ||
astronauts[ac][:country_id] = country[:id] | ||
country[:astronauts] += 1 | ||
build_graph(country, astronauts[ac], astronauts) | ||
end | ||
country | ||
end | ||
|
||
p.times do | ||
a,b = gets.split.map(&:to_i) | ||
|
||
astro_a = begin | ||
@astronauts[a] ||= { id: a, countrymen: [], country_id: nil } | ||
end | ||
astro_b = begin | ||
@astronauts[b] ||= { id: b, countrymen: [], country_id: nil } | ||
end | ||
|
||
astro_a[:countrymen] << b | ||
astro_b[:countrymen] << a | ||
end | ||
|
||
@astronauts.each do |id, astronaut| | ||
next if astronaut[:country_id] | ||
astronaut[:country_id] = id | ||
country = { id: id, astronauts: 1 } | ||
@countries[id] = build_graph(country, astronaut, @astronauts) | ||
end | ||
|
||
countries = @countries.values | ||
(n - @astronauts.length).times { countries << { astronauts: 1 } } | ||
|
||
@count = 0 | ||
countries.length.times do |i| | ||
country = countries.shift | ||
n = country[:astronauts] | ||
k = countries.map{|c| c[:astronauts]}.inject(:+).to_i | ||
@count += n*k | ||
end | ||
|
||
puts @count |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
5 4 | ||
1 2 3 4 5 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/ruby | ||
n,k = gets.strip.split(' ').map(&:to_i) | ||
a = gets.strip.split(' ').map(&:to_i) | ||
puts (a[k..n-1] << a[0..k-1]).join(" ") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Foundation; | ||
let line1 = readLine()!.components(separatedBy: [" "]) | ||
let n = Int(line1[0])! | ||
let k = Int(line1[1])! | ||
let array = readLine()!.components(separatedBy: [" "]) | ||
print("\(Array(array[k ..< n] + array[0 ..< k]).joined(separator: " "))") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 2 3 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
def square_of_sum (my_array, proc_square, proc_sum) | ||
sum = proc_sum.call(my_array) | ||
proc_square.call(sum) | ||
end | ||
|
||
proc_square_number = -> (a) { a*a } | ||
proc_sum_array = -> (ary) { ary.inject(&:+) } | ||
my_array = gets.split().map(&:to_i) | ||
|
||
puts square_of_sum(my_array, proc_square_number, proc_sum_array) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
2 | ||
3 3 2 1 | ||
1 2 | ||
3 1 | ||
2 3 | ||
6 6 2 5 | ||
1 3 | ||
3 4 | ||
2 4 | ||
1 2 | ||
2 3 | ||
5 6 |
Oops, something went wrong.