Semaine 4, jour 2
This commit is contained in:
1
Semaine_04/Exercices/.python-version
Normal file
1
Semaine_04/Exercices/.python-version
Normal file
@@ -0,0 +1 @@
|
||||
3.12
|
||||
0
Semaine_04/Exercices/README.md
Normal file
0
Semaine_04/Exercices/README.md
Normal file
21
Semaine_04/Exercices/coke_kata.py
Normal file
21
Semaine_04/Exercices/coke_kata.py
Normal file
@@ -0,0 +1,21 @@
|
||||
"""
|
||||
An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.
|
||||
|
||||
Example: (Input --> Output)
|
||||
|
||||
"Dermatoglyphics" --> true
|
||||
"aba" --> false
|
||||
"moOse" --> false (ignore letter case)
|
||||
"""
|
||||
|
||||
def is_isogram(string):
|
||||
string = string.lower()
|
||||
for letter in string:
|
||||
if string.count(letter) > 1:
|
||||
return False
|
||||
return True
|
||||
|
||||
# Solution la plus opti
|
||||
|
||||
def is_isogram(string):
|
||||
return len(string) == len(set(string.lower()))
|
||||
72
Semaine_04/Exercices/exo_01.py
Normal file
72
Semaine_04/Exercices/exo_01.py
Normal file
@@ -0,0 +1,72 @@
|
||||
destinations = {
|
||||
"Pyongyang": 5,
|
||||
"Havana": 2,
|
||||
"Caracas": 7
|
||||
}
|
||||
|
||||
destinations["Moscow"] = 0
|
||||
destinations["Ho-Chi-Minh Town"] = 0
|
||||
|
||||
def add_destination(destinations: dict, town: str, number: int):
|
||||
if town not in destinations:
|
||||
destinations[town] = 0
|
||||
|
||||
def add_reservation(destinations: dict, town: str, number: int):
|
||||
if town in destinations:
|
||||
destinations[town] += number
|
||||
else:
|
||||
destinations[town] = number
|
||||
|
||||
def remove_reservation(destinations: dict, town: str):
|
||||
if town in destinations:
|
||||
new_destinations = dict(destinations)
|
||||
del new_destinations[town]
|
||||
return new_destinations
|
||||
|
||||
def show_destinations_available(destinations: dict):
|
||||
for town in destinations:
|
||||
if destinations[town] != 0:
|
||||
print(town + " : " + str(destinations[town]))
|
||||
|
||||
def show_destinations_by_reservations(destinations: dict):
|
||||
print(sorted(destinations)) # ["Pyongyang", "Havana", "Caracas"]
|
||||
print(sorted(destinations.keys())) # ["Pyongyang", "Havana", "Caracas"]
|
||||
print(sorted(destinations.values)) # [5, 2, 7]
|
||||
print(sorted(destinations.items)) # [("Pyongyang", 5), ("Havana", 2), ("Caracas", 7)]
|
||||
|
||||
def return_second_tuple_element(tuple):
|
||||
return tuple[1]
|
||||
|
||||
print(sorted(destinations.items(), key= return_second_tuple_element)) # [('Moscow', 0), ('Ho-Chi-Minh Town', 0), ('Havana', 2), ('Pyongyang', 5), ('Caracas', 7)]
|
||||
print(sorted(destinations.items(), key= lambda tuple : tuple[1])) # [('Moscow', 0), ('Ho-Chi-Minh Town', 0), ('Havana', 2), ('Pyongyang', 5), ('Caracas', 7)]
|
||||
|
||||
#show_destinations_available(destinations)
|
||||
|
||||
# Données déstructurées
|
||||
|
||||
dc_infos = ("Dale", "Cooper", "32")
|
||||
dc_prenom = dc_infos[0]
|
||||
dc_nom = dc_infos[1]
|
||||
dc_age = dc_infos[2]
|
||||
|
||||
dc_prenom, dc_nom, dc_age = dc_infos
|
||||
|
||||
def generator():
|
||||
for i in range(10):
|
||||
yield i
|
||||
|
||||
def square_generator(n):
|
||||
for i in range(n):
|
||||
yield i ** 2
|
||||
|
||||
print(generator())
|
||||
squares = square_generator(10)
|
||||
|
||||
print(next(squares))
|
||||
print(next(squares))
|
||||
print(next(squares))
|
||||
print(next(squares))
|
||||
print(next(squares))
|
||||
print(next(squares))
|
||||
print(next(squares))
|
||||
print(next(squares))
|
||||
28
Semaine_04/Exercices/exo_03.py
Normal file
28
Semaine_04/Exercices/exo_03.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import sys
|
||||
|
||||
input_string = input("Ecris un truc > ")
|
||||
|
||||
if len(input_string) == 0:
|
||||
print("Tu aurais pu participer.")
|
||||
sys.exit()
|
||||
|
||||
def return_string_infos(input_string: str):
|
||||
print("Tu as tapé : " + "\"" + input_string + "\"")
|
||||
print("Elle fait exactement " + str(len(input_string)) + " caractères.")
|
||||
print("EN MAJUSCULES, ELLE DONNE : " + "\"" + input_string.upper() + "\"")
|
||||
print("en minuscules, elle donne : " + "\"" + input_string.lower() + "\"")
|
||||
print("A l'envers, elle donne : " + "\"" + input_string[::-1] + "\"")
|
||||
|
||||
def replace_word_in_string(input_string: str, word_to_replace: str, word_replacement: str) -> str:
|
||||
print(f"Remplaçant \"{word_to_replace}\" par \"{word_replacement}\", cela donne : {input_string.replace(word_to_replace, word_replacement)}")
|
||||
|
||||
def count_occurences_in_string(input_string: str, occurence: str):
|
||||
print(f"L'occurence \"{occurence}\" apparaît {input_string.count(occurence)} fois.")
|
||||
|
||||
def strip_of_spaces(input_string: str):
|
||||
print(f"Sans les espaces de début et de fin, cela donne : {input_string.strip()}")
|
||||
|
||||
return_string_infos(input_string)
|
||||
replace_word_in_string(input_string, "vie", "mort")
|
||||
count_occurences_in_string(input_string, "e")
|
||||
strip_of_spaces(input_string)
|
||||
1
Semaine_04/Exercices/main.py
Normal file
1
Semaine_04/Exercices/main.py
Normal file
@@ -0,0 +1 @@
|
||||
from Semaine_04.Exercice_01.exo_01 import
|
||||
34
Semaine_04/Exercices/pendu.py
Normal file
34
Semaine_04/Exercices/pendu.py
Normal file
@@ -0,0 +1,34 @@
|
||||
word_to_find = "parapluie"
|
||||
word_hidden = "_" * len(word_to_find)
|
||||
list_word_hidden = list(word_hidden)
|
||||
attempts = 10
|
||||
|
||||
already_chosen_letter = []
|
||||
|
||||
print(word_hidden)
|
||||
|
||||
while word_hidden != word_to_find and attempts > 0:
|
||||
chosen_letter = input("Saisir une lettre > ").strip().lower()[0] # on ne prend VRAIMENT que la première lettre
|
||||
|
||||
if chosen_letter in already_chosen_letter:
|
||||
print(f"Vous avez déjà saisie la lettre {chosen_letter.upper()}.")
|
||||
attempts -= 1
|
||||
continue
|
||||
|
||||
already_chosen_letter.append(chosen_letter)
|
||||
|
||||
if chosen_letter not in word_to_find:
|
||||
attempts -= 1
|
||||
continue
|
||||
|
||||
for index, letter in enumerate(word_to_find):
|
||||
if letter == chosen_letter:
|
||||
list_word_hidden[index] = chosen_letter
|
||||
|
||||
word_hidden = "".join(list_word_hidden)
|
||||
print(f"Il reste {attempts} tentatives. \n {word_hidden} \n")
|
||||
|
||||
if word_hidden != word_to_find:
|
||||
print(f"Perdu ! Le mot était {word_to_find.upper()}.")
|
||||
else:
|
||||
print("Gagné !")
|
||||
7
Semaine_04/Exercices/pyproject.toml
Normal file
7
Semaine_04/Exercices/pyproject.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[project]
|
||||
name = "exercice-01"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.12"
|
||||
dependencies = []
|
||||
8
Semaine_04/Exercices/uv.lock
generated
Normal file
8
Semaine_04/Exercices/uv.lock
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
version = 1
|
||||
revision = 3
|
||||
requires-python = ">=3.12"
|
||||
|
||||
[[package]]
|
||||
name = "exercice-01"
|
||||
version = "0.1.0"
|
||||
source = { virtual = "." }
|
||||
Reference in New Issue
Block a user