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

first 6 problems completed #134

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
48 changes: 47 additions & 1 deletion exercises/anagrams/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,52 @@
// anagrams('RAIL! SAFETY!', 'fairy tales') --> True
// anagrams('Hi there', 'Bye there') --> False

function anagrams(stringA, stringB) {}
// Answer 1

const anagrams = (stringA, stringB) => {
const aCharMap = buildCharMap(stringA);
const bCharMap = buildCharMap(stringB);

if (Object.keys(aCharMap).length !== Object.keys(bCharMap).length) {
return false;
}

for (let char in aCharMap){
if (aCharMap[char] !== bCharMap[char]){
return false;
}
}
return true;
}

const buildCharMap = (str) =>{
const charMap = {};
// any character that is not a num, character, cap or lower character, replace with nothing
for ( let char of str.replace(/[^\w]/g, '').toLowerCase()) {
charMap[char] = charMap[char] + 1 || 1;
}

return charMap
}



// Answer 2

// create helper function
//use .sort() to check if they are anagrams

// const anagrams = (stringA, stringB) => {
// // return and invoke helper function while passing in the strings to return a boolean
// return cleanString(stringA) === cleanString(stringB)
// }

// //helper function to make code clean
// const cleanString = (str) => {
// //use regular expression to get rid of spaces and punctuation
// //make every character lowercase
// //use .split to turn it into an array so that you can use the array helper .sort()
// //join the array back into a single string
// return str.replace(/[^\w]/g, '').toLowerCase().split('').sort().join('')
// }
module.exports = anagrams;
46 changes: 45 additions & 1 deletion exercises/capitalize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,50 @@
// capitalize('a lazy fox') --> 'A Lazy Fox'
// capitalize('look, it is working!') --> 'Look, It Is Working!'

function capitalize(str) {}


/* Solution 1 */

// make an empty array 'words'
// split the input string by spaces to get an array
// for each word in the array
// 1. Uppercase the first letter of the word
// 2. Join first letter with rest of the string
// 4. Push result into 'words' array
// Join 'words' into a string and return it

// function capitalize(str) {
// const words = [];
// for (let word of str.split(' ')){
// words.push(word[0].toUpperCase() + word.slice(1))
// }
// return words.join(' ');
// }


/* Solution 2 (Less Effective) */

// Create an empty string called 'result' which is the first character of the input
// string capitalized
// For each character in the string
// IF the character to the left is a space
// Capitalize it and add it to 'result'
// Else
// Add it to 'result'

// const capitalize = (str) => {
// let result = str[0].toUpperCase();
// for(let i = 1; i<str.length;i++) {
// if (str[i-1] === ' ') {
// result += str[i].toUpperCase()
// } else {
// result += str[i]
// }

// }
// return result
// }



module.exports = capitalize;
41 changes: 40 additions & 1 deletion exercises/chunk/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,45 @@
// chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
// chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]

function chunk(array, size) {}
// need two parameters, array and size
// create a new array
// need to loop through array
// need to push elements into based on the size




const chunk = (array, size) => {
const chunked = [];
//using let because variable will change overtime
let index = 0;
while (index < array.length){
chunked.push(array.slice(index, index + size));
index += size;
}
return chunked;
}




// const array = [1,2,3,4,5,6]

// function chunk(array, size) {
// const chunked = [];

// for (let element of array) {
// //logic to look at last element inside of our chunked array
// const last = chunked[chunked.length - 1]
// //for when we need a new chunk in chunked
// if (!last || last.length === size) {
// chunked.push([element])
// } else {
// //for when we have a chunk but it is not yet full
// last.push(element);
// }
// }
// return chunked;
// }

module.exports = chunk;
18 changes: 17 additions & 1 deletion exercises/fizzbuzz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
// 4
// buzz

function fizzBuzz(n) {}
function fizzBuzz(n) {
// i is the current iteration
// i=1 because that is our starting point, stopping at final number, and incrementing
for (let i = 1; i <= n; i++) {
// is the number a multiple of 3 and 5?
if (i % 3 === 0 && i % 5 === 0) {
console.log('fizzbuzz');
//we use else if so that if the above code occurs then we don't run any more
} else if (i % 3 === 0) {
console.log('fizz')
} else if (i % 5 === 0) {
console.log('buzz')
} else {
console.log(i)
}
}
}

module.exports = fizzBuzz;
35 changes: 34 additions & 1 deletion exercises/maxchar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,39 @@
// maxChar("abcccccccd") === "c"
// maxChar("apple 1231111") === "1"

function maxChar(str) {}
//use for in to iterate through an object


function maxChar(str) {
//creating charMap object based on string
const charMap = {};
// creating variable for max character count
let max = 0;
// creating a variable for the character that occurs the most
let maxChar = '';

// looping through the string by the character
for (let char of str) {
// if the character already exists, increment by 1
if (charMap[char]) {
charMap[char]++;
// if this is the first time we have seen the character set it to 1
} else {
charMap[char] = 1;
}
}
// second loop to go through the new character map
for (let char in charMap) {
// if the current char is greater than the current max
if (charMap[char] > max) {
// set it to the new max
max = charMap[char];
// set that new max numbers character to the maxChar
maxChar = char;
}
}
// return the letter that occurs the most
return maxChar;
}

module.exports = maxChar;
44 changes: 43 additions & 1 deletion exercises/palindrome/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,48 @@
// palindrome("abba") === true
// palindrome("abcdefg") === false

function palindrome(str) {}
//******Answer 1******

// function palindrome(str) {
// const reversed = str.split('').reverse().join('');

// return reversed === str
// }





//******Answer 2******

// function palindrome(str) {
// let reversed = ''

// for (const character of str) {
// reversed = character + reversed
// }

// if (reversed === str) {
// return true
// } else {
// return false
// }

// }




//******Answer 3******
// i = index and the first time it is called it equals 0
// second part of the second return statement is the mirrored other side of string
// -i makes sure we increment through every step of loop


function palindrome(str) {
return str.split('').every((char, i) => {
return char === str[str.length - i - 1]
});
}

module.exports = palindrome;
25 changes: 24 additions & 1 deletion exercises/reverseint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@
// reverseInt(-15) === -51
// reverseInt(-90) === -9

function reverseInt(n) {}
//*****Answer 1*******

// function reverseInt(n) {
// const reversed = n.toString().split('').reverse().join('')
// if (n < 0) {
// return parseInt(reversed) * -1;
// }
// return parseInt(reversed)
// }





//****Answer 2****

// when you pass a positive number to .sign() it returns 1 when you pass a
// negative number to .sign() it returns -1

function reverseInt(n) {
const reversed = n.toString().split('').reverse().join('')

return parseInt(reversed)*Math.sign(n)
}

module.exports = reverseInt;
32 changes: 31 additions & 1 deletion exercises/reversestring/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@
// reverse('hello') === 'olleh'
// reverse('Greetings!') === '!sgniteerG'

function reverse(str) {}
//********ANSWER 1********
//split turns it into an array

// function reverse(str) {
// return str.split('').reverse().join('');
// }




//********ANSWER 2********

// function reverse(str) {
// let reversed = '';

// for (let character of str) {
// reversed = character + reversed;
// }
// return reversed;
// }



//********ANSWER 3********
/*reduce array helper is used to take all the different values in an array and
condense them all down to one singular value. The empty string is the second optional starting argument*/
// str = ['a', 'p', 'p', 'l', 'e']
function reverse(str) {

return str.split('').reduce((reversed, character) => character + reversed, '');
}

module.exports = reverse;
28 changes: 28 additions & 0 deletions exercises/reversestring/reversestring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Answer 1 using index

# str = 'hello world'
# print(str[::-1])

# Answer 2 using for in loop

# def reverse(s):
# str=""
# for i in s:
# str = i + str
# return str

# s = "GeeksForGeeks"

# print(reverse(s))

# Answer 3 using recursion

# def reverse(s):
# if len(s)==0:
# return s
# else:
# return s[1:] + s[0]

# s="bird"

# print(reverse(s))
Loading