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 8445155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:35:20+00:00 2026-06-10T09:35:20+00:00

I have the following definition for a function named intervals . From the print

  • 0

I have the following definition for a function named intervals.
From the print statement at the bottom I get this results (which are half-hour time intervals like 9:00, 9:30, etc expressed as lists in lists, I believe).

[[9, 0], [10, 30], [10, 30], [10, 30]]

I would like the following result.

[[9, 0], [ 9, 30], [10, 0], [10, 30]]

The complete log of the run is as follows. Notice that the list list is only updated once in the while loop between the print "b(efore)" and the print "a(fter)" statements and yet list changes after the after and before the next before. How can that happen?

[9, 30] [[9, 0]] b
[9, 30] [[9, 0], [9, 30]] a
[10, 0] [[9, 0], [10, 0]] b
[10, 0] [[9, 0], [10, 0], [10, 0]] a
[10, 30] [[9, 0], [10, 30], [10, 30]] b
[10, 30] [[9, 0], [10, 30], [10, 30], [10, 30]] a
[[9, 0], [10, 30], [10, 30], [10, 30]]

def intervals(start,end):
    if start>=end:
        return []
    else:
        if 0<=start[1]<30:
            start[1]=0
        else:
            start[1]=30
        list = []
        list.append(start)
        last = start
        new=[0,0]
        while end>last:
            if 30==last[1]:
                new[0]=1+last[0]
                new[1]=0
            else:
                new[0]=last[0]
                new[1]=30
            last=new
            print new,list,"b"
            list.append(new)
            print new,list,"a"
        return list

print intervals([9,0],[10,30])

Can anyone fix it?

  • 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-10T09:35:21+00:00Added an answer on June 10, 2026 at 9:35 am

    The reason you see this is that on each iteration of the while loop you append the list new to the end of your list list (which you should really rename, since it masks the built-in list type).

    Just move the new=[0,0] line so that it is the first line in the while loop, this will create a new list on each iteration so that your final list won’t have several references to the same list:

            ...
            while end>last:
                new=[0,0]
                ...
    

    Note that if you plan on adding any more complexity to this (for example different intervals than 30 minutes), I would really suggest using the datetime module, datetime.timedelta(minutes=30) would probably be useful, for example:

    from datetime import datetime, timedelta
    
    def intervals(start, end):
        temp = datetime.min + timedelta(hours=start[0], minutes=start[1])
        last = datetime.min + timedelta(hours=end[0], minutes=end[1])
        interval = timedelta(minutes=30)
        result = []
        while temp <= last:
            result.append([temp.hour, temp.minute])
            temp += interval
        return result
    
    >>> intervals([9, 30], [12, 30])
    [[9, 30], [10, 0], [10, 30], [11, 0], [11, 30], [12, 0], [12, 30]]
    >>> intervals([9, 45], [12, 15])
    [[9, 45], [10, 15], [10, 45], [11, 15], [11, 45], [12, 15]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following C++ function definition, which I am trying to call through
I have a function with the following definition: virtual bool Process(wtdaFileHandler &daHandler, wtdaGather &daGather);
I have a function definition of the following form (within a large code): class
I have following preprocessor (code) block in my project before each function definition to
I have the following definition for p: .contact p { margin: 5px 0 0
I have the following record definition E3Vector3T = packed record public x: E3FloatT; y:
I have the following class definition: template<typename QueueItemT> class QueueBC { protected: QueueBC() {}
I have the following class definition: public interface IItem{} public class FirstObject<T, U> :
I have the following property definition: public static class Extensions { public static readonly
I have the following view definition in my asp.net mvc website: <% Using Ajax.BeginForm(UsrCtlChangePassword,

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.