Created
December 1, 2023 08:33
-
-
Save sys9kdr/cac98b6892f79513fb23e3bbc165db89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FizzBuzz { | |
public static inline var FIZZ = "Fizz"; | |
public static inline var BUZZ = "Buzz"; | |
private var s:String; | |
public function new(s:String) { | |
this.s = s; | |
} | |
public function toString():String { | |
var b = fizz(s) | buzz(s); | |
var r = b ^ 3; | |
var n = (r & 1) * FIZZ.length + ((r >> 1) & r) * BUZZ.length; | |
var l = ((r >> 1) & r) * s.length + (b & 1) * FIZZ.length | |
+ (b >> 1) * BUZZ.length; | |
return (FIZZ + BUZZ + s).substr(n, l); | |
} | |
function fizz(s:String):Int { | |
if (s.length == 1) { | |
switch (s.charCodeAt(0)) { | |
case 48, 51, 54, 57: | |
return 1; | |
default: | |
return 0; | |
} | |
} | |
var n = 0; | |
for (c in s.split('')) { | |
n += c.charCodeAt(0) - 48; | |
} | |
return fizz(Std.string(n)); | |
} | |
function buzz(s:String):Int { | |
switch (s.charCodeAt(s.length - 1)) { | |
case 48, 53: | |
return 2; | |
default: | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment