Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

r""" 

Datatypes for finite words 

""" 

#***************************************************************************** 

# Copyright (C) 2009 Franco Saliola <saliola@gmail.com> 

# Vincent Delecroix <20100.delecroix@gmail.com> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import print_function, absolute_import 

  

from cpython.object cimport Py_EQ, Py_NE 

from itertools import islice 

  

  

cdef class WordDatatype(object): 

r""" 

The generic WordDatatype class. 

  

Any word datatype must contain two attributes (at least):: 

  

- _parent 

- _hash 

  

They are automatically defined here and it's not necessary (and forbidden) 

to define them anywhere else. 

  

TESTS:: 

  

sage: w = Word([0,1,1,0,0,1]) 

sage: isinstance(w, sage.combinat.words.word_datatypes.WordDatatype) 

True 

  

""" 

def __reduce__(self): 

r""" 

Default pickle support 

  

TESTS:: 

  

sage: w = Word([0,1,1,0,0,1]) 

sage: w.__reduce__() 

(Finite words over Set of Python objects of class 'object', ([0, 1, 1, 0, 0, 1],)) 

""" 

return self._parent, (list(self),) 

  

def __hash__(self): 

r""" 

Returns the hash for this word. 

  

TESTS:: 

  

sage: h = hash(Word('abc')) # indirect test 

sage: Word('abc').__hash__() == Word('abc').__hash__() 

True 

  

sage: tm = words.ThueMorseWord() 

sage: hash(tm) 

-973965563 

""" 

cdef int res 

if self._hash is None: 

res = 5381 

for s in islice(self,1024): 

res = ((res << 5) + res) + hash(s) 

self._hash = res 

return self._hash 

  

  

cdef class WordDatatype_list(WordDatatype): 

r""" 

Datatype class for words defined by lists. 

""" 

cdef public list _data 

  

def __init__(self, parent=None, data=None): 

r""" 

Construct a word with a given parent. 

  

.. NOTE:: 

  

It is slower than WordDatatype_str and WordDatatype_tuple. 

  

INPUT: 

  

- ``parent`` - an instance of :class:`Words_all` 

- ``data`` - an iterable 

  

EXAMPLES:: 

  

sage: w = Word([0,1,1,0]) 

sage: isinstance(w, sage.combinat.words.word_datatypes.WordDatatype_list) 

True 

  

""" 

self._parent = parent 

if isinstance(data,list): 

self._data = data 

else: 

self._data = list(data) 

self._hash = None 

  

def __contains__(self, a): 

r""" 

Test whether ``a`` is a letter of ``self``. 

  

INPUT: 

  

- ``a`` - anything 

  

OUTPUT: 

  

- boolean 

  

EXAMPLES:: 

  

sage: w = Word([0,1,1,0]) 

sage: 0 in w 

True 

sage: 3 in w 

False 

  

""" 

return a in self._data 

  

def __iter__(self): 

r""" 

Return an iterator that iterates through the letters of self. 

  

EXAMPLES:: 

  

sage: w = Word([0,1,1,0]) 

sage: list(iter(w)) 

[0, 1, 1, 0] 

  

""" 

return iter(self._data) 

  

def __richcmp__(self, other, int op): 

r""" 

Equality test for self and other if other is an instance of 

WordDatype_list. 

  

INPUT: 

  

- ``other`` - a word 

- ``op`` - integer: 0, 1, 2, 3, 4 or 5 

  

OUTPUT: 

  

boolean or NotImplemented 

  

EXAMPLES:: 

  

sage: w = Word(range(10)) 

sage: w == w 

True 

sage: z = Word(range(20)) 

sage: w == z 

False 

sage: z == w 

False 

  

It works even if the parents are not the same:: 

  

sage: Words([0,1])([0,1,1]) == Words([0,1,2])([0,1,1]) 

True 

  

REFERENCES: 

  

http://docs.cython.org/docs/special_methods.html 

""" 

if isinstance(other, WordDatatype_list): 

if op == Py_EQ: 

return self._data == other._data 

elif op == Py_NE: 

return self._data != other._data 

  

# Otherwise, force FiniteWord_class.__richcmp__ to do it 

from sage.combinat.words.word import FiniteWord_class 

return FiniteWord_class.__richcmp__(self, other, op) 

  

def __len__(self): 

r""" 

Return the length of the word. 

  

.. NOTE:: 

  

This function will be deprecated in a future version 

of Sage. Use ``self.length()`` instead. 

  

EXAMPLES:: 

  

sage: w = Word([0,1,1,0]) 

sage: len(w) 

4 

  

""" 

return len(self._data) 

  

def length(self): 

r""" 

Return the length of the word. 

  

EXAMPLES:: 

  

sage: w = Word([0,1,1,0]) 

sage: w.length() 

4 

  

""" 

return len(self._data) 

  

def __getitem__(self, key): 

r""" 

Implements :meth:`__getitem__` for words stored as lists. 

  

INPUT: 

  

- ``key`` - integer 

  

EXAMPLES:: 

  

sage: w = Word(range(100)) 

sage: w[4] 

4 

sage: w[-1] 

99 

sage: w[3:10:2] 

word: 3579 

  

""" 

if isinstance(key, slice): 

return self._parent(self._data[key]) 

else: 

return self._data[key] 

  

def __mul__(self, other): 

r""" 

Return the concatenation of self and other. 

  

INPUT: 

  

- ``other`` - word represented by a list 

  

OUTPUT: 

  

word 

  

EXAMPLES:: 

  

sage: w = Word(list(range(10))) 

sage: w * w 

word: 01234567890123456789 

  

The type of the concatenation is preserved:: 

  

sage: type(w) 

<class 'sage.combinat.words.word.FiniteWord_list'> 

sage: type(w * w) 

<class 'sage.combinat.words.word.FiniteWord_list'> 

""" 

if isinstance(other, WordDatatype_list): 

return self._parent(self._data + other._data) 

else: 

return super(WordDatatype_list, self).__mul__(other) 

  

__add__ = __mul__ 

  

def count(self, a): 

r""" 

Returns the number of occurrences of the letter ``a`` in the word 

``self``. 

  

INPUT: 

  

- ``a`` - a letter 

  

OUTPUT: 

  

- integer 

  

EXAMPLES:: 

  

sage: w = Word([0,1,1,0,1]) 

sage: w.count(0) 

2 

sage: w.count(1) 

3 

sage: w.count(2) 

0 

  

""" 

return self._data.count(a) 

  

cdef class WordDatatype_str(WordDatatype): 

""" 

Datatype for words defined by strings. 

""" 

cdef public str _data 

  

# TODO : allow initialization from non string data 

def __init__(self, parent=None, data=None): 

r""" 

Construct a word with parent ``parent`` from the string ``data``. 

  

INPUT: 

  

- ``parent`` - instance of :class:`Words_all` 

- ``data`` - string 

  

EXAMPLES:: 

  

sage: w = Word("abba") 

sage: isinstance(w, sage.combinat.words.word_datatypes.WordDatatype_str) 

True 

  

""" 

self._parent = parent 

if isinstance(data, str): 

self._data = data 

else: 

self._data = "".join(str(u) for u in data) 

self._hash = None 

  

def __iter__(self): 

r""" 

Return an iterator that iterates through the letters of ``self``. 

  

EXAMPLES:: 

  

sage: w = Word('abba') 

sage: list(iter(w)) 

['a', 'b', 'b', 'a'] 

  

""" 

return iter(self._data) 

  

def __richcmp__(self, other, int op): 

r""" 

Equality test for self and other if other is an instance of 

WordDatype_str. 

  

INPUT: 

  

- ``other`` - a word 

- ``op`` - integer: 0, 1, 2, 3, 4 or 5 

  

OUTPUT: 

  

boolean or NotImplemented 

  

EXAMPLES:: 

  

sage: w = Word('abcde') 

sage: w == w 

True 

sage: z = Word('epoisudfafgh') 

sage: w == z 

False 

sage: z == w 

False 

  

It works even if the parents are not the same:: 

  

sage: Words('ab')('ababa') == Words('abcd')('ababa') 

True 

sage: Words('ab')('ababa') == Word('ababa') 

True 

  

REFERENCES: 

  

http://docs.cython.org/docs/special_methods.html 

""" 

if isinstance(other, WordDatatype_str): 

if op == Py_EQ: 

return self._data == other._data 

elif op == Py_NE: 

return self._data != other._data 

  

# Otherwise, force FiniteWord_class.__richcmp__ to do it 

from sage.combinat.words.word import FiniteWord_class 

return FiniteWord_class.__richcmp__(self, other, op) 

  

def __contains__(self, a): 

r""" 

Test whether ``a`` is a letter of ``self``. 

  

INPUT: 

  

- ``a`` - anything 

  

EXAMPLES:: 

  

sage: w = Word('abba') 

sage: 'a' in w 

True 

sage: 'c' in w 

False 

  

""" 

# we need to override the non standard comportement of 

# the comportment of the __contains__ of python str 

if not isinstance(a, str): 

return False 

if len(a) != 1: 

return False 

else: 

return a in self._data 

  

cpdef _has_factor_naive(self, w): 

r""" 

A naive test for testing whether the word contains ``w`` as a factor. 

  

.. NOTE:: 

  

This just wraps Python's builtin :meth:`__contains__` for :class:`str`. 

  

INPUT: 

  

- ``w`` - a word, or something that behaves like one (list, tuple, str, ...) 

  

OUTPUT: 

  

- boolean 

  

EXAMPLES:: 

  

sage: w = Word('abba') 

sage: w._has_factor_naive('ba') 

True 

sage: w._has_factor_naive('bab') 

False 

  

""" 

if isinstance(w, WordDatatype_str): 

return w._data in self._data 

elif isinstance(w, str): 

return w in self._data 

raise ValueError 

  

cpdef find(self, sub, start=0, end=None): 

r""" 

Returns the index of the first occurrence of sub in self, 

such that sub is contained within self[start:end]. 

Returns -1 on failure. 

  

INPUT: 

  

- ``sub`` - string or word to search for. 

- ``start`` - non negative integer (default: 0) specifying 

the position from which to start the search. 

- ``end`` - non negative integer (default: None) specifying 

the position at which the search must stop. If None, then 

the search is performed up to the end of the string. 

  

OUTPUT: 

  

non negative integer or -1 

  

EXAMPLES:: 

  

sage: w = Word("abbabaabababa") 

sage: w.find("a") 

0 

sage: w.find("a", 4) 

5 

sage: w.find("a", 4, 5) 

-1 

""" 

if end is None: 

end = len(self._data) 

if isinstance(sub, WordDatatype_str): 

return self._data.find(sub._data, start, end) 

elif isinstance(sub, str): 

return self._data.find(sub, start, end) 

else: 

return super(WordDatatype_str, self).find(sub, start, end) 

  

def rfind(self, sub, start=0, end=None): 

r""" 

Returns the index of the last occurrence of sub in self, 

such that sub is contained within self[start:end]. 

Returns -1 on failure. 

  

INPUT: 

  

- ``sub`` - string or word to search for. 

- ``start`` - non negative integer (default: 0) specifying 

the position at which the search must stop. 

- ``end`` - non negative integer (default: None) specifying 

the position from which to start the search. If None, then 

the search is performed up to the end of the string. 

  

OUTPUT: 

  

non negative integer or -1 

  

EXAMPLES:: 

  

sage: w = Word("abbabaabababa") 

sage: w.rfind("a") 

12 

sage: w.rfind("a", 4, 8) 

6 

sage: w.rfind("a", 4, 5) 

-1 

""" 

if end is None: 

end = len(self._data) 

if isinstance(sub, WordDatatype_str): 

return self._data.rfind(sub._data, start, end) 

elif isinstance(sub, str): 

return self._data.rfind(sub, start, end) 

else: 

return super(WordDatatype_str, self).rfind(sub, start, end) 

  

def __len__(self): 

r""" 

Return the length of the word. 

  

.. NOTE:: 

  

This function will be deprecated in a future version 

of Sage. Use ``self.length()`` instead. 

  

EXAMPLES:: 

  

sage: w = Word("abbabaabababa") 

sage: len(w) 

13 

  

""" 

return len(self._data) 

  

def length(self): 

r""" 

Return the length of the word. 

  

EXAMPLES:: 

  

sage: w = Word("abbabaabababa") 

sage: w.length() 

13 

  

""" 

return len(self._data) 

  

def __getitem__(self, key): 

r""" 

Implements the :meth:`__getitem__`. 

  

TESTS:: 

  

sage: alphabet = [chr(i) for i in range(97, 123)] 

sage: w = Word(alphabet) 

sage: w[4] 

'e' 

sage: w[-1] 

'z' 

sage: w[3:10:2] 

word: dfhj 

sage: all(chr(i+97) == w[i] for i in range(w.length())) 

True 

""" 

if isinstance(key, slice): 

return self._parent(self._data[key]) 

return self._data[key] 

  

def __mul__(self, other): 

r""" 

Return the concatenation of self and other. 

  

INPUT: 

  

- ``other`` - word represented by an str 

  

OUTPUT: 

  

word 

  

EXAMPLES:: 

  

sage: w = Word('abcdef') 

sage: w * w 

word: abcdefabcdef 

  

The type of the concatenation is preserved:: 

  

sage: type(w) 

<class 'sage.combinat.words.word.FiniteWord_str'> 

sage: type(w * w) 

<class 'sage.combinat.words.word.FiniteWord_str'> 

""" 

if isinstance(other, WordDatatype_str): 

return self._parent(self._data + other._data) 

else: 

return super(WordDatatype_str, self).__mul__(other) 

  

__add__ = __mul__ 

  

def count(self, letter): 

r""" 

Count the number of occurrences of ``letter``. 

  

INPUT: 

  

- ``letter`` - a letter 

  

OUTPUT: 

  

- integer 

  

EXAMPLES:: 

  

sage: w = Word("abbabaabababa") 

sage: w.count('a') 

7 

sage: w.count('b') 

6 

sage: w.count('c') 

0 

  

""" 

return self._data.count(letter) 

  

def split(self, sep=None, maxsplit=None): 

r""" 

Returns a list of words, using sep as a delimiter string. 

If maxsplit is given, at most maxsplit splits are done. 

  

See also the partition method. 

  

.. NOTE:: 

  

This just wraps Python's builtin :meth:`str::split` for 

:class:`str`. 

  

INPUT: 

  

- ``sep`` - string or word (optional, default: None) 

  

- ``maxsplit`` - positive integer (optional, default: None) 

  

OUTPUT: 

  

- a list of words 

  

EXAMPLES: 

  

You can split along white space to find words in a sentence:: 

  

sage: w = Word("My tailor is poor") 

sage: w.split(" ") 

[word: My, word: tailor, word: is, word: poor] 

  

The python behavior is kept when no argument is given:: 

  

sage: w.split() 

[word: My, word: tailor, word: is, word: poor] 

  

You can split in two words letters to get the length of blocks in the 

other letter:: 

  

sage: w = Word("ababbabaaba") 

sage: w.split('a') 

[word: , word: b, word: bb, word: b, word: , word: b, word: ] 

sage: w.split('b') 

[word: a, word: a, word: , word: a, word: aa, word: a] 

  

You can split along words:: 

  

sage: w = Word("3230301030323212323032321") 

sage: w.split("32") 

[word: , word: 30301030, word: , word: 12, word: 30, word: , word: 1] 

  

If the separator is not a string a ValueError is raised:: 

  

sage: w = Word("le papa du papa du papa etait un petit pioupiou") 

sage: w.split(Word(['p','a','p','a'])) 

Traceback (most recent call last): 

... 

ValueError: the separator must be a string. 

""" 

if sep is None or isinstance(sep, str): 

pass 

elif isinstance(sep, WordDatatype_str): 

sep = sep._data 

else: 

raise ValueError("the separator must be a string.") 

  

if maxsplit is None: 

return [self._parent(z) for z in self._data.split(sep)] 

else: 

return [self._parent(z) for z in self._data.split(sep, maxsplit)] 

  

def partition(self, sep): 

r""" 

Search for the separator sep in S, and return the part before it, 

the separator itself, and the part after it. The concatenation of 

the terms in the list gives back the initial word. 

  

See also the split method. 

  

.. NOTE:: 

  

This just wraps Python's builtin :meth:`str::partition` for 

:class:`str`. 

  

INPUT: 

  

- ``sep`` - string or word 

  

EXAMPLES:: 

  

sage: w = Word("MyTailorIsPoor") 

sage: w.partition("Tailor") 

[word: My, word: Tailor, word: IsPoor] 

  

:: 

  

sage: w = Word("3230301030323212323032321210121232121010") 

sage: l = w.partition("323") 

sage: print(l) 

[word: , word: 323, word: 0301030323212323032321210121232121010] 

sage: sum(l, Word('')) == w 

True 

  

If the separator is not a string an error is raised:: 

  

sage: w = Word("le papa du papa du papa etait un petit pioupiou") 

sage: w.partition(Word(['p','a','p','a'])) 

Traceback (most recent call last): 

... 

ValueError: the separator must be a string. 

""" 

if isinstance(sep, str): 

return [self._parent(z) for z in self._data.partition(sep)] 

elif isinstance(sep, WordDatatype_str): 

return [self._parent(z) for z in self._data.partition(sep._data)] 

raise ValueError("the separator must be a string.") 

  

def is_suffix(self, other): 

r""" 

Test whether ``self`` is a suffix of ``other``. 

  

INPUT: 

  

- ``other`` - a word (an instance of :class:`Word_class`) or a 

:class:`str`. 

  

OUTPUT: 

  

- boolean 

  

EXAMPLES:: 

  

sage: w = Word("abbabaabababa") 

sage: u = Word("ababa") 

sage: w.is_suffix(u) 

False 

sage: u.is_suffix(w) 

True 

sage: u.is_suffix("abbabaabababa") 

True 

  

TESTS:: 

  

sage: w = Word("abbabaabababa") 

sage: u = Word(['a','b','a','b','a']) 

sage: w.is_suffix(u) 

False 

sage: u.is_suffix(w) 

True 

  

""" 

if isinstance(other, WordDatatype_str): 

return other._data.endswith(self._data) 

elif isinstance(other, str): 

return other.endswith(self._data) 

else: 

return super(WordDatatype_str, self).is_suffix(other) 

  

def has_suffix(self, other): 

""" 

Test whether ``self`` has ``other`` as a suffix. 

  

INPUT: 

  

- ``other`` - a word (an instance of :class:`Word_class`) or a 

:class:`str`. 

  

OUTPUT: 

  

- boolean 

  

EXAMPLES:: 

  

sage: w = Word("abbabaabababa") 

sage: u = Word("ababa") 

sage: w.has_suffix(u) 

True 

sage: u.has_suffix(w) 

False 

sage: u.has_suffix("ababa") 

True 

  

""" 

if isinstance(other, WordDatatype_str): 

return self._data.endswith(other._data) 

elif isinstance(other, str): 

return self._data.endswith(other) 

else: 

return super(WordDatatype_str, self).has_suffix(other) 

  

def is_prefix(self, other): 

r""" 

Test whether ``self`` is a prefix of ``other``. 

  

INPUT: 

  

- ``other`` - a word (an instance of :class:`Word_class`) or a 

:class:`str`. 

  

OUTPUT: 

  

- boolean 

  

EXAMPLES:: 

  

sage: w = Word("abbabaabababa") 

sage: u = Word("abbab") 

sage: w.is_prefix(u) 

False 

sage: u.is_prefix(w) 

True 

sage: u.is_prefix("abbabaabababa") 

True 

  

TESTS:: 

  

sage: ab = Word('ab') 

sage: abba = Word(['a','b','b','a']) 

sage: ab.is_prefix(abba) 

True 

sage: abba.is_prefix(ab) 

False 

  

""" 

if isinstance(other, WordDatatype_str): 

return other._data.startswith(self._data) 

if isinstance(other ,str): 

return other.startswith(self._data) 

else: 

return super(WordDatatype_str, self).is_prefix(other) 

  

def has_prefix(self, other): 

r""" 

Test whether ``self`` has ``other`` as a prefix. 

  

INPUT: 

  

- ``other`` - a word (an instance of :class:`Word_class`) or a 

:class:`str`. 

  

OUTPUT: 

  

- boolean 

  

EXAMPLES:: 

  

sage: w = Word("abbabaabababa") 

sage: u = Word("abbab") 

sage: w.has_prefix(u) 

True 

sage: u.has_prefix(w) 

False 

sage: u.has_prefix("abbab") 

True 

  

TESTS:: 

  

sage: ab = Word('ab') 

sage: abba = Word(['a','b','b','a']) 

sage: ab.has_prefix(abba) 

False 

sage: abba.has_prefix(ab) 

True 

  

""" 

if isinstance(other, WordDatatype_str): 

return self._data.startswith(other._data) 

if isinstance(other, str): 

return self._data.startswith(other) 

else: 

return super(WordDatatype_str, self).has_prefix(other) 

  

cdef class WordDatatype_tuple(WordDatatype): 

r""" 

Datatype class for words defined by tuples. 

""" 

cdef public tuple _data 

  

def __init__(self, parent=None, data=None): 

r""" 

Construct a word with parent ``parent`` from an iterable ``data``. 

  

INPUT: 

  

- ``parent`` - instance of :class:`Words_all` 

- ``data`` - iterable 

  

EXAMPLES:: 

  

sage: w = Word((0,1,1,0)) 

sage: isinstance(w, sage.combinat.words.word_datatypes.WordDatatype_tuple) 

True 

sage: u = Word([0,1,1,0], datatype='tuple') 

sage: isinstance(u, sage.combinat.words.word_datatypes.WordDatatype_tuple) 

True 

  

""" 

self._parent = parent 

if isinstance(data,tuple): 

self._data = data 

else: 

self._data = tuple(data) 

self._hash = None 

  

def __iter__(self): 

r""" 

Return an iterator that iterates through the letters of self. 

  

EXAMPLES:: 

  

sage: w = Word((0,1,1,0)) 

sage: list(iter(w)) 

[0, 1, 1, 0] 

  

""" 

return iter(self._data) 

  

def __richcmp__(self, other, int op): 

r""" 

Equality test for self and other if other is an instance of 

WordDatype_tuple. 

  

INPUT: 

  

- ``other`` - a word 

- ``op`` - integer: 0, 1, 2, 3, 4 or 5 

  

OUTPUT: 

  

boolean or NotImplemented 

  

EXAMPLES:: 

  

sage: Word((1,2,3)) == Word((1,2,3)) 

True 

sage: Word((1,2,3)) == Word(()) 

False 

sage: Word((1,2,3)) == Word((1,2,3,4)) 

False 

sage: Word((1,2,3)) == Word((1,2,3,'a')) 

False 

  

It works even if the parents are not the same:: 

  

sage: Words([1,2])((1,1,1,2)) == Words([1,2,3])((1,1,1,2)) 

True 

sage: Words([1,2])((1,1,1,2)) == Word((1,1,1,2)) 

True 

  

REFERENCES: 

  

http://docs.cython.org/docs/special_methods.html 

""" 

if isinstance(other, WordDatatype_tuple): 

if op == Py_EQ: 

return self._data == other._data 

elif op == Py_NE: 

return self._data != other._data 

  

# Otherwise, force FiniteWord_class.__richcmp__ to do it 

from sage.combinat.words.word import FiniteWord_class 

return FiniteWord_class.__richcmp__(self, other, op) 

  

def __len__(self): 

r""" 

Return the length of the word. 

  

.. NOTE:: 

  

This function will be deprecated in a future version 

of Sage. Use ``self.length()`` instead. 

  

EXAMPLES:: 

  

sage: w = Word((0,1,1,0)) 

sage: len(w) 

4 

  

""" 

return len(self._data) 

  

def length(self): 

r""" 

Return the length of the word. 

  

EXAMPLES:: 

  

sage: w = Word((0,1,1,0)) 

sage: w.length() 

4 

  

""" 

return len(self._data) 

  

def __contains__(self, a): 

r""" 

Test whether ``a`` is a letter of ``self``. 

  

INPUT: 

  

- ``a`` - anything 

  

EXAMPLES:: 

  

sage: w = Word((0,1,1,0)) 

sage: 0 in w 

True 

sage: 3 in w 

False 

  

""" 

return a in self._data 

  

def __getitem__(self, key): 

r""" 

Implements ``__getitem__`` for words stored as tuples. 

  

INPUT: 

  

- ``key`` - an integer 

  

OUTPUT: 

  

- can be anything (an object contained in the word) 

  

EXAMPLES:: 

  

sage: w = Word(tuple(range(100))) 

sage: w[4] 

4 

sage: w[-1] 

99 

sage: w[3:10:2] 

word: 3579 

sage: all(w[i] == i for i in range(100)) 

True 

  

""" 

if isinstance(key, slice): 

return self._parent(self._data[key]) 

return self._data[key] 

  

def __mul__(self, other): 

r""" 

Return the concatenation of self and other. 

  

INPUT: 

  

- ``other`` - word represented by a tuple 

  

OUTPUT: 

  

word 

  

EXAMPLES:: 

  

sage: w = Word((1,2,3,4)) 

sage: w * w 

word: 12341234 

  

The type of the concatenation is preserved:: 

  

sage: type(w) 

<class 'sage.combinat.words.word.FiniteWord_tuple'> 

sage: type(w * w) 

<class 'sage.combinat.words.word.FiniteWord_tuple'> 

sage: type(w + w) 

<class 'sage.combinat.words.word.FiniteWord_tuple'> 

""" 

if isinstance(other, WordDatatype_tuple): 

return self._parent(self._data + other._data) 

else: 

return super(WordDatatype_tuple, self).__mul__(other) 

  

__add__ = __mul__