Skip to content

Instantly share code, notes, and snippets.

@denlab
Created January 6, 2013 20:16
Show Gist options
  • Save denlab/4469890 to your computer and use it in GitHub Desktop.
Save denlab/4469890 to your computer and use it in GitHub Desktop.
Devoxx 2011 - Code Story - Selection - 1st exercice: FooBarQix
(ns cljsta.misc.code-story-2012
(:use [midje.sweet]
[clojure.pprint :only [pprint print-table]]
[clojure.string :only [split join]]
[clojure.repl :only [doc]]
[table.core :only [table]])
(:require [clojure
[string :as str]
[set :as set]
[walk :as w]
[xml :as xml]
[test :as t]]
[clojure.java
[shell :as sh]
[io :as io]]))
;; Premier exercice de sélection, FooBarQix
;; 16/11/2011
;; Ecrivez un programme qui affiche les nombres de 1 à 100. Un nombre par ligne. Respectez les règles suivantes :
;; Si le nombre est divisible par 3 ou contient 3, écrire “Foo” à la place de 3.
;; Si le nombre est divisible par 5 ou contient 5, écrire “Bar” à la place de 5.
;; Si le nombre est divisible par 7 ou contient 7, écrire “Qix” à la place de 7.
;; Voici un exemple de rendu
;; 1
;; 2
;; FooFoo
;; 4
;; BarBar
;; Foo
;; QixQix
;; 8
;; Foo
;; Bar
;; ...
;; Mise à jour : clarifications sur les règles
;; On regarde les diviseurs avant le contenu (ex: 51 -> FooBar)
;; On regarde le contenu dans l’ordre où il apparait (ex: 53 -> BarFoo)
;; On regarde les multiples dans l’ordre Foo, Bar puis Qix (ex: 21 -> FooQix)
;; 13 contient 3 donc s’écrit “Foo”
;; 15 est divisible par 3 et 5 et contient un 5 donc s’écrit “FooBarBar”
;; 33 contient deux fois 3 et est divisible par 3 donc s’écrit “FooFooFoo”
;; A vous de jouer !
;; implem (general functions) -------------------------------------------------
(defn- multiple-of? "Predicate that tells if n is divisble by d"
[n d] (zero? (rem n d)))
(defn- update-map-keys "Update the keys of the given map with the given function"
[m f] (->> m
(map #(update-in % [0] f))
(into {})))
;; implem (FooBarQix functions) -----------------------------------------------
(def #^{:doc "Mapping table between numbers and FooBarQix"
:private true} num->fbq
{3 "Foo"
5 "Bar"
7 "Qix"})
(defn- first-part "Calculate the first part (with the divisors) of the FooBarQix string"
[n] (map #(if (multiple-of? n (% 0)) (% 1))
num->fbq))
(defn- second-part "Calculate the second part (with the presence of numbers) of the FooBarQix string"
[n] (->> n
str
(map (update-map-keys num->fbq (comp first str)))))
(defn foobarqix "Main FooBarQix function"
[n] (let [fbq (reduce str (concat (first-part n) (second-part n)))]
({"" (str n)} fbq fbq)))
;; tests ----------------------------------------------------------------------
(def foobarqix-seq
[[1 "1" ]
[2 "2" ]
[3 "FooFoo" ]
[4 "4" ]
[5 "BarBar" ]
[6 "Foo" ]
[7 "QixQix" ]
[8 "8" ]
[9 "Foo" ]
[10 "Bar" ]
[13 "Foo" ]
[15 "FooBarBar" ]
[21 "FooQix" ]
[33 "FooFooFoo" ]
[53 "BarFoo" ]])
(t/deftest foobarqix-test
(t/is (= (map (fn [x] [x (foobarqix x)])
(map first foobarqix-seq))
foobarqix-seq)))
(comment
(t/run-tests))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment