Skip to content

Commit

Permalink
Add day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Dec 25, 2018
1 parent 3a97e50 commit 05e36a6
Show file tree
Hide file tree
Showing 11 changed files with 531 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -20,6 +20,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
1. [Day 1 -- The Fizz Buzz Problem](./Day1/) -- [http://codetoexpress.tech/dc/day1](http://codetoexpress.tech/dc/day1)
2. [Day 2 -- String Reversal + Palindrome](./Day2/) -- [http://codetoexpress.tech/dc/day2/](http://codetoexpress.tech/dc/day2)
3. [Day 3 -- The Hamming Distance Problem](./Day3/) -- [http://codetoexpress.tech/dc/day3/](http://codetoexpress.tech/dc/day3/)
4. [Day 4 -- Num Vowels + Max Chars](./day4/) -- [http://codetoexpress.tech/dc/day4/](http://codetoexpress.tech/dc/day4/)

## Timeline

Expand Down
33 changes: 33 additions & 0 deletions day4/Java/NumVowels.java
@@ -0,0 +1,33 @@
import java.util.Scanner;

public class NumVowels {
public static void main (String[] args) {
// set count = 0
int count = 0;

// Input the string
Scanner input = new Scanner(System.in);
System.out.println("/* ===== Number of Vowels ===== */");
System.out.print("\nEnter the string: ");
String str = input.next();

// Convert input string to lower case
str = str.toLowerCase();

// Run a loop from 0 to string length
for (int i=0; i<str.length(); i++) {
if (
str.charAt(i) == 'a' ||
str.charAt(i) == 'e' ||
str.charAt(i) == 'i' ||
str.charAt(i) == 'o' ||
str.charAt(i) == 'u'
) {
count++;
}
}

// Print the result
System.out.println("Number of vowels in \"" + str + "\" = " + count);
}
}
34 changes: 34 additions & 0 deletions day4/Java/NumVowels2.java
@@ -0,0 +1,34 @@
import java.util.Scanner;

public class NumVowels2 {
public static void main (String[] args) {
// set count = 0
int count = 0;

// Input the string
Scanner input = new Scanner(System.in);
System.out.println("/* ===== Number of Vowels ===== */");
System.out.print("\nEnter the string: ");
String str = input.next();

// Convert input string to lower case
str = str.toLowerCase();

// Create an array of vowels
char[] vowels = new char[]{ 'a', 'e', 'i', 'o', 'u' };

// Run a loop from 0 to string length
for (int i=0; i<str.length(); i++) {
// Check whether the current character exists in the vowels array
for (int j=0; j<5; j++) {
if (str.charAt(i) == vowels[j]) {
count++;
break;
}
}
}

// Print the result
System.out.println("Number of vowels in \"" + str + "\" = " + count);
}
}
25 changes: 25 additions & 0 deletions day4/JavaScript/partA_sol1.js
@@ -0,0 +1,25 @@
// Step 1: Set count = 0
// Step 2: Run a loop from i=0 to i=string_length
// Step 3: In each iteration check whether the character at position i is ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’. If the condition passes, increment the value of count.
// Step 4: Print and return the results.

function numVowels (str) {
let count = 0;

for (let i=0; i<str.length; i++) {
if (
str[i].toLowerCase() === 'a' ||
str[i].toLowerCase() === 'e' ||
str[i].toLowerCase() === 'i' ||
str[i].toLowerCase() === 'o' ||
str[i].toLowerCase() === 'u'
) {
count++;
}
}

console.log (`The number of vowels in ${str} is = ${count}`);
}

numVowels ('hello');
numVowels ('Greetings');
23 changes: 23 additions & 0 deletions day4/JavaScript/partA_sol2.js
@@ -0,0 +1,23 @@
// Step 1: Set count = 0
// Step 2: Store the vowels in an array (say, vowels)
// Step 3: Run a loop from i=0 to i=string_length
// Step 4: In each iteration check whether the current character (at position i) of the string is there in the vowels array using indexOf() method
// Step 5: Increment the count if the above condition is satisfied.
// Step 6: Print the result

function numVowels (str) {
let count = 0;
let vowels = ['a', 'e', 'i', 'o', 'u'];

// Iterate over each character in the string
for (let i=0; i<str.length; i++) {
if (vowels.indexOf(str[i].toLowerCase()) > 0) {
count++;
}
}

console.log (`The number of vowels in ${str} is = ${count}`);
}

numVowels ('hello');
numVowels ('Greetings');
22 changes: 22 additions & 0 deletions day4/JavaScript/partA_sol3.js
@@ -0,0 +1,22 @@
// Step 1: Set count = 0
// Step 2: Store the vowels in an array (say, vowels)
// Step 3: Run a loop from i=0 to i=string_length
// Step 4: In each iteration, run a nested loop from j=0 to j<5 (5 vowels), and check whether string[i] exists in vowels[j].
// Step 5: Increment the count if the above condition is satisfied.
// Step 6: Print the result

function numVowels (str) {
let count = 0;
let vowels = ['a', 'e', 'i', 'o', 'u'];

for (let char of str) {
for (let vowel of vowels) {
if (char.toLowerCase() === vowel) count++;
}
}

console.log (`The number of vowels in ${str} is = ${count}`);
}

numVowels ('hello');
numVowels ('Greetings');
36 changes: 36 additions & 0 deletions day4/JavaScript/partA_sol4.js
@@ -0,0 +1,36 @@
// Step 1: Set count = 0
// Step 2: Store the vowels in an object (vowels) with initial values as zero
// Step 3: Iterate through each character of string and check whether it exists in the vowels object
// Step 4: If it exists, increment its value, and the value of count variable
// Step 5: Print the result

function numVowels (str) {
let count = 0;
let vowels = {
'a': 0,
'e': 0,
'i': 0,
'o': 0,
'u': 0
};

for (let char of str) {
// if (vowels[char]) {
// vowels [char]++;
// count++;
// }

for (vowel in vowels) {
if (char.toLowerCase() === vowel) {
count++;
vowels[vowel]++
}
}
}

console.log (`The number of vowels in ${str} is = ${count}`);

}

numVowels ('hello');
numVowels ('Greetings');
37 changes: 37 additions & 0 deletions day4/JavaScript/partB_sol1.js
@@ -0,0 +1,37 @@
// Step 1: Declare an empty object letterCount which will store characters as keys and their frequencies as values.
// Step 2: Iterate through each character of input string and add it to the object if it doesn't already exist, else increment its’s value
// Step 3: Find the key with maximum value in the letterCount object.

function maxChars (str) {
let letterCount = {};

// Complete the letter count object
for(let currentChar of str) {
let flag = 0;
for (letter in letterCount) {
if (currentChar.toLowerCase() === letter) {
flag = 1;
letterCount[letter]++;
}
}

if (flag === 0) {
letterCount[currentChar] = 1;
}
}

// Find the maximum value key from the letterCount
let max = 0, maxChar = '';
for (letter in letterCount) {
if (letterCount[letter] >= max) {
max = letterCount[letter];
maxChar = letter;
}
}

// Print the result
console.log(`The maximum frequency letter is: "${maxChar}" and it appears ${letterCount[maxChar]} number of times!`);
return letterCount[maxChar];
}

maxChars('helllllo worlld');
27 changes: 27 additions & 0 deletions day4/JavaScript/partB_sol2.js
@@ -0,0 +1,27 @@
// Answer similar to first solution, but Step 2 has been replaced by a one liner

function maxChars (sentence) {
let characters = {};

// Update characters object with count of each character in sentence
for (let letter of sentence) {
// increment the count of existing letter OR add the new character with value = 1
characters[letter] = characters[letter] + 1 || 1;
}

let max = 0,
maxChar = '';

for (let character in characters) {
if (max < characters[character]) {
max = characters[character];
maxChar = character;
}
}

// Print the result
console.log(`The maximum frequency letter is: "${maxChar}" and it appears ${characters[maxChar]} number of times!`);
return characters[maxChar];
}

maxChars('helllllo worlld');

0 comments on commit 05e36a6

Please sign in to comment.