How can I change the month of a time object? - ruby

I have a 2 time objects, one object has a greater month:
t = Time.new(2017, 8, 1)
n = Time.new(2017, 9, 1)
How can I set the month of both objects with only one input?
I thought something like this would work:
month1 = gets #This works
month2 = month1 + 1
t = Time.new(2017, month1, 1) #This works too
n = Time.new(2017, month2, 1) #But this doesn't work

> month1 = gets.to_i
> 5
> month2 = month1 + 1
> t = Time.new(2017, month1, 1)
#=> 2017-05-01 00:00:00 +0530
> n = Time.new(2017, month2, 1)
#=> 2017-06-01 00:00:00 +0530
Note:
You just need to convert user's input to integer.
in your code month2 = month1 + 1 , month1 is String and you are adding an integer value which is throwing you an error
In Rails:
If you are using rails then there is Time#change inbuilt method is available, you can change any parameter with any of these keys: :year, :month, :day, :hour, :min, :sec, :usec :nsec of time object
for eg:
> Time.new(2017, 8, 1)
#=> 2017-08-01 00:00:00 +0530
> Time.new(2017, 8, 1).change(month: 7)
#=> 2017-07-01 00:00:00 +0530

How about this instead since your question clearly disregards the time portion of the object any way and your solution will have to handle cases in which the value is not between 1 and 11 otherwise an error will be raised for the month being out of range:
require 'date'
loop do
puts "Enter the first month:"
#first_month = gets.to_i
if (1..12).include?(#first_month)
break
else
puts "Incorrect value a month must be between 1 and 12."
puts "Please Try Again."
end
end
t = Date.new(2017,#first_month,1)
n = t >> 1
This uses Date#>> which simply returns a new Date object incremented by n month(s). For example
d = Date.new(2017,11,1)
d >> 1
#=> #<Date: 2017-12-01 ((2458089j,0s,0n),+0s,2299161j)>
d >> 2
#=> #<Date: 2018-01-01 ((2458120j,0s,0n),+0s,2299161j)>
# It can accept negative numbers too
# Although this is usually represented as d << 10
d >> -10
#=> #<Date: 2017-01-01 ((2457755j,0s,0n),+0s,2299161j)>
d >> -10 == d << 10
#=> true

The only thing i need to do, is to convert the string i read in, to an integer:
month1 = gets.to_i
Then my code runs very well.

Related

Find the smallest 4-digit number that decreases by 27 when you move its last digit to the first position. (Use the find or detect method) in Ruby

I'm trying to run standard ruby training programs, but I had a problem with this program, please take a look. Thank you very much!
Code:
q = 9999 #last 4-digit number
while q > 1000 #from 9999 to 1000, for exemple, the cycle has arrived to 6784
d = q.to_s.chars.map(&:to_i) #transform 6784 to array [6, 7, 8, 4]
p = d # create sample array with [6, 7, 8, 4]
tmp = p[0]; # tmp = 6;
p[0] = p[3]; # 6 = 4;
p[3] = tmp; # 4 = 6
g = p.join.to_i # transform [4, 7, 8, 6] to 4786
f = q - g # 6784 - 4786
if f == 27 # i need to find the smallest 4-digit number that decreases by 27 when moving its last digit to the first position
puts q #print 4-digit number that decreases by 27 when moving its last digit to the first position
end
q = q - 1;
end
But the result does not appear, it is because it is not, or somewhere a mistake.
In general, the condition of the task is:
Find the smallest 4-digit number that decreases by 27 when you move its last digit to the first position. (Use the find or detect method). Thank You!
I will first create a helper method to convert an array of digits to an integer.
def digits_to_int(arr)
arr.reduce { |n,d| n*10 + d }
end
For example,
digits_to_int [1,2,3,4]
#=> 1234
This tends to be faster than arr.join.to_i (see sawa's answer here).
We can then simply compute
(1..).find { |n| n-27 == digits_to_int(n.digits.rotate.reverse) }
#=> 30
See Enumerable#reduce (a.k.a. inject), "Endless range", Integer#digits, Array#rotate and Array#reverse.
Here is an example calculation.
n = 243
a = n.digits
#=> [3,4,2]
b = a.rotate
#=> [4,2,3]
c = b.reverse
#=> [3,2,4]
d = digits_to_int(c)
#=> 324
n - 27 == d
#=> 243 - 27 == 324 => false
and another
n = 30
a = n.digits
#=> [0,3]
b = a.rotate
#=> [3,0]
c = b.reverse
#=> [0,3]
d = digits_to_int(c)
#=> 3
n - 27 == d
#=> 30 - 27 == 3 => true
I would define a method to "rotate" the number using string manipulation.
def rotate_number_one_digit(n)
s = n.to_s
"#{s[-1]}#{s[0..-2]}".to_i
end
Then I would use #upto to deal with the iteration.
1000.upto(9999) do |x|
end
Each time around you'll check that the "rotated" number plus 27 equals x. If so, print it and break the loop to prevent further unnecessary iteration.
1000.upto(9999) do |x|
if rotate_number_one_digit(x) + 27 == x then
puts x
break
end
end
Or we can just use the #find method from Enumerable.
1000.upto(9999).find { |x| rotate_number_one_digit(x) + 27 == x }
Or using break to return a value from the loop.
1000.upto(9999) { |x|
break x if rotate_number_one_digit(x) + 27 == x
}

How to multiply integer digits between them?

I want to my n to multiply with next number for example if n=99 i want it to 9*9 and then return a result, and then i want the result (9*9 = 81 then 8*1 = 8) to multiply until it becomes 1 digit.
Here's my code:
def persistence(n)
if n <= 9
puts n
else
n.to_s.each_char do |a|
a.to_i * a.to_i unless n < 9
puts a.to_i
end
end
end
and i want it to return this:
persistence(39) # returns 3, because 3*9=27, 2*7=14, 1*4=4
# and 4 has only one digit
persistence(999) # returns 4, because 9*9*9=729, 7*2*9=126,
# 1*2*6=12, and finally 1*2=2
persistence(4) # returns 0, because 4 is already a one-digit number
def persistence(n)
i = 0
while n.to_s.length != 1
n = n.to_s.each_char.map(&:to_i).reduce(:*)
i +=1
end
i
end
persistence(39) #=> 3
persistence(999) #=> 4
Other version:
def p(n, acc)
return acc if n <= 9
p(n.to_s.each_char.map(&:to_i).reduce(:*), acc+1)
end
def persistence(n)
p(n, 0)
end
I will leave the breaking down of method and understanding what's happening and what is the difference b/w two variations to you. Will love to see your comment explaining it.
def persistence(n)
0.step.each do |i|
break i if n < 10
n = n.digits.reduce(:*)
end
end
persistence 4 #=> 0
persistence 39 #=> 3
persistence 999 #=> 4
persistence 123456789123456789 #=> 2
Regarding the last result, note that 2*5*2*5 #=> 100.

How do I compare month-year combinations in Ruby?

I’m using Rails 4.2.7. I have two pairs of numbers …
month1 # A number between 1 and 12
year1 # a four digit year
month2 # A number between 1 and 12
year2 # A four digit year
How do I write a comparison expression to determine if the “month2-year2” combination is greater than or equal to the “month1-year1” combination? For instance if month2 = 1 and year2 = 2017 and month1 = 12 and year1 = 2016, the month2-year2 combination is greater than the month1-year1 combination.
month1, month2, year1, year2 = 12, 1, 2016, 2017
=> [12, 1, 2016, 2017]
Time.new(year1, month1) >= Time.new(year2, month2)
=> false
Time.new(year2, month2) >= Time.new(year1, month1)
=> true
reference: https://ruby-doc.org/core-2.2.0/Time.html#class-Time-label-Creating+a+new+Time+instance
It's pretty easy and there's no need to create date or time objects.
def first_smaller?(ym1, ym2)
(ym1 <=> ym2) == -1
end
first_smaller? [2016,12], [2017,1]
#=> true
first_smaller? [2017,1], [2016,12]
#=> false
first_smaller? [2017,1], [2017,1]
#=> false
See the third paragraph of the doc for Array#<=> to see how Ruby orders arrays.
If you also wish to know if the two arrays are equal, you could write something like the following:
def ordering(ym1, ym2)
case ym1 <=> ym2
end
which returns -1 if ym1 is smaller, +1 if ym2 is smaller or 0 if the arrays are equal.

Is it better way to do that?

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

Ruby equivalent to JavaScript operator `||`

How can this be achieved in Ruby? Can it be done without repeating the variable?
Javascript:
b = a || 7
This assigns a if a is not 0 and 7 otherwise
One specific case is converting date.wday to 7 if it returns 0 (Sunday).
Just out of curiosity:
class Object
def javascript_or?(other)
(is_a?(FalseClass) || nil? || '' == self || 0 == self) ? nil : self
end
end
and:
a = b.javascript_or?(7)
There are only two falsy values in Ruby: nil and false. So, if you really want this approach
a = b == 0 ? 7 : b
is a plausible solution, because 0 can't be evaluated as false.
However, a better option for your need is cwday, and not wday. Then you don't need to make this comparison anymore, because it returns 1 for Monday, 2 for Tuesday, and finally 7 for Sunday, as you need.
date = Date.new(2016,19,6) # => Jun 19 2016, Sunday
date.cwday # => 7
For the particular case of 0 and 7:
a = (b + 6) % 7 + 1
:)
You can use ternary operator:
date.wday == 0 ? 7 : date.wday
What you're describing here is less of a logical problem and more of a mapping one:
WEEKDAY_MAP = Hash.new { |h,k| h[k] = k < 7 ? k : nil }.merge(0 => 7)
This one re-writes 1..6 to be the same, but 0 becomes 7. All other values are nil.
Then you can use this to re-write your day indicies:
b = WEEKDAY_MAP[a]
If at some point you want to tinker with the logic some more, that's also possible.

Resources