Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
one_liner_fizzbuzz
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
maimu
December 07, 2023
Programming
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
one_liner_fizzbuzz
maimu
December 07, 2023
More Decks by maimu
See All by maimu
Gentoo 1年生 ビルドは終わらない
maimux2x
0
110
Rails 1.0 のコードで学ぶ find_by* と method_missing の仕組み / Learn how find_by_* and method_missing work in Rails 1.0 code
maimux2x
1
1.5k
rails_girls_is_my_gate_to_join_the_ruby_commuinty
maimux2x
0
730
ruby-flip-flop
maimux2x
0
170
before_rails_girls_after_rails_girls
maimux2x
0
900
my_study_of_ruby_method
maimux2x
1
220
about_rails_girls_document_translation
maimux2x
0
6.4k
best_for_fbc
maimux2x
0
93
homemade_service_release_front_and_back
maimux2x
0
530
Other Decks in Programming
See All in Programming
AmebaブログはKMPから卒業します
kmars0605
0
320
LLM 101
dalinaum
0
470
作れるエンジニアから、価値を届けるエンジニアへ
ryunakayama
1
210
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
3
1.7k
コーディング自動化の次へ。人の介入をゼロにするファクトリーとサポートAIによる実践
timakin
5
5.1k
Enforcing Your Architecture with Unit Tests in the AI Era (Swift Connection X)
steliosf
PRO
1
140
[DroidKnights 2026] Workflow AX: From Auto-Documented Policies to E2E Testing
raeminkang
0
520
SODA - FACT BOOK(EN)
sodainc
1
200
そのデザイントークン、Flutterでも同じ見た目になりますか?
nakamizu
0
300
Coding with AI: How to trust the storyteller (AWS Community Day Adria, Ljubljana 2026)
slobodan
0
140
선언형 햅틱 라이브러리 진동을 소개합니다
l2hyunwoo
1
290
Railsバージョンを4倍にした話
optim
0
930
Featured
See All Featured
Six Lessons from altMBA
skipperchong
29
4.6k
Docker and Python
trallard
47
4.3k
Raft: Consensus for Rubyists
vanstee
143
7.9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.7k
We Are The Robots
honzajavorek
0
430
A Tale of Four Properties
chriscoyier
163
24k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
440
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
550
Bash Introduction
62gerente
615
220k
The Curse of the Amulet
leimatthew05
3
17k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
610
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本ノック