Skip to content
Snippets Groups Projects
Commit c259d904 authored by MICHOUD KEVIN's avatar MICHOUD KEVIN
Browse files

Merge branch 'solution'

parents 8a039a4d 0ca3e43d
Branches
No related merge requests found
......@@ -118,8 +118,5 @@ Un dictionnaire des mots valide dans le scrabble français est aussi fourni dans
- **Étape 2** : Ajoutez des tests supplémentaires, en utilisant par exemple `pytest` ou `unittest`.
- **Étape 3** : Ecrivez la fonction.
## Pour la suite...
Ecrivez des tests sur les fonctions python de votre book.
\ No newline at end of file
- **Étape 3** : Ecrivez la fonction.
def load_dic(path):
"""
Loads a list of words from a specified file.
Parameters:
- path (str): The path to the file containing the list of words, one per line.
Returns:
- list: A list of words extracted from the file.
Notes:
- The function assumes that the file encoding is UTF-8.
- It also assumes that each word is on a separate line in the file.
Example:
>>> load_dic("data/sample.txt")
['aalenien', 'aalenienne', 'aaleniennes']
"""
lst = []
with open(path, "r", encoding="UTF-8") as f:
for row in f:
print(row)
lst.append(row.strip().lower())
break
return lst
def french_scrabble_score(word):
# Point values for each letter in French Scrabble
letter_values = {
'a': 1, 'e': 1, 'i': 1, 'o': 1, 'u': 1, 'l': 1, 'n': 1, 'r': 1, 's': 1, 't': 1,
'a': 1, 'e': 1, 'i': 1, 'o': 1, 'u': 1, 'l': 1, 'n': 1, 'r': 1, 's': 1,
't': 1,
'd': 2, 'g': 2, 'm': 2,
'b': 3, 'c': 3, 'p': 3,
'f': 4, 'h': 4, 'v': 4,
......@@ -9,4 +38,21 @@ def french_scrabble_score(word):
'k': 10, 'w': 10, 'x': 10, 'y': 10,
'z': 10
}
pass
dico = load_dic("data/dico.txt")
# Check if the word is in the dictionary
if word not in dico:
return -1
# Check if the word contains any accents
for char in word:
if char not in letter_values:
raise ValueError(
f"Letter '{char}' not handled by the function.")
# Calculate the score
score = 0
# Convert the word to lowercase to ensure matching
for letter in word.lower():
score += letter_values.get(letter, 0)
return score
from unidecode import unidecode
def remove_french_accents(input_string):
"""
Removes French accents from a given input string using unidecode.
Args:
input_string (str): The input string containing French accents.
Returns:
str: The input string with accents removed.
Examples:
>>> remove_french_accents("été")
'ete'
>>> remove_french_accents("déjà vu")
'deja vu'
"""
return unidecode(input_string)
def load_dic(path):
"""
Loads a list of words from a specified file.
Parameters:
- path (str): The path to the file containing the list of words, one per line.
Returns:
- list: A list of words extracted from the file.
Notes:
- The function assumes that the file encoding is UTF-8.
- It also assumes that each word is on a separate line in the file.
Example:
>>> load_dic("data/sample.txt")
['aalenien', 'aalenienne', 'aaleniennes']
"""
lst = []
with open(path, "r", encoding="UTF-8") as f:
for row in f:
lst.append(row.strip().lower())
return lst
def french_scrabble_score(word):
"""
Calculates the French Scrabble score for a given word.
The French Scrabble score is calculated based on the point values for each letter in French Scrabble.
Args:
word (str): The word for which the score needs to be calculated.
Returns:
int: The French Scrabble score for the input word, or -1 if the word is not in the French dictionary.
Examples:
>>> french_scrabble_score("été")
3
>>> french_scrabble_score("xylophone")
32
>>> french_scrabble_score("œil")
4
>>>
Raises:
ValueError: If the word contains a character that is not handled by the function.
"""
letter_values = {
'a': 1, 'e': 1, 'i': 1, 'o': 1, 'u': 1, 'l': 1, 'n': 1, 'r': 1, 's': 1,
't': 1,
'd': 2, 'g': 2, 'm': 2,
'b': 3, 'c': 3, 'p': 3,
'f': 4, 'h': 4, 'v': 4,
'j': 8, 'q': 8,
'k': 10, 'w': 10, 'x': 10, 'y': 10,
'z': 10
}
decoded_lower_word = remove_french_accents(word.lower())
dico = load_dic("data/dico.txt")
# Check if the word is in the dictionary
if decoded_lower_word not in dico:
return -1
# Check if the word contains any accents
for char in decoded_lower_word:
if char not in letter_values:
raise ValueError(
f"Letter '{char}' not handled by the function.")
# Calculate the score
score = 0
# Convert the word to lowercase to ensure matching
for letter in decoded_lower_word.lower():
score += letter_values[letter]
return score
if __name__ == "__main__":
import doctest
doctest.testmod()
import pytest
from solution_exo2 import load_dic, french_scrabble_score, remove_french_accents
def test_accents():
assert remove_french_accents("été") == "ete"
def test_load_dic():
sample_fr = load_dic("data/sample.txt")
assert sample_fr == ['aalenien', 'aalenienne', 'aaleniennes']
def test_word_in_dic():
dico_fr = load_dic("data/dico.txt")
assert "xylophone" in dico_fr
def test_valid_word():
assert french_scrabble_score("bonjour") == 16
def test_word_with_accents():
assert french_scrabble_score("été") == 3
def test_word_not_in_dictionary():
assert french_scrabble_score("xylophone") == 32
def test_word_ligature():
assert french_scrabble_score("œil") == 4
# You can add more test cases as needed
if __name__ == "__main__":
pytest.main()
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment