Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 01 Apr 2013 05:50 PM |
The title is what I came up with while trying to create a challenge longer than the one line return statement + variable declarations that was my previous one.
For this one, create a function called squarePalindromeChecker that can take in any number of arguments and will return whether or not the input, which comes in the form of one-character per argument, can form a square palindrome. Input is a square palindrome if each row, column, and diagonal is a palindrome. To demonstrate this, let's consider the input 'C', '1', 'C', '2', 'C', '2', 'C', '1', 'C'. The square would be constructed as so: C 1 C 2 C 2 C 1 C
The rows are all palindromes (C1C, 2C2, C1C). The columns are all palindromes (C2C, 1C1, C2C) The diagonals are both palindromes (CCC, CCC) Therefore, it is a square palindrome. A case such as 'C', 'C', 'C', 'C' would also be a square palindrome.
Like my previous "challenge" (perhaps I should start calling them problems instead?), it isn't too difficult, but this should take a few more lines and be more fun. |
|
|
| Report Abuse |
|
|
Tenal
|
  |
| Joined: 15 May 2011 |
| Total Posts: 18684 |
|
| |
|
DrHaximus
|
  |
| Joined: 22 Nov 2011 |
| Total Posts: 8410 |
|
|
| 01 Apr 2013 06:06 PM |
tenal the thread contributer
- DrHaximoose |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 01 Apr 2013 06:13 PM |
function spd(s) local t = {} local w = {} local n = true local l = 0 for v in s:gmatch("[^\n]+") do t[#t+1] = v l=(l==0 and #v or l) if v ~= string.reverse(v) or #v ~= l then n = false end end for i = 1, l do local x = "" for _, v in ipairs(t) do x=x..v:sub(i,i) end w[#w+1] = x if x ~= string.reverse(x) then n = false end end if t[1] ~= t[#t] or w[1] ~= w[#w] then n = false end return n end |
|
|
| Report Abuse |
|
|
Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 01 Apr 2013 06:32 PM |
@1Waffle1, Your function only takes one parameter. The input happens per character. To adjust, the test case "CCCCC" also returned true, even though it is not a square palindrome, as you cannot create a square. |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 01 Apr 2013 07:16 PM |
It accepts rectangles. CCCCC is a palindromic rectangle.
spd([[2C2 C1C 2C2]])
> true |
|
|
| Report Abuse |
|
|
Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 02 Apr 2013 03:08 PM |
| The problem with rectangles is that you can make the rectangle a size of 1x#arguments, meaning you can just check for a regular palindrome. Also, I got false when I put in your case with your script, but I might have copied something wrong. |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 02 Apr 2013 03:14 PM |
function spd(s) s=s:gsub("[^\n%w]+","") ... |
|
|
| Report Abuse |
|
|
Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 02 Apr 2013 03:41 PM |
| Yep, that fixed it. The only major difference that I got from the test cases I ran is that an empty string returns true on yours, whereas I made a special case to have it return false. |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 02 Apr 2013 03:50 PM |
| An empty string is a palindrome. It equals nothing. If you reverse nothing, you still have nothing, so nothing is a palindrome. |
|
|
| Report Abuse |
|
|
1waffle1
|
  |
| Joined: 16 Oct 2007 |
| Total Posts: 16381 |
|
|
| 02 Apr 2013 03:54 PM |
| and I don't think returning true vs false for an empty string is a 'major difference.' I think working for rectangles is a major difference. |
|
|
| Report Abuse |
|
|