Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:CodeToExpress/dailycodebase
- Loading branch information
Showing
20 changed files
with
1,342 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* @author: Karthick < karthikn2099@gmail.com > | ||
* @github: https://github.com/karthikn2098 | ||
* @date: 15/01/2018 | ||
*/ | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
void fizzBuzz1(int N) { | ||
|
||
for( int i = 1 ; i <= N ; i++ ) { | ||
|
||
if( (i % 3 == 0) && (i % 5 == 0) ) { | ||
cout << "FizzBuzz "; | ||
} | ||
else if( i % 3 == 0 ) { | ||
cout << "Fizz "; | ||
} | ||
else if( i % 5 == 0 ) { | ||
cout << "Buzz "; | ||
} | ||
else { | ||
cout << i << " "; | ||
} | ||
|
||
} | ||
} | ||
|
||
void fizzBuzz2(int N) { | ||
|
||
for( int i = 1 ; i <= N ; i++ ) { | ||
|
||
string str = ""; | ||
|
||
if(i % 3 == 0) str += "Fizz"; | ||
|
||
if(i % 5 == 0) str += "Buzz"; | ||
|
||
if(str == "") { | ||
cout << i << " "; | ||
} | ||
else { | ||
cout << str << " "; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
int main(void) { | ||
|
||
int N; | ||
|
||
cout << "Enter N: "; | ||
cin >> N; | ||
|
||
fizzBuzz1(N); | ||
cout << endl << endl; | ||
|
||
fizzBuzz2(N); | ||
cout << endl << endl; | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
--------------------- | ||
Sample Input/Output | ||
--------------------- | ||
Enter N: 25 | ||
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 | ||
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @author: Karthick < karthikn2099@gmail.com > | ||
* @github: https://github.com/karthikn2098 | ||
* @date: 15/01/2018 | ||
*/ | ||
|
||
#include <iostream> | ||
#include <string> | ||
using namespace std; | ||
|
||
//function to reverse the string. | ||
string reverse_string( string str ) { | ||
|
||
int i = 0 , j = str.length() - 1; | ||
|
||
while(i <= j) { | ||
swap(str[i++] , str[j--]); | ||
} | ||
|
||
return str; | ||
} | ||
|
||
//function that checks the string is palindrome or not. | ||
bool checkPalindrome( string str ) { | ||
|
||
int i = 0 , j = str.length() - 1; | ||
|
||
while( i <= j ) { | ||
if( str[i++] != str[j--] ) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
int main(void) { | ||
|
||
cout << "Reverse of abcd: " << reverse_string("abcd"); | ||
|
||
cout << "\n\nReverse of abcde: " << reverse_string("abcde"); | ||
|
||
cout << endl; | ||
|
||
if( checkPalindrome("amma") ) { | ||
cout << "\nPalindrome\n"; | ||
} | ||
else { | ||
cout << "\nNot a palindrome\n"; | ||
} | ||
|
||
|
||
return 0; | ||
} | ||
|
||
/** | ||
Output: | ||
Reverse of abcd: dcba | ||
Reverse of abcde: edcba | ||
Palindrome | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package recursion; | ||
|
||
/** | ||
* @date 09/01/19 | ||
* @author SPREEHA DUTTA | ||
*/ | ||
import java.util.*; | ||
public class PascalTriangle { | ||
public static int pascal(int r,int c) | ||
{ | ||
if(r==c || c==0) | ||
return 1; | ||
else | ||
return pascal(r-1,c)+pascal(r-1,c-1); | ||
} | ||
public static void print(int n) | ||
{ | ||
for(int i=0;i<n;i++) | ||
{ | ||
for(int j=0;j<=i;j++) | ||
System.out.print(pascal(i,j)+" "); | ||
System.out.println(); | ||
} | ||
} | ||
public static void main(String []args) | ||
{ | ||
Scanner sc=new Scanner(System.in); | ||
int n; | ||
n=sc.nextInt(); | ||
print(n); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package recursion; | ||
|
||
/** | ||
* @date 14/01/19 | ||
* @author SPREEHA DUTTA | ||
*/ | ||
import java.util.*; | ||
public class nQueen { | ||
static int n; | ||
public static void print(int arr[][]) | ||
{ | ||
for(int i=0;i<n;i++) | ||
{ | ||
for(int j=0;j<n;j++) | ||
System.out.print(arr[i][j]); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
public static boolean safe(int arr[][],int r,int c) | ||
{ | ||
int i,j; | ||
for(i=0;i<c;i++) | ||
if(arr[r][i]==1) | ||
return false; | ||
for(i=r, j=c; i>=0 && j>=0; i--,j--) | ||
if(arr[i][j]==1) | ||
return false; | ||
for(i=r, j=c; i<n && j>=0; i++,j--) | ||
if(arr[i][j]==1) | ||
return false; | ||
return true; | ||
} | ||
|
||
public static boolean placeQueen(int arr[][],int c) | ||
{ | ||
int i; | ||
if(c>=n) | ||
return true; | ||
for(i=0;i<n;i++) | ||
{ | ||
if(safe(arr,i,c)) | ||
{ | ||
arr[i][c]=1; | ||
if(placeQueen(arr,c+1)==true) | ||
return true; | ||
arr[i][c]=0; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static void main(String []args) | ||
{ | ||
Scanner sc=new Scanner(System.in); | ||
n=sc.nextInt(); | ||
int arr[][]=new int[n][n]; | ||
if(placeQueen(arr,0)==false) | ||
{ | ||
System.out.println("All the queens cannot be placed"); | ||
} | ||
else | ||
print(arr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
""" | ||
@author:aaditkamat | ||
@date: 13/01/2019 | ||
""" | ||
def print_chessboard(chessboard): | ||
for i in range(len(chessboard)): | ||
print(chessboard[i]) | ||
|
||
def fill_chessboard(i, j,chessboard): | ||
chessboard[i][j] = 1 | ||
|
||
for x in range(len(chessboard)): | ||
if x != i: | ||
chessboard[x][j] = 0 | ||
|
||
for y in range(len(chessboard)): | ||
if y != j: | ||
chessboard[i][y] = 0 | ||
|
||
x, y = (i + 1, j + 1) | ||
while x < len(chessboard) and y < len(chessboard): | ||
chessboard[x][y] = 0 | ||
x += 1 | ||
y += 1 | ||
|
||
x, y = (i - 1, j - 1) | ||
while x >= 0 and y >= 0: | ||
chessboard[x][y] = 0 | ||
x -= 1 | ||
y -= 1 | ||
|
||
x, y = (i + 1, j - 1) | ||
while x < len(chessboard) and y >= 0: | ||
chessboard[x][y] = 0 | ||
x += 1 | ||
y -= 1 | ||
|
||
x, y = (i - 1, j + 1) | ||
while x >= 0 and y < len(chessboard): | ||
chessboard[x][y] = 0 | ||
x -= 1 | ||
y += 1 | ||
|
||
def try_position(i, chessboard): | ||
fill_chessboard(0, i, chessboard) | ||
|
||
for i in range(1, len(chessboard)): | ||
for j in range(len(chessboard)): | ||
if chessboard[i][j] == -1: | ||
fill_chessboard(i, j, chessboard) | ||
|
||
def reset_chessboard(chessboard): | ||
for i in range(len(chessboard)): | ||
for j in range(len(chessboard)): | ||
chessboard[i][j] = -1 | ||
|
||
def is_correct(chessboard): | ||
for i in range(len(chessboard)): | ||
if chessboard[i].count(1) == 0: | ||
return False | ||
return True | ||
|
||
def n_queens(num): | ||
chessboard = [] | ||
for i in range(num): | ||
chessboard.append([-1] * num) | ||
|
||
for i in range(num): | ||
try_position(i, chessboard) | ||
if (is_correct(chessboard)): | ||
print_chessboard(chessboard) | ||
return | ||
reset_chessboard(chessboard) | ||
|
||
def main(): | ||
print("Enter number of queens: ", end="") | ||
num = int(input()) | ||
n_queens(num) | ||
|
||
main() |
Oops, something went wrong.