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
0
160
one_liner_fizzbuzz
maimu
December 07, 2023
Tweet
Share
More Decks by maimu
See All by maimu
Rails 1.0 のコードで学ぶ find_by* と method_missing の仕組み / Learn how find_by_* and method_missing work in Rails 1.0 code
maimux2x
1
980
rails_girls_is_my_gate_to_join_the_ruby_commuinty
maimux2x
0
600
ruby-flip-flop
maimux2x
0
140
before_rails_girls_after_rails_girls
maimux2x
0
790
my_study_of_ruby_method
maimux2x
1
180
about_rails_girls_document_translation
maimux2x
0
6.2k
best_for_fbc
maimux2x
0
75
homemade_service_release_front_and_back
maimux2x
0
440
enjoy_conferences
maimux2x
0
5.9k
Other Decks in Programming
See All in Programming
go test -json そして testing.T.Attr / Kyoto.go #63
utgwkk
3
310
Updates on MLS on Ruby (and maybe more)
sylph01
1
180
Putting The Genie in the Bottle - A Crash Course on running LLMs on Android
iurysza
0
140
AIでLINEスタンプを作ってみた
eycjur
1
230
rage against annotate_predecessor
junk0612
0
170
Android 16 × Jetpack Composeで縦書きテキストエディタを作ろう / Vertical Text Editor with Compose on Android 16
cc4966
2
230
Namespace and Its Future
tagomoris
6
700
アセットのコンパイルについて
ojun9
0
130
Azure SRE Agentで運用は楽になるのか?
kkamegawa
0
2.3k
RDoc meets YARD
okuramasafumi
4
170
2025 年のコーディングエージェントの現在地とエンジニアの仕事の変化について
azukiazusa1
24
12k
基礎から学ぶ大画面対応(Learning Large-Screen Support from the Ground Up)
tomoya0x00
0
1.5k
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
131
19k
Building Applications with DynamoDB
mza
96
6.6k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.7k
Designing for Performance
lara
610
69k
A Modern Web Designer's Workflow
chriscoyier
696
190k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.1k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
Context Engineering - Making Every Token Count
addyosmani
3
48
Designing for humans not robots
tammielis
253
25k
Site-Speed That Sticks
csswizardry
10
820
Code Review Best Practice
trishagee
70
19k
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本ノック