10000 [ADD] CeasersCipher and palindromeChecker added · luisprooc/js-algorithms@2184820 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2184820

Browse files
committed
[ADD] CeasersCipher and palindromeChecker added
1 parent 0bf834a commit 2184820

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,4 +785,72 @@ orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]) should return [{name: "
785785

786786
orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}]) should return [{name : "iss", orbitalPeriod: 5557}, {name: "hubble", orbitalPeriod: 5734}, {name: "moon", orbitalPeriod: 2377399}].
787787

788+
```
789+
790+
791+
## Caesars Cipher
792+
793+
One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the meanings of the letters are shifted by some set amount.
794+
795+
A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus A ↔ N, B ↔ O and so on.
796+
797+
Write a function which takes a ROT13 encoded string as input and returns a decoded string.
798+
799+
All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.
800+
801+
802+
803+
```javascript
804+
rot13("SERR PBQR PNZC") should decode to the string FREE CODE CAMP
805+
806+
rot13("SERR CVMMN!") should decode to the string FREE PIZZA!
807+
808+
rot13("SERR YBIR?") should decode to the string FREE LOVE?
809+
810+
rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.") should decode to the string THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
811+
812+
```
813+
814+
815+
## Palindrome Checker
816+
817+
Return true if the given string is a palindrome. Otherwise, return false.
818+
819+
A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
820+
821+
Note: You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into the same case (lower or upper case) in order to check for palindromes.
822+
823+
We'll pass strings with varying formats, such as racecar, RaceCar, and race CAR among others.
824+
825+
We'll also pass strings with special symbols, such as 2A3*3a2, 2A3 3a2, and 2_A3*3#A2.
826+
827+
828+
829+
```javascript
830+
palindrome("eye") should return a boolean.
831+
832+
palindrome("eye") should return true.
833+
834+
palindrome("_eye") should return true.
835+
836+
palindrome("race car") should return true.
837+
838+
palindrome("not a palindrome") should return false.
839+
840+
palindrome("A man, a plan, a canal. Panama") should return true.
841+
842+
palindrome("never odd or even") should return true.
843+
844+
palindrome("nope") should return false.
845+
846+
palindrome("almostomla") should return false.
847+
848+
palindrome("My age is 0, 0 si ega ym.") should return true.
849+
850+
palindrome("1 eye for of 1 eye.") should return false.
851+
852+
palindrome("0_0 (: /-\ :) 0-0") should return true.
853+
854+
palindrome("five|\_/|four") should return false.
855+
788856
```

src/ceasersCipher.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function rot13(str) {
2+
let encodedStr = "";
3+
for(let letter in str){
4+
if(!/[\s|!|.|?]/.test(str[letter])){
5+
if(str[letter].charCodeAt() < 78){
6+
encodedStr += String.fromCharCode(str[letter].charCodeAt() + 13);
7+
}
8+
9+
else{
10+
encodedStr += String.fromCharCode(str[letter].charCodeAt() - 13);
11+
}
12+
}
13+
14+
else{
15+
encodedStr += str[letter];
16+
}
17+
18+
}
19+
return encodedStr;
20+
}
21+
22+
console.log(rot13("SERR PBQR PNZC"));

src/palindromeChecker.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function palindrome(str) {
2+
let check = ""
3+
str = str.toLowerCase().replace(/[\W_]/g,"");
4+
for(let i = str.length -1 ; i >= 0; --i){
5+
check+= str[i];
6+
}
7+
if(check === str){
8+
return true;
9+
}
10+
11+
return false;
12+
}
13+
14+
15+
16+
console.log(palindrome("eye"));

src/steamroller.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function flatten(arr){
2+
let newArr = []
3+
if(!arr.length){
4+
return newArr;
5+
}
6+
7+
else if(typeof(arr) !== "object"){
8+
newArr.push(arr);
9+
return newArr + flatten(arr) + 1;
10+
}
11+
12+
else{
13+
return flatten(arr.splice(1))
14+
}
15+
}
16+
17+
function steamrollArray(arr) {
18+
let newArr = [];
19+
for(let i = 0; i < arr.length; ++i){
20+
if(arr[i].length && typeof(arr[i]) !== "string"){
21+
newArr.push(arr[i]);
22+
}
23+
else{
24+
console.log(flatten(arr));
25+
}
26+
}
27+
return newArr;
28+
}
29+
30+
console.log(flatten([3, [[4]]]));
31+
//console.log(steamrollArray([1,[2], [3, [[4]]]]));

0 commit comments

Comments
 (0)
0