Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
17
Task/Semordnilap/8th/semordnilap.8th
Normal file
17
Task/Semordnilap/8th/semordnilap.8th
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
[] var, results
|
||||
|
||||
: processline \ m s --
|
||||
clone nip
|
||||
tuck s:rev
|
||||
m:exists? if
|
||||
results @ rot a:push drop
|
||||
else
|
||||
swap true m:!
|
||||
then ;
|
||||
|
||||
{} "unixdict.txt" app:asset >s
|
||||
' processline s:eachline
|
||||
|
||||
results @ dup a:len . " pairs" . cr
|
||||
a:shuffle
|
||||
( a:shift dup . " is the reverse of " . s:rev . cr ) 5 times bye
|
||||
30
Task/Semordnilap/EchoLisp/semordnilap.echolisp
Normal file
30
Task/Semordnilap/EchoLisp/semordnilap.echolisp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
(lib 'struct)
|
||||
(lib 'sql)
|
||||
(lib 'words)
|
||||
|
||||
(lib 'dico.fr.no-accent) ;; load dictionary
|
||||
(string-delimiter "")
|
||||
|
||||
;; check reverse r of w is a word
|
||||
;; take only one pair : r < w
|
||||
(define (semordnilap? w)
|
||||
(define r (list->string (reverse (string->list w))))
|
||||
(and (word? r) (string<? r w)))
|
||||
|
||||
;; to get longest first
|
||||
(define (string-sort a b) (> (string-length a) (string-length b)))
|
||||
|
||||
(define (task)
|
||||
;; select unique words into the list 'mots'
|
||||
(define mots (make-set (words-select #:any null 999999)))
|
||||
(define semordnilap
|
||||
(list-sort string-sort (for/list ((w mots))
|
||||
#:when (semordnilap? w)
|
||||
w )))
|
||||
(writeln 'pairs '→ (length semordnilap))
|
||||
(writeln 'longest '→ (take semordnilap 5)))
|
||||
|
||||
{{out}}
|
||||
(task)
|
||||
pairs → 345
|
||||
longest → (rengager tresser strasse reveler retrace)
|
||||
80
Task/Semordnilap/FreeBASIC/semordnilap.freebasic
Normal file
80
Task/Semordnilap/FreeBASIC/semordnilap.freebasic
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
' version 20-06-2015
|
||||
' compile with: fbc -s console
|
||||
|
||||
Function reverse(norm As String) As String
|
||||
|
||||
Dim As String rev
|
||||
Dim As Integer i, l = Len(norm) -1
|
||||
|
||||
rev = norm
|
||||
For i = 0 To l
|
||||
rev[l-i] = norm[i]
|
||||
Next
|
||||
|
||||
Return rev
|
||||
|
||||
End Function
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As Integer i, j, count, amount, ff = FreeFile
|
||||
Dim As String in_str, rev, big = " " ' big needs to start with a space
|
||||
Dim As String norm(27000), result(270, 2)
|
||||
|
||||
Print
|
||||
Print "Start reading unixdict.txt";
|
||||
|
||||
Open "unixdict.txt" For Input As #ff
|
||||
|
||||
While Not Eof(ff) ' read to end of file
|
||||
|
||||
Line Input #ff, in_str ' get line = word
|
||||
in_str = Trim(in_str) ' we don't want spaces
|
||||
If Len(in_str) > 1 Then ' if length > 1 then reverse
|
||||
rev = reverse(in_str)
|
||||
If in_str <> rev Then ' if in_str is not a palingdrome
|
||||
count = count + 1 ' increase counter
|
||||
norm(count) = in_str ' store in the array
|
||||
big = big + rev + " " ' create big string with reversed words
|
||||
End If
|
||||
End If
|
||||
|
||||
Wend
|
||||
|
||||
Close #ff
|
||||
|
||||
Print " ... Done"
|
||||
Print : Print "Start looking for semordnilap"
|
||||
|
||||
For i = 1 To count
|
||||
For j = 1 To amount ' check to avoid the double
|
||||
If result(j, 2) = norm(i) Then Continue For, For
|
||||
Next
|
||||
j = InStr(big, " " + norm(i) + " ")
|
||||
If j <> 0 Then ' found one
|
||||
amount = amount + 1 ' increase counter
|
||||
result(amount,1) = norm(i) ' store normal word
|
||||
result(amount,2) = reverse(norm(i)) ' store reverse word
|
||||
End If
|
||||
Next
|
||||
|
||||
Print : Print "Found"; amount; " unique semordnilap pairs"
|
||||
Print : Print "Display 5 semordnilap pairs"
|
||||
Print
|
||||
|
||||
count = 0
|
||||
For i = 1 To amount
|
||||
If Len(result(i,1)) >= 5 Then
|
||||
count = count + 1
|
||||
Print result(i, 1), result(i, 2)
|
||||
If count >= 5 Then Exit For
|
||||
EndIf
|
||||
Next
|
||||
|
||||
Print
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
Print : Print "Hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
29
Task/Semordnilap/Lasso/semordnilap.lasso
Normal file
29
Task/Semordnilap/Lasso/semordnilap.lasso
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
local(
|
||||
words = string(include_url('http://www.puzzlers.org/pub/wordlists/unixdict.txt')) -> split('\n'),
|
||||
semordnilaps = array,
|
||||
found_size,
|
||||
example,
|
||||
haveexamples = false,
|
||||
examples = array
|
||||
)
|
||||
|
||||
#words -> removeall('')
|
||||
|
||||
with word in #words do {
|
||||
local(reversed = string(#word) -> reverse&)
|
||||
if(not(#word == #reversed) and not(#semordnilaps >> #word) and not(#semordnilaps >> #reversed) and #words >> #reversed) => {
|
||||
#semordnilaps -> insert(#word = #reversed)
|
||||
}
|
||||
}
|
||||
|
||||
#found_size = #semordnilaps -> size
|
||||
|
||||
while(not(#haveexamples)) => {
|
||||
#example = #semordnilaps -> get(integer_random(#found_size, 1))
|
||||
not(#examples >> #example -> name) ? #examples -> insert(#example)
|
||||
#examples -> size >= 5 ? #haveexamples = true
|
||||
}
|
||||
'Total found: '
|
||||
#found_size
|
||||
'<br />'
|
||||
#examples
|
||||
16
Task/Semordnilap/Nim/semordnilap.nim
Normal file
16
Task/Semordnilap/Nim/semordnilap.nim
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import strutils, sequtils, sets, algorithm
|
||||
|
||||
proc reverse(s): string =
|
||||
result = newString(s.len)
|
||||
for i,c in s:
|
||||
result[s.high - i] = c
|
||||
|
||||
let
|
||||
words = readFile("unixdict.txt").strip.splitLines
|
||||
wordset = words.toSet
|
||||
revs = words.map(reverse)
|
||||
var pairs = zip(words, revs).filterIt(it[0] < it[1] and it[1] in wordset)
|
||||
|
||||
echo "Total number of semordnilaps: ", pairs.len
|
||||
pairs.sort(proc (x,y): auto = cmp(x[0].len,y[0].len))
|
||||
echo pairs[pairs.high-4..pairs.high]
|
||||
8
Task/Semordnilap/Oforth/semordnilap.oforth
Normal file
8
Task/Semordnilap/Oforth/semordnilap.oforth
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
: semordnilap
|
||||
| w wr wrds |
|
||||
ListBuffer new ->wrds
|
||||
ListBuffer new
|
||||
File new("unixdict.txt") forEach: w [
|
||||
wrds include(w reverse dup ->wr) ifTrue: [ [wr, w] over add ]
|
||||
w wr < ifTrue: [ wrds add(w) ]
|
||||
] ;
|
||||
20
Task/Semordnilap/Phix/semordnilap.phix
Normal file
20
Task/Semordnilap/Phix/semordnilap.phix
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
sequence words={}, semilordnaps={}
|
||||
object word
|
||||
constant fn = open("demo\\unixdict.txt","r")
|
||||
|
||||
while 1 do
|
||||
word = trim(gets(fn))
|
||||
if atom(word) then exit end if
|
||||
if find(reverse(word),words) then
|
||||
semilordnaps = append(semilordnaps,word)
|
||||
end if
|
||||
words = append(words,word)
|
||||
end while
|
||||
|
||||
close(fn)
|
||||
|
||||
?length(semilordnaps)
|
||||
for i=1 to 5 do
|
||||
word = semilordnaps[i]
|
||||
printf(1,"%s - %s\n",{word,reverse(word)})
|
||||
end for
|
||||
17
Task/Semordnilap/Ring/semordnilap.ring
Normal file
17
Task/Semordnilap/Ring/semordnilap.ring
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
load "stdlib.ring"
|
||||
aList = file2list("C:\Ring\unixdict.txt")
|
||||
for m = 1 to 10
|
||||
nr = random(len(aList)-1) + 1
|
||||
bool = semordnilap(aList[nr])
|
||||
see aList[nr] + nl
|
||||
if bool = 0 see "is palindrome" + nl + nl
|
||||
else see "is semordnilap" + nl + nl ok
|
||||
next
|
||||
|
||||
func semordnilap aString
|
||||
bString = ""
|
||||
for i=len(aString) to 1 step -1
|
||||
bString = bString + aString[i]
|
||||
next
|
||||
see aString
|
||||
if aString = bString return 0 else return 1 ok
|
||||
11
Task/Semordnilap/Sidef/semordnilap.sidef
Normal file
11
Task/Semordnilap/Sidef/semordnilap.sidef
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
var c = 0
|
||||
var seen = Hash()
|
||||
|
||||
ARGF.each { |line|
|
||||
line.chomp!
|
||||
var r = line.reverse
|
||||
((seen{r} := 0 ++) && (c++ < 5) && say "#{line} #{r}") ->
|
||||
|| (seen{line} := 0 ++)
|
||||
}
|
||||
|
||||
say c
|
||||
11
Task/Semordnilap/jq/semordnilap.jq
Normal file
11
Task/Semordnilap/jq/semordnilap.jq
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# Produce a stream
|
||||
def report:
|
||||
split("\n") as $list
|
||||
# construct the dictionary:
|
||||
| (reduce $list[] as $entry ({}; . + {($entry): 1})) as $dict
|
||||
# construct the list of semordnilaps:
|
||||
| $list[]
|
||||
| select( (explode|reverse|implode) as $rev
|
||||
| (. < $rev and $dict[$rev]) );
|
||||
|
||||
[report] | (.[0:5][], "length = \(length)")
|
||||
Loading…
Add table
Add a link
Reference in a new issue