2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,37 +1,20 @@
|
|||
Write at least one function/method (or whatever it is called in your
|
||||
preferred language) to check if a sequence of characters (or bytes)
|
||||
is a [[wp:Palindrome|palindrome]] or not.
|
||||
The ''function'' must return a boolean value
|
||||
(or something that can be used as boolean value, like an integer).
|
||||
A [[wp:Palindrome|palindrome]] is a phrase which reads the same backward and forward.
|
||||
|
||||
It is not mandatory to write also an example code that uses
|
||||
the ''function'', unless its usage could be not clear
|
||||
(e.g. the provided recursive C solution
|
||||
needs explanation on how to call the function).
|
||||
{{task heading}}
|
||||
|
||||
It is not mandatory to handle properly encodings (see [[String length]]),
|
||||
i.e. it is admissible that the function does not recognize 'salàlas'
|
||||
as palindrome.
|
||||
Write a function or program that checks whether a given sequence of characters (or, if you prefer, bytes)
|
||||
is a palindrome.
|
||||
|
||||
The function must not ignore spaces and punctuations.
|
||||
The compliance to the aforementioned, strict or not,
|
||||
requirements completes the task.
|
||||
'''''For extra credit:'''''
|
||||
* Support Unicode characters.
|
||||
* Write a second function (possibly as a wrapper to the first) which detects ''inexact'' palindromes, i.e. phrases that are palindromes if white-space and punctuation is ignored and case-insensitive comparison is used.
|
||||
|
||||
'''Example'''<br>
|
||||
An example of a Latin palindrome is the sentence
|
||||
"''In girum imus nocte et consumimur igni''",
|
||||
roughly translated as: we walk around in the night and we are burnt by the fire (of love).
|
||||
To do your test with it, you must make it all the same case and strip spaces.
|
||||
|
||||
'''Clarification'''<br>
|
||||
To remove some confusion expressed in the discussion tab:
|
||||
|
||||
* To satisfy this task, the function should only identify exact palindromes; it should *not* strip spaces or punctuation or convert case.
|
||||
* You may additionally write a wrapper function to detect inexact palindromes, such as the Latin example in the task description, or simply do the conversion in the calling function of your test code.
|
||||
|
||||
'''Notes'''<br>
|
||||
{{task heading|Hints}}
|
||||
* It might be useful for this task to know how to [[Reversing a string|reverse a string]].
|
||||
* This task's entries might also form the subjects of the task [[Test a function]].
|
||||
|
||||
;Cf.
|
||||
* [[Semordnilap]]
|
||||
{{task heading|Related tasks}}
|
||||
|
||||
{{Related tasks/Word plays}}
|
||||
|
||||
<hr>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,73 @@
|
|||
use framework "Foundation"
|
||||
|
||||
|
||||
-- isPalindrome :: String -> Bool
|
||||
on isPalindrome(s)
|
||||
s = intercalate("", reverse of characters of s)
|
||||
end isPalindrome
|
||||
|
||||
|
||||
|
||||
-- TEST
|
||||
|
||||
on run
|
||||
|
||||
isPalindrome(lowerCaseNoSpace("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 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
|
||||
|
||||
-- intercalate :: Text -> [Text] -> Text
|
||||
on intercalate(strText, lstText)
|
||||
set {dlm, my text item delimiters} to {my text item delimiters, strText}
|
||||
set strJoined to lstText as text
|
||||
set my text item delimiters to dlm
|
||||
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)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -1,32 +1,80 @@
|
|||
DECLARE FUNCTION isPalindrome% (what AS STRING)
|
||||
' OPTION _EXPLICIT ' For QB64. In VB-DOS remove the underscore.
|
||||
|
||||
DATA "My dog has fleas", "Madam, I'm Adam.", "1 on 1", "In girum imus nocte et consumimur igni"
|
||||
DIM txt$
|
||||
|
||||
DIM L1 AS INTEGER, w AS STRING
|
||||
FOR L1 = 1 TO 4
|
||||
READ w
|
||||
IF isPalindrome(w) THEN
|
||||
PRINT CHR$(34); w; CHR$(34); " is a palindrome"
|
||||
ELSE
|
||||
PRINT CHR$(34); w; CHR$(34); " is not a palindrome"
|
||||
END IF
|
||||
NEXT
|
||||
' Palindrome
|
||||
CLS
|
||||
PRINT "This is a palindrome detector program."
|
||||
PRINT
|
||||
INPUT "Please, type a word or phrase: ", txt$
|
||||
|
||||
FUNCTION isPalindrome% (what AS STRING)
|
||||
DIM whatcopy AS STRING, chk AS STRING, tmp AS STRING * 1, L0 AS INTEGER
|
||||
IF IsPalindrome(txt$) THEN
|
||||
PRINT "Is a palindrome."
|
||||
ELSE
|
||||
PRINT "Is Not a palindrome."
|
||||
END IF
|
||||
|
||||
FOR L0 = 1 TO LEN(what)
|
||||
tmp = UCASE$(MID$(what, L0, 1))
|
||||
SELECT CASE tmp
|
||||
CASE "A" TO "Z"
|
||||
whatcopy = whatcopy + tmp
|
||||
chk = tmp + chk
|
||||
CASE "0" TO "9"
|
||||
PRINT "Numbers are cheating! ("; CHR$(34); what; CHR$(34); ")"
|
||||
isPalindrome = 0
|
||||
EXIT FUNCTION
|
||||
END SELECT
|
||||
NEXT
|
||||
END
|
||||
|
||||
|
||||
FUNCTION IsPalindrome (AText$)
|
||||
' Var
|
||||
DIM CleanTXT$, RvrsTXT$
|
||||
|
||||
CleanTXT$ = CleanText$(AText$)
|
||||
RvrsTXT$ = RvrsText$(CleanTXT$)
|
||||
|
||||
IsPalindrome = (CleanTXT$ = RvrsTXT$)
|
||||
|
||||
END FUNCTION
|
||||
|
||||
FUNCTION CleanText$ (WhichText$)
|
||||
' Var
|
||||
DIM i%, j%, c$, NewText$, CpyTxt$, AddIt%, SubsTXT$
|
||||
CONST False = 0, True = NOT False
|
||||
|
||||
SubsTXT$ = "AIOUE"
|
||||
CpyTxt$ = UCASE$(WhichText$)
|
||||
j% = LEN(CpyTxt$)
|
||||
|
||||
FOR i% = 1 TO j%
|
||||
c$ = MID$(CpyTxt$, i%, 1)
|
||||
|
||||
' See if it is a letter. Includes Spanish letters.
|
||||
SELECT CASE c$
|
||||
CASE "A" TO "Z"
|
||||
AddIt% = True
|
||||
CASE " ", "¡", "¢", "£"
|
||||
c$ = MID$(SubsTXT$, ASC(c$) - 159, 1)
|
||||
AddIt% = True
|
||||
CASE "‚"
|
||||
c$ = "E"
|
||||
AddIt% = True
|
||||
CASE "¤"
|
||||
c$ = "¥"
|
||||
AddIt% = True
|
||||
CASE ELSE
|
||||
AddIt% = False
|
||||
END SELECT
|
||||
|
||||
IF AddIt% THEN
|
||||
NewText$ = NewText$ + c$
|
||||
END IF
|
||||
NEXT i%
|
||||
|
||||
CleanText$ = NewText$
|
||||
|
||||
END FUNCTION
|
||||
|
||||
FUNCTION RvrsText$ (WhichText$)
|
||||
' Var
|
||||
DIM i%, c$, NewText$, j%
|
||||
|
||||
j% = LEN(WhichText$)
|
||||
FOR i% = 1 TO j%
|
||||
NewText$ = MID$(WhichText$, i%, 1) + NewText$
|
||||
NEXT i%
|
||||
|
||||
RvrsText$ = NewText$
|
||||
|
||||
isPalindrome = ((whatcopy) = chk)
|
||||
END FUNCTION
|
||||
|
|
|
|||
19
Task/Palindrome-detection/COBOL/palindrome-detection.cobol
Normal file
19
Task/Palindrome-detection/COBOL/palindrome-detection.cobol
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
identification division.
|
||||
function-id. palindromic-test.
|
||||
|
||||
data division.
|
||||
linkage section.
|
||||
01 test-text pic x any length.
|
||||
01 result pic x.
|
||||
88 palindromic value high-value
|
||||
when set to false low-value.
|
||||
|
||||
procedure division using test-text returning result.
|
||||
|
||||
set palindromic to false
|
||||
if test-text equal function reverse(test-text) then
|
||||
set palindromic to true
|
||||
end-if
|
||||
|
||||
goback.
|
||||
end function palindromic-test.
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
bool isPolindrome(String s){
|
||||
bool isPalindrome(String s){
|
||||
for(int i = 0; i < s.length/2;i++){
|
||||
if(s[i] != s[(s.length-1) -i])
|
||||
return false;
|
||||
|
|
|
|||
10
Task/Palindrome-detection/Go/palindrome-detection-2.go
Normal file
10
Task/Palindrome-detection/Go/palindrome-detection-2.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
func isPalindrome(s string) bool {
|
||||
runes := []rune(s)
|
||||
numRunes := len(runes) - 1
|
||||
for i := 0; i < len(runes)/2; i++ {
|
||||
if runes[i] != runes[numRunes-i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
10
Task/Palindrome-detection/Go/palindrome-detection-3.go
Normal file
10
Task/Palindrome-detection/Go/palindrome-detection-3.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
func isPalindrome(s string) bool {
|
||||
runes := []rune(s)
|
||||
for len(runes) > 1 {
|
||||
if runes[0] != runes[len(runes)-1] {
|
||||
return false
|
||||
}
|
||||
runes = runes[1 : len(runes)-1]
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
(function (strSample) {
|
||||
|
||||
// isPalindrome :: String -> Bool
|
||||
let isPalindrome = s =>
|
||||
s.split('')
|
||||
.reverse()
|
||||
.join('') === s;
|
||||
|
||||
|
||||
|
||||
// TESTING
|
||||
|
||||
// lowerCaseNoSpace :: String -> String
|
||||
let lowerCaseNoSpace = s =>
|
||||
concatMap(c => c !== ' ' ? [c.toLowerCase()] : [],
|
||||
s.split(''))
|
||||
.join(''),
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
concatMap = (f, xs) => [].concat.apply([], xs.map(f));
|
||||
|
||||
|
||||
return isPalindrome(
|
||||
lowerCaseNoSpace(strSample)
|
||||
);
|
||||
|
||||
|
||||
})("In girum imus nocte et consumimur igni");
|
||||
|
|
@ -1,4 +1,8 @@
|
|||
(define (palindrome? s)
|
||||
(setq r s)
|
||||
(reverse r) ; Reverse is destructive.
|
||||
(= s r))
|
||||
(setq r s)
|
||||
(reverse r) ; Reverse is destructive.
|
||||
(= s r))
|
||||
|
||||
;; Make ‘reverse’ non-destructive and avoid a global variable
|
||||
(define (palindrome? s)
|
||||
(= s (reverse (copy s))))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
function Test-Palindrome
|
||||
{
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Tests if a string is a palindrome.
|
||||
.DESCRIPTION
|
||||
Tests if a string is a true palindrome or, optionally, an inexact palindrome.
|
||||
.EXAMPLE
|
||||
Test-Palindrome -Text "racecar"
|
||||
.EXAMPLE
|
||||
Test-Palindrome -Text '"Deliver desserts," demanded Nemesis, "emended, named, stressed, reviled."' -Inexact
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
[OutputType([bool])]
|
||||
Param
|
||||
(
|
||||
# The string to test for palindrominity.
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]
|
||||
$Text,
|
||||
|
||||
# When specified, detects an inexact palindrome.
|
||||
[switch]
|
||||
$Inexact
|
||||
)
|
||||
|
||||
if ($Inexact)
|
||||
{
|
||||
# Strip all punctuation and spaces
|
||||
$Text = [Regex]::Replace("$Text($7&","[^1-9a-zA-Z]","")
|
||||
}
|
||||
|
||||
$Text -match "^(?'char'[a-z])+[a-z]?(?:\k'char'(?'-char'))+(?(char)(?!))$"
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
Test-Palindrome -Text 'radar'
|
||||
|
|
@ -0,0 +1 @@
|
|||
Test-Palindrome -Text "In girum imus nocte et consumimur igni."
|
||||
|
|
@ -0,0 +1 @@
|
|||
Test-Palindrome -Text "In girum imus nocte et consumimur igni." -Inexact
|
||||
28
Task/Palindrome-detection/Python/palindrome-detection-5.py
Normal file
28
Task/Palindrome-detection/Python/palindrome-detection-5.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
def p_loop():
|
||||
import re, string
|
||||
re1="" # Beginning of Regex
|
||||
re2="" # End of Regex
|
||||
pal=raw_input("Please Enter a word or phrase: ")
|
||||
pd = pal.replace(' ','')
|
||||
for c in string.punctuation:
|
||||
pd = pd.replace(c,"")
|
||||
if pal == "" :
|
||||
return -1
|
||||
c=len(pd) # Count of chars.
|
||||
loops = (c+1)/2
|
||||
for x in range(loops):
|
||||
re1 = re1 + "(\w)"
|
||||
if (c%2 == 1 and x == 0):
|
||||
continue
|
||||
p = loops - x
|
||||
re2 = re2 + "\\" + str(p)
|
||||
regex= re1+re2+"$" # regex is like "(\w)(\w)(\w)\2\1$"
|
||||
#print(regex) # To test regex before re.search
|
||||
m = re.search(r'^'+regex,pd,re.IGNORECASE)
|
||||
if (m):
|
||||
print("\n "+'"'+pal+'"')
|
||||
print(" is a Palindrome\n")
|
||||
return 1
|
||||
else:
|
||||
print("Nope!")
|
||||
return 0
|
||||
19
Task/Palindrome-detection/Rust/palindrome-detection.rust
Normal file
19
Task/Palindrome-detection/Rust/palindrome-detection.rust
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
fn is_palindrome(string: &str) -> bool {
|
||||
string.chars().zip(string.chars().rev()).all(|(x, y)| x == y)
|
||||
}
|
||||
|
||||
macro_rules! test {
|
||||
( $( $x:tt ),* ) => { $( println!("'{}': {}", $x, is_palindrome($x)); )* };
|
||||
}
|
||||
|
||||
fn main() {
|
||||
test!("",
|
||||
"a",
|
||||
"ada",
|
||||
"adad",
|
||||
"ingirumimusnocteetconsumimurigni",
|
||||
"人人為我,我為人人",
|
||||
"Я иду с мечем, судия",
|
||||
"아들딸들아",
|
||||
"The quick brown fox");
|
||||
}
|
||||
9
Task/Palindrome-detection/Vala/palindrome-detection.vala
Normal file
9
Task/Palindrome-detection/Vala/palindrome-detection.vala
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
bool is_palindrome (string str) {
|
||||
var tmp = str.casefold ().replace (" ", "");
|
||||
return tmp == tmp.reverse ();
|
||||
}
|
||||
|
||||
int main (string[] args) {
|
||||
print (is_palindrome (args[1]).to_string () + "\n");
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue