September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -3,4 +3,5 @@ category:
|
|||
- Recursion
|
||||
- String manipulation
|
||||
- Classic CS problems and programs
|
||||
- Palindromes
|
||||
note: Text processing
|
||||
|
|
|
|||
2
Task/Palindrome-detection/Ada/palindrome-detection-2.ada
Normal file
2
Task/Palindrome-detection/Ada/palindrome-detection-2.ada
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
function Palindrome (Text : String) return Boolean is
|
||||
(for all i in Text'Range => Text(i)= Text(Text'Last-i+Text'First));
|
||||
|
|
@ -1,43 +1,48 @@
|
|||
use framework "Foundation"
|
||||
|
||||
-- CASE-INSENSITIVE PALINDROME, IGNORING SPACES ? ----------------------------
|
||||
|
||||
-- isPalindrome :: String -> Bool
|
||||
on isPalindrome(s)
|
||||
s = intercalate("", reverse of characters of s)
|
||||
end isPalindrome
|
||||
|
||||
-- toSpaceFreeLower :: String -> String
|
||||
on spaceFreeToLower(s)
|
||||
script notSpace
|
||||
on |λ|(s)
|
||||
s is not space
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
intercalate("", filter(notSpace, characters of toLower(s)))
|
||||
end spaceFreeToLower
|
||||
|
||||
|
||||
-- TEST
|
||||
|
||||
-- TEST ----------------------------------------------------------------------
|
||||
on run
|
||||
|
||||
isPalindrome(lowerCaseNoSpace("In girum imus nocte et consumimur igni"))
|
||||
isPalindrome(spaceFreeToLower("In girum imus nocte et consumimur igni"))
|
||||
|
||||
--> true
|
||||
|
||||
end run
|
||||
|
||||
-- lowerCaseNoSpace :: String -> String
|
||||
on lowerCaseNoSpace(s)
|
||||
script notSpace
|
||||
on lambda(s)
|
||||
s is not space
|
||||
end lambda
|
||||
end script
|
||||
|
||||
intercalate("", filter(notSpace, characters of toLowerCase(s)))
|
||||
end lowerCaseNoSpace
|
||||
-- GENERIC FUNCTIONS ---------------------------------------------------------
|
||||
|
||||
|
||||
-- GENERIC LIBRARY FUNCTIONS
|
||||
|
||||
-- toLowerCase :: String -> String
|
||||
on toLowerCase(str)
|
||||
set ca to current application
|
||||
((ca's NSString's stringWithString:(str))'s ¬
|
||||
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
|
||||
end toLowerCase
|
||||
-- filter :: (a -> Bool) -> [a] -> [a]
|
||||
on filter(f, xs)
|
||||
tell mReturn(f)
|
||||
set lst to {}
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to item i of xs
|
||||
if |λ|(v, i, xs) then set end of lst to v
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end filter
|
||||
|
||||
-- intercalate :: Text -> [Text] -> Text
|
||||
on intercalate(strText, lstText)
|
||||
|
|
@ -47,19 +52,6 @@ on intercalate(strText, lstText)
|
|||
return strJoined
|
||||
end intercalate
|
||||
|
||||
-- filter :: (a -> Bool) -> [a] -> [a]
|
||||
on filter(f, xs)
|
||||
tell mReturn(f)
|
||||
set lst to {}
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to item i of xs
|
||||
if lambda(v, i, xs) then set end of lst to v
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end filter
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
|
|
@ -67,7 +59,14 @@ on mReturn(f)
|
|||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- toLower :: String -> String
|
||||
on toLower(str)
|
||||
set ca to current application
|
||||
((ca's NSString's stringWithString:(str))'s ¬
|
||||
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
|
||||
end toLower
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
10 INPUT S$
|
||||
20 FOR I=1 TO LEN S$/2
|
||||
30 IF S$(I)<>S$(LEN S$-I+1) THEN GOTO 60
|
||||
40 NEXT I
|
||||
50 GOTO 70
|
||||
60 PRINT "NOT A ";
|
||||
70 PRINT "PALINDROME"
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
15 GOSUB 90
|
||||
80 STOP
|
||||
90 LET T$=""
|
||||
100 FOR I=1 TO LEN S$
|
||||
110 IF S$(I)>="A" AND S$(I)<="Z" THEN LET T$=T$+S$(I)
|
||||
120 NEXT I
|
||||
130 LET S$=T$
|
||||
140 RETURN
|
||||
11
Task/Palindrome-detection/BaCon/palindrome-detection.bacon
Normal file
11
Task/Palindrome-detection/BaCon/palindrome-detection.bacon
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
OPTION COMPARE TRUE
|
||||
|
||||
INPUT "Enter your line... ", word$
|
||||
|
||||
IF word$ = REVERSE$(word$) THEN
|
||||
PRINT "This is an exact palindrome!"
|
||||
ELIF EXTRACT$(word$, "[[:punct:]]|[[:blank:]]", TRUE) = REVERSE$(EXTRACT$(word$, "[[:punct:]]|[[:blank:]]", TRUE)) THEN
|
||||
PRINT "This is an inexact palindrome!"
|
||||
ELSE
|
||||
PRINT "Not a palindrome."
|
||||
ENDIF
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
isPalindrome = (str) ->
|
||||
stripped = str.toLowerCase().replace /\W/g, ""
|
||||
stripped == (stripped.split "").reverse().join ""
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
strings = [
|
||||
"In girum imus nocte et consumimur igni"
|
||||
"A man, a plan, a canal: Panama!"
|
||||
"There is no spoon."
|
||||
]
|
||||
|
||||
console.log "'#{str}' : #{isPalindrome str}" for str in strings
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
#APPTYPE CONSOLE
|
||||
|
||||
FUNCTION stripNonAlpha(BYVAL s AS STRING) AS STRING
|
||||
DIM sTemp AS STRING = ""
|
||||
DIM c AS STRING
|
||||
FOR DIM i = 1 TO LEN(s)
|
||||
c = MID(s, i, 1)
|
||||
IF INSTR("ABCDEFGHIJKLMNOPQRSTUVWXYZ", c, 0, 1) THEN
|
||||
sTemp = stemp & c
|
||||
END IF
|
||||
NEXT
|
||||
RETURN sTemp
|
||||
END FUNCTION
|
||||
|
||||
FUNCTION IsPalindrome(BYVAL s AS STRING) AS INTEGER
|
||||
FOR DIM i = 1 TO STRLEN(s) \ 2 ' only check half of the string, as scanning from both ends
|
||||
IF s{i} <> s{STRLEN - (i - 1)} THEN RETURN FALSE 'comparison is not case sensitive
|
||||
NEXT
|
||||
|
||||
RETURN TRUE
|
||||
END FUNCTION
|
||||
|
||||
PRINT IsPalindrome(stripNonAlpha("A Toyota"))
|
||||
PRINT IsPalindrome(stripNonAlpha("Madam, I'm Adam"))
|
||||
PRINT IsPalindrome(stripNonAlpha("the rain in Spain falls mainly on the rooftops"))
|
||||
|
||||
PAUSE
|
||||
|
|
@ -1 +0,0 @@
|
|||
Outputs 1, 1, 0 respectively
|
||||
31
Task/Palindrome-detection/Forth/palindrome-detection-2.fth
Normal file
31
Task/Palindrome-detection/Forth/palindrome-detection-2.fth
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
variable temp-addr
|
||||
|
||||
: valid-char? ( addr1 u -- f ) ( check for valid character )
|
||||
+ dup C@ 48 58 within
|
||||
over C@ 65 91 within or
|
||||
swap C@ 97 123 within or ;
|
||||
|
||||
: >upper ( c1 -- c2 )
|
||||
dup 97 123 within if 32 - then ;
|
||||
|
||||
: strip-input ( addr1 u -- addr2 u ) ( Strip characters, then copy stripped string to temp-addr )
|
||||
pad temp-addr !
|
||||
temp-addr @ rot rot 0 do dup I 2dup valid-char? if
|
||||
+ C@ >upper temp-addr @ C! 1 temp-addr +!
|
||||
else 2drop
|
||||
then loop drop temp-addr @ pad - ;
|
||||
|
||||
: get-phrase ( -- addr1 u )
|
||||
." Type a phrase: " here 1024 accept here swap -trailing cr ;
|
||||
|
||||
: position-phrase ( addr1 u -- addr1 u addr2 u addr1 addr2 u )
|
||||
temp-addr @ over 2over 2over drop swap ;
|
||||
|
||||
: reverse-copy ( addr1 addr2 u -- addr1 addr2 )
|
||||
0 do over I' 1- I - + over I + 1 cmove loop 2drop ;
|
||||
|
||||
: palindrome? ( addr1 u1 -- )
|
||||
get-phrase strip-input position-phrase reverse-copy compare 0= if
|
||||
." << Valid >> Palindrome."
|
||||
else ." << Not >> a Palindrome."
|
||||
then cr ;
|
||||
|
|
@ -1,3 +1,19 @@
|
|||
is_palindrome_r x | length x <= 1 = True
|
||||
| head x == last x = is_palindrome_r . tail. init $ x
|
||||
| otherwise = False
|
||||
import Data.Char (toLower)
|
||||
|
||||
isPalindrome :: String -> Bool
|
||||
isPalindrome = (==) <*> reverse
|
||||
|
||||
-- Alternatively, comparing just the first half with the reversed latter half
|
||||
isPal :: String -> Bool
|
||||
isPal s =
|
||||
let (q, r) = quotRem (length s) 2
|
||||
in take q s == reverse (drop (q + r) s)
|
||||
|
||||
sample :: String
|
||||
sample = "In girum imus nocte et consumimur igni"
|
||||
|
||||
prepared :: String -> String
|
||||
prepared = filter (' ' /=) . fmap toLower
|
||||
|
||||
main :: IO ()
|
||||
main = print $ [isPalindrome, isPal] <*> [prepared sample]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
is_palindrome_r x | length x <= 1 = True
|
||||
| head x == last x = is_palindrome_r . tail. init $ x
|
||||
| otherwise = False
|
||||
1
Task/Palindrome-detection/K/palindrome-detection.k
Normal file
1
Task/Palindrome-detection/K/palindrome-detection.k
Normal file
|
|
@ -0,0 +1 @@
|
|||
is_palindrome:{x~|x}
|
||||
29
Task/Palindrome-detection/Kotlin/palindrome-detection.kotlin
Normal file
29
Task/Palindrome-detection/Kotlin/palindrome-detection.kotlin
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// version 1.1.2
|
||||
|
||||
/* These functions deal automatically with Unicode as all strings are UTF-16 encoded in Kotlin */
|
||||
|
||||
fun isExactPalindrome(s: String) = (s == s.reversed())
|
||||
|
||||
fun isInexactPalindrome(s: String): Boolean {
|
||||
var t = ""
|
||||
for (c in s) if (c.isLetterOrDigit()) t += c
|
||||
t = t.toLowerCase()
|
||||
return t == t.reversed()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val candidates = arrayOf("rotor", "rosetta", "step on no pets", "été")
|
||||
for (candidate in candidates) {
|
||||
println("'$candidate' is ${if (isExactPalindrome(candidate)) "an" else "not an"} exact palindrome")
|
||||
}
|
||||
println()
|
||||
val candidates2 = arrayOf(
|
||||
"In girum imus nocte et consumimur igni",
|
||||
"Rise to vote, sir",
|
||||
"A man, a plan, a canal - Panama!",
|
||||
"Ce repère, Perec" // note: 'è' considered a distinct character from 'e'
|
||||
)
|
||||
for (candidate in candidates2) {
|
||||
println("'$candidate' is ${if (isInexactPalindrome(candidate)) "an" else "not an"} inexact palindrome")
|
||||
}
|
||||
}
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
print isPalindrome("In girum imus nocte et consumimur igni")
|
||||
print isPalindrome(removePunctuation$("In girum imus nocte et consumimur igni", "S"))
|
||||
print isPalindrome(removePunctuation$("In girum imus nocte et consumimur igni", "SC"))
|
||||
|
||||
function isPalindrome(string$)
|
||||
isPalindrome = 1
|
||||
for i = 1 to int(len(string$)/2)
|
||||
if mid$(string$, i, 1) <> mid$(string$, len(string$)-i+1, 1) then isPalindrome = 0 : exit function
|
||||
next i
|
||||
end function
|
||||
|
||||
function removePunctuation$(string$, remove$)
|
||||
'P = remove puctuation. S = remove spaces C = remove case
|
||||
If instr(upper$(remove$), "C") then string$ = lower$(string$)
|
||||
If instr(upper$(remove$), "P") then removeCharacters$ = ",.!'()-&*?<>:;~[]{}"
|
||||
If instr(upper$(remove$), "S") then removeCharacters$ = removeCharacters$;" "
|
||||
|
||||
for i = 1 to len(string$)
|
||||
if instr(removeCharacters$, mid$(string$, i, 1)) then string$ = left$(string$, i-1);right$(string$, len(string$)-i) : i = i - 1
|
||||
next i
|
||||
removePunctuation$ = string$
|
||||
end function
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
0
|
||||
0
|
||||
1
|
||||
|
|
@ -2,4 +2,23 @@ function is_palindrome(sequence s)
|
|||
return s==reverse(s)
|
||||
end function
|
||||
|
||||
?is_palindrome(lower(substitute("In girum imus nocte et consumimur igni"," ",""))) -- prints 1
|
||||
?is_palindrome("rotator") -- prints 1
|
||||
?is_palindrome("tractor") -- prints 0
|
||||
|
||||
constant punctuation = " `~!@#$%^&*()-=_+[]{}\\|;:',.<>/?",
|
||||
nulls = repeat("",length(punctuation))
|
||||
|
||||
function extra_credit(sequence s)
|
||||
s = utf8_to_utf32(lower(substitute_all(s,punctuation,nulls)))
|
||||
return s==reverse(s)
|
||||
end function
|
||||
|
||||
-- these all print 1 (true)
|
||||
?extra_credit("Madam, I'm Adam.")
|
||||
?extra_credit("A man, a plan, a canal: Panama!")
|
||||
?extra_credit("In girum imus nocte et consumimur igni")
|
||||
?extra_credit("人人為我,我為人人")
|
||||
?extra_credit("Я иду с мечем, судия")
|
||||
?extra_credit("아들딸들아")
|
||||
?extra_credit("가련하시다 사장집 아들딸들아 집장사 다시 하련가")
|
||||
?extra_credit("tregða, gón, reiði - er nóg að gert")
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
Function Test-Palindrome( [String] $Text ){
|
||||
$CharArray = $Text.ToCharArray()
|
||||
[Array]::Reverse($CharArray)
|
||||
$Text.ToCharArray() -eq $CharArray
|
||||
$Text -like (-join $CharArray)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
fn is_palindrome(string: &str) -> bool {
|
||||
string.chars().zip(string.chars().rev()).all(|(x, y)| x == y)
|
||||
let half_len = string.len()/2;
|
||||
string.chars().take(half_len).eq(string.chars().take(half_len).rev())
|
||||
}
|
||||
|
||||
macro_rules! test {
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
extern crate unicode_segmentation;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
fn is_palindrome(string: &str) -> bool {
|
||||
string.graphemes(true).eq(string.graphemes(true).rev())
|
||||
}
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
def isPalindrome(s : String) : Boolean = s match {
|
||||
case s if s.length > 1 => (s.head == s.last) && isPalindrome(s.slice(1, s.length-1))
|
||||
case _ => true
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@
|
|||
(and (char=? (car s) (car r))
|
||||
(loop (cdr s) (cdr r))))))
|
||||
|
||||
<lang scheme>> (palindrome? "ingirumimusnocteetconsumimurigni")
|
||||
> (palindrome? "ingirumimusnocteetconsumimurigni")
|
||||
#t
|
||||
> (palindrome? "This is not a palindrome")
|
||||
#f
|
||||
|
|
|
|||
33
Task/Palindrome-detection/Simula/palindrome-detection.simula
Normal file
33
Task/Palindrome-detection/Simula/palindrome-detection.simula
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
BEGIN
|
||||
|
||||
BOOLEAN PROCEDURE ISPALINDROME(T); TEXT T;
|
||||
BEGIN
|
||||
BOOLEAN RESULT;
|
||||
INTEGER I, J;
|
||||
I := 1;
|
||||
J := T.LENGTH;
|
||||
RESULT := TRUE;
|
||||
WHILE RESULT AND I < J DO
|
||||
BEGIN
|
||||
CHARACTER L, R;
|
||||
T.SETPOS(I); L := T.GETCHAR; I := I + 1;
|
||||
T.SETPOS(J); R := T.GETCHAR; J := J - 1;
|
||||
RESULT := L = R;
|
||||
END;
|
||||
ISPALINDROME := RESULT;
|
||||
END ISPALINDROME;
|
||||
|
||||
TEXT T;
|
||||
FOR T :- "", "A", "AA", "ABA", "SALALAS", "MADAMIMADAM",
|
||||
"AB", "AAB", "ABCBDA"
|
||||
DO
|
||||
BEGIN
|
||||
OUTTEXT(IF ISPALINDROME(T) THEN "IS " ELSE "ISN'T");
|
||||
OUTTEXT(" PALINDROME: ");
|
||||
OUTCHAR('"');
|
||||
OUTTEXT(T);
|
||||
OUTCHAR('"');
|
||||
OUTIMAGE;
|
||||
END;
|
||||
|
||||
END.
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
fun palindrome s =
|
||||
let val cs = explode s in
|
||||
cs = rev cs
|
||||
end
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
0
|
||||
-1
|
||||
0
|
||||
-1
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
; x86_84 Linux nasm
|
||||
section .text
|
||||
|
||||
isPalindrome:
|
||||
mov rsi, rax
|
||||
mov rdi, rax
|
||||
|
||||
get_end:
|
||||
cmp byte [rsi], 0
|
||||
je get_result
|
||||
inc rsi
|
||||
jmp get_end
|
||||
|
||||
get_result:
|
||||
mov rax, 0
|
||||
dec rsi
|
||||
|
||||
compare:
|
||||
mov cl, byte [rdi]
|
||||
cmp byte [rsi], cl
|
||||
jne not_palindrome
|
||||
cmp rsi, rdi
|
||||
je palindrome
|
||||
inc rdi
|
||||
cmp rdi, rsi
|
||||
je palindrome
|
||||
dec rsi
|
||||
jmp compare
|
||||
|
||||
not_palindrome:
|
||||
mov rax, 0
|
||||
ret
|
||||
palindrome:
|
||||
mov rax, 1
|
||||
ret
|
||||
5
Task/Palindrome-detection/Zkl/palindrome-detection.zkl
Normal file
5
Task/Palindrome-detection/Zkl/palindrome-detection.zkl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
fcn pali(text){
|
||||
if (text.len()<2) return(False);
|
||||
text==text.reverse();
|
||||
}
|
||||
fcn pali2(text){ pali((text - " \t\n.,").toLower()) } // or whatever punctuation is
|
||||
Loading…
Add table
Add a link
Reference in a new issue