Skip to content

Commit 1589999

Browse files
committed
did a few kata on codewars
1 parent c3938f9 commit 1589999

7 files changed

Lines changed: 181 additions & 0 deletions
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')

0 commit comments

Comments
 (0)