logo
Tags down

shadow

Is using [::-1] in python to reverse a list O(1) space?


By : Yoco
Date : July 29 2020, 11:00 AM
I wish this help you You are right that some_list[::-1] creates a new list, and that that list will have n "slots", and thus need O(n) memory.
Furthermore in CPython [GitHub], an interpreter of Python, the .reverse() is done in O(1) memory. Indeed, if we look at the reverse method [GitHub], we see:
code :
/*[clinic input]
list.reverse
Reverse *IN PLACE*.
[clinic start generated code]*/

static PyObject *
list_reverse_impl(PyListObject *self)
/*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
{
    if (Py_SIZE(self) > 1)
        reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
    Py_RETURN_NONE;
}
/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
static void
reverse_slice(PyObject **lo, PyObject **hi)
{
    assert(lo && hi);

    --hi;
    while (lo < hi) {
        PyObject *t = *lo;
        *lo = *hi;
        *hi = t;
        ++lo;
        --hi;
    }
}


Share : facebook icon twitter icon

how to reverse a list with O(1) space and O(n) time?


By : MartinTilma
Date : March 29 2020, 07:55 AM
this one helps. Just read one of the following. It is the thing you're talking about.
Please note that we're talking about singly 'linked' lists.

Reverse a linked list using constant amount of addtional space


By : Alex Zhao
Date : March 29 2020, 07:55 AM
Hope this helps since you're creating new instance of linked list - you will have 2 copies of list in memory. The list will contain references to objects, so if you want to count instances of objects - then you are safe and amount of additional memory is O(1). But list itself takes some memory, so if you count number of "cells" in lists - then yes, you have O(N) additional memory being used.

Python Sorting list of tuples reverse by value and non-reverse by key (two different ways)


By : youhunt
Date : March 29 2020, 07:55 AM
may help you . The title is rather confusing but the question is rather straightforward. , In this specific case the problem is easy...
code :
L.sort(key = lambda x : (-x[1], x[0]))

Python 2.7 reverse a text by space character


By : Faiz
Date : March 29 2020, 07:55 AM
Does that help I'm assuming your newline character is \n and not /n as you've posted.

reverse print an immutable linked list with less than O(n) space


By : user5580423
Date : March 29 2020, 07:55 AM
hope this fix your issue This is an O(n) time and O(sqrt(n)) space algorithm. In the second part of the post it will be extended to a linear time and O(n^(1/t)) space algorithm for an arbitrary positive integer t.
High-level idea: Split the list into sqrt(n) many (almost) equal-sized parts. Print the parts one after the other in reverse order using a naive linear-time, linear-space method, from the last to the first.
Related Posts Related Posts :
  • Adding quotations around words preceding a colon in Python
  • Write a class that draws an image in the center of the screen in PyGame
  • Python requirements conflict with PyPi
  • A good way to make classes for more complex playing card types than those found in a standard deck?
  • How to find the longest chain of consecutively recurring character groupings in file
  • Class variable that is an instance of itself
  • SQLAlchemy(Python)/Sequelize(Node.js) rollback transation (undo button)
  • Word2Vec compare vectors from different models with different sizes
  • Pandas: Create a table with a “dummy variable” of another table
  • How to implement Python's multiprocessing Pool to convert thousands of files
  • Remove elements from lists, code is leaving just one
  • Ensure python script isn't run as root
  • How do I sum 2 specific column rows in a DataFrame if some of the values are NaN?
  • Mocking __init__ of a class imported in file
  • How to check a greater than check in reg expression?
  • How to make the progressbar work, It freezes the window
  • How to update the state of a Toggle Button after process completion?
  • How to specify the outer product of certain axis in python?
  • multiprocessing creates zombie process even after .join()
  • Why does the robot framework add quotation marks to a command string?
  • How to extract the orderconfirmedoid from from the current url using Selenium and Python
  • How to find a sum of even numbers in range recursively?
  • How to convert list of pixel into image using python
  • How can I bind the enter key to a command while having a button do the same thing?
  • python: subsetting and renaming columns by name in list of dataframes
  • print x over y in matplotlib python
  • Python Image Processing on Captcha how to remove noise
  • Pass value to string imported from another module using f-string
  • Greatest of 3 numbers code not working for specific numbers
  • Use of function inside a for loop
  • Cant match a particular pattern in regex using OR
  • when i build the docker with pip install psycopg2 has error
  • 'import ase' does not load submodule 'ase.io'
  • I have two tuple comparison. Almost the same data. Why this error:
  • Why hasn't this list changed?
  • Scrape html data using beautifulsoup and Python
  • JupyterLab - python open() function results in FileNotFoundError
  • I'm not getting proper output for checking a string to be palindrome or not
  • alphabet split instead of word split using lists
  • Celsius to Fahrenheit method doesn't work
  • How to import a module at each execution of python?
  • find contours of thresholded image
  • How to use sleep to limit data put in logs?
  • Dataframe expanding window and applying multiple functions
  • How to get integer list from a string but not integer list?
  • Rename dataframe in Python for loop
  • Unable to fetch rows from PostGresSQL table
  • How to pass values to a function which is a list item in Python
  • Easiest way to subclass a widget in Python for use with Qt Designer
  • Finding the cumulative sum of a list
  • Pandas Dataframe Update Rows
  • How to solve this loop problem in python flask?
  • Python Opencv: Filter Image for Text Detection
  • Python - Count Numbers of Different Digits in String
  • How to update, rotate and display a ply file with python?
  • Grouping by and filtering for column value containing string and aggregate function?
  • pandas group by day or week or month for timestamp
  • Django converting year to a full date
  • Clunky/slow dragging functionality on my analog clock project
  • Setting the labels of colorbar matplotlib
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org