Skip to content

Commit 8907d50

Browse files
committed
exercism 30-35
1 parent 7763de6 commit 8907d50

File tree

6 files changed

+194
-1
lines changed

6 files changed

+194
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
> ### Featured on [Awesome-algorithms](https://github.com/tayllan/awesome-algorithms) & [Ruby-bookmarks](https://github.com/dreikanter/ruby-bookmarks)
33
44
This repository contains Ruby implementation of various Algorithms and Data structures and
5-
solution of many challenges of [Leetcode](https://leetcode.com/),[Exercism](https://exercism.org/),[HackerRank](https://www.hackerrank.com) and [Codility](https://codility.com)
5+
solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](https://exercism.org/), [HackerRank](https://www.hackerrank.com) and [Codility](https://codility.com)
66

77
## Content :
88
* [Searching](#searching)

Diff for: exercism/clock.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/clock
2+
3+
#Solution
4+
class Clock
5+
attr_accessor :hour, :minute
6+
7+
def initialize(hour:0,minute: 0)
8+
@hour = (hour+(minute/60)) % 24
9+
@minute = minute%60
10+
end
11+
12+
def to_s
13+
display_time(@hour,@minute)
14+
end
15+
16+
def ==(other)
17+
self.class == other.class &&
18+
@hour == other.hour &&
19+
@minute == other.minute
20+
end
21+
22+
def +(other)
23+
raise TypeError unless self.class == other.class
24+
Clock.new(hour:@hour+other.hour,minute:@minute+other.minute)
25+
end
26+
27+
def -(other)
28+
raise TypeError unless self.class == other.class
29+
Clock.new(hour:@hour-other.hour,minute:@minute-other.minute)
30+
end
31+
32+
private
33+
def display_time(hour,minute)
34+
display_hour = hour <10 ? "0#{hour}" : hour
35+
display_minute = minute<10 ? "0#{minute}" : minute
36+
"#{display_hour}:#{display_minute}"
37+
end
38+
end

Diff for: exercism/gigasecond.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# problem: https://exercism.org/tracks/ruby/exercises/gigasecond
2+
3+
#Solution
4+
class Gigasecond
5+
def self.from(input_date)
6+
input_date+10**9
7+
end
8+
end

Diff for: exercism/luhn.rb

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#Problem :https://exercism.org/tracks/ruby/exercises/luhn
2+
3+
#Solution 1
4+
class Luhn
5+
def self.valid?(card_num)
6+
is_index_odd = true
7+
card_len = 0
8+
sum=0
9+
for i in (card_num.length-1).downto(0)
10+
next if card_num[i]==" "
11+
return false if card_num[i]<"0" or card_num[i]>"9"
12+
card_len+=1
13+
digit = is_index_odd ? card_num[i].to_i : (card_num[i].to_i*2)
14+
digit = digit>9 ? digit-9 : digit
15+
sum+=digit
16+
is_index_odd = !is_index_odd
17+
end
18+
sum%10==0 and card_len>1
19+
end
20+
end
21+
22+
#Solution 2
23+
# Toggling multiplier between one and two
24+
class Luhn
25+
def self.valid?(card_num)
26+
multiplier = 2
27+
card_len = 0
28+
sum=0
29+
for i in (card_num.length-1).downto(0)
30+
next if card_num[i]==" "
31+
return false if card_num[i]<"0" or card_num[i]>"9"
32+
card_len+=1
33+
multiplier = 3-multiplier
34+
digit = (card_num[i].to_i*multiplier)
35+
digit = digit>9 ? digit-9 : digit
36+
sum+=digit
37+
end
38+
sum%10==0 and card_len>1
39+
end
40+
end
41+
42+
#Solution 3
43+
class Luhn
44+
def self.valid?(card_num)
45+
card_num = card_num.gsub(/\s/,'')
46+
return false unless card_num.scan(/\D/).empty?
47+
card_num_len = card_num.length
48+
return false if card_num_len<=1
49+
card_digits =card_num.reverse.chars.map(&:to_i)
50+
multiplier = 2
51+
for i in 0...card_num_len
52+
multiplier = 3-multiplier #This will toggle between 1 and 2
53+
card_digits[i]*=multiplier
54+
card_digits[i]= card_digits[i]-9 if card_digits[i]>9
55+
end
56+
card_digits.sum%10==0
57+
end
58+
end
59+

Diff for: exercism/tournament.rb

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#Problem: https://exercism.org/tracks/ruby/exercises/tournament
2+
3+
#Solution
4+
class Tournament
5+
def self.tally(input)
6+
matches = input.split("\n")
7+
point_table = {}
8+
matches.each do |match_str|
9+
team1,team2,result = match_str.split(";")
10+
self.set_team_data(team1,point_table) unless point_table.has_key?(team1)
11+
self.set_team_data(team2,point_table) unless point_table.has_key?(team2)
12+
point_table[team1][:MP]+=1
13+
point_table[team2][:MP]+=1
14+
if result == "win"
15+
point_table[team1][:W]+=1
16+
point_table[team1][:P]+=3
17+
point_table[team2][:L]+=1
18+
elsif result=="loss"
19+
point_table[team2][:W]+=1
20+
point_table[team2][:P]+=3
21+
point_table[team1][:L]+=1
22+
elsif result=="draw"
23+
point_table[team1][:D]+=1
24+
point_table[team2][:D]+=1
25+
point_table[team1][:P]+=1
26+
point_table[team2][:P]+=1
27+
else
28+
raise InputError
29+
end
30+
end
31+
self.format_scorebaord(point_table)
32+
end
33+
34+
private
35+
def self.set_team_data(team,point_table)
36+
point_table[team] = { "MP":0, "W":0, "L":0 , "D":0, "P":0 }
37+
end
38+
39+
def self.format_scorebaord(point_table)
40+
point_table = point_table.sort_by{|team,score| [-score[:P],team]}
41+
formatted = point_table.map do |team, result|
42+
played = result[:MP].to_s.rjust(2)
43+
points = result[:P ].to_s.rjust(2)
44+
wins = result[:W ].to_s.rjust(2)
45+
draws = result[:D ].to_s.rjust(2)
46+
losses = result[:L ].to_s.rjust(2)
47+
"#{team.ljust(30)} | #{played} | #{wins} | #{draws} | #{losses} | #{points}\n"
48+
end
49+
header = "Team | MP | W | D | L | P\n"
50+
header + formatted.join
51+
end
52+
end

Diff for: exercism/twelve_days.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#Problem: https://exercism.org/tracks/ruby/exercises/twelve-days
2+
3+
#Solution
4+
class TwelveDays
5+
DAYS = {
6+
1=>["first","a Partridge in a Pear Tree"],
7+
2=>["second","two Turtle Doves"],
8+
3=>["third","three French Hens"],
9+
4=>["fourth","four Calling Birds"],
10+
5=>["fifth","five Gold Rings"],
11+
6=>["sixth","six Geese-a-Laying"],
12+
7=>["seventh","seven Swans-a-Swimming"],
13+
8=>["eighth","eight Maids-a-Milking"],
14+
9=>["ninth","nine Ladies Dancing"],
15+
10=>["tenth","ten Lords-a-Leaping"],
16+
11=>["eleventh","eleven Pipers Piping"],
17+
12=>["twelfth","twelve Drummers Drumming"]
18+
}
19+
def self.song
20+
song = ""
21+
base_line = "On the %{ith} day of Christmas my true love gave to me: %{phrase}.\n"
22+
for i in 1..12
23+
phrase = ""
24+
j =i
25+
while j>0
26+
phrase+="and " if j==1 and i!=1
27+
phrase+=DAYS[j][1]
28+
phrase+=", " unless j==1
29+
j-=1
30+
end
31+
song+="\n" unless song==""
32+
song+= base_line % {ith:DAYS[i][0],phrase:phrase}
33+
end
34+
song
35+
end
36+
end

0 commit comments

Comments
 (0)