Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,8 @@
;; returns #t or #f
(define (palindrome? string)
(equal? (string->list string) (reverse (string->list string))))
;; to strip spaces, use the following
;;(define (palindrome? string)
;;(let ((string (string-replace string "/\ /" "" "g")))
;;(equal? (string->list string) (reverse (string->list string)))))

View file

@ -0,0 +1,45 @@
import String exposing (reverse, length)
import Html exposing (Html, Attribute, text, div, input)
import Html.Attributes exposing (placeholder, value, style)
import Html.Events exposing (on, targetValue)
import Html.App exposing (beginnerProgram)
-- The following function (copied from Haskell) satisfies the
-- rosettacode task description.
is_palindrome x = x == reverse x
-- The remainder of the code demonstrates the use of the function
-- in a complete Elm program.
main = beginnerProgram { model = "" , view = view , update = update }
update newStr oldStr = newStr
view : String -> Html String
view candidate =
div []
([ input
[ placeholder "Enter a string to check."
, value candidate
, on "input" targetValue
, myStyle
]
[]
] ++
[ let testResult =
is_palindrome candidate
statement =
if testResult then "PALINDROME!" else "not a palindrome"
in div [ myStyle] [text statement]
])
myStyle : Attribute msg
myStyle =
style
[ ("width", "100%")
, ("height", "20px")
, ("padding", "5px 0 0 5px")
, ("font-size", "1em")
, ("text-align", "left")
]

View file

@ -0,0 +1,76 @@
' version 20-06-2015
' compile with: fbc -s console "filename".bas
#Ifndef TRUE ' define true and false for older freebasic versions
#Define FALSE 0
#Define TRUE Not FALSE
#EndIf
Function reverse(norm As String) As Integer
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
If norm = rev Then
Return TRUE
Else
Return FALSE
End If
End Function
Function cleanup(in As String, action As String = "") As String
' action = "" do nothing, [l|L] = convert to lowercase,
' [s|S] = strip spaces, [p|P] = strip punctuation.
If action = "" Then Return in
Dim As Integer i, p_, s_
Dim As String ch
action = LCase(action)
For i = 1 To Len(action)
ch = Mid(action, i, 1)
If ch = "l" Then in = LCase(in)
If ch = "p" Then
p_ = 1
ElseIf ch = "s" Then
s_ = 1
End If
Next
If p_ = 0 And s_ = 0 Then Return in
Dim As String unwanted, clean
If s_ = 1 Then unwanted = " "
If p_ = 1 Then unwanted = unwanted + "`~!@#$%^&*()-=_+[]{}\|;:',.<>/?"
For i = 1 To Len(in)
ch = Mid(in, i, 1)
If InStr(unwanted, ch) = 0 Then clean = clean + ch
Next
Return clean
End Function
' ------=< MAIN >=------
Dim As String test = "In girum imus nocte et consumimur igni"
'IIf ( cond, true, false ), true and false must be of the same type (num, string, UDT)
Print
Print " reverse(test) = "; IIf(reverse(test) = FALSE, "FALSE", "TRUE")
Print " reverse(cleanup(test,""l"")) = "; IIf(reverse(cleanup(test,"l")) = FALSE, "FALSE", "TRUE")
Print " reverse(cleanup(test,""ls"")) = "; IIf(reverse(cleanup(test,"ls")) = FALSE, "FALSE", "TRUE")
Print "reverse(cleanup(test,""PLS"")) = "; IIf(reverse(cleanup(test,"PLS")) = FALSE, "FALSE", "TRUE")
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print : Print "Hit any key to end program"
Sleep
End

View file

@ -0,0 +1,16 @@
define ispalindrome(text::string) => {
local(_text = string(#text)) // need to make copy to get rid of reference issues
#_text -> replace(regexp(`(?:$|\W)+`), -ignorecase)
local(reversed = string(#_text))
#reversed -> reverse
return #_text == #reversed
}
ispalindrome('Tätatät') // works with high ascii
ispalindrome('Hello World')
ispalindrome('A man, a plan, a canoe, pasta, heros, rajahs, a coloratura, maps, snipe, percale, macaroni, a gag, a banana bag, a tan, a tag, a banana bag again (or a camel), a crepe, pins, Spam, a rut, a Rolo, cash, a jar, sore hats, a peon, a canal Panama!')

View file

@ -0,0 +1,16 @@
function palindrome txt exact
if exact is empty or exact is not false then
set caseSensitive to true --default is false
else
replace space with empty in txt
put lower(txt) into txt
end if
return txt is reverse(txt)
end palindrome
function reverse str
repeat with i = the length of str down to 1
put byte i of str after revstr
end repeat
return revstr
end reverse

View file

@ -0,0 +1,9 @@
proc reverse(s): string =
result = newString(s.len)
for i,c in s:
result[s.high - i] = c
proc isPalindrome(s): bool =
s == reverse(s)
echo isPalindrome("FoobooF")

View file

@ -0,0 +1 @@
String method: isPalindrome self reverse self == ;

View file

@ -0,0 +1,5 @@
function is_palindrome(sequence s)
return s==reverse(s)
end function
?is_palindrome(lower(substitute("In girum imus nocte et consumimur igni"," ",""))) -- prints 1

View file

@ -0,0 +1,11 @@
# The readable recursive version
palindrome_i = (s, b, e):
if (e <= b): true.
elsif (s ord(b) != s ord(e)): false.
else: palindrome_i(s, b+1, e-1).
.
palindrome = (s):
palindrome_i(s, 0, s length - 1).
palindrome(argv(1))

View file

@ -0,0 +1,8 @@
aString = "radar"
bString = ""
for i=len(aString) to 1 step -1
bString = bString + aString[i]
next
see aString
if aString = bString see " is a palindrome." + nl
else see " is not a palindrome" + nl ok

View file

@ -0,0 +1,3 @@
import <Utilities/Sequence.sl>;
isPalindrome(string(1)) := equalList(string, reverse(string));

View file

@ -0,0 +1,5 @@
isPalindrome(string(1)) :=
let
compares[i] := string[i] = string[size(string) - (i - 1)] foreach i within 1 ... (size(string) / 2);
in
all(compares);

View file

@ -0,0 +1 @@
say "noon".is_palindrome; # true

View file

@ -0,0 +1,3 @@
func palindrome(s) {
s == s.reverse
}

View file

@ -0,0 +1,11 @@
func palindrome(s) {
if (s.len <= 1) {
true
}
elsif (s.first != s.last) {
false
}
else {
__FUNC__(s.ft(1, -2))
}
}

View file

@ -0,0 +1,19 @@
import Foundation
// Allow for easy character checking
extension String {
subscript (i: Int) -> String {
return String(Array(self)[i])
}
}
func isPalindrome(str:String) -> Bool {
if (count(str) == 0 || count(str) == 1) {
return true
}
let removeRange = Range<String.Index>(start: advance(str.startIndex, 1), end: advance(str.endIndex, -1))
if (str[0] == str[count(str) - 1]) {
return isPalindrome(str.substringWithRange(removeRange))
}
return false
}

View file

@ -0,0 +1,5 @@
func isPal(str: String) -> Bool {
let c = str.characters
return lazy(c).reverse()
.startsWith(c[c.startIndex...advance(c.startIndex, c.count / 2)])
}

View file

@ -0,0 +1,13 @@
@let {
; Using a hook
pal1 @(= @rev)
; Function with argument
pal2 &s = s @rev s
; for inexact palindromes
pal3 ^(@(= @rev) .toLowerCase. &\@replace[&"\s+"g ""])
[[
!pal1 "abcba"
!pal2 "abcbac"
!pal3 "In girum imus nocte et consumimur igni"
]]
}

View file

@ -0,0 +1 @@
def palindrome: explode as $in | ($in|reverse) == $in;