将布尔值转换为数字。有更好的算法吗?

huangapple go评论110阅读模式
英文:

Convert bool to number. Better Algorithm?

问题

最近,我在一家公司面试,并收到了一个测试任务:

  1. 实现一个接受任何数据类型并将布尔类型如果有的话转换为数值的函数可以使用任何嵌套级别的对象数组字符串数字等作为参数

示例:

  1. booleanToInt('qwerty') // 'qwerty'
  2. booleanToInt(1) // 1
  3. booleanToInt(false) // 0
  4. booleanToInt(true) // 1
  5. booleanToInt([1, 'qwerty', false]) // [1, 'qwerty', 0]
  6. booleanToInt([1, 'qwerty', { a: true }]) // [1, 'qwerty', { a: 1 }]
  7. booleanToInt({ a: { b: true }, c: false, d: 'qwerty' }) // { a: { b: 1 }, c: 0, d: 'qwerty' }

我写了自己的实现,但被告知这太复杂并且是一个庞大的解决方案。在我询问如何优化答案的问题上,并未给出答案。

我的决定:

  1. // 检查对象
  2. function isObject(value) {
  3. return typeof value === "object" && !Array.isArray(value) && value !== null;
  4. }
  5. // 转换对象
  6. function transformObject(obj) {
  7. Object.keys(obj).forEach((key) => {
  8. if (isObject(obj[key])) {
  9. transformObject(obj[key]);
  10. } else if (Array.isArray(obj[key])) {
  11. transformArray(obj[key]);
  12. } else {
  13. obj[key] = transformStatic(obj[key]);
  14. }
  15. });
  16. }
  17. // 转换数组
  18. function transformArray(list) {
  19. list.forEach((item, i) => {
  20. if (isObject(item)) {
  21. transformObject(item);
  22. } else if (Array.isArray(item)) {
  23. transformArray(item);
  24. } else {
  25. list[i] = transformStatic(item);
  26. }
  27. });
  28. }
  29. // 转换静态类型
  30. function transformStatic(value) {
  31. let res = value;
  32. if (typeof value === "boolean") {
  33. res = +value;
  34. }
  35. return res;
  36. }
  37. // 主函数
  38. function booleanToInt(value) {
  39. if (isObject(value)) {
  40. transformObject(value);
  41. return value;
  42. }
  43. if (Array.isArray(value)) {
  44. transformArray(value);
  45. return value;
  46. }
  47. return transformStatic(value);
  48. }
  49. console.log(booleanToInt('qwerty')); // 'qwerty'
  50. console.log(booleanToInt(1)); // 1
  51. console.log(booleanToInt(false)); // 0
  52. console.log(booleanToInt(true)); // 1
  53. console.log(booleanToInt([1, 'qwerty', false])); // [1, 'qwerty', 0]
  54. console.log(booleanToInt([1, 'qwerty', { a: true }])); //[1, 'qwerty', { a: 1 }]

我想知道还能想出什么其他算法吗?也许会更短更简洁?

英文:

Recently, I interviewed in one company and received a test task:

  1. Implement a function that accepts any data type and convert the boolean type (if any) to a numeric value.
  2. Objects of any nesting level, arrays, strings, numbers, etc. can be used as parameters.

Example:

  1. booleanToInt('qwerty') // 'qwerty'
  2. booleanToInt(1) // 1
  3. booleanToInt(false) // 0
  4. booleanToInt(true) // 1
  5. booleanToInt([1, 'qwerty', false]) // [1, 'qwerty', 0]
  6. booleanToInt([1, 'qwerty', { a: true }]) // [1, 'qwerty', { a: 1 }]
  7. booleanToInt({ a: { b: true }, c: false, d: 'qwerty' }) // { a: { b: 1 }, c: 0, d: 'qwerty' }

I wrote my own implementation, but I was told that this is too complicated and a big solution. To my question, what and where can I optimize the answer was not given to me.

My decision:

  1. // Checking for an object
  2. function isObject(value) {
  3. return typeof value === "object" && !Array.isArray(value) && value !== null;
  4. }
  5. // Transforming an object
  6. function transformObject(obj) {
  7. Object.keys(obj).forEach((key) => {
  8. if (isObject(obj[key])) {
  9. transformObject(obj[key]);
  10. } else if (Array.isArray(obj[key])) {
  11. transformArray(obj[key]);
  12. } else {
  13. obj[key] = transformStatic(obj[key]);
  14. }
  15. });
  16. }
  17. // Transforming the array
  18. function transformArray(list) {
  19. list.forEach((item, i) => {
  20. if (isObject(item)) {
  21. transformObject(item);
  22. } else if (Array.isArray(item)) {
  23. transformArray(item);
  24. } else {
  25. list[i] = transformStatic(item);
  26. }
  27. });
  28. }
  29. // Transforming Static Types
  30. function transformStatic(value) {
  31. let res = value;
  32. if (typeof value === "boolean") {
  33. res = +value;
  34. }
  35. return res;
  36. }
  37. // main function
  38. function booleanToInt(value) {
  39. if (isObject(value)) {
  40. transformObject(value);
  41. return value;
  42. }
  43. if (Array.isArray(value)) {
  44. transformArray(value);
  45. return value;
  46. }
  47. return transformStatic(value);
  48. }
  49. console.log(booleanToInt("qwerty")); // 'qwerty'
  50. console.log(booleanToInt(1)); // 1
  51. console.log(booleanToInt(false)); // 0
  52. console.log(booleanToInt(true)); // 1
  53. console.log(booleanToInt([1, "qwerty", false])); // [1, 'qwerty', 0]
  54. console.log(booleanToInt([1, "qwerty", { a: true }])); //[1, 'qwerty', { a: 1 }]

LIVE HERE

I wonder what other algorithm can come up with?
Perhaps it will be shorter and more concise?

答案1

得分: 4

Sure, here is the translated code portion:

  1. function booleanToInt(value) {
  2. if (typeof value === "boolean") return +value;
  3. if (typeof value === "object") for (const k in value) value[k] = booleanToInt(value[k]);
  4. return value;
  5. }
  6. console.log(booleanToInt("qwerty")); // 'qwerty'
  7. console.log(booleanToInt(1)); // 1
  8. console.log(booleanToInt(false)); // 0
  9. console.log(booleanToInt(true)); // 1
  10. console.log(booleanToInt([1, "qwerty", false])); // [1, 'qwerty', 0]
  11. console.log(booleanToInt([1, "qwerty", { a: true })); //[1, 'qwerty', { a: 1 }]

I have translated the code and removed the unnecessary parts as per your request.

英文:

I'd simplify it to single function and use for ... in

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. function booleanToInt(value) {
  2. if (typeof value === &quot;boolean&quot;) return +value;
  3. if (typeof value === &quot;object&quot;) for (const k in value) value[k] = booleanToInt(value[k]);
  4. return value;
  5. }
  6. console.log(booleanToInt(&quot;qwerty&quot;)); // &#39;qwerty&#39;
  7. console.log(booleanToInt(1)); // 1
  8. console.log(booleanToInt(false)); // 0
  9. console.log(booleanToInt(true)); // 1
  10. console.log(booleanToInt([1, &quot;qwerty&quot;, false])); // [1, &#39;qwerty&#39;, 0]
  11. console.log(booleanToInt([1, &quot;qwerty&quot;, { a: true }])); //[1, &#39;qwerty&#39;, { a: 1 }]

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 03:16:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定