Skip to content

Commit ac1f4b5

Browse files
committed
readme file modified
2 parents 01d845d + ebd74ab commit ac1f4b5

File tree

3 files changed

+111
-3
lines changed

3 files changed

+111
-3
lines changed

10. Caesar Cipher/CaesarCipher.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const allShifts = [...Array(26).keys()]
2+
const cipher = (str, keys=allShifts) => {
3+
const chars = str.split("")
4+
const res = keys.map( key => {
5+
return chars.map( c => {
6+
const char = c.charCodeAt(0)
7+
if(char > 64 && char < 91)
8+
{
9+
return String.fromCharCode(65 + (char + key) % 65 % 26)
10+
}
11+
else if(char > 96 && char < 123)
12+
{
13+
return String.fromCharCode(97 + (char + key) % 97 % 26)
14+
}
15+
else
16+
{
17+
return String.fromCharCode(char)
18+
}
19+
})
20+
})
21+
22+
return res.map( (r, i) => { return { key: keys[i], result: r.join("") } } )
23+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//LEXICOGRAPHICALLY EQUAL STRINGS CHALLENGE:
2+
3+
/*
4+
Algorithm to find whether the two given strings in text format are lexicographically equal or not.
5+
6+
A lexicographically equal strings are those strings which has the same length and contain the same characters in the same positions.
7+
8+
*/
9+
//code Implementation
10+
// 1. chaining all the functions that are used.
11+
12+
13+
function check_lexicographic(str1,str2){
14+
15+
//Step-1: Convert the strings into the arrays of character using "split()" method.
16+
let arr_str1=str1.split("");
17+
let arr_str2=str2.split("");
18+
19+
//Step-2: Check whether the given strings has same lenght or not by calling "check_length()" function.
20+
let len=check_length(str1,str2);
21+
22+
//Step-3: Check whether the given strings contain the same characters in the same positions or not by calling "check_positions()" function.
23+
let positions=check_positions(arr_str1,arr_str2,len);
24+
25+
//Step-4: Return whether the strings are lexicographically equal or not.
26+
return positions;
27+
}
28+
29+
// 2. finding length of both the strings using lenght property and comparing them .
30+
function check_length(str1,str2){
31+
//Finding lengths.
32+
let len_str1=str1.length;
33+
let len_str2=str2.length;
34+
35+
//Comapring lengths.
36+
if (len_str1===len_str2){
37+
return true;
38+
}
39+
else{
40+
return false;
41+
42+
}
43+
}
44+
45+
// 3. comparing positions of every characters in both the strings.
46+
function check_positions(arr_str1,arr_str2,len){
47+
48+
if(len===true){
49+
50+
let count=0;
51+
//Using for loop.
52+
for(let i=0;i<arr_str1.length;i++){
53+
if (arr_str1[i]===arr_str2[i]){
54+
count++;
55+
}
56+
}
57+
58+
//Checking whether the strings are lexicographically equal or not.
59+
if (count===arr_str1.length){
60+
return true;
61+
}
62+
else{
63+
return false;
64+
}
65+
}
66+
else{
67+
return false;
68+
}
69+
}
70+
71+
//displaying result in the console
72+
console.log(check_lexicographic("javascript","javascript"));

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,8 @@ It's that simple! Hope this helps.
703703
<hr>
704704
<hr>
705705
706+
707+
=======
706708
<b>8. Binary Search Tree </b>
707709
Building, traversing or finding values in Binary Search Trees
708710
@@ -971,13 +973,23 @@ It's very simple ones you realize it.
971973
<hr>
972974
<hr>
973975
974-
<b>10. Name </b>
976+
<b>10. Caesar Cipher </b>
975977
976-
__The challenge:__ <p> </p>
978+
__The challenge:__ <p> Given a string and a shift key, encrypt the string through Caesar Cipher.</p>
977979
980+
__Algorithmic Thinking:__ <p> This is a very simple algorithm that requires only a tiny bit of prerequisite knowledge regarding ASCII, and also some discretion when processing possible over 26 keys. Basically we just need to know that all chracters are stored as numbers in computer memory according to the ASCII standard: https://www.w3schools.com/charsets/ref_html_ascii.asp. And that the modulus function is our friend :)</p>
978981
979-
__Algorithmic Thinking:__ <p> </p>
980982
983+
__code Implementation:__ <p>
984+
985+
So first of all we need to split the string into characters to be processed individually and convert it to ASCII codes. Then we must check whether the character is either uppercase or lowercase(everything else should be kept the same) then add the key to it accordingly. But it is not a simple matter of simply doing ```char + key``` because for example, by shifting X by 3 we should get A. However, X(88) + 3 equals to 91 which is "\[". Thus what we should be doing is:
986+
987+
```js
988+
// js has built in String.charCodeAt() method to help us get the ASCII code
989+
// https://www.w3schools.com/jsref/jsref_charcodeat.asp
990+
// the reverse is String.fromCharCode()
991+
const char = charCodeAt("X")
992+
const key = 3
981993

982994
__code Implementation:__ <p> </p>
983995

@@ -995,3 +1007,4 @@ __Algorithmic Thinking:__ <p> </p>
9951007
__code Implementation:__ <p> </p>
9961008
<hr>
9971009
<hr>
1010+

0 commit comments

Comments
 (0)