Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rings
#1
i was thinking about the concept of a new container type. this is not for any particular need i have so i have no need to implement this. the name "ring" seems to make sense. maybe someone has a better name. a ring is very much like a list.

does anyone think this might have some uses. it seems to me like practice implementing a container.

a ring is a list with the end connected back to its own beginning. indexing is modulo the len() of the ring. a ring of 8 items can be indexed at [90] and access the very same position as [2] or [-6]. as in a list, index [-1] is the last item in a ring. a ring can be empty. trying to index an empty ring raises IndexError. indexing beyond the size of the ring indexes % len(ring)

ring.pop(pos) removes and returns one item from the ring, making the ring smaller by one. pop defaults to -1.

ring.insert(item,pos) inserts item where it comes to be after position pos making the ring one larger. pos defaults to -1 which inserts between the last and first like append() for a list.

ring.insertbefore(item,pos) might be implemented.

ring.rotateforward(count) shifts items forward in the right with items in the end (at [-1]) moving into the beginning (at [0]). this is repeated count times. a negative count rotates in reverse.

ring.reverse() flips a ring over, so to speak. all items will now be in reverse order.

ring() creates an new empty ring.

ring(ring) creates a duplicate ring with the same items and references.

ring(list/tuple) creates a ring with all items and references in the same order.

ring(a,b,c,d) is like ring((a,b,c,d))

to create a ring with one container do ring((container)) or do ring([container]).

a ring used as an iterator starts at position 0 and never ends.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
rings - by Skaperen - Oct-15-2020, 03:26 PM
RE: rings - by Gribouillis - Oct-15-2020, 07:43 PM
RE: rings - by buran - Oct-15-2020, 08:18 PM
RE: rings - by ndc85430 - Oct-16-2020, 02:35 PM
RE: rings - by Skaperen - Oct-16-2020, 06:55 PM
RE: rings - by metulburr - Oct-17-2020, 11:23 AM
RE: rings - by Gribouillis - Oct-17-2020, 11:47 AM
RE: rings - by buran - Oct-17-2020, 02:58 PM
RE: rings - by Gribouillis - Oct-17-2020, 03:05 PM
RE: rings - by buran - Oct-17-2020, 03:12 PM
RE: rings - by Gribouillis - Oct-17-2020, 03:25 PM
RE: rings - by buran - Oct-17-2020, 03:45 PM
RE: rings - by Gribouillis - Oct-17-2020, 04:10 PM
RE: rings - by Skaperen - Oct-19-2020, 08:37 PM
RE: rings - by Skaperen - Oct-19-2020, 08:39 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020