éFizzBuzzåé¡ (Inverse FizzBuzz)
just another scala quantãæ¥æ¬èªã«ãã¾ããã
ã¡ãªã¿ã«ãç§ã®è§£ã¯ãã¡ãã«ã
- æåã®è§£ç
- はてブに書いた解答方針ãInverse Fizzbuzz (FizzBuzzの逆関数) - Qiita - ä¸ããããç¯å²å ã®ãã¹ã¦ã®è§£ãæ°ãä¸ãã¦ã¾ãã
- ãã£ã¨ç°¡æ½ãªè§£ç
- 逆FizzBuzz問題 解きなおし - Qiita
ããã§ã¯ãåé¡ã®æ¥æ¬èªè¨³ãã©ããã
éFizzbuzzåé¡
2012å¹´ã§ã¯ãªãã2016å¹´ã®ã話ã
ä¸ã®ä¸ã¯å¤§ãã¦å¤ãã£ã¦ããªãã
OOPã¨æ¸ãæãå¯è½ãªãªãã¸ã§ã¯ãã«ãã£ã¦ä½åº¦ãã²ã©ãç®ã«ãã£ãå¾ãä¸çã¯ãã£ã¨ã®ãã¨ã§John Hughesの考察が正しかったことã«æ°ã¥ãã颿°åããã°ã©ãã³ã°ã«ç§»è¡ãããGoogleã¯Typesafe社ãè²·åããScalaãAndroidä¸ã§ãã¤ãã£ãã«åä½ããããã«ãªã£ã¦ãããGoogleã«è² ããå£ãããAppleã¯Haskellã§æ¸ããã¦ããªãiOSã¢ããªãç¦æ¢ãã¦ãããWindowsã®ä¸çã¯ãã¹ã¦F#ã§æ¸ãããããã«ãªã£ã¦ããã
ããããé¤ãã°ãä¸ã®ä¸ã¯å¤§ãã¦å¤ãã£ã¦ããªãã
The canonicalã®Shipperさんã¯ãã³ã·ã«ããã¢å¤§å¦ã忥ããæG社ã®ãã¯ã¤ããã¼ã颿¥ãåãã¦åããã¨ããã ã
Google: ã¨ããã§ShipperãããFizzBuzzã¯ãåç¥ã§ããï¼
Shipper: ä½ã®åè«ã§ããï¼ãã2016å¹´ã§ããã2012å¹´ãããªããã§ããããFizzBuzzãªãã¦èª°ã ã£ã¦ç¥ã£ã¦ã¾ããåãã®ãã°ãã¡ããã§ããç¥ã£ã¦ã¦ããããããªãã
Google: ãããã¾ãããã§ã¯FizzBuzzãå°ãæ¹å¤ããåé¡ããã£ã¦ã¿ã¾ãããã
åé¡: å·¦ã®ãªã¹ããå³ã®ãªã¹ãã«ååããã«ã¯ã©ããã¾ããï¼

Shipper: ããã¨ãå·¦ã®ãªã¹ãã®ä¸èº«ãå³ã®ãªã¹ãã®ä¸èº«ã«å¯¾å¿ä»ããã£ã¦ãã¨ã§ããã
Google: ããã
Shipper: ãããããã¹ã¦ã®è¦ç´ ã§ã¯ãªãã3ã5ã®åæ°ã ããªãã§ããã
Google: ããã§ãã
Shipper: Partial Function(é¨å颿°)ã§ããããã ãªã
Google: Partial Functionã¨ã¯ä½ã§ããï¼
Shipper: ã¾ãã¯ããã«ãæ¡ä»¶ãæºããè¿°èªé¢æ°ã¯ãããªãã¾ãã
scala> val isFizzBuzz = ((x:Int) => x%3 ==0 || x%5 ==0)
Google: ããã§ãããããã§ã¯ãããã©ãå©ç¨ããã®ã§ããï¼
Shipper: 次ã«ãååãå®ç¾©ããå¥ã®ã©ã ããä½ãã¾ãã
scala> val toFizzBuzz = ((x:Int) => (x%3,x%5) match {
case (0,0) => "fizzbuzz"
case (0,_) => "fizz"
case _ => "buzz"
})Google: çµæ§ãããªãã¯ãã¿ã¼ã³ãããã®ä½¿ãæ¹ããåç¥ã¨ãããã¨ã§â¦â¦
Shipper: ãã®ããä»å¹´ã¯2016å¹´ã§2012å¹´ãããªãã¨è¨ãã¾ããããï¼èª°ã ã£ã¦å©ã ã£ã¦â¦â¦
Google: ã¯ããã¯ãããããã¾ãããããã§ã¯Partial Functionã説æãã¦ããã ãã¾ããï¼
Shipper: Partial Functionã¯è¿°èªé¢æ°ã¨ååãã¾ã¨ããã ãã®ãã®ã§ããããã¦ã
scala> val fizzbuzz = new PartialFunction[Int,String] {
def isDefinedAt(x:Int) = isFizzBuzz(x)
def apply(x:Int) = toFizzBuzz(x)
}ããããã ãã§ããã®Partial Functionãå®ç¾©ããã¦ããç¯å²ã®ã¿ã®çµæãéãããã¨ãã§ãã¾ãã
scala> (1 to 100).collect( fizzbuzz )
res1: scala.collection.immutable.IndexedSeq[String] = Vector(fizz, buzz, fizz,fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizz,buzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz)
Google: ãã©ãã¼ï¼ãããã«æ¸ãã¾ãããã
Shipper: ã§ãããï¼ï¼
Google: ãã ã¡ãã£ã¨æ°ã«ãªããã§ãããShipperããããªãPartial Functionãå¿
è¦ãªãã§ããï¼ãã£ã¨åç´ã«ã§ããªããã®ã§ããããï¼
Shipper: ã¤ã¾ããmapã¨filterã¨ãï¼
Google: ããã§ããããã£ã¦ã¿ã¦ãããã¾ããï¼
Shipper: ããã§ããã°ãã¾ãè¿°èªé¢æ°ã§filterãããã¦ãååãé©ç¨ãããã¨ã«ãªãã¾ãã
scala> (1 to 100).filter( isFizzBuzz ).map( toFizzBuzz )
res2: scala.collection.immutable.IndexedSeq[String] = Vector(fizz, buzz, fizz,fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizz,buzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz)
Google: ããã§ããï¼ã§ãã¾ã ä»ã«ãããæ¹ãããã¾ããã
Shipper: ãã¼ãâ¦â¦
Google: éåãæãåºãã¦ãã ãããæ°å¦ã§ã¯ã©ããã£ã¦éåãå®ç¾©ãã¾ããï¼
Shipper: ãããリスト内包ã£ã¦ãã¨ã§ããï¼
Google: ãã®ã¨ããï¼
Shipper:
scala> for( x<- (1 to 100); if( isFizzBuzz(x))) yield toFizzBuzz(x)
res3: scala.collection.immutable.IndexedSeq[String] = Vector(fizz, buzz, fizz,fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizz,buzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz)
Google: ç´ æ´ãããï¼ããªãã¯FizzBuzzã®ããæ¹ãå°ãªãã¨ã3種é¡ããã£ã¦ãããã¨ã«ãªãã¾ãã
Shipper: ããã§æ¡ç¨æ±ºå®ã§ããï¼
Google: ããã¦ãªãã§ãShipperãããã¾ã æ°ãæ©ãã§ãï¼ããã¯Googleãªãã§ãããã颿¥ã«æ¥ã¦FizzBuzzãè§£ãã人å
¨å¡ãéãããã«ã¯ããã¾ãããããã2016å¹´ãªãã§ãããã誰ã§ãéããã¨ã«ãªã£ã¦ãã¾ãã¾ãã
Shipper: ã§ã¯â¦â¦
Google: ããã§ãããGoogleã§ã¯æ°ãããã¹ããèãã¾ãããéFizzBuzzã¨ããååã§ãã
Shipper: éFizzBuzzï¼
Google: ããã§ããï¼èãããã¨ããªãã¨ããæ§åã§ãï¼
Shipper: ã¯ããã¾ã£ããï¼
Google: ããã¯ã§ãããç§ãã¡ãå
é±èããã°ãããªãã§ããã§ãããã¾ã§é£ããã¯ãªãã§ãããæ°å¦ã«ãããéã¨ã¯ä½ãåããã¾ããï¼
Shipper: æ°å¦ã«ãããéâ¦â¦
Google: ããã§ãããã¨ãã°ã5ã«ä½ãè¶³ãã¨0ã«ãªãã¾ããï¼
Shipper: -5ã§ãã
Google: ã¤ã¾ã-5ã¨ã¯ï¼
Shipper: ããï¼ãã£ããããããã¨ãåããã¾ããããã¤ãã¹5 ã¯å æ³ã«é¢ãã5ã®éå
ã§ãã
Google: ã§ã¯ä¹æ³ã«é¢ãã5ã®éå
ã¯ï¼
Shipper: ãã¼ã¨ã0.2ã§ãã0.2ã5åããã°1ã«ãªãã¾ãã
Google: æ£è§£ã§ãï¼1/2ã®éæ£å¼¦ã¯ï¼
Shipper: 30度ã§ããsin(30°) = 0.5 ãªã®ã§ã
Google: çµæ§ã1/2ã®é対æ°ã¯ï¼
Shipper: eã®å¹³æ¹æ ¹ã§ãã
Google: ããã§ãã¾ããï¼ã§ã¯éFizzBuzzã«æ»ãã¾ãããã
Shipper: ã¯ããéFizzBuzzã¯ã©ã®ããã«å®ç¾©ããã®ã§ããï¼
Google: åç´ã§ããããããªã¹ããä¸ããããã¨ãã«ãFizzBuzzãå®è¡ããã¨ãã®ãªã¹ããåºåãããããªæçã®é£ç¶æ°åãè¿ãã®ã§ãã
Shipper : ãï¼ï¼
Google: éFizzBuzzåé¡ããããªã¹ããä¸ããããã¨ãã«ãFizzBuzzãå®è¡ããã¨ãã®ãªã¹ããåºåãããããªæçã®é£ç¶æ°åãæ±ããã
Shipper: çè§£ã追ãã¤ãã¾ããã
Google: ã§ã¯åç´ãªä¾ããå§ãã¾ãããã{ fizz }ãåºåããæçã®é£ç¶æ°åã¯ï¼
Shipper: ãã¼ã¨ã{ 1,2,3 }ã§ãã
Google: Shipperãããã£ãããã¦ãã ããããã£ã¨ããçããããã§ãããã
Shipper: ãã{ 3 }ã§ããã®ãã
Google: { buzz }ãåºåããæçã®é£ç¶æ°åã¯ï¼
Shipper: { 5 }ã§ãã
Google: { fizz, buzz }ãåºåããæçã®é£ç¶æ°åã¯ï¼
Shipper: { 3, 4, 5 }ã
Google: 䏿£è§£ï¼
Shipper: ãããã¡ãã£ã¨å¾
ã£ã¦ãã ããâ¦â¦{ 9,10 }ã§ããã
Google: ç´ æ´ãããï¼ { buzz, fizz }ãåºåããæçã®é£ç¶æ°åã¯ï¼
Shipper: { 5,6 }ã
Google: { fizz, buzz, fizz }ãåºåããæçã®é£ç¶æ°åã¯ï¼
Shipper: { 3, 4, 5, 6 }ã
Google: { buzz, fizz, buzz }ãåºåããæçã®é£ç¶æ°åã¯ï¼
Shipper: ãã¼ã¨â¦â¦ããã¯ãªã¹ããæ£ãããªããããªâ¦â¦
Google: æ£è§£ã§ããããã°ã©ã ãæ¸ãã¨ãã¯ãããæ¤åºã§ããããã«ãã¦ãã ãããããå°ãç¶ãã¾ãããã{ fizz, fizz }ãåºåããã®ã¯ï¼
Shipper: { 6,7,8,9 }ã
Google: { fizz, fizz, buzz }ãªãï¼
Shipper: { 6,7,8,9,10}ã
Google: ã¯ããããã¾ã§ã®ä¾ãã¾ã¨ããã®ãããã§ããããªãã®ã©ã ããããã¹ããã¨ã¯ããã«ãªãã¾ãã

Google: ãããããã§éFizzBuzzã®ã©ã ããæ¸ãã¾ãããï¼ï¼
Shipper: ã§ãããã®åé¡ã®è§£ã¯ä¸æã§ã¯ãªãã§ãããã{fizz}ã¯{ 3 }ã«å¯¾å¿ä»ããããã ããããªãã¦ã{6}ã{9}ã«ã対å¿ãã¾ããâ¦â¦
Google: ããã§ããããªãã°ãè§£ã®ä¸ææ§ã¯åé¡ã«ãã¾ãããæãçãé£ç¶æ°ã§ããã°ãããã¨ã«ãã¾ãã
Shipper: ãã¿ã¾ãããåé¡ãããä¸åº¦æãã¦ãããã¾ããï¼
Google: ãããªã¹ããä¸ããããã¨ãã«ãFizzBuzzãå®è¡ããã¨ãã®ãªã¹ããåºåãããããªæçã®é£ç¶æ°åãæ±ããã
Shipper: æ£ç´ã«è¨ã£ã¦ãããããããã¾ããã
Google: ããã§ãããåé¡ã®è§£æ³ãåãããªãã¨ãã«ã¯ä½ããã¾ããï¼
Shipper: å
¨é¨è©¦ãã¦ã¿ã¾ããåããã£ã¦ãã¤ã§ãã
Google: ããã§ãããããªãã§ããï¼
Shipper: ãããåããã£ã¦è¨ã£ã¦ãï¼å
¥åã®æååã«å¯¾ãã¦æ°åã®ãã¿ã¼ã³ã¯ç¡éã«èããããã¨ããã°ã©ãããããããã§ããã
Google: ã§ã¯æåã®100åã®æ°ã ãã対象ã«ãã¾ãããã1ãã100ã¾ã§ã§ãããã£ãã®ããã«å°ããã¤è¡ãã¾ããããã1ãã100ã¾ã§ã§èããããæ°åã®çµã¿åããã¯ã©ããªãã¾ããï¼
Shipper: ããã¨ãFizzBuzzã«1ãä¸ãã¦ã次ã¯{1,2}ã§ããã®æ¬¡ã¯{1,2,3}ã§ããã®æ¬¡ã¯{1,2,3,4}ã§â¦â¦ã¨ããããã«ãã¦{1,2,....100}ã¾ã§ãèãããã¾ãã
Google: ããããã¨100éãã§ãããShipperãããã§ãFizzBuzzã2ããå§ãã¦ãããã§ããããæ¬¡ã¯{2,3}ããã®æ¬¡ã¯{2,3,4}ããã®æ¬¡ã¯{2,3,4,5}ãããã¦{2,....100}ã¾ã§ã
Google: ãã¦ãããã¯ä½éãããã¾ããï¼
Shipper: 99éãã§ãããããã¾ãããããããã¨ã100 + 99 + 98 + ...1 = (101)*100/2 = 5050éãã§ãã
Google: 5000åå¼·ãããããæ°ã§ã¯ãªãã§ãããã
Shipper: ããã§ãããããããéè¤ã¯æ¨ã¦ã¾ãããæçã®æ°åã¸ã®ååã ããæã£ã¦ããã°ããããã§ãã
Google: ããã§ããããããã°ãæçµçãªãªã¹ãã¯5050ããå°ãªããªãã¾ãããã
Shipper: ãããã¾ããããªãã ããã£ããè§£ããããªæ°ããã¦ãã¾ããã
scala> val all = (1 to 100).map(x=> (x to 100).map( y=> (x,y) )).flatten
all: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((1,1), (1,2),(1,3), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,10), (1,11), (1,12), (1,13), (1,14), (1,15), (1,16), (1,17), (1,18), (1,19), (1,20), (1,21), (1,22), (1,23), (1,24), (1,25), (1,26), (1,27), (1,28), (1,29), (1,30), (1,31), (1,32), (1,33), (1,34), (1,35), (1,36), (1,37), (1,38), (1,39), (1,40), (1,41), (1,42), (1,43),......(2,2),(2,3),......
scala> all.size
res22: Int = 5050
Google: ããã§ãã²ã¨ã¾ã5050éãã®ç¯å²ãå¾ããããã¨ã«ãªãã¾ããã
Shipper: ã¯ããã§ã¯ãã®5050éãã®ç¯å²ã«å¯¾ãã¦FizzBuzzãé©ç¨ãã¦ã¿ã¾ãã
scala> val results = all.map( x=> (x._1 to x._2).collect( fizzbuzz ))
Shipper: ããã¦ãå ¥åã¨åºåã並ã¹ã¦ã¿ã¾ãã
scala> val io = all.zip( results )
Shipper: 次ã«åºåã§ã°ã«ã¼ãã³ã°ãã¾ããããããã°ãæååã®ãªã¹ãããæ°åã®ãªã¹ãã¸ã®ååãå¾ãããã¯ãã§ãã
scala> val inverses = io.groupBy( x=> x._2)
Google: ãã®ãããã§ãæ£ããéçãé²ãã§ãããã©ãã確èªãã¾ããããååã®ãã¼ã®æ°ãæ°ãã¦ã¿ã¦ãã ããã5050ããå°ãããªã£ã¦ããã¯ãã§ããã
scala> inverses.keys.size
res36: Int = 302
Google: ããã§ããï¼ã¦ãã¼ã¯ãªãã¼ã¯302åã ãã5050ã§ã¯ããã¾ãããããã§ã¯ããã¼ã«å¯¾å¿ããæãçãå¤ã¯ã©ããã£ã¦æ¢ãã¾ããããï¼
Shipper: ã¾ã"fizz"ã§ãã¹ããã¦ã¿ã¾ãã
scala> inverses(Vector("fizz"))res28: scala.collection.immutable.IndexedSeq[((Int, Int), scala.collection.immutable.IndexedSeq[String])] = Vector(((1,3),Vector(fizz)), ((1,4),Vector(fizz)), ((2,3),Vector(fizz)), ((2,4),Vector(fizz)), ((3,3),Vector(fizz)), ((3,4),Vector(fizz)), ((6,6),Vector(fizz)), ((6,7),Vector(fizz)), ((6,8),Vector(fizz)), ((7,9),Vector(fizz)), ((8,9),Vector(fizz)), ((9,9),Vector(fizz)), ((11,12),Vector(fizz)), ((11,13),Vector(fizz)), ((11,14),Vector(fizz)), ((12,12),Vector(fizz)), ((12,13),Vector(fizz)), ((12,14),Vector(fizz)), ((16,18),Vector(fizz)), ((16,19),Vector(fizz)), ((17,18),Vector(fizz)), ((17,19),Vector(fizz)), ((18,18),Vector(fizz)), ((18,19),Vector(fizz)), ((21,21),Vector(fizz)), ((21,22),Vector(fizz)), ((21,23),Vector(fizz)), ((22,24),Vector(fizz)), ((23,24),Vector(fizz)), ((24,24),V...
Shipper: 確ãã«ããã¼ã«å¯¾å¿ãããã¹ã¦ã®ç¯å²ãæ±ãããã¦ãã¾ããå¿ è¦ãªã®ã¯æçã®ç¯å²ã ãã§ããminByã試ãã¦ã¿ã¾ãã
scala> inverses(Vector("fizz")).minBy(x=>x._1._2 - x._1._1)res33: ((Int, Int), scala.collection.immutable.IndexedSeq[String]) = ((3,3),Vector(fizz))
Shipper: ãããã§ããã¿ããã ï¼
scala> val answer = inverses.keys.map( k=> (k,inverses(k).minBy(x => x._1._2 - x._1._1)._1)).toMap
answer: scala.collection.immutable.Map[scala.collection.immutable.IndexedSeq[String],(Int, Int)] = Map(Vector(fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz) -> (3,33), Vector(buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz) -> (10,93), Vector(fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz, fizz, buzz, fizz, fizz, buzz, fizz, fizzbuzz) -> (15,75), Vector(fizz, buzz, fizz, fizzbuzz) -> (9,15), Vector(fizzbuzz, fizz, buzz, fizz, fizz, b...
Google: 7ã¤ã®ä¾ã®çãã試ãã¦ã¿ã¾ããã
scala> answer(Vector("fizz"))(3,3)
scala> answer(Vector("buzz"))(5,5)
scala> answer(Vector("fizz","fizz","buzz"))(6,10)
scala> answer(Vector("fizz","buzz"))(9,10)
scala> answer(Vector("buzz","fizz"))(5,6)
scala> answer(Vector("fizz","buzz","fizz"))(3,6)
scala> answer(Vector("fizz","fizz"))(6,9)
Shipper: ã§ããï¼
Google: 確ãã«ãã§ãã¾ãããï¼
Shipper: ããã§æ¡ç¨æ±ºå®ã§ããï¼
Google: ãã¼ããä»åã¯åããã§è§£ãã¾ãããããããã§ãããåããã§ãªãæ¹æ³ãæãã¤ãããæ¡ç¨ã¨ãã¾ãããã
Shipper: ãã¼ã§ããªãâ¦â¦ãã¼ã§ããªãâ¦â¦
追伸ï¼å®éã¯ãããªã«é£ãããªãã§ããã:)
ã§ã¯ãã¾ã¨ãã¾ãã
- åé¡
- éFizzBuzzåé¡ããããªã¹ããä¸ããããã¨ãã«ãFizzBuzzãå®è¡ããã¨ãã®ãªã¹ããåºåãããããªæçã®é£ç¶æ°åãæ±ããã
- åããã®è§£
val all = (1 to 100).map(x=> (x to 100).map( y=> (x,y) )).flatten val results = all.map( x=> (x._1 to x._2).collect( fizzbuzz )) val io = all.zip( results ) val inverses = io.groupBy( x=> x._2) val answer = inverses.keys.map( k=> (k,inverses(k).minBy(x => x._1._2 - x._1._1)._1)).toMap