Skip to content

Commit 028ebbc

Browse files
committed
chore: update Conway's Look and Say - Generalized
1 parent aca2f66 commit 028ebbc

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
title: Conway's Look and Say - Generalized
3+
description: Your task is to create a function that will take an integer and return the result of the look-and-say function on that integer. This should be a general function that takes as input any positive integer, and returns an integer; inputs are not limited to the sequence which starts with "1".
4+
keywords: [Codewars, Conway's Look and Say - Generalized, 5 kyu, Algorithms]
5+
tags: [Codewars, 5 kyu, Algorithms]
6+
---
7+
8+
# [{frontMatter.title}](https://www.codewars.com/kata/530045e3c7c0f4d3420001af/train/javascript)
9+
10+
import Tabs from "@theme/Tabs";
11+
import TabItem from "@theme/TabItem";
12+
import CodeBlock from "@theme/CodeBlock";
13+
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
14+
import Solution from "!!raw-loader!./solution.js";
15+
import Description from "./_Description.md";
16+
import Examples from "./_Examples.md";
17+
18+
<DifficultyBadge kyu={5} />
19+
20+
<Tabs>
21+
<TabItem value="description" label="題目描述" default>
22+
<details open>
23+
<summary>Description</summary>
24+
<Description />
25+
<Examples />
26+
</details>
27+
</TabItem>
28+
29+
<TabItem value="solution" label="解答">
30+
## Solution
31+
<CodeBlock language="js">{Solution}</CodeBlock>
32+
</TabItem>
33+
</Tabs>
34+
35+
### 解題思路
36+
37+
```js
38+
function lookSay(number) {
39+
const numStr = number.toString();
40+
let result = "";
41+
let count = 1;
42+
43+
for (let i = 0; i < numStr.length; i++) {
44+
// 如果下一個數字跟現在的一樣,就累加
45+
if (numStr[i] === numStr[i + 1]) {
46+
count++;
47+
} else {
48+
// 否則就把目前的統計加進結果字串
49+
result += count.toString() + numStr[i];
50+
count = 1; // 重置計數
51+
}
52+
}
53+
54+
return parseInt(result, 10); // 轉成數字回傳
55+
}
56+
```
57+
58+
### 心得
59+
60+
不使用 Object 的 Counter,而是簡化為一個數字。
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Your task is to create a function that will take an integer and return the result of the look-and-say function on that integer. This should be a general function that takes as input any positive integer, and returns an integer; inputs are not limited to the sequence which starts with "1".
2+
3+
Conway's Look-and-say sequence goes like this:
4+
5+
Start with a number.
6+
Look at the number, and group consecutive digits together.
7+
For each digit group, say the number of digits, then the digit itself.
8+
This can be repeated on its result to generate the sequence.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
For example:
2+
3+
- Start with 1.
4+
- There is one 1 --> 11
5+
- Start with 11. There are two 1 digits --> 21
6+
- Start with 21. There is one 2 and one 1 --> 1211
7+
- Start with 1211. There is one 1, one 2, and two 1s --> 111221
8+
9+
Sample inputs and outputs::
10+
11+
- 0 --> 10
12+
- 2014 --> 12101114
13+
- 9000 --> 1930
14+
- 22322 --> 221322
15+
- 222222222222 --> 122
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function lookSay(number) {
2+
const numStr = number.toString();
3+
let result = "";
4+
let count = 1;
5+
6+
for (let i = 0; i < numStr.length; i++) {
7+
if (numStr[i] === numStr[i + 1]) {
8+
count++;
9+
} else {
10+
result += count.toString() + numStr[i];
11+
count = 1;
12+
}
13+
}
14+
15+
return parseInt(result, 10);
16+
}

0 commit comments

Comments
 (0)