mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-09-19 16:46:11 -04:00
0.1.1 palindrome
This commit is contained in:
parent
2464abf608
commit
5f1faef934
11
libraries/palindrome/.arduino-ci.yml
Normal file
11
libraries/palindrome/.arduino-ci.yml
Normal file
@ -0,0 +1,11 @@
|
||||
compile:
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
- uno
|
||||
# - due
|
||||
# - zero
|
||||
# - leonardo
|
||||
- m4
|
||||
- esp32
|
||||
# - esp8266
|
||||
# - mega2560
|
13
libraries/palindrome/.github/workflows/arduino-lint.yml
vendored
Normal file
13
libraries/palindrome/.github/workflows/arduino-lint.yml
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
name: Arduino-lint
|
||||
|
||||
on: [push, pull_request]
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: arduino/arduino-lint-action@v1
|
||||
with:
|
||||
library-manager: update
|
||||
compliance: strict
|
17
libraries/palindrome/.github/workflows/arduino_test_runner.yml
vendored
Normal file
17
libraries/palindrome/.github/workflows/arduino_test_runner.yml
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
name: Arduino CI
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
runTest:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: 2.6
|
||||
- run: |
|
||||
gem install arduino_ci
|
||||
arduino_ci.rb
|
18
libraries/palindrome/.github/workflows/jsoncheck.yml
vendored
Normal file
18
libraries/palindrome/.github/workflows/jsoncheck.yml
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
name: JSON check
|
||||
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- '**.json'
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: json-syntax-check
|
||||
uses: limitusus/json-syntax-check@v1
|
||||
with:
|
||||
pattern: "\\.json$"
|
||||
|
21
libraries/palindrome/LICENSE
Normal file
21
libraries/palindrome/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021-2022 Rob Tillaart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
86
libraries/palindrome/examples/demo/demo.ino
Normal file
86
libraries/palindrome/examples/demo/demo.ino
Normal file
@ -0,0 +1,86 @@
|
||||
//
|
||||
// FILE: demo.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: search the longest palindrome
|
||||
// DATE: 2021-12-02
|
||||
// URL: https://github.com/RobTillaart/
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "palindrome.h"
|
||||
|
||||
palindrome pd;
|
||||
|
||||
|
||||
uint32_t start, stop;
|
||||
|
||||
#define PLEN 1600
|
||||
char str[PLEN];
|
||||
|
||||
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
for (int i = 0; i < PLEN - 1; i++)
|
||||
{
|
||||
str[i] = 'a' + random(4);
|
||||
}
|
||||
str[PLEN - 1] = 0;
|
||||
|
||||
//Serial.println(str);
|
||||
// delay(100);
|
||||
|
||||
int position = 0;
|
||||
int length = 0;
|
||||
start = micros();
|
||||
pd.findOddPalindrome(str, position, length);
|
||||
stop = micros();
|
||||
Serial.print(stop - start);
|
||||
Serial.print("\t");
|
||||
printPartial(str, position, length);
|
||||
Serial.println();
|
||||
delay(100);
|
||||
|
||||
start = micros();
|
||||
pd.findEvenPalindrome(str, position, length);
|
||||
stop = micros();
|
||||
Serial.print(stop - start);
|
||||
Serial.print("\t");
|
||||
printPartial(str, position, length);
|
||||
Serial.println();
|
||||
delay(100);
|
||||
|
||||
start = micros();
|
||||
pd.findPalindrome(str, position, length);
|
||||
stop = micros();
|
||||
Serial.print(stop - start);
|
||||
Serial.print("\t");
|
||||
printPartial(str, position, length);
|
||||
Serial.println();
|
||||
delay(100);
|
||||
|
||||
Serial.println("done...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void printPartial(char * str, int pos, int len)
|
||||
{
|
||||
Serial.print(pos);
|
||||
Serial.print("\t");
|
||||
Serial.print(len);
|
||||
Serial.print("\t");
|
||||
for (int a = pos; a < pos + len; a++) Serial.print(str[a]);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
85
libraries/palindrome/examples/demo_count/demo_count.ino
Normal file
85
libraries/palindrome/examples/demo_count/demo_count.ino
Normal file
@ -0,0 +1,85 @@
|
||||
//
|
||||
// FILE: demo_count.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: search the longest palindrome
|
||||
// DATE: 2021-12-03
|
||||
// URL: https://github.com/RobTillaart/
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "palindrome.h"
|
||||
|
||||
palindrome pd;
|
||||
|
||||
|
||||
uint32_t start, stop;
|
||||
|
||||
#define PLEN 1600
|
||||
char str[PLEN];
|
||||
|
||||
int count;
|
||||
float percentage;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
for (int i = 0; i < PLEN - 1; i++)
|
||||
{
|
||||
str[i] = 'a' + random(4);
|
||||
}
|
||||
str[PLEN - 1] = 0;
|
||||
|
||||
// Serial.println(str);
|
||||
// delay(100);
|
||||
|
||||
start = micros();
|
||||
count = pd.palindromeCount(str);
|
||||
stop = micros();
|
||||
Serial.print(stop - start);
|
||||
Serial.print("\t");
|
||||
Serial.print(count);
|
||||
Serial.println();
|
||||
delay(100);
|
||||
|
||||
start = micros();
|
||||
percentage = pd.palindromePercentage(str);
|
||||
stop = micros();
|
||||
Serial.print(stop - start);
|
||||
Serial.print("\t");
|
||||
Serial.print(percentage, 1);
|
||||
Serial.print("%");
|
||||
Serial.println();
|
||||
delay(100);
|
||||
|
||||
Serial.println("done...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
/*
|
||||
for (int size = 100; size < 200; size++)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
str[i] = 'a' + random(4);
|
||||
}
|
||||
str[size - 1] = 0;
|
||||
start = micros();
|
||||
percentage = pd.palindromePercentage(str);
|
||||
stop = micros();
|
||||
// Serial.print(stop - start);
|
||||
Serial.print("\t");
|
||||
Serial.print(percentage);
|
||||
Serial.println();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -0,0 +1,60 @@
|
||||
//
|
||||
// FILE: demo_isPalindrome.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: search the longest palindrome
|
||||
// DATE: 2021-12-03
|
||||
// URL: https://github.com/RobTillaart/
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "palindrome.h"
|
||||
|
||||
palindrome pd;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
test("");
|
||||
test("a");
|
||||
test("aa");
|
||||
test("aaa");
|
||||
test("aaaa");
|
||||
Serial.println();
|
||||
|
||||
test("ab");
|
||||
test("aba");
|
||||
test("abaa");
|
||||
test("aabaa");
|
||||
test("abababa");
|
||||
Serial.println();
|
||||
|
||||
test("11011");
|
||||
test("0011011");
|
||||
test("123");
|
||||
test("/* */");
|
||||
test("##############");
|
||||
Serial.println();
|
||||
|
||||
Serial.println("done...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void test(const char * str)
|
||||
{
|
||||
bool b = pd.isPalindrome(str);
|
||||
Serial.print(str);
|
||||
Serial.println(b ? "\ttrue" : "\tfalse");
|
||||
}
|
||||
|
||||
|
||||
|
||||
// -- END OF FILE --
|
165
libraries/palindrome/examples/develop/develop.ino
Normal file
165
libraries/palindrome/examples/develop/develop.ino
Normal file
@ -0,0 +1,165 @@
|
||||
//
|
||||
// FILE: develop.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: search the longest palindrome
|
||||
// DATE: 2021-12-02
|
||||
// URL: https://github.com/RobTillaart/
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
|
||||
uint32_t start, stop;
|
||||
|
||||
#define PLEN 1600
|
||||
char str[PLEN];
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
for (int i = 0; i < PLEN - 1; i++)
|
||||
{
|
||||
str[i] = 'a' + random(4);
|
||||
}
|
||||
str[PLEN - 1] = 0;
|
||||
|
||||
//Serial.println(str);
|
||||
// delay(100);
|
||||
|
||||
int position = 0;
|
||||
int length = 0;
|
||||
start = micros();
|
||||
findOddPalindrome(str, position, length);
|
||||
stop = micros();
|
||||
Serial.print(stop - start);
|
||||
Serial.print("\t");
|
||||
printPartial(str, position, length);
|
||||
Serial.println();
|
||||
delay(100);
|
||||
|
||||
start = micros();
|
||||
findEvenPalindrome(str, position, length);
|
||||
stop = micros();
|
||||
Serial.print(stop - start);
|
||||
Serial.print("\t");
|
||||
printPartial(str, position, length);
|
||||
Serial.println();
|
||||
delay(100);
|
||||
|
||||
start = micros();
|
||||
findPalindrome(str, position, length);
|
||||
stop = micros();
|
||||
Serial.print(stop - start);
|
||||
Serial.print("\t");
|
||||
printPartial(str, position, length);
|
||||
Serial.println();
|
||||
delay(100);
|
||||
|
||||
Serial.println("done...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int findPalindrome(const char * str, int &position, int &length)
|
||||
{
|
||||
int posOdd = 0, lengthOdd = 0;
|
||||
int posEven = 0, lengthEven = 0;
|
||||
findOddPalindrome(str, posOdd, lengthOdd);
|
||||
findEvenPalindrome(str, posEven, lengthEven);
|
||||
if (lengthEven > lengthOdd)
|
||||
{
|
||||
position = posEven;
|
||||
length = lengthEven;
|
||||
return length;
|
||||
}
|
||||
position = posOdd;
|
||||
length = lengthOdd;
|
||||
return length;
|
||||
}
|
||||
|
||||
|
||||
int findEvenPalindrome(const char * str, int &position, int &length)
|
||||
{
|
||||
int sl = strlen(str);
|
||||
if (sl == 0) return -1;
|
||||
|
||||
int newpos = 0;
|
||||
int newlen = 1;
|
||||
for (int i = 0; i < sl; i++)
|
||||
{
|
||||
if (str[i] != str[i + 1]) continue;
|
||||
int j = i - 1;
|
||||
int k = i + 2;
|
||||
while (0 <= j && k < sl)
|
||||
{
|
||||
if (str[j] != str[k]) break;
|
||||
j--;
|
||||
k++;
|
||||
}
|
||||
int pos = j + 1;
|
||||
int len = k - j - 1;
|
||||
if (len > newlen)
|
||||
{
|
||||
newlen = len;
|
||||
newpos = pos;
|
||||
// printPartial(str, newpos, newlen);
|
||||
}
|
||||
}
|
||||
position = newpos;
|
||||
length = newlen;
|
||||
return length;
|
||||
}
|
||||
|
||||
|
||||
int findOddPalindrome(const char * str, int &position, int &length)
|
||||
{
|
||||
int sl = strlen(str);
|
||||
if (sl == 0) return -1;
|
||||
|
||||
int newpos = 0;
|
||||
int newlen = 1;
|
||||
for (int i = 1; i < sl; i++)
|
||||
{
|
||||
int j = i - 1;
|
||||
int k = i + 1;
|
||||
while (0 <= j && k < sl)
|
||||
{
|
||||
if (str[j] != str[k]) break;
|
||||
j--;
|
||||
k++;
|
||||
}
|
||||
int pos = j + 1;
|
||||
int len = k - j - 1;
|
||||
if (len > newlen)
|
||||
{
|
||||
newlen = len;
|
||||
newpos = pos;
|
||||
// printPartial(str, newpos, newlen);
|
||||
}
|
||||
}
|
||||
position = newpos;
|
||||
length = newlen;
|
||||
return length;
|
||||
}
|
||||
|
||||
|
||||
void printPartial(char * str, int pos, int len)
|
||||
{
|
||||
Serial.print(pos);
|
||||
Serial.print("\t");
|
||||
Serial.print(len);
|
||||
Serial.print("\t");
|
||||
for (int a = pos; a < pos + len; a++) Serial.print(str[a]);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
15
libraries/palindrome/keywords.txt
Normal file
15
libraries/palindrome/keywords.txt
Normal file
@ -0,0 +1,15 @@
|
||||
# Syntax Colouring Map For palindrome
|
||||
|
||||
# Data types (KEYWORD1)
|
||||
palindrome KEYWORD1
|
||||
|
||||
|
||||
# Methods and Functions (KEYWORD2)
|
||||
KEYWORD2
|
||||
KEYWORD2
|
||||
|
||||
|
||||
# Constants (LITERAL1)
|
||||
|
||||
|
||||
|
23
libraries/palindrome/library.json
Normal file
23
libraries/palindrome/library.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "palindrome",
|
||||
"keywords": "",
|
||||
"description": "palindrome library for Arduino.",
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Rob Tillaart",
|
||||
"email": "Rob.Tillaart@gmail.com",
|
||||
"maintainer": true
|
||||
}
|
||||
],
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/palindrome.git"
|
||||
},
|
||||
"version": "0.1.1",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
"headers": "palindrome.h"
|
||||
}
|
11
libraries/palindrome/library.properties
Normal file
11
libraries/palindrome/library.properties
Normal file
@ -0,0 +1,11 @@
|
||||
name=palindrome
|
||||
version=0.1.1
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Palindrome library
|
||||
paragraph=
|
||||
category=Data Processing
|
||||
url=https://github.com/RobTillaart/palindrome
|
||||
architectures=*
|
||||
includes=palindrome.h
|
||||
depends=
|
153
libraries/palindrome/palindrome.cpp
Normal file
153
libraries/palindrome/palindrome.cpp
Normal file
@ -0,0 +1,153 @@
|
||||
//
|
||||
// FILE: palindrome.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.1
|
||||
// PURPOSE: Arduino library to do palindrome experiments.
|
||||
// URL: https://github.com/RobTillaart/palindrome
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 2021-12-02 initial version
|
||||
// 0.1.1 2021-12-03 add palindromeCount(), palindromePercentage()
|
||||
|
||||
|
||||
|
||||
#include "palindrome.h"
|
||||
|
||||
|
||||
palindrome::palindrome()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool palindrome::isPalindrome(const char * str)
|
||||
{
|
||||
if (str == NULL) return false;
|
||||
int sl = strlen(str);
|
||||
if (sl == 0) return false;
|
||||
|
||||
int j = 0;
|
||||
int k = sl - 1;
|
||||
bool palin = (str[j++] == str[k--]);
|
||||
while (palin & ( j < k ))
|
||||
{
|
||||
palin = (str[j++] == str[k--]);
|
||||
}
|
||||
|
||||
return palin;
|
||||
}
|
||||
|
||||
|
||||
int palindrome::findPalindrome(const char * str, int & position, int & length)
|
||||
{
|
||||
if (str == NULL) return 0;
|
||||
int posOdd = 0, lengthOdd = 0;
|
||||
int posEven = 0, lengthEven = 0;
|
||||
findOddPalindrome(str, posOdd, lengthOdd);
|
||||
findEvenPalindrome(str, posEven, lengthEven);
|
||||
if (lengthEven > lengthOdd)
|
||||
{
|
||||
position = posEven;
|
||||
length = lengthEven;
|
||||
return length;
|
||||
}
|
||||
position = posOdd;
|
||||
length = lengthOdd;
|
||||
return length;
|
||||
}
|
||||
|
||||
|
||||
int palindrome::findEvenPalindrome(const char * str, int & position, int & length)
|
||||
{
|
||||
if (str == NULL) return 0;
|
||||
int sl = strlen(str);
|
||||
if (sl == 0) return -1;
|
||||
|
||||
int newpos = 0;
|
||||
int newlen = 1;
|
||||
for (int i = 0; i < sl; i++)
|
||||
{
|
||||
if (str[i] != str[i + 1]) continue;
|
||||
int j = i - 1;
|
||||
int k = i + 2;
|
||||
while (0 <= j && k < sl)
|
||||
{
|
||||
if (str[j] != str[k]) break;
|
||||
j--;
|
||||
k++;
|
||||
}
|
||||
int pos = j + 1;
|
||||
int len = k - j - 1;
|
||||
if (len > newlen)
|
||||
{
|
||||
newlen = len;
|
||||
newpos = pos;
|
||||
}
|
||||
}
|
||||
position = newpos;
|
||||
length = newlen;
|
||||
return length;
|
||||
}
|
||||
|
||||
|
||||
int palindrome::findOddPalindrome(const char * str, int & position, int & length)
|
||||
{
|
||||
if (str == NULL) return 0;
|
||||
int sl = strlen(str);
|
||||
if (sl == 0) return -1;
|
||||
|
||||
int newpos = 0;
|
||||
int newlen = 1;
|
||||
for (int i = 1; i < sl; i++)
|
||||
{
|
||||
int j = i - 1;
|
||||
int k = i + 1;
|
||||
while (0 <= j && k < sl)
|
||||
{
|
||||
if (str[j] != str[k]) break;
|
||||
j--;
|
||||
k++;
|
||||
}
|
||||
int pos = j + 1;
|
||||
int len = k - j - 1;
|
||||
if (len > newlen)
|
||||
{
|
||||
newlen = len;
|
||||
newpos = pos;
|
||||
}
|
||||
}
|
||||
position = newpos;
|
||||
length = newlen;
|
||||
return length;
|
||||
}
|
||||
|
||||
|
||||
int palindrome::palindromeCount(const char * str)
|
||||
{
|
||||
if (str == NULL) return 0;
|
||||
int sl = strlen(str);
|
||||
if (sl == 0) return 0;
|
||||
|
||||
int j = 0;
|
||||
int k = sl - 1;
|
||||
int count = 0;
|
||||
while (j <= k)
|
||||
{
|
||||
if (str[j++] == str[k--]) count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
float palindrome::palindromePercentage(const char * str)
|
||||
{
|
||||
if (str == NULL) return 0;
|
||||
int sl = strlen(str);
|
||||
if (sl == 0) return 0;
|
||||
if (sl % 2 == 1) sl++; // correction for odd length strings
|
||||
return (200.0 * palindromeCount(str)) / sl;
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
37
libraries/palindrome/palindrome.h
Normal file
37
libraries/palindrome/palindrome.h
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
//
|
||||
// FILE: palindrome.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.1
|
||||
// PURPOSE: Arduino library to do palindrome experiments.
|
||||
// URL: https://github.com/RobTillaart/palindrome
|
||||
//
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#define PALINDROME_LIB_VERSION (F("0.1.1"))
|
||||
|
||||
|
||||
class palindrome
|
||||
{
|
||||
public:
|
||||
palindrome();
|
||||
|
||||
bool isPalindrome(const char * str);
|
||||
|
||||
int findPalindrome(const char * str, int & position, int & length);
|
||||
int findEvenPalindrome(const char * str, int & position, int & length);
|
||||
int findOddPalindrome(const char * str, int & position, int & length);
|
||||
|
||||
int palindromeCount(const char * str);
|
||||
float palindromePercentage(const char * str);
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
56
libraries/palindrome/readme.md
Normal file
56
libraries/palindrome/readme.md
Normal file
@ -0,0 +1,56 @@
|
||||
|
||||
[![Arduino CI](https://github.com/RobTillaart/palindrome/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
||||
[![Arduino-lint](https://github.com/RobTillaart/palindrome/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/palindrome/actions/workflows/arduino-lint.yml)
|
||||
[![JSON check](https://github.com/RobTillaart/palindrome/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/palindrome/actions/workflows/jsoncheck.yml)
|
||||
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/palindrome/blob/master/LICENSE)
|
||||
[![GitHub release](https://img.shields.io/github/release/RobTillaart/palindrome.svg?maxAge=3600)](https://github.com/RobTillaart/palindrome/releases)
|
||||
|
||||
|
||||
# Palindrome
|
||||
|
||||
Library for palindrome search
|
||||
|
||||
|
||||
## Description
|
||||
|
||||
The palindrome library can test if a string is a palindrome and is able to find
|
||||
the longest palindrome within a character string.
|
||||
|
||||
The library is tested with an Arduino UNO, random string of 1600 characters.
|
||||
|
||||
|
||||
## Interface
|
||||
|
||||
- **palindrome()** constructor.
|
||||
- **int findPalindrome(const char \* str, int & position, int & length)** find the longest palindrome in a string. (first occurrence).
|
||||
- **int findEvenPalindrome(const char \* str, int & position, int & length)** find the longest palindrome in a string with even length. (first occurrence).
|
||||
- **int findOddPalindrome(const char \* str, int & position, int & length)** find the longest palindrome in a string with odd length. (first occurrence).
|
||||
- **bool isPalindrome(const char \* str)** checks if a string is a palindrome.
|
||||
- **int palindromeCount(const char \* str)** returns the count of matching pairs in a string. This is at most the ```length / 2 + 1```.
|
||||
- **float palindromePercentage(const char \* str)** returns the count as percentage 0.0 .. 100.0 %
|
||||
|
||||
|
||||
## Operation
|
||||
|
||||
The examples show the basic working of the functions.
|
||||
|
||||
|
||||
## Questions
|
||||
|
||||
- is an empty string a palindrome?
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
- update documentation
|
||||
- should this be a class?
|
||||
- function names?
|
||||
- palindromeCount -- symmetryCount?
|
||||
- palindromePercentage -- symmetryPercentage?
|
||||
- improve algorithms
|
||||
- merge odd / even find algorithms?
|
||||
- investigate Print interface?
|
||||
- investigate palindrome for numbers - radix 2..16 (36?)
|
||||
- investigate case (in)sensitive flag?
|
||||
- investigate ignore spaces flag?
|
||||
|
165
libraries/palindrome/test/unit_test_001.cpp
Normal file
165
libraries/palindrome/test/unit_test_001.cpp
Normal file
@ -0,0 +1,165 @@
|
||||
//
|
||||
// FILE: unit_test_001.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2021-12-02
|
||||
// PURPOSE: unit tests for the palindrome library
|
||||
// https://github.com/RobTillaart/palindrome
|
||||
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
|
||||
//
|
||||
|
||||
// supported assertions
|
||||
// ----------------------------
|
||||
// assertEqual(expected, actual)
|
||||
// assertNotEqual(expected, actual)
|
||||
// assertLess(expected, actual)
|
||||
// assertMore(expected, actual)
|
||||
// assertLessOrEqual(expected, actual)
|
||||
// assertMoreOrEqual(expected, actual)
|
||||
// assertTrue(actual)
|
||||
// assertFalse(actual)
|
||||
// assertNull(actual)
|
||||
|
||||
|
||||
#include <ArduinoUnitTests.h>
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "palindrome.h"
|
||||
|
||||
|
||||
unittest_setup()
|
||||
{
|
||||
}
|
||||
|
||||
unittest_teardown()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
unittest(test_constructor)
|
||||
{
|
||||
fprintf(stderr, "PALINDROME_LIB_VERSION: %s\n", (char *) PALINDROME_LIB_VERSION);
|
||||
|
||||
palindrome pd;
|
||||
int position;
|
||||
int length;
|
||||
char str[1000];
|
||||
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
str[i] = 'a' + random(1);
|
||||
}
|
||||
str[999] = 0;
|
||||
pd.findPalindrome(str, position, length);
|
||||
fprintf(stderr, "POS: %d LEN: %d\n", position, length);
|
||||
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
str[i] = 'a' + random(2);
|
||||
}
|
||||
str[999] = 0;
|
||||
pd.findPalindrome(str, position, length);
|
||||
fprintf(stderr, "POS: %d LEN: %d\n", position, length);
|
||||
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
str[i] = 'a' + random(3);
|
||||
}
|
||||
str[999] = 0;
|
||||
pd.findPalindrome(str, position, length);
|
||||
fprintf(stderr, "POS: %d LEN: %d\n", position, length);
|
||||
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
str[i] = 'a' + random(4);
|
||||
}
|
||||
str[999] = 0;
|
||||
pd.findPalindrome(str, position, length);
|
||||
fprintf(stderr, "POS: %d LEN: %d\n", position, length);
|
||||
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
str[i] = 'a' + random(5);
|
||||
}
|
||||
str[999] = 0;
|
||||
pd.findPalindrome(str, position, length);
|
||||
fprintf(stderr, "POS: %d LEN: %d\n", position, length);
|
||||
}
|
||||
|
||||
|
||||
unittest(test_isPalindrome)
|
||||
{
|
||||
palindrome pd;
|
||||
|
||||
assertFalse(pd.isPalindrome(NULL));
|
||||
assertFalse(pd.isPalindrome(""));
|
||||
assertTrue(pd.isPalindrome("a"));
|
||||
assertTrue(pd.isPalindrome("aa"));
|
||||
assertTrue(pd.isPalindrome("aba"));
|
||||
assertFalse(pd.isPalindrome("aab"));
|
||||
}
|
||||
|
||||
|
||||
unittest(test_count)
|
||||
{
|
||||
palindrome pd;
|
||||
char str[1000];
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
str[i] = 'a' + random(1);
|
||||
}
|
||||
str[999] = 0;
|
||||
int length = pd.palindromeCount(str);
|
||||
float perc = pd.palindromePercentage(str);
|
||||
fprintf(stderr, "LEN: %d PERC: %f\n", length, perc);
|
||||
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
str[i] = 'a' + random(2);
|
||||
}
|
||||
str[999] = 0;
|
||||
length = pd.palindromeCount(str);
|
||||
perc = pd.palindromePercentage(str);
|
||||
fprintf(stderr, "LEN: %d PERC: %f\n", length, perc);
|
||||
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
str[i] = 'a' + random(3);
|
||||
}
|
||||
str[999] = 0;
|
||||
length = pd.palindromeCount(str);
|
||||
perc = pd.palindromePercentage(str);
|
||||
fprintf(stderr, "LEN: %d PERC: %f\n", length, perc);
|
||||
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
str[i] = 'a' + random(4);
|
||||
}
|
||||
str[999] = 0;
|
||||
length = pd.palindromeCount(str);
|
||||
perc = pd.palindromePercentage(str);
|
||||
fprintf(stderr, "LEN: %d PERC: %f\n", length, perc);
|
||||
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
str[i] = 'a' + random(5);
|
||||
}
|
||||
str[999] = 0;
|
||||
length = pd.palindromeCount(str);
|
||||
perc = pd.palindromePercentage(str);
|
||||
fprintf(stderr, "LEN: %d PERC: %f\n", length, perc);
|
||||
}
|
||||
|
||||
|
||||
unittest_main()
|
||||
|
||||
|
||||
// -- END OF FILE --
|
Loading…
Reference in New Issue
Block a user