Skip to content

Instantly share code, notes, and snippets.

Avatar

TheGeekInTheShell TGITS

View GitHub Profile
View reduce_basic_example.py
# Solution classique avec une boucle for
sum_of_numbers_1 = 0
for number in range(1, 11):
sum_of_numbers_1 = sum_of_numbers_1 + number
print("Somme des nombres de 1 à 10 avec une boucle for :", sum_of_numbers_1)
sum_of_numbers_2 = functools.reduce(lambda a, b: a + b, range(1, 11))
print("Somme des nombres de 1 à 10 avec reduce :", sum_of_numbers_2)
sum_of_numbers_3 = functools.reduce(lambda a, b: a + b, [1])
View flatmap_in_python.py
import itertools
import functools
import operator
sequence_1 = map(lambda x: x + 1, range(0, 10))
print(list(sequence_1))
sequence_2 = map(lambda x: [x, x + 1], range(0, 10))
print(list(sequence_2))
@TGITS
TGITS / map_examples.py
Last active May 24, 2021
Exemples d'utilisation de la fonction map
View map_examples.py
import itertools
import functools
import operator
def squared(x): return x**2
print("Itérateurs créés à partir d'une map")
iterator_of_powers_of_2_for_first_numbers = map(squared, range(1, 10))
@TGITS
TGITS / filter_examples.py
Created Apr 11, 2021
Exemples d'utilisation de la fonction filter
View filter_examples.py
import itertools
houses = ["tyrell", "stark", "lannister", "tarly", "baratheon", "targaryen"]
print('Houses :', houses)
print('\n##############\n')
houses_starting_with_t = filter(lambda s: s.startswith("t"), houses)
print("La fonction filter retourne un objet iterator :", houses_starting_with_t)
print("C'est bien un objet iterator avec les méthodes __iter__ et __next__")
@TGITS
TGITS / zip_examples_with_got.py
Created Nov 22, 2020
Exemples d'utilisation de zip en Python
View zip_examples_with_got.py
houses = ["Stark", "Lannister", "Baratheon", "Greyjoy"]
seats = ["Winterfell", "Casterly Rock", "Storm's End", "Pyke"]
sigils = ["A Gray Direwolf", "A Golden Lion",
"A Crowned Black Stag", "A Golden Kraken"]
words = ["Winter is coming", "Hear me roar !",
"Our is the fury !", "We do not sow"]
print(f"houses : {houses}")
print(f"seats : {seats}")
print(f"sigils : {sigils}")
@TGITS
TGITS / comprehension_examples.py
Created Oct 31, 2020
Différents exemples de compréhensions en Python
View comprehension_examples.py
import itertools
print('Compréhension de liste')
list_of_powers_of_2_for_first_numbers = [x**2 for x in range(1, 10)]
print(list_of_powers_of_2_for_first_numbers)
print('\n##############\n')
print("Compréhension d'ensemble")
set_of_powers_of_2_for_first_numbers = {x**2 for x in range(1, 10)}
@TGITS
TGITS / reversing_list.py
Created Oct 23, 2020
Manière d'inverser les éléments d'une liste ou d'une string en Python
View reversing_list.py
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print('liste de nombres :', numbers)
numbers.reverse()
print('liste de nombres après reverse() :', numbers)
print('\n##############\n')
numbers = list(range(10))
print('liste de nombres :', numbers)
reversed(numbers)
@TGITS
TGITS / using_enumerate.py
Created Oct 21, 2020
Exemple d'utilisation de enumerate en Python
View using_enumerate.py
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print('letters {}'.format(letters))
print('\n##########\n')
print('In Python, the for is a foreach : ')
for letter in letters:
print(letter)
View Ressources pour pratiquer les Katas
@startmindmap kata-mindmap
* Ressources pour pratiquer les Katas
** Catalogues de Katas
*** Kata-Log : https://kata-log.rocks/
*** CodingDojo : https://codingdojo.org/
**** https://codingdojo.org/kata/
*** Code Kata : http://codekata.com/
** Katas spécialisés
*** Refactoring Katas
**** Tennis : https://github.com/emilybache/Tennis-Refactoring-Kata
@TGITS
TGITS / frequencies_map.py
Last active Oct 23, 2020
Frequencies map with Python
View frequencies_map.py
from itertools import groupby
from collections import Counter
def frequencies_map_with_for_from(elements):
frequencies_map = {}
for element in elements:
if element in frequencies_map.keys():
frequencies_map[element] = frequencies_map[element] + 1
else: