Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 38 C++ implementation #246

Closed
wants to merge 128 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
248bea9
Add @aaditkamat as a contributor
Dec 21, 2018
85abd67
Add Ruby code for Day 1: FizzBuzz problem
Dec 21, 2018
0c80762
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 22, 2018
76cf6c5
Add Ruby code for Day 2: String reverse problem
Dec 22, 2018
39210a0
Update README.md for Day 2
Dec 22, 2018
64a0ca2
Modify Ruby code and README
Dec 22, 2018
ab743a6
Add condition for nil and wrong type edge cases
Dec 22, 2018
48e33c7
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 22, 2018
bdeefe5
Add a seperate Ruby source code file for palindrome
Dec 22, 2018
fc41b6a
Modify code for reverse.rb
Dec 22, 2018
784ee39
Add seperate palindrome and reverse code sections in README
Dec 22, 2018
b24fa8c
Update gitignore
Dec 23, 2018
b40d361
Refactor palindrome.rb and rename heading in README
Dec 23, 2018
763683a
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 24, 2018
5f1a01d
Add solution for Day 3: Hamming Problem
Dec 24, 2018
8d37216
Add condition for strings of unequal lengths
Dec 24, 2018
3183f2d
Update README
Dec 24, 2018
08c9ca1
Change project name and owner in.all-contributorsrc
Dec 25, 2018
6350c3f
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 25, 2018
af34b2a
Remove merge conflict lines
Dec 25, 2018
e07c5f1
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 25, 2018
c3d05d6
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 25, 2018
6db4527
Add @shivank86 as a contributor
Dec 25, 2018
3667ef7
Merge remote-tracking branch 'upstream/master'
Dec 26, 2018
6ff55a0
Merge branch 'master' of github.com:aaditkamat/dailycodebase
Dec 26, 2018
e4ea3ad
Add C++ files for different patterns
Dec 26, 2018
a46eeab
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 26, 2018
b74a9e5
Add author and date comments at the top of C++ files
Dec 26, 2018
0d16771
Update README.md
Dec 26, 2018
a0879ef
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 26, 2018
8bf3f5b
Merge branch 'master' into master
Dec 27, 2018
f0527fc
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 27, 2018
543d167
Add solution for Day 6 Problem in Python
Dec 27, 2018
9d7d81f
Update README
Dec 27, 2018
46dbf7b
Refactor code files
Dec 27, 2018
cc40fc8
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 27, 2018
1134c2d
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 27, 2018
bb3ab0c
Modify string representation of output in python files
Dec 27, 2018
d96703e
Add Ruby solutions for Day 6 problem
Dec 27, 2018
c2c91da
Update README for Ruby code
Dec 27, 2018
ceadf27
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 28, 2018
c1cbccb
Merge branch 'master' of github.com:aaditkamat/dailycodebase
Dec 28, 2018
2e855d2
Add first version of solutions for Day 7 problem in C++, Java & Ruby
Dec 28, 2018
d9a1e8c
Modify solutions
Dec 28, 2018
5f16c38
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 28, 2018
5499b34
Update Day 7 README
Dec 28, 2018
96b645f
Remove merge conflicts from CONTRIBUTORS.md
Dec 28, 2018
815d24e
Add back removed lines in CONTRIBUTORS.md
Dec 28, 2018
12443c2
Merge branch 'master' into master
Dec 29, 2018
0d95ea5
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 29, 2018
1e45c12
Merge branch 'master' of github.com:aaditkamat/dailycodebase
Dec 29, 2018
38724d2
Add code sections contributed by @imkaka to day 6 README
Dec 29, 2018
0cc23ba
Update README.md
MadhavBahl Dec 29, 2018
5c74b48
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 31, 2018
2f5c30d
Merge branch 'master' of github.com:aaditkamat/dailycodebase
Dec 31, 2018
3ddedb4
Add C++ solution
Dec 31, 2018
b84a4d4
Add Day 8 solution in C++
Dec 31, 2018
26f256c
Add Day 8 solution in Java
Dec 31, 2018
905596d
Add Day 8 solution in Ruby
Dec 31, 2018
a457d3c
Add Day 8 solution in Python
Dec 31, 2018
873cc51
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Dec 31, 2018
f1a18c3
Add credits at the top of the code
Dec 31, 2018
a283abf
Update README
Dec 31, 2018
486b459
Update C++ implementation
Dec 31, 2018
c4c0f9d
Update Python implementation
Dec 31, 2018
ef2afe7
Merge branch 'master' into master
Jan 1, 2019
35a928f
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Jan 1, 2019
f5b8355
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Jan 2, 2019
23f8eea
Merge remote-tracking branch 'origin/master'
Jan 2, 2019
61b941f
Add solution for Day 10: String Permutation Problem in Python
Jan 2, 2019
cee4779
Update Day 10 README
Jan 2, 2019
1e7be81
Merge branch 'master' into master
MadhavBahl Jan 2, 2019
08da228
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Jan 3, 2019
daf1625
Merge remote-tracking branch 'origin/master'
Jan 3, 2019
f97ce93
Change heading in README and remove empty directory in day 9
Jan 3, 2019
a0f88dc
Merge pull request #1 from CodeToExpress/master
Jan 3, 2019
f1e7f00
Update README.md
MadhavBahl Jan 3, 2019
2811166
Merge branch 'master' into master
MadhavBahl Jan 3, 2019
537062c
Merge master branch of CodeToExpress/dailycodebase into master branch…
Jan 4, 2019
2636dc6
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Jan 7, 2019
b3ee288
Add Ruby solutions for Day 13
Jan 7, 2019
dc8fe63
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Jan 7, 2019
8df29df
Update README
Jan 7, 2019
e9c2cef
Add credits to the code
Jan 7, 2019
dcc1b87
Add Python solution for Day 13
Jan 7, 2019
4ce5d73
Update Day 13 README
Jan 7, 2019
38e180b
Update fibonacci.py
Jan 7, 2019
2213514
Modify Fibonacci python code section in day 13 README
Jan 7, 2019
0deaaf4
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Jan 8, 2019
5e8b5d5
Merge remote-tracking branch 'origin/master'
Jan 8, 2019
3eae5c2
Merge branch 'master' of github.com:CodeToExpress/dailycodebase
Jan 10, 2019
84c49f5
Merge branch 'master' of github.com:CodeToExpress/dailycodebase
Jan 11, 2019
5758d99
Add Ruby solution to Day 16 problem
Jan 11, 2019
3cf81e5
Update Day 16 README with Ruby implementation
Jan 11, 2019
d4e98e9
Change wording of print statement
Jan 11, 2019
ca423b9
Merge branch 'master' into master
MadhavBahl Jan 12, 2019
6011f87
Merge branch 'master' of github.com:CodeToExpress/dailycodebase
Jan 13, 2019
0b34d25
Merge branch 'master' of github.com:CodeToExpress/dailycodebase
Jan 13, 2019
438887f
Add Python solution to Day 17 problem
Jan 13, 2019
54f6d4f
Add Python implementation to Day 17 README
Jan 13, 2019
0eee5ba
Merge remote-tracking branch 'origin/master'
Jan 13, 2019
ab956d9
Merge branch 'master' of github.com:CodeToExpress/dailycodebase
Jan 15, 2019
1ab5ae7
Merge pull request #4 from CodeToExpress/master
Jan 15, 2019
d0a9d5d
Merge pull request #5 from aaditkamat/day-18
Jan 15, 2019
d93291b
Add solution to Day 18 Question A: Frequency Counter
Jan 15, 2019
45c5082
Merge branch 'master' into master
MadhavBahl Jan 15, 2019
bb7a8bd
Add solution to Day 18 Question B: Count Uniques
Jan 15, 2019
7eebd06
Add solution to Day 18 Question C: Check Power N
Jan 15, 2019
79988b9
Merge branch 'master' of https://github.com/CodeToExpress/dailycodeba…
Jan 15, 2019
37af7b0
Update day 18 README with personal Python solution
Jan 15, 2019
a660086
Update check_power_n.py
Jan 15, 2019
bc1b5bb
Update count_uniques.py
Jan 15, 2019
7be2582
Merge branch 'day-18'
Jan 15, 2019
fd7580f
Merge branch 'master' of github.com:aaditkamat/dailycodebase
Jan 15, 2019
a97173b
Add Python implementation for Day 19 and update Day 18 solutions
Jan 15, 2019
b012425
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Jan 16, 2019
8af374c
Add Ruby solution for Day 20 problem
Jan 16, 2019
2d7c8d2
Update Day 20 README with Ruby solution
Jan 16, 2019
19a8a06
Merge branch 'master' of github.com:CodeToExpress/dailycodebase
Jan 17, 2019
3d374aa
Merge branch 'master' of https://github.com/CodeToExpress/dailycodebase
Jan 28, 2019
0d6908a
Add C++ solution and test cases for day 28 problem
Jan 28, 2019
5ab5a04
Add credits and brief explanation
Jan 28, 2019
7c610ea
Add C++ solution to day 28 README
Jan 28, 2019
57bd470
Merge branch 'master' of github.com:CodeToExpress/dailycodebase
Feb 8, 2019
2c0cb76
Merge branch 'master' of github.com:aaditkamat/dailycodebase
Feb 8, 2019
a9bff19
Merge branch 'master' of github.com:CodeToExpress/dailycodebase into …
Feb 11, 2019
9e970ac
Add code for day 38 in C++
Feb 12, 2019
cec4190
Update Day 38 README with the C++ implementation
Feb 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 81 additions & 81 deletions day20/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Write a program to divide the given array into sub arrays where each sub array i
```
input: partition([1,2,3,4,5,6,7,8], 2)
output: [[1, 2], [3, 4], [5, 6], [7, 8]]

input: partition([1,2,3,4,5,6,7], 2)
output: [[1, 2], [3, 4], [5, 6], [7]]
```
Expand All @@ -25,18 +25,18 @@ A very simple solution, all we have to is loop through i=0 to array length. In e

```js
function partition (array, size) {
let partitionedArray = [],
toBeAdded = [];

for (let i=0; i<array.length; i++) {
if ((i)%size === 0) {
if (toBeAdded.length > 0) partitionedArray.push (toBeAdded);
toBeAdded = [];
}
toBeAdded.push (array[i]);
}
if (toBeAdded.length > 0) partitionedArray.push (toBeAdded);
return partitionedArray;
let partitionedArray = [],
toBeAdded = [];

for (let i=0; i<array.length; i++) {
if ((i)%size === 0) {
if (toBeAdded.length > 0) partitionedArray.push (toBeAdded);
toBeAdded = [];
}
toBeAdded.push (array[i]);
}
if (toBeAdded.length > 0) partitionedArray.push (toBeAdded);
return partitionedArray;
}
```

Expand All @@ -46,18 +46,18 @@ The second solution is also similar to the first one. Here, we create an empty a

```js
function partition (array, size) {
let partitionedArray = [];

for (let element of array) {
let partition = partitionedArray[partitionedArray.length - 1];
if (!partition || partition.length === size) {
partitionedArray.push ([element]);
} else {
lastEle.push (element);
}
}
let partitionedArray = [];

for (let element of array) {
let partition = partitionedArray[partitionedArray.length - 1];
if (!partition || partition.length === size) {
partitionedArray.push ([element]);
} else {
lastEle.push (element);
}
}

return partitionedArray;
return partitionedArray;
}
```

Expand All @@ -78,12 +78,12 @@ array.sllice (startIndex, endIndex); will return a subarray of array including e

```js
function partition (array, size) {
let partitionedArray = [], i=0;
while (i<array.length) {
partitionedArray.push (array.slice(i, i+size));
i += size;
}
return partitionedArray;
let partitionedArray = [], i=0;
while (i<array.length) {
partitionedArray.push (array.slice(i, i+size));
i += size;
}
return partitionedArray;
}
```

Expand Down Expand Up @@ -129,34 +129,34 @@ main

```java
/**
* @date 16/01/19
* @author SPREEHA DUTTA
*/
* @date 16/01/19
* @author SPREEHA DUTTA
*/
import java.util.*;
public class Partitions {
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int n,p,i,j;int k=0;
System.out.println("Enter size of original array");
n=sc.nextInt();
int arr[]=new int[n];
for(i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println("Enter size of partition");
p=sc.nextInt();
for(i=0;i<arr.length;i=i+p)
{
int part[]=Arrays.copyOfRange(arr,i,i+p);
k++;
if(arr.length%2!=0 && k==(arr.length/p+1))
p=arr.length%p;
System.out.print("{ ");
for(j=0;j<p;j++)
System.out.print(part[j]+" ");
System.out.print("}\n");
}
}
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int n,p,i,j;int k=0;
System.out.println("Enter size of original array");
n=sc.nextInt();
int arr[]=new int[n];
for(i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println("Enter size of partition");
p=sc.nextInt();
for(i=0;i<arr.length;i=i+p)
{
int part[]=Arrays.copyOfRange(arr,i,i+p);
k++;
if(arr.length%2!=0 && k==(arr.length/p+1))
p=arr.length%p;
System.out.print("{ ");
for(j=0;j<p;j++)
System.out.print(part[j]+" ");
System.out.print("}\n");
}
}
}
```

Expand All @@ -168,33 +168,33 @@ public class Partitions {

```cpp
/**
* @author divyakhetan
* @date 16/1/2019
*/
* @author divyakhetan
* @date 16/1/2019
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
cout << "Enter length of 1st and 2nd array";
int n1, n2;
cin >> n1 >> n2;
int a[n1];
int b[n2];
for(int i = 0; i < n1; i++){
cin >> a[i];
}
for(int i = 0; i < n2; i++){
cin >> b[i];
}
for(int i = 0; i < n1; i++){
for(int j = 0; j < n2; j++){
cout << "[ " << a[i] << " " << b[j] << " ] " << endl;
}
}
return 0;
}
```
cout << "Enter length of 1st and 2nd array";
int n1, n2;
cin >> n1 >> n2;
int a[n1];
int b[n2];
for(int i = 0; i < n1; i++){
cin >> a[i];

}

for(int i = 0; i < n2; i++){
cin >> b[i];
}

for(int i = 0; i < n1; i++){
for(int j = 0; j < n2; j++){
cout << "[ " << a[i] << " " << b[j] << " ] " << endl;
}
}
return 0;
}
```
1 change: 1 addition & 0 deletions day28/C++/Aadit/sample001.ans
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
2 changes: 2 additions & 0 deletions day28/C++/Aadit/sample001.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2
1 2 3 4 5
1 change: 1 addition & 0 deletions day28/C++/Aadit/sample002.ans
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
undefined
2 changes: 2 additions & 0 deletions day28/C++/Aadit/sample002.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
7
1 2 3 4 5
31 changes: 31 additions & 0 deletions day28/C++/Aadit/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<iostream>

/**
* @author: aaditkamat
* @date: 28/1/2019
*/

/*
* The input is assumed to be of the following form:
* The first line contains the number to be searched
* The second line contains the numbers to be searched seperated by spaces.
*/
int main() {
int num;
std::cin >> num;
int ctr = 0;
bool flag = false;
while (!std::cin.eof()) {
int value;
std::cin >> value;
if (value == num) {
std::cout << ctr << std::endl;
flag = true;
}
ctr++;
}
if (!flag) {
std::cout << "undefined" << std::endl;
}
return 0;
}
18 changes: 18 additions & 0 deletions day28/C++/Aadit/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

clear
g++ solution.cpp -std=gnu++17

if [ $? -eq 0 ]; then
DIR=rationalsequence2

for i in {1..2}
do
./a.out < ${DIR}/sample00${i}.in > ${DIR}/sample00${i}.o && diff ${DIR}/sample00${i}.o ${DIR}/sample00${i}.ans
open ${DIR}/sample00${i}.o
done
fi




78 changes: 37 additions & 41 deletions day29/C++/Binary_Search.cpp
Original file line number Diff line number Diff line change
@@ -1,59 +1,55 @@
/**
* @author: Karthick < karthikn2099@gmail.com >
* @github: https://github.com/karthikn2098
* @date: 31/01/2018
*/

#include <iostream>
using namespace std;

/** @desc: splits the array into two sections at each step, then checks the element in the desired one.
* @param: array , key (to be found), size of the array.
* @return: index of key if present , -1 otherwise.
* @TimeComplexity: O( logn ) < n = size of the array >
*/
int Binary_search(int arr[] , int key, int N) {
/*
* @author : imkaka
* @date : 31/1/2019
*/

int low = 0 , high = N-1 , mid;
#include<iostream>
#include<cstdio>

while(low <= high) {
using namespace std;

mid = low + (high - low) / 2; //like (low + high)/2 but efficient.
int binary_search_itr(int arr[], int size, int val){
int l = 0, r = size-1;
int mid = (l + r) / 2;

if(key == arr[mid]) {
return mid;
}
else if( key > arr[mid] ) {
low = mid + 1;
while(arr[mid] != val && l <= r){
if(val < arr[mid]){
r = mid - 1;
}
else {
high = mid - 1;
else{
l = mid +1;
}

mid = (l + r) / 2;
}

if(arr[mid] == val)
return mid;

return -1;
}

int main(void) {

int arr[] = { 1, 3, 5, 7, 8, 9, 100, 234, 788, 899 };
int binary_search_rec(int arr[], int left, int right, int val){
if(right >= left){

int sizeOfArray = sizeof(arr) / sizeof(arr[0]);
int mid = left + (right - left) / 2;

cout << "\nBinary_search(arr , 100) = " << Binary_search(arr, 100, sizeOfArray ) << endl;
if(arr[mid] == val) return mid;

cout << "\nBinary_search(arr , 1000) = " << Binary_search(arr, 1000, sizeOfArray ) << endl;
if(arr[mid] > val)
return binary_search_rec(arr, left, mid-1, val);
return binary_search_rec(arr, mid+1, right, val);
}

return 0;
return -1;
}

/*
---------------
Sample Output
---------------

Binary_search(arr , 100) = 6
int main(){

Binary_search(arr , 1000) = -1

*/
int arr[] = {1, 2, 3, 6, 9, 15, 16, 14};
int val = 9;
int size = sizeof(arr)/sizeof(arr[0]);
cout << "Iterative: " << binary_search_itr(arr, size, val) << endl;
cout << "Recursive: " << binary_search_rec(arr, 0, size-1, val) << endl;
return 0;
}
Loading