Skip to content

Commit

Permalink
Add Day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Dec 27, 2018
1 parent 5487f08 commit 4223c74
Show file tree
Hide file tree
Showing 25 changed files with 595 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Day1/README.md
@@ -1,3 +1,5 @@
![Fizz Buzz](./cover.png)

# Day 1 -- The Fizz Buzz Challenge

**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"
Expand All @@ -7,7 +9,7 @@
**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.

![Fizz Buzz](./cover.png)
![ques](./ques.png)

## JavaScript Implementation

Expand Down
4 changes: 4 additions & 0 deletions Day2/README.md
@@ -1,9 +1,13 @@
![cover](./cover.png)

# Day 2 -- String Reversal and Palindrome

## Part A -- String Reversal

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

![ques](./ques.png)

## JavaScript Implementation

### [Solution 1](./JavaScript/sol1.js)
Expand Down
4 changes: 3 additions & 1 deletion Day3/README.md
@@ -1,3 +1,5 @@
![Hamming](./cover.png)

# Day 3 -- The Hamming Distance Problem

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

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

![Hamming](./cover.png)
![ques](./ques.png)

**Example**

Expand Down
Binary file added day1/ques.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added day2/ques.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added day3/ques.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions day4/README.md
@@ -1,11 +1,13 @@
# Day 4 -- Number of Vowels and Max Chars

![cover](./cover.png)

# Day 4 -- Number of Vowels and Max Chars

## Part A -- Number of Vowels

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

![ques](./ques.png)

## JavaScript Implementation

### [Solution 1](./JavaScript/partA_sol1.js)
Expand Down
Binary file added day4/ques.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions day5/README.md
@@ -1,10 +1,10 @@
![Patterns](./cover.png)

# Day 5 -- Patterns

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 😀

![Patterns](./cover.png)

Here's the questions for today 😁
Here's the questions for today

### Pattern 1

Expand Down Expand Up @@ -117,6 +117,8 @@ input = 5
* *
```

![ques](./ques.png)

## Pattern 1

```
Expand Down
Binary file added day5/ques.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions day6/Java/SentenceCap1.java
@@ -0,0 +1,23 @@
/**
* @author MadhavBahlMD
* @date 27/12/2018
*/

import java.util.Scanner;

public class SentenceCap1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the sentence: ");
String str = input.nextLine();

String[] words = str.split("\\s+");

for (int i=0; i<words.length; i++) {
words[i] = Character.toUpperCase(words[i].charAt(0)) + words[i].substring(1, words[i].length());
}

// Join the array
System.out.print(String.join(" ", words));
}
}
29 changes: 29 additions & 0 deletions day6/Java/SentenceCap2.java
@@ -0,0 +1,29 @@
/**
* @author MadhavBahlMD
* @date 27/12/2018
*/

import java.util.Scanner;

public class SentenceCap2 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter the sentence: ");
String sentence = input.nextLine();
String capitalized = "";

for (int i=0; i<sentence.length(); i++) {
if (i==0) capitalized += Character.toUpperCase(sentence.charAt(i));
else {
if (sentence.charAt(i-1) == ' ') {
capitalized += Character.toUpperCase(sentence.charAt(i));
} else {
capitalized += sentence.charAt(i);
}
}
}

// Print the results
System.out.println("Capitalized String is: " + capitalized);
}
}
29 changes: 29 additions & 0 deletions day6/Java/WordRev.java
@@ -0,0 +1,29 @@
/**
* @author MadhavBahlMD
* @date 27/12/2018
*/

import java.util.Scanner;
import java.lang.*;

public class WordRev {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter the sentence: ");
String sentence = input.nextLine();

String[] words = sentence.split("\\s+");
String reversed;

for (int i=0; i<words.length; i++) {
reversed = "";
for (int j=0; j<words[i].length(); j++) {
reversed = words[i].charAt(j) + reversed;
}
words[i] = reversed;
}

String wordsReversed = String.join(" ", words);
System.out.println("String with reversed words: " + wordsReversed);
}
}
31 changes: 31 additions & 0 deletions day6/JavaScript/anagram1.js
@@ -0,0 +1,31 @@
/**
* @author MadhavBahlMD
* @date 27/12/2018
* METHOD -- Check the lengths of both strings, sort them and then check whether they are same
*/

function anagram (str1, str2) {
let len1 = str1.length,
len2 = str2.length;

// Compare lengths
if (len1 !== len2) {
console.log ('Invalid Input');
return -1;
}

// sort the strings
let sortedStr1 = str1.split('').sort().join(''),
sortedStr2 = str2.split('').sort().join('');

// Compare both strings
if (sortedStr1 === sortedStr2) {
console.log(`"${str1}" and "${str2}" are Anagrams`);
return 1;
} else {
console.log(`"${str1}" and "${str2}" are not Anagrams`);
return 0;
}
}

anagram ('LISTEN', 'SILENT');
42 changes: 42 additions & 0 deletions day6/JavaScript/anagram2.js
@@ -0,0 +1,42 @@
/**
* @author MadhavBahlMD
* @date 27/12/2018
* METHOD -- Prepare 2 objects which stores frequency of every character in both strings, compare those 2 objects (dictionaries in python)
*/

function anagram (str1, str2) {
let len1 = str1.length,
len2 = str2.length;

// Compare lengths
if (len1 !== len2) {
console.log ('Invalid Input');
return -1;
}

// Make frequency objects
let countObj1 = {},
countObj2 = {};

for (let i=0; i<len1; i++) {
countObj1[str1[i]] = countObj1[str1[i]] + 1 || 1;
}

for (let i=0; i<len2; i++) {
countObj2[str2[i]] = countObj2[str2[i]] + 1 || 1;
}

// compare frequency objects
// Please note that there is no direct way of comparing 2 objects.
// 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 :)
for (let key in countObj1) {
if (countObj1[key] !== countObj2[key]) {
console.log(`"${str1}" and "${str2}" are not Anagrams`);
return 0;
}
}

console.log(`"${str1}" and "${str2}" are Anagrams`);
}

anagram ('LISTEN', 'MILENT');
49 changes: 49 additions & 0 deletions day6/JavaScript/anagram3.js
@@ -0,0 +1,49 @@
/**
* @author MadhavBahlMD
* @date 27/12/2018
* 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
* Please note that this is not at all an efficient method. Do not use this.
*/

function anagram (str1, str2) {
let len1 = str1.length,
len2 = str2.length;

// Lengths of both strings must be same
if (len1 !== len2) {
console.log ('Invalid Input');
return -1;
}

// check characters of string 1 are there in string 2
let flag = 1;
for (let char of str1) {
if (str2.indexOf(char) < 0) {
flag = 0;
break;
}
}

if (flag !== 1) {
console.log (`${str1} and ${str2} are not Anagrams`);
return 0;
}

for (let char of str2) {
if (str1.indexOf(char) < 0) {
flag = 0;
break;
}
}

if (flag !== 1) {
console.log (`${str1} and ${str2} are not Anagrams`);
return 0;
}
else {
console.log (`${str1} and ${str2} are Anagrams`);
return 1;
}
}

anagram ('LISTEN', 'SILENT');
1 change: 1 addition & 0 deletions day6/JavaScript/sentenceCap1.js
@@ -0,0 +1 @@

Empty file added day6/JavaScript/sentenceCap2.js
Empty file.
Empty file added day6/JavaScript/sentenceCap3.js
Empty file.
Empty file added day6/JavaScript/wordRev1.js
Empty file.
Empty file added day6/JavaScript/wordRev2.js
Empty file.
49 changes: 49 additions & 0 deletions day6/JavaScript/wordRev3.js
@@ -0,0 +1,49 @@
/**
* @author MadhavBahlMD
* @date 27/12/2018
*/

// Without inbuilt reverse or split, a straightforward method

function wordRev (line) {
// iterate through the string and add each word in a new array (splitting it on white space)
let words = [], count = 0, word = '';

for (let i=0; i<line.length; i++) {
if (line[i] !== ' ') {
word += line[i];
} else {
words[count] = word;
word = '';
count++;
}
}
// Add the last word as well to the words array as well
words[count] = word;
count++;

// Reverse the words
let temp;
for (let i=0; i<count; i++) {
temp = '';
for (let j=words[i].length-1; j>=0; j--) {
temp += words[i][j];
}
words[i] = temp;
}

// join the elements (Ok, let's not use the traditional join() method -_-)
let reversed = '';
for (let i=0; i<count; i++) {
if (i!=count-1) {
reversed += words[i] + ' ';
} else {
reversed += words[i];
}
}

// print the result
return reversed;
}

console.log(wordRev ('hello world greetings'));

0 comments on commit 4223c74

Please sign in to comment.