Skip to content

Commit 4223c74

Browse files
committed
Add Day 6
1 parent 5487f08 commit 4223c74

25 files changed

+595
-7
lines changed

Day1/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
![Fizz Buzz](./cover.png)
2+
13
# Day 1 -- The Fizz Buzz Challenge
24

35
**Brief History** - Fizz Buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word "fizz", and any number divisible by five with the word "buzz" and any number divisible by both three and five is replaced with the word "fizzbuzz"
@@ -7,7 +9,7 @@
79
810
**Question**- Write a program that prints the numbers from 1 to n and for multiples of '3' print "Fizz" instead of the number, for the multiples of '5' print "Buzz", and for the numbers which are divisible by both 3 and 5, print FizzBuzz.
911

10-
![Fizz Buzz](./cover.png)
12+
![ques](./ques.png)
1113

1214
## JavaScript Implementation
1315

Day2/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
![cover](./cover.png)
2+
13
# Day 2 -- String Reversal and Palindrome
24

35
## Part A -- String Reversal
46

57
**Question** - Given a string, write a program to return a new string with reversed order of characters.
68

9+
![ques](./ques.png)
10+
711
## JavaScript Implementation
812

913
### [Solution 1](./JavaScript/sol1.js)

Day3/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
![Hamming](./cover.png)
2+
13
# Day 3 -- The Hamming Distance Problem
24

35
The Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different.
@@ -8,7 +10,7 @@ Read more about Hamming Distance [here…](https://en.wikipedia.org/wiki/Hamming
810

911
**Question**- Given 2 strings, we will find the number of positions at which the corresponding characters are different.
1012

11-
![Hamming](./cover.png)
13+
![ques](./ques.png)
1214

1315
**Example**
1416

day1/ques.png

892 KB
Loading

day2/ques.png

913 KB
Loading

day3/ques.png

1.03 MB
Loading

day4/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# Day 4 -- Number of Vowels and Max Chars
2-
31
![cover](./cover.png)
42

3+
# Day 4 -- Number of Vowels and Max Chars
4+
55
## Part A -- Number of Vowels
66

77
**Question** - Given a string, Write a program that prints the number of vowels in it.
88

9+
![ques](./ques.png)
10+
911
## JavaScript Implementation
1012

1113
### [Solution 1](./JavaScript/partA_sol1.js)

day4/ques.png

610 KB
Loading

day5/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
![Patterns](./cover.png)
2+
13
# Day 5 -- Patterns
24

35
Pattern programs like printing a pyramid, inverted pyramid etc. are some very famous problems and a good way to test your logic and knowledge of nested loops. Hence, on the day 5 of Daily Codes, let’s do some pattern based programs 😀
46

5-
![Patterns](./cover.png)
6-
7-
Here's the questions for today 😁
7+
Here's the questions for today
88

99
### Pattern 1
1010

@@ -117,6 +117,8 @@ input = 5
117117
* *
118118
```
119119

120+
![ques](./ques.png)
121+
120122
## Pattern 1
121123

122124
```

day5/ques.png

1.12 MB
Loading

day6/Java/SentenceCap1.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 27/12/2018
4+
*/
5+
6+
import java.util.Scanner;
7+
8+
public class SentenceCap1 {
9+
public static void main(String[] args) {
10+
Scanner input = new Scanner(System.in);
11+
System.out.print("Enter the sentence: ");
12+
String str = input.nextLine();
13+
14+
String[] words = str.split("\\s+");
15+
16+
for (int i=0; i<words.length; i++) {
17+
words[i] = Character.toUpperCase(words[i].charAt(0)) + words[i].substring(1, words[i].length());
18+
}
19+
20+
// Join the array
21+
System.out.print(String.join(" ", words));
22+
}
23+
}

day6/Java/SentenceCap2.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 27/12/2018
4+
*/
5+
6+
import java.util.Scanner;
7+
8+
public class SentenceCap2 {
9+
public static void main(String[] args) {
10+
Scanner input = new Scanner (System.in);
11+
System.out.print("Enter the sentence: ");
12+
String sentence = input.nextLine();
13+
String capitalized = "";
14+
15+
for (int i=0; i<sentence.length(); i++) {
16+
if (i==0) capitalized += Character.toUpperCase(sentence.charAt(i));
17+
else {
18+
if (sentence.charAt(i-1) == ' ') {
19+
capitalized += Character.toUpperCase(sentence.charAt(i));
20+
} else {
21+
capitalized += sentence.charAt(i);
22+
}
23+
}
24+
}
25+
26+
// Print the results
27+
System.out.println("Capitalized String is: " + capitalized);
28+
}
29+
}

day6/Java/WordRev.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 27/12/2018
4+
*/
5+
6+
import java.util.Scanner;
7+
import java.lang.*;
8+
9+
public class WordRev {
10+
public static void main(String[] args) {
11+
Scanner input = new Scanner (System.in);
12+
System.out.print("Enter the sentence: ");
13+
String sentence = input.nextLine();
14+
15+
String[] words = sentence.split("\\s+");
16+
String reversed;
17+
18+
for (int i=0; i<words.length; i++) {
19+
reversed = "";
20+
for (int j=0; j<words[i].length(); j++) {
21+
reversed = words[i].charAt(j) + reversed;
22+
}
23+
words[i] = reversed;
24+
}
25+
26+
String wordsReversed = String.join(" ", words);
27+
System.out.println("String with reversed words: " + wordsReversed);
28+
}
29+
}

day6/JavaScript/anagram1.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 27/12/2018
4+
* METHOD -- Check the lengths of both strings, sort them and then check whether they are same
5+
*/
6+
7+
function anagram (str1, str2) {
8+
let len1 = str1.length,
9+
len2 = str2.length;
10+
11+
// Compare lengths
12+
if (len1 !== len2) {
13+
console.log ('Invalid Input');
14+
return -1;
15+
}
16+
17+
// sort the strings
18+
let sortedStr1 = str1.split('').sort().join(''),
19+
sortedStr2 = str2.split('').sort().join('');
20+
21+
// Compare both strings
22+
if (sortedStr1 === sortedStr2) {
23+
console.log(`"${str1}" and "${str2}" are Anagrams`);
24+
return 1;
25+
} else {
26+
console.log(`"${str1}" and "${str2}" are not Anagrams`);
27+
return 0;
28+
}
29+
}
30+
31+
anagram ('LISTEN', 'SILENT');

day6/JavaScript/anagram2.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 27/12/2018
4+
* METHOD -- Prepare 2 objects which stores frequency of every character in both strings, compare those 2 objects (dictionaries in python)
5+
*/
6+
7+
function anagram (str1, str2) {
8+
let len1 = str1.length,
9+
len2 = str2.length;
10+
11+
// Compare lengths
12+
if (len1 !== len2) {
13+
console.log ('Invalid Input');
14+
return -1;
15+
}
16+
17+
// Make frequency objects
18+
let countObj1 = {},
19+
countObj2 = {};
20+
21+
for (let i=0; i<len1; i++) {
22+
countObj1[str1[i]] = countObj1[str1[i]] + 1 || 1;
23+
}
24+
25+
for (let i=0; i<len2; i++) {
26+
countObj2[str2[i]] = countObj2[str2[i]] + 1 || 1;
27+
}
28+
29+
// compare frequency objects
30+
// Please note that there is no direct way of comparing 2 objects.
31+
// We can either use some librries like Lowdash, or we can check the equality of each key value pair in objects, which is indeed a tedious task, but still, lets do it :)
32+
for (let key in countObj1) {
33+
if (countObj1[key] !== countObj2[key]) {
34+
console.log(`"${str1}" and "${str2}" are not Anagrams`);
35+
return 0;
36+
}
37+
}
38+
39+
console.log(`"${str1}" and "${str2}" are Anagrams`);
40+
}
41+
42+
anagram ('LISTEN', 'MILENT');

day6/JavaScript/anagram3.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 27/12/2018
4+
* A simple method which first compares the lengths of strings and then iterates through the characters of any string and check whether it exists in the other one as well and does same for the other string
5+
* Please note that this is not at all an efficient method. Do not use this.
6+
*/
7+
8+
function anagram (str1, str2) {
9+
let len1 = str1.length,
10+
len2 = str2.length;
11+
12+
// Lengths of both strings must be same
13+
if (len1 !== len2) {
14+
console.log ('Invalid Input');
15+
return -1;
16+
}
17+
18+
// check characters of string 1 are there in string 2
19+
let flag = 1;
20+
for (let char of str1) {
21+
if (str2.indexOf(char) < 0) {
22+
flag = 0;
23+
break;
24+
}
25+
}
26+
27+
if (flag !== 1) {
28+
console.log (`${str1} and ${str2} are not Anagrams`);
29+
return 0;
30+
}
31+
32+
for (let char of str2) {
33+
if (str1.indexOf(char) < 0) {
34+
flag = 0;
35+
break;
36+
}
37+
}
38+
39+
if (flag !== 1) {
40+
console.log (`${str1} and ${str2} are not Anagrams`);
41+
return 0;
42+
}
43+
else {
44+
console.log (`${str1} and ${str2} are Anagrams`);
45+
return 1;
46+
}
47+
}
48+
49+
anagram ('LISTEN', 'SILENT');

day6/JavaScript/sentenceCap1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

day6/JavaScript/sentenceCap2.js

Whitespace-only changes.

day6/JavaScript/sentenceCap3.js

Whitespace-only changes.

day6/JavaScript/wordRev1.js

Whitespace-only changes.

day6/JavaScript/wordRev2.js

Whitespace-only changes.

day6/JavaScript/wordRev3.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 27/12/2018
4+
*/
5+
6+
// Without inbuilt reverse or split, a straightforward method
7+
8+
function wordRev (line) {
9+
// iterate through the string and add each word in a new array (splitting it on white space)
10+
let words = [], count = 0, word = '';
11+
12+
for (let i=0; i<line.length; i++) {
13+
if (line[i] !== ' ') {
14+
word += line[i];
15+
} else {
16+
words[count] = word;
17+
word = '';
18+
count++;
19+
}
20+
}
21+
// Add the last word as well to the words array as well
22+
words[count] = word;
23+
count++;
24+
25+
// Reverse the words
26+
let temp;
27+
for (let i=0; i<count; i++) {
28+
temp = '';
29+
for (let j=words[i].length-1; j>=0; j--) {
30+
temp += words[i][j];
31+
}
32+
words[i] = temp;
33+
}
34+
35+
// join the elements (Ok, let's not use the traditional join() method -_-)
36+
let reversed = '';
37+
for (let i=0; i<count; i++) {
38+
if (i!=count-1) {
39+
reversed += words[i] + ' ';
40+
} else {
41+
reversed += words[i];
42+
}
43+
}
44+
45+
// print the result
46+
return reversed;
47+
}
48+
49+
console.log(wordRev ('hello world greetings'));

0 commit comments

Comments
 (0)