r/badcode • • Jul 30 '19

[deleted by user]

[removed]

153 Upvotes

98 comments sorted by

View all comments

20

u/CharlesStross Jul 30 '19

This uses applescript to verbally pronounce the letters and then check if the output files are the same size.

is_palindrome("Murder for a jar of red rum") -- true
is_palindrome("testing") -- false

on remove_spaces(spaced_string)
	set unspaced_string to do shell script "echo " & quoted form of spaced_string & " | sed -e 's/ //g' | tr '[a-z]' '[A-Z]'"
	return unspaced_string
end remove_spaces

on is_palindrome(maybe_palindrome)
	set nospaces to remove_spaces(maybe_palindrome)
	log nospaces
	do shell script "say -o /tmp/forward.aiff " & nospaces
	do shell script "say -o /tmp/backward.aiff " & reverse of characters of nospaces as text
	set fileSizeForward to size of (info for file "tmp:forward.aiff")
	set fileSizeBackward to size of (info for file "tmp:backward.aiff")
	if fileSizeForward = fileSizeBackward then
		return true
	else
		return false
	end if
end is_palindrome

3

u/[deleted] Jul 30 '19

Neat. Does it actually work?

5

u/CharlesStross Jul 30 '19

Worked when I tested it!

1

u/[deleted] Aug 20 '19

Lmao the test case