Skip to content

Instantly share code, notes, and snippets.

@gabriel-dehan
Last active December 13, 2016 14:46
Show Gist options
  • Save gabriel-dehan/3863e7a2d2bd4caf2c64a35717a04493 to your computer and use it in GitHub Desktop.
Save gabriel-dehan/3863e7a2d2bd4caf2c64a35717a04493 to your computer and use it in GitHub Desktop.
exerices - jour 2
=begin
def array_to_hash(array)
hash = {}
i = 0
array.each do |element|
hash[index] = element
i += 1
end
hash
end
=end
def array_to_hash(array)
hash = {}
array.each_with_index do |element, index|
hash[index] = element
end
hash
end
names = ["George", "Mickael", "Roger"]
hash = array_to_hash(names)
p hash
# => { 0 => "George", 1 => "Mickael", 2 => "Roger" }
def sum_odd_indexed(array)
# TODO: Faire la somme de tout les éléments dans le tableau dont l'index est un nombre impair
# Astuce: Utiliser Enumerable#each_with_index
sum = 0
array.each_with_index do |number, index|
if index.odd?
sum += number
end
end
sum
end
array = [10, 23, 47, 12]
# 0 1 2 3
# puts sum_odd_indexed(array) # => 35
def even_numbers(array)
array.select { |number| number.even? }
end
# p even_numbers([1,2,3,4,5,6]) # => [2, 4, 6]
def short_words(array, max_length)
array.reject { |word| word.length >= max_length }
# TODO: En partant d'un tableau de mots, retourner ceux qui sont moins long que `max_length`
# Astuce: Utiliser Enumerable#reject
end
fruits = ["raisins", "ananas", "mangues", "kiwi", "poire"]
# p short_words(fruits, 6) # => [kiwi, poire]
def first_under(array, limit)
array.find { |number| number <= limit }
# TODO: Retourner le premier chiffre dans le tableau qui est en dessous de la `limit`
# Astuce: Utiliser Enumerable#find
end
numbers = [50, 70, 23, 47, 12]
# 0 1 2 3
# puts first_under(numbers, 30) # => 10
def add_bang(array)
array.map { |word| word + "!" }
# TODO: En partant d'un tableau de mots, retourner un nouveau tableau avec un `!` à la fin de chaque mot.
# Astuce: Utiliser Enumerable#map
end
words = %w(banane kiwi peche mangue)
# add_bang(words) # => [banane!, kiwi!, peche!, mangue!]
def array_ordered(array)
array.sort
# TODO: En partant d'un tableau de mots, retourner ce tableau trié dans l'ordre alphabétique
# Astuce: N/A
end
words = %w(kiwi peche mangue banane)
p array_ordered(words)
def array_together(array)
# array * ", "
array.join(", ")
# TODO: En partant d'un tableau de mots, concatenez tous les éléments du tableau en une chaîne de caractère, chaque élément séparé par une `,`.
end
words = %w(kiwi peche mangue banane)
p array_together(words) # => "kiwi, peche, mangue, banane"
# On prend un mot
# Si ca commence par une voyelle on rajoute un L
# devant et un suffixe a la fin
# Si ca commence par une consonne on prend le premier
# groupe de consonne
# et on le met a la fin du mot
# On rajoute un L en debut de mot
# Et on rajoute un suffixe aleatoire
# en fin de mot parmis les suivants
# -em/ème, -ji, -oc, -ic, -uche, -ès
def translate_louchebem(word)
vowels = %w(a e i o u y)
letters = word.split("")
first_vowel_index = letters.find_index do |letter|
vowels.include?(letter)
end
if first_vowel_index.zero?
# Notre mot commence par une voyelle
new_word = letters
else
# Notre mot commence par une consonne
consonant_group = letters[0...first_vowel_index]
new_word = letters[first_vowel_index...letters.size]
new_word += consonant_group
end
final_world = "l" + new_word.join + random_suffix()
end
def random_suffix
%w(em ème ji oc ic uche ès).sample
end
def translate_sentence(sentence)
result = sentence.downcase.split(" ").map do |word|
translate_louchebem(word)
end
result.join(" ").capitalize
end
p translate_sentence("Bonjour tout le monde")
p translate_louchebem("chat")
p translate_louchebem("achat")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment