Έχουν δημοσιευτεί Τετάρτη, 25 Ιουνίου 2008 8:38 μμ από το μέλος PALLADIN

F# numeric_textual small break

Χθες μετά το 19o community event... σε συζήτηση που είχα με τους φίλους (Γιώργο Καπνιά, Παναγιώτη Καναβό και Νίκο Κανελλόπουλο) επεσε στο τραπέζι η ιδέα να γράψω μια έκδοση της Function ΟΛΟΓΡΑΦΩΣ σε F#...

Πριν από λίγο "έκλεψα" 30 λεπτά από την δουλειά μου και είπα να "ξεσκάσω" λίγο με κάτι small and fun... κατέληξα σε αυτό...

let one_digit n = 
   match n with
   | '1' -> "ΕΝΑ"
   | '2' -> "ΔΥΟ"
   | '3' -> "ΤΡΙΑ"
   | '4' -> "ΤΕΣΣΕΡΑ"
   | '5' -> "ΠΕΝΤΕ"
   | '6' -> "ΕΞΙ"
   | '7' -> "ΕΠΤΑ"
   | '8' -> "ΟΚΤΩ"
   | '9' -> "ΕΝΝΕΑ"
   | _ -> ""

let two_digits n = 
   match n with
   | (one, two) -> match one with
                  | '1' -> match two with
                           | '1' -> "ΕΝΤΕΚΑ"
                           | '2' -> "ΔΩΔΕΚΑ"
                           | _ -> "ΔΕΚΑ" + (one_digit two)
                  | '2' -> "ΕΙΚΟΣΙ" + (one_digit two)
                  | '3' -> "ΤΡΙΑΝΤΑ" + (one_digit two)
                  | '4' -> "ΣΑΡΑΝΤΑ" + (one_digit two)
                  | '5' -> "ΠΕΝΗΝΤΑ" + (one_digit two)
                  | '6' -> "ΕΞΗΝΤΑ" + (one_digit two)
                  | '7' -> "ΕΒΔΟΜΗΤΑ" + (one_digit two)
                  | '8' -> "ΟΓΔΟΝΤΑ" + (one_digit two)
                  | '9' -> "ΕΝΕΝΗΝΤΑ" + (one_digit two) 
                  | '0' -> (one_digit two)
                  | _ -> ""

let three_digits n = 
   match n with
   | (one, two, three) -> match one with
                           | '1' -> "ΕΚΑΤΟ " + (two_digits (two, three))
                           | '2' -> "ΔΙΑΚΟΣΙΑ " + (two_digits (two, three))
                           | '3' -> "ΤΡΙΑΚΟΣΙΑ " + (two_digits (two, three))
                           | '4' -> "ΤΕΤΡΑΚΟΣΙΑ " + (two_digits (two, three))
                           | '5' -> "ΠΕΝΤΑΚΟΣΙΑ " + (two_digits (two, three))
                           | '6' -> "ΕΞΑΚΟΣΙΑ " + (two_digits (two, three))
                           | '7' -> "ΕΠΤΑΚΟΣΙΑ " + (two_digits (two, three))
                           | '8' -> "ΟΚΤΑΚΟΣΙΑ " + (two_digits (two, three))
                           | '9' -> "ΕΝΝΙΑΚΟΣΙΑ " + (two_digits (two, three))
                           | '0' -> (two_digits (two, three))
                           | _ -> ""

let four_digits n = 
   match n with
   | ('1', two, three, four) -> "ΧΙΛΙΑ " + (three_digits (two, three, four))
   | (one, two, three, four) -> (one_digit one) + " ΧΙΛΙΑΔΕΣ " + (three_digits (two, three, four))

let five_digits n =
   match n with
   | ('1', two, three, four, five) -> (two_digits ('1', two)) + " ΧΙΛΙΑΔΕΣ " + (three_digits (three, four, five))
   | (one, two, three, four, five) -> match (one, two) with
                                       | ('0', '0') -> three_digits (three, four, five)
                                       | ('1', '1') -> (two_digits ('1', two)) + " ΧΙΛΙΑΔΕΣ " + (three_digits (three, four, five))
                                       | ('1', '2') -> (two_digits ('1', two)) + " ΧΙΛΙΑΔΕΣ " + (three_digits (three, four, five))
                                       | ( _ , '1') -> (two_digits (one, '0')) + "MIA ΧΙΛΙΑΔΕΣ " + (three_digits (three, four, five))
                                       | ( _ , '3') -> (two_digits (one, '0')) + " ΤΡΕΙΣ ΧΙΛΙΑΔΕΣ " + (three_digits (three, four, five))
                                       | ( _ , '4') -> (two_digits (one, '0')) + " ΤΕΣΣΕΡΙΣ ΧΙΛΙΑΔΕΣ " + (three_digits (three, four, five))
                                       | ( _ , _ ) -> (two_digits (one, two)) + " ΧΙΛΙΑΔΕΣ " + (three_digits (three, four, five))

let six_digits n = 
   match n with
   | (one, two, three, four, five, six) -> match one with
                                          | '1' -> "EKATON " + (five_digits (two, three, four, five, six))
                                          | '2' -> "ΔΙΑΚΟΣΙΕΣ " + (five_digits (two, three, four, five, six))
                                          | '3' -> "ΤΡΙΑΚΟΣΙΕΣ " + (five_digits (two, three, four, five, six))
                                          | '4' -> "ΤΕΤΡΑΚΟΣΙΕΣ " + (five_digits (two, three, four, five, six))
                                          | '5' -> "ΠΕΝΤΑΚΟΣΙΕΣ " + (five_digits (two, three, four, five, six))
                                          | '6' -> "ΕΞΑΚΟΣΙΕΣ " + (five_digits (two, three, four, five, six))
                                          | '7' -> "ΕΠΤΑΚΟΣΙΕΣ " + (five_digits (two, three, four, five, six))
                                          | '8' -> "ΟΚΤΑΚΟΣΙΕΣ " + (five_digits (two, three, four, five, six))
                                          | '9' -> "ΕΝΝΙΑΚΟΣΙΕΣ " + (five_digits (two, three, four, five, six))
                                          | '0' -> (five_digits (two, three, four, five, six))
                                          | _ -> ""

let money_to_textual (n : int) =
   let internal_money_to_textual n = 
      match n with
      | [one] -> one_digit one
      | one :: two :: [] -> two_digits (one, two)
      | one :: two :: three :: [] -> three_digits (one, two, three)
      | one :: two :: three :: four :: [] -> four_digits (one, two, three, four)
      | one :: two :: three :: four :: five :: [] -> five_digits (one, two, three, four, five)
      | one :: two :: three :: four :: five :: six :: [] -> six_digits (one, two, three, four, five, six)
      | x :: xs -> ""
      | [] -> ""
   internal_money_to_textual (List.of_seq (n.ToString()))

 

Δεν έχω τεστάρει όλα τα corner cases... και ούτε υλοποίησα (millions) οποτε το αφήνω σαν άσκηση για όσους φίλους πειραματιστούν με τον κώδικα... Τα 30 λεπτά πέρασαν και δυστυχώς πρέπει να σταματήσω το hacking με την F#... back to work

      
Share


Ενημέρωση για Σχόλια

Αν θα θέλατε να λαμβάνετε ένα e-mail όταν γίνονται ανανεώσεις στο περιεχόμενο αυτής της δημοσίευσης, παρακαλούμε γίνετε συνδρομητής εδώ

Παραμείνετε ενήμεροι στα τελευταία σχόλια με την χρήση του αγαπημένου σας RSS Aggregator και συνδρομή στη Τροφοδοσία RSS με σχόλια

Σχόλια:

Χωρίς Σχόλια

Ποιά είναι η άποψή σας για την παραπάνω δημοσίευση;

(απαιτούμενο)
απαιτούμενο
προαιρετικό
απαιτούμενο
ÅéóÜãåôå ôïí êùäéêü:
CAPTCHA Image