Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:44:28+00:00 2026-06-11T16:44:28+00:00

I am working on a problem I found on the internet: The human body

  • 0

I am working on a problem I found on the internet:

The human body goes through 90 minute sleep cycles during the night, and you feel more refreshed if you wake up at the end of a sleep cycle than if you wake up during a sleep cycle. The challenge is to make a program that takes a wake-up time and outputs the possible times to fall asleep so that you will wake up at the end of a sleep cycle.*

My approach to this problem is to subtract 90m from the time five times after parsing it to DateTime and storing it into an array. However, I’m new to Rails and am not quite sure how to do this. How do I display my time in the original format?

Here is what I have so far:

require 'date'

def sleep_time time
  a = []

  5.times do |i|
    multiple = i * 90 * 60
    a << time - multiple
  end

  puts a
end

puts "Enter wake-up time: "
time = DateTime.strptime(gets, '%I:%M %p').to_time

puts time
sleep_time(time)

Edit: I figured out how to subtract 90m (using seconds).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:44:29+00:00Added an answer on June 11, 2026 at 4:44 pm

    Time supports strftime, which takes a format string that is consistent with strptime. Use:

    time.strftime('%I:%M %p')
    

    For instance:

    Time.now.strftime('%I:%M %p')
    => "01:29 PM"
    

    Also, don’t parse it into a DateTime object then convert to a Time object using to_time. Instead parse directly into a Time object. Time supports strptime also:

    require 'time'
    Time.strptime('01:29 PM', '%I:%M %p')
    => 2012-09-20 13:29:00 -0700
    

    If you want to parse from a given format, then output in that format again, define a constant and use it in the strptime and strftime methods:

    TIME_FORMAT = '%I:%M %p'
    Time.strptime('01:29 PM', TIME_FORMAT)
    time.strftime(TIME_FORMAT)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'am currently working on GWTs Activity-Place implementation. Now I have found problem with the
I'm working on a problem that uses pointer arithmetic and I have found this
I'm currently working a problem that requires my web application to generate a chart
I'm working on problem four of Project Euler and am running into a stackoverflow
I had a problem working with the image classes in java. I am creating
I am currently working on Problem 62 I have tried the following code to
I'm working on problem 9 in Project Euler: There exists exactly one Pythagorean triplet
I have a strange problem working with HTML,CSS in different browsers: Firefox 3.6 and
Problem When working with MasterPages, a common irritation I run into is that script
I am working on a problem and got stuck at a wall I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.