Skip to content

Added Happy Number #57

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

Merged
merged 5 commits into from
Oct 7, 2022
Merged
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
49 changes: 49 additions & 0 deletions LeetcodeProblems/Algorithms/Happy_Number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
Happy Number
https://leetcode.com/problems/happy-number/

Write an algorithm to determine if a number n is happy.

A happy number is a number defined by the following process:

Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy.

Return true if n is a happy number, and false if not.

Example 1:
Input: n = 19
Output: true
Explanation:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1

Example 2:
Input: n = 2
Output: false


*/

/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function(n) {
return checkHappyNumber(n);
};

function checkHappyNumber(n){
strNumber = n.toString();
splitNumber = strNumber.split("");
Comment on lines +40 to +41
Copy link
Owner

@ignacio-chiazzo ignacio-chiazzo Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another solution that doesn't require translating the integer to a String would be to
get the number, apply mod 10 and divide it by 10, get the digit and repeat the process.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, i don't get it. could you give an example?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last_digit = n % 10 
n = (n - last_digit) / 10

if(splitNumber.length <= 1){
return (n <= 1)? true:false;
}
const digit = splitNumber.reduce((a,b)=> parseInt(a) + Math.pow(parseInt(b),2),0);
return checkHappyNumber(digit)
}

module.exports.isHappy = isHappy;
33 changes: 33 additions & 0 deletions LeetcodeProblems/Algorithms/Shuffle_String.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
You are given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.

Return the shuffled string.



Example 1:

Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
Output: "leetcode"
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.

Example 2:

Input: s = "abc", indices = [0,1,2]
Output: "abc"
Explanation: After shuffling, each character remains in its position.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this assumes the input indices is a valid input e.g. it cannot be [-1, 1023234234]

*/

/**
* @param {string} s
* @param {number[]} indices
* @return {string}
*/
var restoreString = function(s, indices) {
let arrshuffle = [];
indices.forEach((sindex, index) => arrshuffle[sindex] = s.charAt(index))
return arrshuffle.join('')
};

module.exports.restoreString = restoreString;
9 changes: 9 additions & 0 deletions LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const assert = require('assert');
const isHappy = require('../../LeetcodeProblems/Algorithms/Happy_Number').isHappy;

var test = function () {
assert.equal(isHappy(19),true);
assert.equal(isHappy(2),false);
}

module.exports.test = test;
10 changes: 10 additions & 0 deletions LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const assert = require('assert');
const restoreString = require('../../LeetcodeProblems/Algorithms/Shuffle_String').restoreString;

var test = function () {
assert.equal(restoreString('codeleet',[4,5,6,7,0,2,1,3]),'leetcode');
assert.equal(restoreString('abc',[0,1,2]),'abc');
assert.equal(restoreString('hacktoberfest',[9,10,11,12,4,5,6,7,8,0,1,2,3]),'festtoberhack');
}

module.exports.test = test;
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
| [Permutations With Duplicates ](/LeetcodeProblems/Algorithms/Permutations_With_Duplicates.js) | | |
| [Deletion Distance](/LeetcodeProblems/Algorithms/Deletion_Distance.js) | | |
| [Award Budget Cuts](/LeetcodeProblems/Algorithms/Award_Budget_Cuts.js) | | |
| [Happy Number](https://leetcode.com/problems/happy-number/) | Easy | https://leetcode.com/problems/happy-number/ |
| [Shuffle String](https://leetcode.com/problems/shuffle-string/) | Easy | https://leetcode.com/problems/shuffle-string/ |

### Sorting Algorithms
| Algoritmhs |
Expand Down