Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
one_liner_fizzbuzz
Search
maimu
December 07, 2023
Programming
170
0
Share
one_liner_fizzbuzz
maimu
December 07, 2023
More Decks by maimu
See All by maimu
Gentoo 1年生 ビルドは終わらない
maimux2x
0
81
Rails 1.0 のコードで学ぶ find_by* と method_missing の仕組み / Learn how find_by_* and method_missing work in Rails 1.0 code
maimux2x
1
1.3k
rails_girls_is_my_gate_to_join_the_ruby_commuinty
maimux2x
0
670
ruby-flip-flop
maimux2x
0
150
before_rails_girls_after_rails_girls
maimux2x
0
840
my_study_of_ruby_method
maimux2x
1
190
about_rails_girls_document_translation
maimux2x
0
6.3k
best_for_fbc
maimux2x
0
79
homemade_service_release_front_and_back
maimux2x
0
470
Other Decks in Programming
See All in Programming
Rethinking API Platform Filters
vinceamstoutz
0
11k
それはエンジニアリングの糧である:AI開発のためにAIのOSSを開発する現場より / It serves as fuel for engineering: insights from the field of developing open-source AI for AI development.
nrslib
1
830
Laravel Nightwatchの裏側 - Laravel公式Observabilityツールを支える設計と実装
avosalmon
1
320
How Swift's Type System Guides AI Agents
koher
0
140
ふりがな Deep Dive try! Swift Tokyo 2026
watura
0
110
まかせられるPM・まかせられないPM / DevTech GUILD Meetup
yusukemukoyama
0
110
へんな働き方
yusukebe
6
2.9k
Swift Concurrency Type System
inamiy
0
290
AIエージェントで業務改善してみた
taku271
0
490
Nuxt Server Components
wattanx
0
250
レガシーPHP転生 〜父がドメインエキスパートだったのでDDD+Claude Codeでチート開発します〜
panda_program
0
540
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
270
Featured
See All Featured
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
160
Unsuck your backbone
ammeep
672
58k
Fireside Chat
paigeccino
42
3.9k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
120
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.1k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
64
54k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
500
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.3k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
190
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
160
The Cult of Friendly URLs
andyhume
79
6.8k
Transcript
ワンライナーで FizzBuzz 2023/12/07 maimu
自己紹介 名前:maimu Xアカウント:maimux2x 所属:永和システムマネジメント エンジニア歴:4ヶ月 好きなRubyメソッド: Enumerable#map 好きなLinuxコマンド: tee
ワンライナーでFizzBuzz
なぜこのテーマなのか Linuxの勉強で学んだコマンドを使ってみたかった
ワンライナーとは? 1行で書かれたコマンドの組み合わせ
今回のお題
試作 seq 100 | awk '$1%15==0{print "FizzBuzz"}$1%5==0{print "Buzz"}$1%3==0{print "Fizz"}$1%15{print $1}'
コマンドについて seq : 連続した数字の列を出力・表示するコマンド awk : grepにプログラム機能をつけたようなもの(プログラミング言語) $1 : 読み込んだ行の1列目の文字列または数値
結果は・・・?
条件分岐を追加! seq 100 | awk '{ if ($1 % 15
== 0) print "FizzBuzz"; else if ($1 % 3 == 0) print "Fizz"; else if ($1 % 5 == 0) print "Buzz"; else print $1; }'
今度はできた!
他のコマンドも試したい
ebanさんのツイートにまさにシェル芸という模範解答が https://twitter.com/eban/status/830272982303936512 seq 1 100|sed '5~5s/.*/Buzz/;3~3s/[^B]*/Fizz/' 模範解答を見つけてしまった・・・
参考にしつつ自分もやってみる seq 100 | sed -e '3~3s/.*/Fizz/' -e '5~5s/.*/Buzz/' -e
'15~15s/.*/FizzBuzz/'
コマンドについて sed : 指定したファイルをコマンドに従って処理する -e : sedで複数のコマンドを指定する際に使用する s : 指定されたパターンに基づいて置換する
~ : 処理対象の位置を指定するアドレス指定子
できた!
もう1個やってみる seq 100 | sed -e '15~15c\FizzBuzz' -e '3~3c\Fizz' -e
'5~5c\Buzz'
コマンドについて c : 指定した行をテキストに置き換える
できた!
Rubyワンライナーでもやってみた seq 100 | ruby -ne 'if $_.to_i%15==0; puts "FizzBuzz"
elsif $_.to_i%5==0; puts "Buzz" elsif $_.to_i%3==0; puts "Fizz" else puts $_ end'
コマンドについて -e : 引数に記述されたRubyのコードを実行するためのオプション -n : 標準入力の内容を1行ずつ読み込む $_ : -nオプションで読み込んだ文字列が格納される変数
できた!
やってみた結果 • ワンライナーでFizzBuzzを4つ実行できた! ◦ 効率性は完全無視・・・ ◦ Rubyワンライナーは普通にコードを書いた方が分かりやすい・・・ • Linuxコマンドと少し仲良くなれた •
Rubyコマンドのオプションを初めて使った
今後の展望 • シェル・ワンライナー160本ノックをやり切りたい! • アイディアと手段を駆使したワンライナーを書いてみたい
参考文献 • man • 1日1問半年以内に習得 シェル・ワンライナー160本ノック