Skip to content

Commit 36acf5e

Browse files
committed
2 parents 161db1a + a944451 commit 36acf5e

Some content is hidden

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

53 files changed

+4254
-0
lines changed

coderbyte/01-letter_changes.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def LetterChanges(str)
2+
3+
str = str.each_char.map do |c|
4+
if c =~/[dDhHnNtT]/
5+
c.next.upcase
6+
elsif c =~ /[a-yA-Y]/
7+
c.next
8+
elsif c =~ /[zZ]/
9+
(c.ord - 25).chr.upcase
10+
else
11+
c
12+
end
13+
end.join
14+
15+
return str
16+
17+
end
18+
19+
# keep this function call here
20+
# to see how to enter arguments in Ruby scroll down
21+
puts LetterChanges(STDIN.gets)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def next_bigger(n)
2+
return unless n.integer? && n > 0
3+
4+
digits = n.to_s.split("").map{|digit| digit.to_i}
5+
least_significant_digits = []
6+
loop do
7+
least_significant_digits << digits.pop
8+
break if digits.empty?
9+
if digits.last < least_significant_digits.max
10+
temp = least_significant_digits.select{|digit| digit > digits.last}.sort.first
11+
least_significant_digits.delete_at(least_significant_digits.index(temp))
12+
least_significant_digits << digits.pop
13+
digits << temp
14+
return (digits + least_significant_digits.sort).join.to_i
15+
end
16+
end
17+
return -1
18+
end
19+
20+
puts next_bigger(12)
21+
puts next_bigger(513)
22+
puts next_bigger(2017)
23+
puts next_bigger(414)
24+
puts next_bigger(144)
25+
puts next_bigger(9)
26+
puts next_bigger(111)
27+
puts next_bigger(531)

codewars/01-text_align_justify.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Your task in this Kata is to emulate text justification in monospace font.
2+
# You will be given a single-lined text and the expected justification width.
3+
# The longest word will never be greater than this width.
4+
#
5+
# Here are the rules:
6+
#
7+
# *Use spaces to fill in the gaps between words.
8+
# *Each line should contain as many words as possible.
9+
# *Use '\n' to separate lines.
10+
# *Gap between words can't differ by more than one space.
11+
# *Lines should end with a word not a space.
12+
# *'\n' is not included in the length of a line.
13+
# *Large gaps go first, then smaller ones: 'Lorem---ipsum---dolor--sit--amet'
14+
# (3, 3, 2, 2 spaces).
15+
# *Last line should not be justified, use only one space between words.
16+
# *Last line should not contain '\n'
17+
# *Strings with one word do not need gaps ('somelongword\n').
18+
19+
20+
21+
def justify(text, width)
22+
# Split the string into words
23+
words = text.split
24+
result = ""
25+
26+
# shift words from the words array into the line array until required
27+
# characters exceeds width.
28+
loop do
29+
line = []
30+
characters_in_line = 0
31+
while characters_in_line + words.first.length <= width
32+
line << words.shift
33+
characters_in_line += line.last.length + 1
34+
if words.empty?
35+
characters_in_line = width + 1
36+
break
37+
end
38+
end
39+
40+
# add spaces & newline
41+
characters_in_line -= 1
42+
extra_spaces = width - characters_in_line
43+
until line.count == 1
44+
spaces = 1 + extra_spaces.fdiv(line.count - 1).ceil
45+
result += line.shift + " " * spaces
46+
extra_spaces -= (spaces - 1)
47+
end
48+
result += line.shift
49+
50+
return result if words.empty?
51+
result += "\n"
52+
end
53+
end
54+
55+
test_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc consequat ligula et lorem facilisis posuere. Proin nec lacus ac turpis vestibulum pretium vitae ac felis. Nulla rhoncus, ante et dapibus maximus, eros nunc sodales sem, ut semper massa erat non ipsum. Suspendisse pretium, massa ut hendrerit semper, felis ex elementum leo, ut faucibus quam eros eu libero. Suspendisse gravida ut turpis non dignissim. Suspendisse sit amet mauris neque. Cras eget urna velit. Sed ultricies, lacus ut rutrum ultrices, erat ligula venenatis ex, et ornare risus ipsum id urna. Integer accumsan hendrerit aliquam. Quisque semper dictum nulla, ac ornare nisl pulvinar lacinia. Quisque rutrum turpis et augue ullamcorper, a placerat ex consequat. Pellentesque euismod est eu ex pharetra pulvinar. Donec tincidunt mattis pharetra. Donec urna ligula, mattis a convallis eget, mollis at urna. Ut tincidunt sagittis semper."
56+
short_text = "123 45 6"
57+
58+
puts justify(test_text, 30)
59+
puts justify(short_text, 7)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Create a function taking a positive integer as its parameter and '
2+
# returning a string containing the Roman Numeral representation of that integer.
3+
#
4+
# Modern Roman numerals are written by expressing each digit separately starting
5+
# with the left most digit and skipping any digit with a value of zero.
6+
# In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC.
7+
# 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in
8+
# descending order: MDCLXVI.

codewars/greed.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def score( dice )
2+
score = 0
3+
dice = dice.sort.join
4+
(1..6).each do |n|
5+
dice.scan(/#{n}{3}/) do |triple|
6+
score += (triple[0] == "1" ? 1000 : triple[0].to_i * 100)
7+
dice = dice.sub(triple, "")
8+
end
9+
end
10+
dice.scan(/[1]/) {|n| score += 100}
11+
dice.scan(/[5]/) {|n| score += 50}
12+
score
13+
end
14+
15+
score( [1,1,1,1,5])

codewars/int32_to_IPv4.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def int32_to_ip(int32)
2+
binary = int32.to_s(2).rjust(32, "0")
3+
ip_address = binary.scan(/\d{8}/).map {|octet| octet.to_i(2).to_s}.join(".")
4+
end
5+
6+
puts int32_to_ip 2154959208
7+
puts int32_to_ip 0

codewars/pagination_helper.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# TODO: complete this class
2+
3+
class PaginationHelper
4+
5+
# The constructor takes in an array of items and a integer indicating how many
6+
# items fit within a single page
7+
def initialize(collection, items_per_page)
8+
@items_per_page = items_per_page
9+
@pages = Array.new(collection.count.fdiv(items_per_page).ceil) do |i|
10+
Array.new()
11+
end
12+
page_number = 0
13+
until collection.empty?
14+
@items_per_page.times do |item_number|
15+
@pages[page_number][item_number] = collection.shift
16+
break if collection.empty?
17+
end
18+
page_number += 1
19+
end
20+
end
21+
22+
# returns the number of items within the entire collection
23+
def item_count
24+
@pages.inject(0){|item_count, page| item_count + page.count}
25+
end
26+
27+
# returns the number of pages
28+
def page_count
29+
@pages.count
30+
end
31+
32+
# returns the number of items on the current page. page_index is zero based.
33+
# this method should return -1 for page_index values that are out of range
34+
def page_item_count(page_index)
35+
return -1 if page_index < 0 || page_index >= self.page_count
36+
@pages[page_index].count
37+
end
38+
39+
# determines what page an item is on. Zero based indexes.
40+
# this method should return -1 for item_index values that are out of range
41+
def page_index(item_index)
42+
return -1 if item_index < 0 || item_index >= self.item_count
43+
location = item_index.divmod @items_per_page
44+
location[0]
45+
end
46+
end
47+
48+
helper = PaginationHelper.new(['a','b','c','d','e','f'], 4)
49+
puts helper.page_count # should == 2
50+
puts helper.item_count # should == 6
51+
puts helper.page_item_count(0) # should == 4
52+
puts helper.page_item_count(1) # last page - should == 2
53+
puts helper.page_item_count(2) # should == -1 since the page is invalid
54+
55+
# page_ndex takes an item index and returns the page that it belongs on
56+
puts helper.page_index(5) # should == 1 (zero based index)
57+
puts helper.page_index(2) # should == 0
58+
puts helper.page_index(20) # should == -1
59+
puts helper.page_index(-10) # should == -1 because negative indexes are invalid

codewars/weirdcase.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def weirdcase string
2+
a = string.downcase.split.map{|word| word.chars.map.with_index{|c, i| i.even? ? c.upcase : c}.join}
3+
string
4+
end
5+
6+
weirdcase('This is a test')

koans/.path_progress

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0,0,0,1,2,3,5,6,6,6,6,7,7,7,8,8,9,10,11,11,11,12,13,13,14,13,13,14,14,15,16,17,18,18,18,18,18,18,19,20,21,21,22,23,27,27,29,31,32,33,34,34,35,36,36,37,38,39,39,40,41,41,41,42,43,45,46,47,49,49,50,50,50,51,51,52,56,59,59,60,62,64,65,65,65,65,65,66,68,70,73,74,74,74,74,74,74,74,74,74,74,75,76,77,78,79,80,81,81,82,83,83,83,84,85,85,87,88,89,90,91,92,93,94,95,96,97,98,98,99,100,102,104,105,106,106,106,107,108,108,110,111,111,113,113,113,113,114,115,116,119,120,121,121,121,121,122,124,124,125,126,129,130,132,133,136,138,141,142,143,145,148,152

koans/GREED_RULES.txt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
= Playing Greed
2+
3+
Greed is a dice game played among 2 or more players, using 5
4+
six-sided dice.
5+
6+
== Playing Greed
7+
8+
Each player takes a turn consisting of one or more rolls of the dice.
9+
On the first roll of the game, a player rolls all five dice which are
10+
scored according to the following:
11+
12+
Three 1's => 1000 points
13+
Three 6's => 600 points
14+
Three 5's => 500 points
15+
Three 4's => 400 points
16+
Three 3's => 300 points
17+
Three 2's => 200 points
18+
One 1 => 100 points
19+
One 5 => 50 points
20+
21+
A single die can only be counted once in each roll. For example,
22+
a "5" can only count as part of a triplet (contributing to the 500
23+
points) or as a single 50 points, but not both in the same roll.
24+
25+
Example Scoring
26+
27+
Throw Score
28+
--------- ------------------
29+
5 1 3 4 1 50 + 2 * 100 = 250
30+
1 1 1 3 1 1000 + 100 = 1100
31+
2 4 4 5 4 400 + 50 = 450
32+
33+
The dice not contributing to the score are called the non-scoring
34+
dice. "3" and "4" are non-scoring dice in the first example. "3" is
35+
a non-scoring die in the second, and "2" is a non-score die in the
36+
final example.
37+
38+
After a player rolls and the score is calculated, the scoring dice are
39+
removed and the player has the option of rolling again using only the
40+
non-scoring dice. If all of the thrown dice are scoring, then the
41+
player may roll all 5 dice in the next roll.
42+
43+
The player may continue to roll as long as each roll scores points. If
44+
a roll has zero points, then the player loses not only their turn, but
45+
also accumulated score for that turn. If a player decides to stop
46+
rolling before rolling a zero-point roll, then the accumulated points
47+
for the turn is added to his total score.
48+
49+
== Getting "In The Game"
50+
51+
Before a player is allowed to accumulate points, they must get at
52+
least 300 points in a single turn. Once they have achieved 300 points
53+
in a single turn, the points earned in that turn and each following
54+
turn will be counted toward their total score.
55+
56+
== End Game
57+
58+
Once a player reaches 3000 (or more) points, the game enters the final
59+
round where each of the other players gets one more turn. The winner
60+
is the player with the highest score after the final round.
61+
62+
== References
63+
64+
Greed is described on Wikipedia at
65+
http://en.wikipedia.org/wiki/Greed_(dice_game), however the rules are
66+
a bit different from the rules given here.

0 commit comments

Comments
 (0)