Post Reply 
list slicing question
11-23-2022, 02:55 PM
Post: #1
list slicing question
I wanted to translate XCas code to HP Prime Cas, and have problem translate a[::n]

XCas> range(7)[::2]      → [0, 2, 4, 6]

Here, XCas behave like Python:

>>> range(7)[::2]
[0, 2, 4, 6]
>>> range(7)[0:None:2] # explicitly showing defaults
[0, 2, 4, 6]

HP Prime Cas, however, is 1 based. (list last index has unintuitive -1 + 1 = 0)

CAS> range(7)[::2] --> range(7)[1:0:2] --> [0,2,4]

Is there an equivalent to Python's None ?
Find all posts by this user
Quote this message in a reply
11-24-2022, 12:04 AM
Post: #2
RE: list slicing question
By trial and error, this seems to work, but weird, with 1 = beyond end-of-list

CAS> range(7)[1:1:2]      → [0,2,4,6]
CAS> range(7)[2:1:2]      → [1,3,5]
CAS> range(7)[3:1:2]      → [2,4,6]
Find all posts by this user
Quote this message in a reply
11-24-2022, 11:32 AM
Post: #3
RE: list slicing question
(11-24-2022 12:04 AM)Albert Chan Wrote:  By trial and error, this seems to work, but weird, with 1 = beyond end-of-list

At least it is consistently weird, for 0-based XCas, 0 = beyond end-of list

XCas> range(7)[0:0:2]      → [0,2,4,6]
XCas> range(7)[1:0:2]      → [1,3,5]
XCas> range(7)[2:0:2]      → [2,4,6]
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)