Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fizzbuzz variants in SQL, Scala and Python, displayed as tables in Apache Zeppelin
//idiomatic scala version
val fizzbuzz: (Int => String) = (i) => (i % 3, i % 5) match {
case (0, 0) => "fizzbuzz"
case (0, _) => "fizz"
case (_, 0) => "buzz"
case _ => ""
}
1 to 100 map fizzbuzz
%sql
with
yo as (SELECT 1 as a UNION ALL SELECT 1 as a)
,yo2 as (SELECT y1.a from yo as y1 CROSS JOIN yo)
,yo3 as (SELECT y1.a from yo2 as y1 CROSS JOIN yo2)
,yo4 as (SELECT y1.a from yo3 as y1 CROSS JOIN yo3)
SELECT
rownum,
CASE
WHEN rownum % 15 = 0 THEN 'FIZZBUZZ'
WHEN rownum % 3 = 0 THEN 'FIZZ'
WHEN rownum % 5 = 0 THEN 'BUZZ'
ELSE ''
END as Solution
FROM
(
SELECT row_number() OVER (Order by a) as rownum from yo4 LIMIT 100
)
print """%table\nNumber\tSolution\n"""
for i in xrange(1,101):
if i % 15 == 0:
print i,"\t", "fizzbuzz"
elif i % 3 ==0:
print i,"\t", "fizz"
elif i % 5 == 0:
print i,"\t", "buzz"
else:
print i,"\t", "blank"
def zeppTable (x:Int, s:String):String = {
s"""$x\t$s\n"""
}
val fizzer: String = 1 to 100 map {x =>
if (x % 15 == 0) zeppTable(x, "fizzbuzz")
else if (x % 3 == 0) zeppTable(x, "fizz")
else if (x % 5 == 0) zeppTable(x, "buzz")
else zeppTable(x, "blank")
} mkString
z.getInterpreterContext.out.clear
print(s"""%table
Number\tSolution
""" + fizzer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.