SlideShare ist ein Scribd-Unternehmen logo
1 von 42
Downloaden Sie, um offline zu lesen
F*cking with FizzBuzz
Scott Windsor
@swindsor
Wednesday, July 3, 13
about me
2001-2007 amazon.com
2007-2007 BillMonk/Obopay
2007-2012 TeachStreet
2012-? amazon local
Wednesday, July 3, 13
I interview a lot.
Wednesday, July 3, 13
FizzBuzz
Wednesday, July 3, 13
FizzBuzz
Print numbers 1-100.
Wednesday, July 3, 13
FizzBuzz
Print numbers 1-100.
For multiples of 3, print “Fizz”.
Wednesday, July 3, 13
FizzBuzz
Print numbers 1-100.
For multiples of 3, print “Fizz”.
For multiples of 5, print “Buzz”.
Wednesday, July 3, 13
FizzBuzz
Print numbers 1-100.
For multiples of 3, print “Fizz”.
For multiples of 5, print “Buzz”.
For multiples of 3 and 5, print “FizzBuzz”.
Wednesday, July 3, 13
fizzbuzz.rb
1.upto(100) do |i|
if i % 15 == 0
puts "FizzBuzz"
elsif i % 3 == 0
puts "Fizz"
elsif i % 5 == 0
puts "Buzz"
else
puts i
end
end
Wednesday, July 3, 13
Wednesday, July 3, 13
Not good for interviews
Wednesday, July 3, 13
Not good for interviews
∅ data structures
Wednesday, July 3, 13
Not good for interviews
∅ data structures
∅ algorithms
Wednesday, July 3, 13
Not good for interviews
∅ data structures
∅ algorithms
∅ problem solving
Wednesday, July 3, 13
Not good for interviews
∅ data structures
∅ algorithms
∅ problem solving
~ coding
Wednesday, July 3, 13
Not good for interviews
∅ data structures
∅ algorithms
∅ problem solving
~ coding
Wednesday, July 3, 13
How can we make this
more fun?
Wednesday, July 3, 13
Fun with DSLs
class FizzBuzz < Bazinator
default_rule ->(i){ i }
rule ->(i){"Fizz" if i % 3 == 0 }
rule ->(i){"Buzz" if i % 5 == 0 }
end
FizzBuzz.new(1..100).print
Wednesday, July 3, 13
Wednesday, July 3, 13
ActiveRecord-style
class Bazinator
attr_accessor :range
def initialize(range)
@range = range
end
def rules
@@rules
end
def default_rule
@@default_rule
end
def self.default_rule(rule)
@@default_rule = rule
end
def self.rule(rule)
@@rules ||= []
@@rules << rule
end
...
Wednesday, July 3, 13
ActiveRecord-style
...
def run_rules(i)
results = rules.map{|rule| rule.call(i) }
results.delete_if(&:nil?)
results << default_rule.call(i) if results.empty?
results
end
def each(&block)
range.each do |i|
yield run_rules(i).join('')
end
end
def print
each do |item|
puts item
end
end
end
Wednesday, July 3, 13
I can haz DSL.
class FizzBuzz < Bazinator
default_rule ->(i){ i }
rule ->(i){"Fizz" if i % 3 == 0 }
rule ->(i){"Buzz" if i % 5 == 0 }
end
FizzBuzz.new(1..100).print
Wednesday, July 3, 13
What about other
languages?
Wednesday, July 3, 13
Let’s try some C
#include <stdio.h>
#include <string.h>
const char* fizz(int i) {
if((i % 3) == 0) {
return "Fizz";
}
else {
return "";
}
}
const char* buzz(int i) {
if((i % 5) == 0) {
return "Buzz";
}
else {
return "";
}
}
Wednesday, July 3, 13
Function Pointers++
Wednesday, July 3, 13
Function Pointers++#define MAX_BUFF 10
#define NUMBER_FUNCTIONS 2
const char* (*dispatch[NUMBER_FUNCTIONS])(int i) = {fizz,buzz};
void itoa(int i, char* a) {
snprintf(a, sizeof(a), "%i", i);
}
void clear(char* s) {
strncpy(s, "", sizeof(s));
}
void run_functions(int i, char* result) {
int f;
for(f=0; f < NUMBER_FUNCTIONS; f++) {
strlcat(result,(*dispatch[f])(i),sizeof(result));
}
}
void add_number(int i, char* result) {
char number[MAX_BUFF];
if(strnlen(result,sizeof(result)) < 1) {
itoa(i,number);
strlcat(result,number,sizeof(result));
}
}
Wednesday, July 3, 13
Somewhere, Knuth is
crying.
int main(){
char result[MAX_BUFF];
int i;
for(i=1; i < 100; i++) {
clear(result);
run_functions(i, result);
add_number(i, result);
printf("%sn", result);
}
return 0;
}
Wednesday, July 3, 13
But wait, Java is missing
out.
package com.sentientmonkey.fizzbuzz;
import com.sentientmonkey.fizzbuzz.service.FizzBuzzService;
public class Runner {
public static void main(String[] args) {
FizzBuzzService fizzBuzz = new FizzBuzzService();
fizzBuzz.print(1, 100);
}
}
Wednesday, July 3, 13
Best thing is the
patterns.
Wednesday, July 3, 13
First you get the Service
Facade.
package com.sentientmonkey.fizzbuzz.service;
import com.sentientmonkey.fizzbuzz.rules.*;
public class FizzBuzzService {
RuleManager ruleManager;
public FizzBuzzService() {
ruleManager = new RuleManager();
ruleManager.addRule(new FizzRule());
ruleManager.addRule(new BuzzRule());
ruleManager.addRule(new DefaultRule());
}
public void print(int start, int end) {
for (int i = start; i <= end; i++) {
System.out.println(ruleManager.evaluateRules(i));
}
}
}
Wednesday, July 3, 13
Then you get the
Manager.
package com.sentientmonkey.fizzbuzz.rules;
import java.util.ArrayList;
public class RuleManager {
ArrayList<Rule> rules;
public RuleManager() {
rules = new ArrayList<Rule>();
}
public void addRule(Rule rule) {
rules.add(rule);
}
public String evaluateRules(int number) {
StringBuilder builder = new StringBuilder();
for (Rule rule : rules) {
rule.withBuilder(builder).append(number);
}
return builder.toString();
}
}
Wednesday, July 3, 13
Now you get a Builder.
package com.sentientmonkey.fizzbuzz.rules;
public abstract class Rule {
StringBuilder builder = null;
public Rule withBuilder(StringBuilder builder) {
this.builder = builder;
return this;
}
public abstract void append(int number);
}
Wednesday, July 3, 13
Default Rule.
package com.sentientmonkey.fizzbuzz.rules;
public class DefaultRule extends Rule {
@Override
public void append(int number) {
if (builder.length() == 0) {
builder.append(number);
}
}
}
Wednesday, July 3, 13
Fizz Rule.
package com.sentientmonkey.fizzbuzz.rules;
public class FizzRule extends Rule {
@Override
public void append(int number) {
if ((number % 3) == 0) {
builder.append("Fizz");
}
}
}
Wednesday, July 3, 13
Buzz Rule.
package com.sentientmonkey.fizzbuzz.rules;
public class BuzzRule extends Rule {
@Override
public void append(int number) {
if ((number % 5) == 0) {
builder.append("Buzz");
}
}
}
Wednesday, July 3, 13
le java.
package com.sentientmonkey.fizzbuzz;
import com.sentientmonkey.fizzbuzz.service.FizzBuzzService;
public class Runner {
public static void main(String[] args) {
FizzBuzzService fizzBuzz = new FizzBuzzService();
fizzBuzz.print(1, 100);
}
}
Wednesday, July 3, 13
Don’t forget clojure.
(defn fizz [i]
(if(= (mod i 3) 0)
"Fizz"))
(defn buzz [i]
(if(= (mod i 5) 0)
"Buzz"))
(defn join [a b]
(str a b))
(defn run-rules [numbers]
(map join (map fizz numbers) (map buzz numbers)))
(defn number [s n]
(if (= s "")
n
s))
Wednesday, July 3, 13
Something clever about
McCarthy.
(defn fizzbuzz [start end]
(let
[numbers (range start end)]
(doall
(map println
(map number
(run-rules numbers) numbers)))))
(fizzbuzz 1 100)
Wednesday, July 3, 13
or javascript
function fizz(i) {
if((i % 3) == 0){
return "Fizz";
}
}
function buzz(i) {
if((i % 5) == 0){
return "Buzz";
}
}
var rules = [fizz,buzz];
Wednesday, July 3, 13
or javascript.
function default_rule(result,i){
if(result.length > 0) {
return result;
} else {
return i;
}
}
function fizz_buzz(start,end) {
for(var i=start; i<=end; i++) {
var result = rules.map(function(rule){
return rule(i);
}).join('');
print(default_rule(result,i));
}
};
fizz_buzz(1,100);
Wednesday, July 3, 13
So what did we learn?
• Interviewees: Even fizzbuzz can be fun
• Interviewers:Ask better interview questions
• Everyone: Coding is still hard
Wednesday, July 3, 13
Thanks!
Play along at home:
http://github.com/sentientmonkey/fizzbuzz
Questions?
@swindsor
P.S. we’re hiring
Wednesday, July 3, 13

Weitere ähnliche Inhalte

Kürzlich hochgeladen

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Kürzlich hochgeladen (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

F*cking with fizz buzz

  • 1. F*cking with FizzBuzz Scott Windsor @swindsor Wednesday, July 3, 13
  • 2. about me 2001-2007 amazon.com 2007-2007 BillMonk/Obopay 2007-2012 TeachStreet 2012-? amazon local Wednesday, July 3, 13
  • 3. I interview a lot. Wednesday, July 3, 13
  • 6. FizzBuzz Print numbers 1-100. For multiples of 3, print “Fizz”. Wednesday, July 3, 13
  • 7. FizzBuzz Print numbers 1-100. For multiples of 3, print “Fizz”. For multiples of 5, print “Buzz”. Wednesday, July 3, 13
  • 8. FizzBuzz Print numbers 1-100. For multiples of 3, print “Fizz”. For multiples of 5, print “Buzz”. For multiples of 3 and 5, print “FizzBuzz”. Wednesday, July 3, 13
  • 9. fizzbuzz.rb 1.upto(100) do |i| if i % 15 == 0 puts "FizzBuzz" elsif i % 3 == 0 puts "Fizz" elsif i % 5 == 0 puts "Buzz" else puts i end end Wednesday, July 3, 13
  • 11. Not good for interviews Wednesday, July 3, 13
  • 12. Not good for interviews ∅ data structures Wednesday, July 3, 13
  • 13. Not good for interviews ∅ data structures ∅ algorithms Wednesday, July 3, 13
  • 14. Not good for interviews ∅ data structures ∅ algorithms ∅ problem solving Wednesday, July 3, 13
  • 15. Not good for interviews ∅ data structures ∅ algorithms ∅ problem solving ~ coding Wednesday, July 3, 13
  • 16. Not good for interviews ∅ data structures ∅ algorithms ∅ problem solving ~ coding Wednesday, July 3, 13
  • 17. How can we make this more fun? Wednesday, July 3, 13
  • 18. Fun with DSLs class FizzBuzz < Bazinator default_rule ->(i){ i } rule ->(i){"Fizz" if i % 3 == 0 } rule ->(i){"Buzz" if i % 5 == 0 } end FizzBuzz.new(1..100).print Wednesday, July 3, 13
  • 20. ActiveRecord-style class Bazinator attr_accessor :range def initialize(range) @range = range end def rules @@rules end def default_rule @@default_rule end def self.default_rule(rule) @@default_rule = rule end def self.rule(rule) @@rules ||= [] @@rules << rule end ... Wednesday, July 3, 13
  • 21. ActiveRecord-style ... def run_rules(i) results = rules.map{|rule| rule.call(i) } results.delete_if(&:nil?) results << default_rule.call(i) if results.empty? results end def each(&block) range.each do |i| yield run_rules(i).join('') end end def print each do |item| puts item end end end Wednesday, July 3, 13
  • 22. I can haz DSL. class FizzBuzz < Bazinator default_rule ->(i){ i } rule ->(i){"Fizz" if i % 3 == 0 } rule ->(i){"Buzz" if i % 5 == 0 } end FizzBuzz.new(1..100).print Wednesday, July 3, 13
  • 24. Let’s try some C #include <stdio.h> #include <string.h> const char* fizz(int i) { if((i % 3) == 0) { return "Fizz"; } else { return ""; } } const char* buzz(int i) { if((i % 5) == 0) { return "Buzz"; } else { return ""; } } Wednesday, July 3, 13
  • 26. Function Pointers++#define MAX_BUFF 10 #define NUMBER_FUNCTIONS 2 const char* (*dispatch[NUMBER_FUNCTIONS])(int i) = {fizz,buzz}; void itoa(int i, char* a) { snprintf(a, sizeof(a), "%i", i); } void clear(char* s) { strncpy(s, "", sizeof(s)); } void run_functions(int i, char* result) { int f; for(f=0; f < NUMBER_FUNCTIONS; f++) { strlcat(result,(*dispatch[f])(i),sizeof(result)); } } void add_number(int i, char* result) { char number[MAX_BUFF]; if(strnlen(result,sizeof(result)) < 1) { itoa(i,number); strlcat(result,number,sizeof(result)); } } Wednesday, July 3, 13
  • 27. Somewhere, Knuth is crying. int main(){ char result[MAX_BUFF]; int i; for(i=1; i < 100; i++) { clear(result); run_functions(i, result); add_number(i, result); printf("%sn", result); } return 0; } Wednesday, July 3, 13
  • 28. But wait, Java is missing out. package com.sentientmonkey.fizzbuzz; import com.sentientmonkey.fizzbuzz.service.FizzBuzzService; public class Runner { public static void main(String[] args) { FizzBuzzService fizzBuzz = new FizzBuzzService(); fizzBuzz.print(1, 100); } } Wednesday, July 3, 13
  • 29. Best thing is the patterns. Wednesday, July 3, 13
  • 30. First you get the Service Facade. package com.sentientmonkey.fizzbuzz.service; import com.sentientmonkey.fizzbuzz.rules.*; public class FizzBuzzService { RuleManager ruleManager; public FizzBuzzService() { ruleManager = new RuleManager(); ruleManager.addRule(new FizzRule()); ruleManager.addRule(new BuzzRule()); ruleManager.addRule(new DefaultRule()); } public void print(int start, int end) { for (int i = start; i <= end; i++) { System.out.println(ruleManager.evaluateRules(i)); } } } Wednesday, July 3, 13
  • 31. Then you get the Manager. package com.sentientmonkey.fizzbuzz.rules; import java.util.ArrayList; public class RuleManager { ArrayList<Rule> rules; public RuleManager() { rules = new ArrayList<Rule>(); } public void addRule(Rule rule) { rules.add(rule); } public String evaluateRules(int number) { StringBuilder builder = new StringBuilder(); for (Rule rule : rules) { rule.withBuilder(builder).append(number); } return builder.toString(); } } Wednesday, July 3, 13
  • 32. Now you get a Builder. package com.sentientmonkey.fizzbuzz.rules; public abstract class Rule { StringBuilder builder = null; public Rule withBuilder(StringBuilder builder) { this.builder = builder; return this; } public abstract void append(int number); } Wednesday, July 3, 13
  • 33. Default Rule. package com.sentientmonkey.fizzbuzz.rules; public class DefaultRule extends Rule { @Override public void append(int number) { if (builder.length() == 0) { builder.append(number); } } } Wednesday, July 3, 13
  • 34. Fizz Rule. package com.sentientmonkey.fizzbuzz.rules; public class FizzRule extends Rule { @Override public void append(int number) { if ((number % 3) == 0) { builder.append("Fizz"); } } } Wednesday, July 3, 13
  • 35. Buzz Rule. package com.sentientmonkey.fizzbuzz.rules; public class BuzzRule extends Rule { @Override public void append(int number) { if ((number % 5) == 0) { builder.append("Buzz"); } } } Wednesday, July 3, 13
  • 36. le java. package com.sentientmonkey.fizzbuzz; import com.sentientmonkey.fizzbuzz.service.FizzBuzzService; public class Runner { public static void main(String[] args) { FizzBuzzService fizzBuzz = new FizzBuzzService(); fizzBuzz.print(1, 100); } } Wednesday, July 3, 13
  • 37. Don’t forget clojure. (defn fizz [i] (if(= (mod i 3) 0) "Fizz")) (defn buzz [i] (if(= (mod i 5) 0) "Buzz")) (defn join [a b] (str a b)) (defn run-rules [numbers] (map join (map fizz numbers) (map buzz numbers))) (defn number [s n] (if (= s "") n s)) Wednesday, July 3, 13
  • 38. Something clever about McCarthy. (defn fizzbuzz [start end] (let [numbers (range start end)] (doall (map println (map number (run-rules numbers) numbers))))) (fizzbuzz 1 100) Wednesday, July 3, 13
  • 39. or javascript function fizz(i) { if((i % 3) == 0){ return "Fizz"; } } function buzz(i) { if((i % 5) == 0){ return "Buzz"; } } var rules = [fizz,buzz]; Wednesday, July 3, 13
  • 40. or javascript. function default_rule(result,i){ if(result.length > 0) { return result; } else { return i; } } function fizz_buzz(start,end) { for(var i=start; i<=end; i++) { var result = rules.map(function(rule){ return rule(i); }).join(''); print(default_rule(result,i)); } }; fizz_buzz(1,100); Wednesday, July 3, 13
  • 41. So what did we learn? • Interviewees: Even fizzbuzz can be fun • Interviewers:Ask better interview questions • Everyone: Coding is still hard Wednesday, July 3, 13
  • 42. Thanks! Play along at home: http://github.com/sentientmonkey/fizzbuzz Questions? @swindsor P.S. we’re hiring Wednesday, July 3, 13