Skip to content

Commit 62e46e2

Browse files
committed
Merge branch 'master' of github.com:CodeToExpress/dailycodebase
2 parents 88e5a92 + 0ce465a commit 62e46e2

20 files changed

+1342
-0
lines changed

Day1/C++/FizzBuzz_Program.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @author: Karthick < karthikn2099@gmail.com >
3+
* @github: https://github.com/karthikn2098
4+
* @date: 15/01/2018
5+
*/
6+
7+
#include <iostream>
8+
using namespace std;
9+
10+
void fizzBuzz1(int N) {
11+
12+
for( int i = 1 ; i <= N ; i++ ) {
13+
14+
if( (i % 3 == 0) && (i % 5 == 0) ) {
15+
cout << "FizzBuzz ";
16+
}
17+
else if( i % 3 == 0 ) {
18+
cout << "Fizz ";
19+
}
20+
else if( i % 5 == 0 ) {
21+
cout << "Buzz ";
22+
}
23+
else {
24+
cout << i << " ";
25+
}
26+
27+
}
28+
}
29+
30+
void fizzBuzz2(int N) {
31+
32+
for( int i = 1 ; i <= N ; i++ ) {
33+
34+
string str = "";
35+
36+
if(i % 3 == 0) str += "Fizz";
37+
38+
if(i % 5 == 0) str += "Buzz";
39+
40+
if(str == "") {
41+
cout << i << " ";
42+
}
43+
else {
44+
cout << str << " ";
45+
}
46+
47+
}
48+
49+
}
50+
51+
int main(void) {
52+
53+
int N;
54+
55+
cout << "Enter N: ";
56+
cin >> N;
57+
58+
fizzBuzz1(N);
59+
cout << endl << endl;
60+
61+
fizzBuzz2(N);
62+
cout << endl << endl;
63+
64+
return 0;
65+
}
66+
67+
/**
68+
---------------------
69+
Sample Input/Output
70+
---------------------
71+
72+
Enter N: 25
73+
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz
74+
75+
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz
76+
77+
*/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @author: Karthick < karthikn2099@gmail.com >
3+
* @github: https://github.com/karthikn2098
4+
* @date: 15/01/2018
5+
*/
6+
7+
#include <iostream>
8+
#include <string>
9+
using namespace std;
10+
11+
//function to reverse the string.
12+
string reverse_string( string str ) {
13+
14+
int i = 0 , j = str.length() - 1;
15+
16+
while(i <= j) {
17+
swap(str[i++] , str[j--]);
18+
}
19+
20+
return str;
21+
}
22+
23+
//function that checks the string is palindrome or not.
24+
bool checkPalindrome( string str ) {
25+
26+
int i = 0 , j = str.length() - 1;
27+
28+
while( i <= j ) {
29+
if( str[i++] != str[j--] ) {
30+
return false;
31+
}
32+
}
33+
34+
return true;
35+
}
36+
37+
38+
int main(void) {
39+
40+
cout << "Reverse of abcd: " << reverse_string("abcd");
41+
42+
cout << "\n\nReverse of abcde: " << reverse_string("abcde");
43+
44+
cout << endl;
45+
46+
if( checkPalindrome("amma") ) {
47+
cout << "\nPalindrome\n";
48+
}
49+
else {
50+
cout << "\nNot a palindrome\n";
51+
}
52+
53+
54+
return 0;
55+
}
56+
57+
/**
58+
Output:
59+
60+
Reverse of abcd: dcba
61+
62+
Reverse of abcde: edcba
63+
64+
Palindrome
65+
66+
*/

day15/Java/PascalTriangle.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package recursion;
7+
8+
/**
9+
* @date 09/01/19
10+
* @author SPREEHA DUTTA
11+
*/
12+
import java.util.*;
13+
public class PascalTriangle {
14+
public static int pascal(int r,int c)
15+
{
16+
if(r==c || c==0)
17+
return 1;
18+
else
19+
return pascal(r-1,c)+pascal(r-1,c-1);
20+
}
21+
public static void print(int n)
22+
{
23+
for(int i=0;i<n;i++)
24+
{
25+
for(int j=0;j<=i;j++)
26+
System.out.print(pascal(i,j)+" ");
27+
System.out.println();
28+
}
29+
}
30+
public static void main(String []args)
31+
{
32+
Scanner sc=new Scanner(System.in);
33+
int n;
34+
n=sc.nextInt();
35+
print(n);
36+
}
37+
}

day15/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,41 @@ def main():
216216
print(f'Pascal triangle cannot have {num} rows')
217217

218218
main()
219+
```
220+
221+
## Java Implementation
222+
223+
### [Solution](./Java/PascalTriangle.java)
219224

225+
```java
226+
/**
227+
* @date 09/01/19
228+
* @author SPREEHA DUTTA
229+
*/
230+
import java.util.*;
231+
public class PascalTriangle {
232+
public static int pascal(int r,int c)
233+
{
234+
if(r==c || c==0)
235+
return 1;
236+
else
237+
return pascal(r-1,c)+pascal(r-1,c-1);
238+
}
239+
public static void print(int n)
240+
{
241+
for(int i=0;i<n;i++)
242+
{
243+
for(int j=0;j<=i;j++)
244+
System.out.print(pascal(i,j)+" ");
245+
System.out.println();
246+
}
247+
}
248+
public static void main(String []args)
249+
{
250+
Scanner sc=new Scanner(System.in);
251+
int n;
252+
n=sc.nextInt();
253+
print(n);
254+
}
255+
}
220256
```

day17/Java/nQueen.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package recursion;
7+
8+
/**
9+
* @date 14/01/19
10+
* @author SPREEHA DUTTA
11+
*/
12+
import java.util.*;
13+
public class nQueen {
14+
static int n;
15+
public static void print(int arr[][])
16+
{
17+
for(int i=0;i<n;i++)
18+
{
19+
for(int j=0;j<n;j++)
20+
System.out.print(arr[i][j]);
21+
System.out.println();
22+
}
23+
}
24+
25+
public static boolean safe(int arr[][],int r,int c)
26+
{
27+
int i,j;
28+
for(i=0;i<c;i++)
29+
if(arr[r][i]==1)
30+
return false;
31+
for(i=r, j=c; i>=0 && j>=0; i--,j--)
32+
if(arr[i][j]==1)
33+
return false;
34+
for(i=r, j=c; i<n && j>=0; i++,j--)
35+
if(arr[i][j]==1)
36+
return false;
37+
return true;
38+
}
39+
40+
public static boolean placeQueen(int arr[][],int c)
41+
{
42+
int i;
43+
if(c>=n)
44+
return true;
45+
for(i=0;i<n;i++)
46+
{
47+
if(safe(arr,i,c))
48+
{
49+
arr[i][c]=1;
50+
if(placeQueen(arr,c+1)==true)
51+
return true;
52+
arr[i][c]=0;
53+
}
54+
}
55+
return false;
56+
}
57+
58+
public static void main(String []args)
59+
{
60+
Scanner sc=new Scanner(System.in);
61+
n=sc.nextInt();
62+
int arr[][]=new int[n][n];
63+
if(placeQueen(arr,0)==false)
64+
{
65+
System.out.println("All the queens cannot be placed");
66+
}
67+
else
68+
print(arr);
69+
}
70+
}

day17/Python/n_queens.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""
2+
@author:aaditkamat
3+
@date: 13/01/2019
4+
"""
5+
def print_chessboard(chessboard):
6+
for i in range(len(chessboard)):
7+
print(chessboard[i])
8+
9+
def fill_chessboard(i, j,chessboard):
10+
chessboard[i][j] = 1
11+
12+
for x in range(len(chessboard)):
13+
if x != i:
14+
chessboard[x][j] = 0
15+
16+
for y in range(len(chessboard)):
17+
if y != j:
18+
chessboard[i][y] = 0
19+
20+
x, y = (i + 1, j + 1)
21+
while x < len(chessboard) and y < len(chessboard):
22+
chessboard[x][y] = 0
23+
x += 1
24+
y += 1
25+
26+
x, y = (i - 1, j - 1)
27+
while x >= 0 and y >= 0:
28+
chessboard[x][y] = 0
29+
x -= 1
30+
y -= 1
31+
32+
x, y = (i + 1, j - 1)
33+
while x < len(chessboard) and y >= 0:
34+
chessboard[x][y] = 0
35+
x += 1
36+
y -= 1
37+
38+
x, y = (i - 1, j + 1)
39+
while x >= 0 and y < len(chessboard):
40+
chessboard[x][y] = 0
41+
x -= 1
42+
y += 1
43+
44+
def try_position(i, chessboard):
45+
fill_chessboard(0, i, chessboard)
46+
47+
for i in range(1, len(chessboard)):
48+
for j in range(len(chessboard)):
49+
if chessboard[i][j] == -1:
50+
fill_chessboard(i, j, chessboard)
51+
52+
def reset_chessboard(chessboard):
53+
for i in range(len(chessboard)):
54+
for j in range(len(chessboard)):
55+
chessboard[i][j] = -1
56+
57+
def is_correct(chessboard):
58+
for i in range(len(chessboard)):
59+
if chessboard[i].count(1) == 0:
60+
return False
61+
return True
62+
63+
def n_queens(num):
64+
chessboard = []
65+
for i in range(num):
66+
chessboard.append([-1] * num)
67+
68+
for i in range(num):
69+
try_position(i, chessboard)
70+
if (is_correct(chessboard)):
71+
print_chessboard(chessboard)
72+
return
73+
reset_chessboard(chessboard)
74+
75+
def main():
76+
print("Enter number of queens: ", end="")
77+
num = int(input())
78+
n_queens(num)
79+
80+
main()

0 commit comments

Comments
 (0)