I need to have a list with id's for each list item being #one, #two etc.
Is this the most efficient way or am I missing an in built ruby function here?
-num_array = ["one", "two", "three", "four", "five", "six", "seven"]
-navigation[:primary_level].each_with_index do |primary_item, idx|
%li{ :id => "#{num_array[idx]}"}
The humanize gem converts digits into words.
Outside of using the humanize gem, using a hash would be way easier than the array stuff:
lookup = {"one" => 1, "two" => 2, "three" => 3, etc...}
text = "two"
num = lookup[text]
I'm sure this goes well beyond what you need, but there's code to do that at Rosetta Code
Here's my attempt at a solution in Ruby. It is likely suboptimal and has not been checked for correctness.
<<documentation
Converting Numbers to Human Readable Pretty Print Strings
General Description
===================
- Divide the number into groups of three
- e.g. turn 87012940 -> 87,012,940
- Parse each individual group
- e.g. 940 -> "nine hundred forty"
- Only parse the rightmost two numbers
- 0 -> 12: special cases; use hardcoded switch statements
- e.g. "one, two, three ... ten, eleven, twelve"
- 13 -> 19: same hardcoded switch statement + a "-teen" prefix
- e.g. "thirteen, fourteen, fifteen ... nineteen"
- 20 -> 99:
- Parse left digit and return according to the following rule:
- "twenty, thirty, forty, fifty, sixty ... ninety"
- Return the simple name of the right digit:
- "one, two, nine"
- special case: zero -> ""
- This is because the hundredth's place follows a simple prefix rule
- e.g. one-hundred, two-hundred, three-hundred ... nine-hundred
- special case: zero -> " "
- Add place modifiers according to each group's placement
- e.g. the middle '012' -> "twelve thousand"
- Concatenate all and return as solution
Algorithm (slightly modified)
=============================
Modifications
-------------
- No need to divide number into groups of three; simply parse right-to-left one at a time
- When finished processing one group, insert the result leftmost into our final solution string
documentation
def convert(num)
return 'zero' if (num == 0)
answer = ''
places = ['',
'thousand ',
'million ',
'billion ',
'trillion ',
'quadrillion ',
'quintillion ']
place = 0
loop do
break if num == 0
# Get the rightmost group of three
first_three_digits = num % 1000
# Truncate the original number by those three digits
num /= 1000
answer.insert(0, convert_group_of_three(first_three_digits) + places[place])
place += 1
end
answer.strip!
end
def convert_group_of_three(num)
str = ''
# Zero returns an empty string
special_cases = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ',
'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen ']
return special_cases[num % 100] if (0 .. special_cases.length - 1).include? (num % 100)
# If not in special cases, num must be at least a two digit number
# Pull the first digit
first_digit = num % 10
num /= 10
str.insert(0, special_cases[first_digit])
# Pull the second digit
second_digit = num % 10
num /= 10
second_digit_str = ''
case second_digit
when 2
second_digit_str = 'twenty '
when 3
second_digit_str = 'thirty '
when 4
second_digit_str = 'forty '
when 5
second_digit_str = 'fifty '
when 6
second_digit_str = 'sixty '
when 7
second_digit_str = 'seventy '
when 8
second_digit_str = 'eighty '
when 9
second_digit_str = 'ninety '
end
str.insert(0, second_digit_str)
# If there is a third digit
if num > 0
third_digit = num % 10
str.insert(0, special_cases[third_digit] + 'hundred ')
end
str
end
p convert(2389475623984756)
Output:
"two quadrillion three hundred eighty nine trillion four hundred seventy five billion six hundred twenty three million nine hundred eighty four thousand seven hundred fifty six"
Related
I have been attempting the test below on codewars. I am relatively new to coding and will look for more appropriate solutions as well as asking you for feedback on my code. I have written the solution at the bottom and for the life of me cannot understand what is missing as the resultant figure is always 0. I'd very much appreciate feedback on my code for the problem and not just giving your best solution to the problem. Although both would be much appreciated. Thank you in advance!
The test posed is:
If we list all the natural numbers below 10 that are multiples of 3 or
5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Finish the solution so that it returns the sum of all the multiples of
3 or 5 below the number passed in. Additionally, if the number is
negative, return 0 (for languages that do have them).
Note: If the number is a multiple of both 3 and 5, only count it once.
My code is as follows:
def solution(number)
array = [1..number]
multiples = []
if number < 0
return 0
else
array.each { |x|
if x % 3 == 0 || x % 5 == 0
multiples << x
end
}
end
return multiples.sum
end
In a situation like this, when something in your code produces an unexpected result you should debug it, meaning, run it line by line with the same argument and see what each variable holds. Using some kind of interactive console for running code (like irb) is very helpfull.
Moving to your example, let's start from the beginning:
number = 10
array = [1..number]
puts array.size # => 1 - wait what?
puts array[0].class # => Range
As you can see the array variable doesn't contain numbers but rather a Range object. After you finish filtering the array the result is an empty array that sums to 0.
Regardless of that, Ruby has a lot of built-in methods that can help you accomplish the same problem typing fewer words, for example:
multiples_of_3_and_5 = array.select { |number| number % 3 == 0 || number % 5 == 0 }
When writing a multiline block of code, prefer the do, end syntax, for example:
array.each do |x|
if x % 3 == 0 || x % 5 == 0
multiples << x
end
end
I'm not suggesting that this is the best approach per se, but using your specific code, you could fix the MAIN problem by editing the first line of your code in one of 2 ways:
By either converting your range to an array. Something like this would do the trick:
array = (1..number).to_a
or by just using a range INSTEAD of an array like so:
range = 1..number
The latter solution inserted into your code might look like this:
number = 17
range = 1..number
multiples = []
if number < 0
return 0
else range.each{|x|
if x % 3 == 0 || x % 5 == 0
multiples << x
end
}
end
multiples.sum
#=> 60
The statement return followed by end suggests that you were writing a method, but the def statement is missing. I believe that should be
def tot_sum(number, array)
multiples = []
if number < 0
return 0
else array.each{|x|
if x % 3 == 0 || x % 5 == 0
multiples << x
end
}
end
return multiples.sum
end
As you point out, however, this double-counts numbers that are multiples of 15.
Let me suggest a more efficient way of writing that. First consider the sum of numbers that are multiples of 3 that do not exceed a given number n.
Suppose
n = 3
m = 16
then the total of numbers that are multiples of three that do not exceed 16 can be computed as follows:
3 * 1 + 3 * 2 + 3 * 3 + 3 * 4 + 3 * 5
= 3 * (1 + 2 + 3 + 4 + 5)
= 3 * 5 * (1 + 5)/2
= 45
This makes use of the fact that 5 * (1 + 5)/2 equals the sum of an algebraic series: (1 + 2 + 3 + 4 + 5).
We may write a helper method to compute this sum for any number n, with m being the number that multiples of n cannot exceed:
def tot_sum(n, m)
p = m/n
n * p * (1 + p)/2
end
For example,
tot_sum(3, 16)
#=> 45
We may now write a method that gives the desired result (remembering that we need to account for the fact that multiples of 15 are multiples of both 3 and 5):
def tot(m)
tot_sum(3, m) + tot_sum(5, m) - tot_sum(15, m)
end
tot( 9) #=> 23
tot( 16) #=> 60
tot(9999) #=> 23331668
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
def multiples_of(number)
number = number.to_f - 1.0
result = 0
if (number / 5.0) == 1 || (number / 3.0) == 1
return result = result + 5.0 + 3.0
elsif (number % 3).zero? || (number % 5).zero?
result += number
multiples_of(number-1)
else
multiples_of(number-1)
end
return result
end
p multiples_of(10.0)
My code is returning 9.0 rather than 23.0.
Using Core Methods to Select & Sum from a Range
It's not entirely clear what you really want to do here. This is clearly a homework assignment, so it's probably intended to get you to think in a certain way about whatever the lesson is. If that's the case, refer to your lesson plan or ask your instructor.
That said, if you restrict the set of possible input values to integers and use iteration rather than recursion, you can trivially solve for this using Array#select on an exclusive Range, and then calling Array#sum on the intermediate result. For example:
(1...10).select { |i| i.modulo(3).zero? || i.modulo(5).zero? }.sum
#=> 23
(1...1_000).select { |i| i.modulo(3).zero? || i.modulo(5).zero? }.sum
#=> 233168
Leave off the #sum if you want to see all the selected values. In addition, you can create your own custom validator by comparing your logic to an expected result. For example:
def valid_result? range_end, checksum
(1 ... range_end).select do |i|
i.modulo(3).zero? || i.modulo(5).zero?
end.sum.eql? checksum
end
valid_result? 10, 9
#=> false
valid_result? 10, 23
#=> true
valid_result? 1_000, 233_168
#=> true
There are a number of issues with your code. Most importantly, you're making recursive calls but you aren't combining their results in any way.
Let's step through what happens with an input of 10.
You assign number = number.to_f - 1.0 which will equal 9.
Then you reach the elsif (number % 3).zero? || (number % 5).zero? condition which is true, so you call result += number and multiples_of(number-1).
However, you're discarding the return value of the recursive call and call return result no matter what. So, your recursion doesn't have any impact on the return value. And for any input besides 3 or 5 you will always return input-1 as the return value. That's why you're getting 9.
Here's an implementation which works, for comparison:
def multiples_of(number)
number -= 1
return 0 if number.zero?
if number % 5 == 0 || number % 3 == 0
number + multiples_of(number)
else
multiples_of(number)
end
end
puts multiples_of(10)
# => 23
Note that I'm calling multiples_of(number) instead of multiples_of(number - 1) because you're already decrementing the input on the function's first line. You don't need to decrement twice - that would cause you to only process every other number e.g. 9,7,5,3
explanation
to step throgh the recursion a bit to help you understand it. Let's say we have an input of 4.
We first decrement the input so number=3. Then we hits the if number % 5 == 0 || number % 3 == 0 condition so we return number + multiples_of(number).
What does multiples_of(number) return? Now we have to evaluate the next recursive call. We decrement the number so now we have number=2. We hit the else block so now we'll return multiples_of(number).
We do the same thing with the next recursive call, with number=1. This multiples_of(1). We decrement the input so now we have number=0. This matches our base case so finally we're done with recursive calls and can work up the stack to figure out what our actual return value is.
For an input of 6 it would look like so:
multiples_of(6)
\
5 + multiples_of(5)
\
multiples_of(4)
\
3 + multiples_of(3)
\
multiples_of(2)
\
multiples_of(1)
\
multiples_of(0)
\
0
The desired result can be obtained from a closed-form expression. That is, no iteration is required.
Suppose we are given a positive integer n and wish to compute the sum of all positive numbers that are multiples of 3 that do not exceed n.
1*3 + 2*3 +...+ m*3 = 3*(1 + 2 +...+ m)
where
m = n/3
1 + 2 +...+ m is the sum of an algorithmic expression, given by:
m*(1+m)/2
We therefore can write:
def tot(x,n)
m = n/x
x*m*(1+m)/2
end
For example,
tot(3,9) #=> 18 (1*3 + 2*3 + 3*3)
tot(3,11) #=> 18
tot(3,12) #=> 30 (18 + 4*3)
tot(3,17) #=> 45 (30 + 5*3)
tot(5,9) #=> 5 (1*5)
tot(5,10) #=> 15 (5 + 2*5)
tot(5,14) #=> 15
tot(5,15) #=> 30 (15 + 3*5)
The sum of numbers no larger than n that are multiple of 3's and 5's is therefore given by the following:
def sum_of_multiples(n)
tot(3,n) + tot(5,n) - tot(15,n)
end
- tot(15,n) is needed because the first two terms double-count numbers that are multiples of 15.
sum_of_multiples(9) #=> 23 (3 + 6 + 9 + 5)
sum_of_multiples(10) #=> 33 (23 + 2*5)
sum_of_multiples(11) #=> 33
sum_of_multiples(12) #=> 45 (33 + 4*3)
sum_of_multiples(14) #=> 45
sum_of_multiples(15) #=> 60 (45 + 3*5)
sum_of_multiples(29) #=> 195
sum_of_multiples(30) #=> 225
sum_of_multiples(1_000) #=> 234168
sum_of_multiples(10_000) #=> 23341668
sum_of_multiples(100_000) #=> 2333416668
sum_of_multiples(1_000_000) #=> 233334166668
x = 16
while x != 1 do
if x % 2 == 0
x = x / 2
print "#{x} "
end
break if x < 0
end
Hi, the result I get from above is 8 4 2 1 . Is there any way to remove the space at the end?
One of Rubys main features is its beauty - you can shorten that loop to a nice one liner when using an array:
x = 16
arr = []
arr.push(x /= 2) while x.even?
puts arr.join(' ')
# => "8 4 2 1"
* As sagarpandya82 suggested x.even? is the same as using x % 2 == 0, leading to even more readable code
Don't print the values into the loop. Put them into a list (array) then, after the loop, join the array items using space as glue.
x = 16
a = []
while x != 1 do
if x % 2 == 0
x = x / 2
a << x
end
break if x < 0
end
puts '<' + a.join(' ') + '>'
The output is:
<8 4 2 1>
As #Bathsheba notes in a comment, this solution uses extra memory (the array) to store the values and also the call to Array#join generates a string that doubles the memory requirements. This is not an issue for small lists as the one in the question but needs to be considered the list becomes very large.
loop.reduce([[], 16]) do |(acc, val), _|
break acc if val <= 1
acc << val / 2 if val.even?
[acc, val / 2]
end.join ' '
if x != 0
print " "
end
is one way, having dropped the suffixed space from the other print. I/O will always be the performance bottleneck; an extra if will have a negligible effect on performance, and the extra print will merely contribute to the output stream which is normally buffered.
I wrote a simple script to sum all digits of positive integer input until 1 digit is left ( for example for input 12345 result is 6 because 1+2+3+4+5 = 15 and 1+5 = 6). It works but is it better way to do that? ( more correct?)
here is a code:
def sum(n)
string=n.to_s
while string.length > 1 do
result=string.chars.inject { |sum,n| sum = sum.to_i + n.to_i}
string=result.to_s
end
puts "Sum of digits is " + string
end
begin
p "please enter a positive integer number:"
number = Integer(gets.chomp)
while number<0
p "Number must be positive!Enter again:"
number = Integer(gets.chomp)
end
rescue
p "You didnt enter integer!:"
retry
end
sum(number)
According to Wikipedia, the formula is:
dr(n) = 1 + ((n − 1) mod 9)
So it boils down to:
def sum(n)
1 + (n - 1) % 9
end
To account for 0, you can add return 0 if n.zero?
You could use divmod (quotient and modulus) to calculate the digit sum without converting to / from string. Something like this should work:
def sum(number)
result = 0
while number > 0 do
number, digit = number.divmod(10)
result += digit
if number == 0 && result >= 10
number = result
result = 0
end
end
result
end
sum(12345) #=> 6
The line
number, digit = number.divmod(10)
basically strips off the last digit:
12345.divmod(10) #=> [1234, 5]
1234 becomes the new number and 5 is being added to result. If number eventually becomes zero and result is equal or greater than 10 (i.e. more than one digit), result becomes the new number (e.g. 15) and the loops starts over. If result is below 10 (i.e. one digit), the loop exits and result is returned.
Short recursive version:
def sum_of_digits(digits)
sum = digits.chars.map(&:to_i).reduce(&:+).to_s
sum.size > 1 ? sum_of_digits(sum) : sum
end
p sum_of_digits('12345') #=> "6"
Single call version:
def sum_of_digits(digits)
digits = digits.chars.map(&:to_i).reduce(&:+).to_s until digits.size == 1
return digits
end
It's looking good to me. You might do things a little more conscise like use map to turn every char into an integer.
def sum(n)
string=n.to_s
while string.length > 1 do
result = string.chars.map(&:to_i).inject(&:+)
string = result.to_s
end
puts "Sum of digits is " + string
end
You could also use .digits, so you don't have to convert the input into a string.
def digital_root(n)
while n.digits.count > 1
array = n.digits
n = array.sum
end
return n
end
input like this:
100000000
1000
100
100000
10000
i need to start inserting char (,) from end of the string.first time after three char from end and then repeat the insert (,) after every 2 char
output
10,00,00,000
1,000
100
1,00,000
10,000
any hint guys how can i do this, i need to start inserting char(',') from end of the string.
thanks!
Here are two ways, the first using a regular expression, the last just inserting commas in a loop.
a = %w| 100000000 1000 100 100000 10000 |
#=> ["100000000", "1000", "100", "100000", "10000"]
#1 Use a regex
r = /
(?<=\d) # match digit in positive lookbehind
\d{2} # match two digits
(?= # begin positive lookahead
(?:\d{2})* # match two digits, repeated zero or more times
\d # match last digit
\z # match end of string
) # end positive lookahead
/x # extended mode
a.each { |s| puts "#{s} -> #{ s.gsub(r) { |ss| ',' + ss } }" }
100000000 -> 10,00,00,000
1000 -> 1,000
100 -> 100
100000 -> 1,00,000
10000 -> 10,000
This regex is similar to the one given earlier by #Avinish, but I chose to use a positive lookbehind and no capture group, and presented it in extended mode to help readers understand how it worked. I would use a regular expression here.
#2 Insert commas
If you do not wish to use a regular expression, you could determine the position of the last comma to insert (p below), the number of commas to insert (n below) and then insert them, back to front:
def insert_commas(string)
sz = string.size
str = string.dup
p = sz - 3
n = (sz - 2)/2
n.times { str.insert(p, ','); p -= 2 }
str
end
a.each { |s| puts "#{s} -> #{insert_commas(s)}" }
100000000 -> 10,00,00,000
1000 -> 1,000
100 -> 100
100000 -> 1,00,000
10000 -> 10,000
I duped string on the assumption you did not want to mutate string.
Alternatively,
def insert_commas(string)
sz = string.size
return string if sz < 4
p = sz.even? ? 1 : 2
string[0,p] + string[p..-2].gsub(/\d{2}/) { |s| ",#{s}" } + string[-1]
end
Use the below positive lookahead based regex.
gsub(/(\d)(?=(?:\d{2})*\d{3}$)/, "$1,")
DEMO