Skip to content

Commit fb0087d

Browse files
authored
Merge pull request #238 from myoshi2891/dev-from-macmini
Add Valid Phone Numbers bash solution with detailed regex explanation
2 parents 8e32553 + 5474aa1 commit fb0087d

1 file changed

Lines changed: 245 additions & 0 deletions

File tree

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "e7bafc2d",
6+
"metadata": {},
7+
"source": [
8+
"# 電話番号フィルタリング問題の解説\n",
9+
"\n",
10+
"この問題を段階的に解説し、bash one-linerで解決します。\n",
11+
"\n",
12+
"## 問題の理解\n",
13+
"\n",
14+
"**有効な電話番号の形式:**\n",
15+
"1. `(xxx) xxx-xxxx` - カッコ付き形式\n",
16+
"2. `xxx-xxx-xxxx` - ハイフン形式\n",
17+
"\n",
18+
"ここで `x` は数字(0-9)を表します。\n",
19+
"\n",
20+
"## 解決策"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"id": "39aacb70",
27+
"metadata": {
28+
"vscode": {
29+
"languageId": "powershell"
30+
}
31+
},
32+
"outputs": [],
33+
"source": [
34+
"#!/bin/bash\n",
35+
"\n",
36+
"# Solution 1: Using grep with extended regex\n",
37+
"# Analyze Complexity\n",
38+
"# Runtime 63 ms\n",
39+
"# Beats 58.79%\n",
40+
"# Memory 3.47 MB\n",
41+
"# Beats 84.10%\n",
42+
"\n",
43+
"grep -E '^([0-9]{3}-[0-9]{3}-[0-9]{4}|\\([0-9]{3}\\) [0-9]{3}-[0-9]{4})$' file.txt\n",
44+
"\n",
45+
"# Solution 2: Using sed (alternative)\n",
46+
"# Analyze Complexity\n",
47+
"# Runtime 65 ms\n",
48+
"# Beats 45.95%\n",
49+
"# Memory 3.42 MB\n",
50+
"# Beats 84.10%\n",
51+
"\n",
52+
"sed -n -E '/^([0-9]{3}-[0-9]{3}-[0-9]{4}|\\([0-9]{3}\\) [0-9]{3}-[0-9]{4})$/p' file.txt\n",
53+
"\n",
54+
"# Solution 3: Using awk (alternative)\n",
55+
"# Analyze Complexity\n",
56+
"# Runtime 71 ms\n",
57+
"# Beats 15.78%\n",
58+
"# Memory 3.84 MB\n",
59+
"# Beats 3.13%\n",
60+
"\n",
61+
"awk '/^([0-9]{3}-[0-9]{3}-[0-9]{4}|\\([0-9]{3}\\) [0-9]{3}-[0-9]{4})$/' file.txt"
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"id": "e5ef6417",
67+
"metadata": {},
68+
"source": [
69+
"## 最もシンプルな解答\n",
70+
"\n",
71+
"```bash\n",
72+
"grep -E '^([0-9]{3}-[0-9]{3}-[0-9]{4}|\\([0-9]{3}\\) [0-9]{3}-[0-9]{4})$' file.txt\n",
73+
"```\n",
74+
"\n",
75+
"---\n",
76+
"\n",
77+
"## 詳細な図解による解説\n",
78+
"\n",
79+
"### 1. 正規表現パターンの構造\n",
80+
"\n",
81+
"```\n",
82+
"^([0-9]{3}-[0-9]{3}-[0-9]{4}|\\([0-9]{3}\\) [0-9]{3}-[0-9]{4})$\n",
83+
"```\n",
84+
"\n",
85+
"この正規表現を分解して理解しましょう:\n",
86+
"\n",
87+
"#### **全体構造**\n",
88+
"```\n",
89+
" ^ $\n",
90+
" | |\n",
91+
" 行頭 OR演算子 行末\n",
92+
" ┌──────────────────┴──────────────────┐\n",
93+
" | |\n",
94+
" パターン1 パターン2\n",
95+
" xxx-xxx-xxxx (xxx) xxx-xxxx\n",
96+
"```\n",
97+
"\n",
98+
"#### **パターン1: `[0-9]{3}-[0-9]{3}-[0-9]{4}`**\n",
99+
"\n",
100+
"```\n",
101+
" [0-9]{3} - [0-9]{3} - [0-9]{4}\n",
102+
" │ │ │ │ │\n",
103+
" 3桁の数字 ハイフン 3桁の数字 ハイフン 4桁の数字\n",
104+
" │ │ │\n",
105+
" 987 123 4567\n",
106+
"\n",
107+
"例: 987-123-4567\n",
108+
"```\n",
109+
"\n",
110+
"#### **パターン2: `\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}`**\n",
111+
"\n",
112+
"```\n",
113+
" \\( [0-9]{3} \\) スペース [0-9]{3} - [0-9]{4}\n",
114+
" │ │ │ │ │ │ │\n",
115+
" 左カッコ 3桁 右カッコ 空白 3桁 ハイフン 4桁\n",
116+
" │ │ │ │ │\n",
117+
" ( 123 ) 456 7890\n",
118+
"\n",
119+
"例: (123) 456-7890\n",
120+
"```\n",
121+
"\n",
122+
"**重要ポイント:** `\\(` と `\\)` はエスケープが必要です(カッコ自体を表すため)\n",
123+
"\n",
124+
"---\n",
125+
"\n",
126+
"### 2. grep コマンドの動作フロー\n",
127+
"\n",
128+
"```\n",
129+
"┌─────────────────────────────────────────────────┐\n",
130+
"│ file.txt の内容 │\n",
131+
"├─────────────────────────────────────────────────┤\n",
132+
"│ 行1: 987-123-4567 │\n",
133+
"│ 行2: 123 456 7890 │\n",
134+
"│ 行3: (123) 456-7890 │\n",
135+
"├─────────────────────────────────────────────────┤\n",
136+
" ↓ grep -E で各行をチェック\n",
137+
"├─────────────────────────────────────────────────┤\n",
138+
"│ パターンマッチング │\n",
139+
"├─────────────────────────────────────────────────┤\n",
140+
"│ 行1: 987-123-4567 │\n",
141+
"│ ✓ パターン1にマッチ → 出力 │\n",
142+
"│ │\n",
143+
"│ 行2: 123 456 7890 │\n",
144+
"│ ✗ どちらのパターンにもマッチしない │\n",
145+
"│ (スペースがハイフンではない) │\n",
146+
"│ │\n",
147+
"│ 行3: (123) 456-7890 │\n",
148+
"│ ✓ パターン2にマッチ → 出力 │\n",
149+
"├─────────────────────────────────────────────────┤\n",
150+
" ↓ 結果出力\n",
151+
"├─────────────────────────────────────────────────┤\n",
152+
"│ 987-123-4567 │\n",
153+
"│ (123) 456-7890 │\n",
154+
"└─────────────────────────────────────────────────┘\n",
155+
"```\n",
156+
"\n",
157+
"---\n",
158+
"\n",
159+
"### 3. オプションの説明\n",
160+
"\n",
161+
"```bash\n",
162+
"grep -E '^pattern$' file.txt\n",
163+
" │ │ └─ 入力ファイル\n",
164+
" │ └─ 正規表現パターン\n",
165+
" └─ Extended Regular Expression (拡張正規表現)\n",
166+
"```\n",
167+
"\n",
168+
"- **`-E`**: 拡張正規表現を使用(+, ?, |, () などが使える)\n",
169+
"- **`^`**: 行頭にマッチ(余分な文字がないことを保証)\n",
170+
"- **`$`**: 行末にマッチ(余分な文字がないことを保証)\n",
171+
"\n",
172+
"---\n",
173+
"\n",
174+
"### 4. テストケースの検証\n",
175+
"\n",
176+
"#### **有効な番号**\n",
177+
"```\n",
178+
"✓ 987-123-4567\n",
179+
" [0-9]{3}-[0-9]{3}-[0-9]{4} にマッチ\n",
180+
" \n",
181+
"✓ (123) 456-7890\n",
182+
" \\([0-9]{3}\\) [0-9]{3}-[0-9]{4} にマッチ\n",
183+
"```\n",
184+
"\n",
185+
"#### **無効な番号**\n",
186+
"```\n",
187+
"✗ 123 456 7890\n",
188+
" 理由: ハイフンではなくスペースで区切られている\n",
189+
" \n",
190+
"✗ 1234567890\n",
191+
" 理由: 区切り文字がない\n",
192+
" \n",
193+
"✗ (123)456-7890\n",
194+
" 理由: カッコの後にスペースがない\n",
195+
" \n",
196+
"✗ 12-345-6789\n",
197+
" 理由: 最初のグループが2桁(3桁が必要)\n",
198+
"```\n",
199+
"\n",
200+
"---\n",
201+
"\n",
202+
"### 5. 実行例\n",
203+
"\n",
204+
"```bash\n",
205+
"# file.txtを作成\n",
206+
"$ cat > file.txt << EOF\n",
207+
"987-123-4567\n",
208+
"123 456 7890\n",
209+
"(123) 456-7890\n",
210+
"EOF\n",
211+
"\n",
212+
"# スクリプトを実行\n",
213+
"$ grep -E '^([0-9]{3}-[0-9]{3}-[0-9]{4}|\\([0-9]{3}\\) [0-9]{3}-[0-9]{4})$' file.txt\n",
214+
"\n",
215+
"# 出力\n",
216+
"987-123-4567\n",
217+
"(123) 456-7890\n",
218+
"```\n",
219+
"\n",
220+
"---\n",
221+
"\n",
222+
"## まとめ\n",
223+
"\n",
224+
"**最適解:**\n",
225+
"```bash\n",
226+
"grep -E '^([0-9]{3}-[0-9]{3}-[0-9]{4}|\\([0-9]{3}\\) [0-9]{3}-[0-9]{4})$' file.txt\n",
227+
"```\n",
228+
"\n",
229+
"この一行で:\n",
230+
"- 2つの有効な形式を検出\n",
231+
"- 行頭と行末の厳密なマッチング\n",
232+
"- シンプルで効率的な実装\n",
233+
"\n",
234+
"が実現できます!"
235+
]
236+
}
237+
],
238+
"metadata": {
239+
"language_info": {
240+
"name": "python"
241+
}
242+
},
243+
"nbformat": 4,
244+
"nbformat_minor": 5
245+
}

0 commit comments

Comments
 (0)