-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecond.txt
198 lines (162 loc) · 7.01 KB
/
second.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
describe("Reverse String test suit", () => {
it("should reverse the provided string", () => {
const input = "The quick brown fox";
const output = "fox brown quick The";
expect(reverseString(input)).to.be.equal(output);
})
it("should return a single word correctly", () => {
expect(reverseString("Quick")).to.be.equal("Quick");
})
it("should return empty string in case of non-string or empty input", () => {
expect(reverseString(123)).to.be.equal("");
expect(reverseString("")).to.be.equal("");
})
it("should work properly for special characters", () => {
const input = "Hello-world @mohitbhandari45"
const output = "@mohitbhandari45 Hello-world"
expect(reverseString(input)).to.be.equal(output);
})
it('should reverse a string with multiple spaces correctly', () => {
const input = reverseString(' hello world ');
expect(input).to.equal(' world hello ');
})
})
/* String Distances */
describe("Hamming Distance", () => {
it("should return correct output for different inputs", () => {
expect(hammingDistance("karolin", "kathrin")).to.be.equal(3);
expect(hammingDistance("Mohit", "Rawat")).to.be.equal(4);
})
it("should return correct output for number strings", () => {
expect(hammingDistance("1011101", "1001001")).to.equal(2);
expect(hammingDistance("1011101", "1101011")).to.equal(4);
})
it("should return 0 for identical strings", () => {
expect(hammingDistance("same", "same")).to.be.equal(0);
})
it("should throw an error for different length strings", () => {
expect(() => hammingDistance("hello", "hell")).to.throw("Strings must be of equal length");
})
})
describe("Levenstein Distance", () => {
it("should return 0 when both strings are empty", () => {
expect(levenshtein("", "")).to.be.equal(0);
})
it("should return the length of the other string if any one of them is empty", () => {
expect(levenshtein("str", "")).to.be.equal(3);
expect(levenshtein("", "strsd")).to.be.equal(5);
})
it("should give the distance", () => {
expect(levenshtein("horse", "ros")).to.be.equal(3);
expect(levenshtein("kitten", "sitting")).to.equal(3);
expect(levenshtein("flaw", "lawn")).to.equal(2);
expect(levenshtein("intention", "execution")).to.equal(5);
})
it("should return correct distance for completely different strings", () => {
expect(levenshtein("xyz", "abd")).to.be.equal(3);
})
it("should handle case sensitive information", () => {
expect(levenshtein("ans", "ANS")).to.be.equal(3);
})
it("should handle spaces properly", () => {
expect(levenshtein("hello world", "hello")).to.be.equal(6);
})
})
describe('Is Anagram check', () => {
it("should return true for valid anagrams", () => {
expect(isAnagram("listen", "silent")).to.be.equal(true);
expect(isAnagram("triangle", "integral")).to.be.equal(true);
expect(isAnagram("a", "a")).to.be.equal(true);
expect(isAnagram("Tom Marvolo Riddle", "I am Lord Voldemort")).to.be.equal(true);
expect(isAnagram("elbow", "below")).to.be.equal(true);
})
it("should return false for invalid anagrams", () => {
expect(isAnagram("listen", "silence")).to.be.equal(false);
expect(isAnagram("hello", "world")).to.be.equal(false);
expect(isAnagram("aaa", "aaaa")).to.be.equal(false);
})
it("should handle edge cases", () => {
expect(isAnagram("", "")).to.be.equal(true);
expect(isAnagram("a", "")).to.be.equal(false);
expect(isAnagram("", "a")).to.be.equal(false);
expect(isAnagram("123", "321")).to.be.equal(true);
expect(isAnagram("aBc", "CbA")).to.be.equal(true);
})
// it("should handle special characters", () => {
// expect(isAnagram("!@#", "#!@")).to.be.equal(true);
// expect(isAnagram("an@gram", "nag@ram")).to.be.equal(true);
// expect(isAnagram("anagram!", "nagaram")).to.be.equal(false);
// expect(isAnagram("T@om Ma!rvolo R!ddle", "I a!m Lord V@oldemort")).to.be.equal(true);
// })
})
describe('RLE', () => {
it("should compress a string properly with repeated character", () => {
const input = "AAAABBBCCDAA";
const expected = "4A3B2C1D2A";
expect(compressString(input)).to.be.equal(expected);
})
it("should handle empty strings", () => {
expect(compressString("")).to.equal("");
})
it("should handle single character string", () => {
expect(compressString("BBBBBB")).to.be.equal("6B");
})
it("should handle all unique strings", () => {
expect(compressString("ABC")).to.be.equal("1A1B1C");
})
it("should compress a string with different runs of characters", () => {
const input = "AABBBCCCCDDDDD";
const expected = "2A3B4C5D";
expect(compressString(input)).to.equal(expected);
});
// it("should handle numeric characters", () => {
// const input = "111223333444";
// const expected = "3122333444";
// expect(compressString(input)).to.equal(expected);
// });
it("should handle special characters", () => {
const input = "!!!@@@###";
const expected = "3!3@3#";
expect(compressString(input)).to.equal(expected);
})
it("should compress a string with a long single run", () => {
const input = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
const expected = "27Z";
expect(compressString(input)).to.equal(expected);
})
it("should handle mixed case characters", () => {
const input = "aaBBccDD";
const expected = "2a2B2c2D";
expect(compressString(input)).to.equal(expected);
})
it("should compress a string with a single character", () => {
const input = "a";
const expected = "1a";
expect(compressString(input)).to.equal(expected);
})
// it("should handle spaces in the string", () => {
// const input = " AA B ";
// const expected = "4 1A2 1B3 ";
// expect(compressString(input)).to.equal(expected);
// });
it("should handle alternating characters correctly", () => {
const input = "ABABAB";
const expected = "1A1B1A1B1A1B";
expect(compressString(input)).to.equal(expected);
});
it("should compress a large string", () => {
const input = "X".repeat(1000) + "Y".repeat(2000);
const expected = "1000X2000Y";
expect(compressString(input)).to.equal(expected);
});
it("should handle varying runs of a single character", () => {
const input = "AAAABBBBCCCC";
const expected = "4A4B4C";
expect(compressString(input)).to.equal(expected);
})
// it("should compress a string with mixed case, numbers, and special characters", () => {
// const input = "a1!a1!B2@B2@";
// const expected = "1a11!1a11!1B21@1B21@";
// expect(compressString(input)).to.equal(expected);
// });
})