Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2024/25/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Slicing

Slicing in D is very similar to Python. However, they are not completely identical.

Similarities

(1) similarities
def main():
    numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    s = "0123456789"

    print(numbers[0:2])  # [0, 1]
    print(s[0:2])  # 01

    print(numbers[7:])  # [7, 8, 9]
    print(s[7:])  # 789

    print(numbers[-2:])  # [8, 9]
    print(s[-2:])  # 89
import std.stdio;

void main()
{
    int[] numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    string s = "0123456789";

    writeln(numbers[0 .. 2]); // [0, 1]
    writeln(s[0 .. 2]); // 01

    writeln(numbers[7 .. $]); // [7, 8, 9]
    writeln(s[7 .. $]); // 789

    writeln(numbers[$ - 2 .. $]); // [8, 9]
    writeln(s[$ - 2 .. $]); // 89
}

Differences

(2) differences
def main():
    numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    s = "0123456789"

    print(numbers[2:0])  # []
    print(s[2:0])  # "" (empty string)

    print(numbers[8:100])  # [8, 9]
    print(s[8:100])  # "89"
import std.stdio;

void main()
{
    int[] numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    string s = "0123456789";

    // if start index > end index => error
    // writeln(numbers[2 .. 0]); // error
    // writeln(s[2 .. 0]); // error

    // if end index > length => error
    // writeln(numbers[8 .. 100]); // error
    // writeln(s[8 .. 100]); // error
}

Solution

We can have a Python-like behaviour with the introduction of a safety function.

(3) solution
def main():
    numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    s = "0123456789"

    print(numbers[2:0])  # []
    print(s[2:0])  # "" (empty string)

    print(numbers[8:100])  # [8, 9]
    print(s[8:100])  # "89"

    # normal call
    print(numbers[2:5])  # [2, 3, 4]
    print(s[2:5])  # "234"
import std.stdio;

T[] safeSlice(T)(T[] arr, size_t start, size_t end)
{
    if (start >= arr.length || start > end)
        return []; // empty slice
    // else:
    return (end <= arr.length) ? arr[start .. end] : arr[start .. $];
}

void main()
{
    int[] numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    string s = "0123456789";

    writeln(safeSlice(numbers, 2, 0)); // []
    writeln(safeSlice(s, 2, 0)); // "" (empty string)

    writeln(safeSlice(numbers, 8, 100)); // [8, 9]
    writeln(safeSlice(s, 8, 100)); // "89"

    // normal call
    writeln(safeSlice(numbers, 2, 5)); // [2, 3, 4]
    writeln(safeSlice(s, 2, 5)); // "234"
}
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 June 30, 11:19