3 # This file is part of NIT ( http://www.nitlanguage.org ).
4 # This program is public domain
6 # Task: Last letter-first letter
7 # SEE: <http://rosettacode.org/wiki/Last letter-first letter>
8 module first_letter_last_letter
10 fun search
(words
: String): Array[String]
12 var list
= words
.split
("[ \n]".to_re
)
13 var res
= search_2
(new Array[String], list
)
17 fun search_2
(state
, remain
: Array[String]): Array[String]
19 if remain
.is_empty
then return state
22 if state
.is_empty
then last
= null else last
= state
.last
.chars
.last
24 if last
!= null and word
.chars
.first
!= last
then continue
25 var new_state
= state
+ [word
]
26 var new_remain
= remain
.clone
27 new_remain
.remove
(word
)
28 var sub_res
= search_2
(new_state
, new_remain
)
29 if sub_res
.length
> res
.length
then
36 var words
= """audino bagon baltoy banette bidoof braviary
37 bronzor carracosta charmeleon cresselia croagunk darmanitan deino
38 emboar emolga exeggcute gabite girafarig gulpin haxorus heatmor
39 heatran ivysaur jellicent jumpluff kangaskhan kricketune landorus
40 ledyba loudred lumineon lunatone machamp magnezone mamoswine nosepass
41 petilil pidgeotto pikachu pinsir poliwrath poochyena porygon2
42 porygonz registeel relicanth remoraid rufflet sableye scolipede
43 scrafty seaking sealeo silcoon simisear snivy snorlax spoink starly
44 tirtouga trapinch treecko tyrogue vigoroth vulpix wailord wartortle
45 whismur wingull yamask"""
46 print search
(words
).join
("\n")