Skip to content

Commit

Permalink
initial commit with a few challenges done in ruby and swift
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan McAllister committed Jun 8, 2017
0 parents commit 7959f7f
Show file tree
Hide file tree
Showing 32 changed files with 1,384 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rep for Hackerrank coding challenges

7 changes: 7 additions & 0 deletions bigsort/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
6
31415926535897932384626433832795
1
3
10
5
3
1,001 changes: 1,001 additions & 0 deletions bigsort/input2.txt

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions bigsort/prog.rb
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
9 changes: 9 additions & 0 deletions bigsort/prog.swift
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"))")
2 changes: 2 additions & 0 deletions coinchange/input0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
4 3
1 2 3
2 changes: 2 additions & 0 deletions coinchange/input1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10 4
2 5 3 6
2 changes: 2 additions & 0 deletions coinchange/input2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
11 2
4 3
2 changes: 2 additions & 0 deletions coinchange/input3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
11 3
4 3 5
2 changes: 2 additions & 0 deletions coinchange/input6.txt
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
1 change: 1 addition & 0 deletions coinchange/output0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
1 change: 1 addition & 0 deletions coinchange/output1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5
1 change: 1 addition & 0 deletions coinchange/output2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
23 changes: 23 additions & 0 deletions coinchange/prog.rb
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
31 changes: 31 additions & 0 deletions coinchange/prog1.rb
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
5 changes: 5 additions & 0 deletions equal/input1.txt
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
16 changes: 16 additions & 0 deletions equal/prog.rb
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
1 change: 1 addition & 0 deletions fibonacci/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
10 changes: 10 additions & 0 deletions fibonacci/prog.rb
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)
4 changes: 4 additions & 0 deletions journeytomoon/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
5 3
0 1
2 3
0 4
2 changes: 2 additions & 0 deletions journeytomoon/input0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
4 1
0 2
8 changes: 8 additions & 0 deletions journeytomoon/input1.txt
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
42 changes: 42 additions & 0 deletions journeytomoon/prog.rb
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
61 changes: 61 additions & 0 deletions journeytomoon/prog.swift
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)")
49 changes: 49 additions & 0 deletions journeytomoon/prog_t.rb
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
2 changes: 2 additions & 0 deletions leftrotation/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
5 4
1 2 3 4 5
4 changes: 4 additions & 0 deletions leftrotation/prog.rb
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(" ")
6 changes: 6 additions & 0 deletions leftrotation/prog.swift
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: " "))")
1 change: 1 addition & 0 deletions procs/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2 3
11 changes: 11 additions & 0 deletions procs/prog.rb
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)

12 changes: 12 additions & 0 deletions roadsandlibs/input.txt
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
Loading

0 comments on commit 7959f7f

Please sign in to comment.