Skip to content

Commit 7af0d8f

Browse files
committed
mass commit for the cpp impl. of homeworks
1 parent cbfa37e commit 7af0d8f

7 files changed

+74
-18
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/out

README

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
21
Introduction to Algorithms to Sumeyya

is_palindrome.py

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ def is_palindrome(string):
1515
has_visited_odd = True
1616
return True
1717

18-
19-
2018
def main():
2119
string = "TactCoa"
2220
isp = is_palindrome(string)

is_permutation.cpp

+4-12
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,19 @@ bool isPermutationHT(string str1,string str2){
2323

2424
char* cstr2 = (char*) str2.c_str();
2525
for(int i=0; i<str2.length(); i++){
26-
27-
if(ht[cstr2[i]] != 0){
28-
ht[cstr2[i]] = ht[cstr2[i]] - 1;
26+
ht[cstr2[i]] = ht[cstr2[i]] - 1;
2927

3028
if(ht[cstr2[i]] < 0){
3129
return false;
3230
}
33-
}
34-
35-
else {
36-
return false;
37-
}
38-
39-
return true;
4031
}
32+
return true;
4133
}
4234

4335
int main(int argc, char **argv){
4436

45-
string string1 = "sumyeya";
46-
string string2 = "suymeya";
37+
string string1 = "syyueya";
38+
string string2 = "suyyeya";
4739

4840
cout << isPermutationHT(string1,string2) << endl;
4941

one_way.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
12
def one_way(string1, string2):
23
l1 = len(string1)
34
l2 = len(string2)
4-
if abs(l1 - l2) > 1:
5+
if abs(l1 - l2) > 1:
56
return False
67
has_operated = False
78
i = 0
@@ -20,8 +21,8 @@ def one_way(string1, string2):
2021
return True
2122

2223
def main():
23-
string1 = "pales"
24-
string2 = "pale"
24+
string1 = "pale"
25+
string2 = "paske"
2526
iow = one_way(string1, string2)
2627
print(iow)
2728

rotate_matrix.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
bool rotate(int **matrix,int len){
6+
7+
if(len == 0){
8+
//((matrix.sizeof())/(matrix[0].sizeof()))){
9+
return false;
10+
}
11+
12+
for(int layer=0; layer<len/2; layer++){
13+
cout << "layer " << layer << endl;
14+
int first=layer;
15+
int last=len-1-layer;
16+
17+
for(int i=first; i<last; i++){
18+
int offset = i-first;
19+
int temp;
20+
printf("offset %d\n", offset);
21+
22+
//keep the top
23+
temp = matrix[first][i];
24+
//left to top;
25+
matrix[first][i] = matrix[last-offset][first];
26+
//bottom to left
27+
matrix[last-offset][first] = matrix[last][last-offset];
28+
//right to bottom
29+
matrix[last][last-offset] = matrix[i][last];
30+
//temp to right
31+
matrix[i][last] = temp;
32+
}
33+
}
34+
return true;
35+
}
36+
37+
int main(){
38+
39+
int **matrix;
40+
int len = 4;
41+
matrix = new int *[len];
42+
for (int i = 0; i < len; i++) {
43+
matrix[i] = new int[len];
44+
for (int j = 0; j < len; j++) {
45+
matrix[i][j] = i * len + j;
46+
}
47+
}
48+
for (int i = 0; i < len; i++) {
49+
for (int j = 0; j < len; j++) {
50+
cout << matrix[i][j] << "\t";
51+
}
52+
cout << endl;
53+
}
54+
rotate(matrix,len);
55+
56+
for (int i = 0; i < len; i++) {
57+
for (int j = 0; j < len; j++) {
58+
cout << matrix[i][j] << "\t";
59+
}
60+
cout << endl;
61+
}
62+
63+
return 0;
64+
}

rotate_matrix.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rotate_matrix.py

0 commit comments

Comments
 (0)