Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5487f08
commit 4223c74
Showing
25 changed files
with
595 additions
and
7 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
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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)); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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'); |
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,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'); |
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,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'); |
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 @@ | ||
|
Empty file.
Empty file.
Empty file.
Empty file.
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,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')); |
Oops, something went wrong.