Compare commits
11 Commits
f915ffb79b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1ebc36c197 | ||
|
|
17b1da0822 | ||
|
|
1cf9f29496 | ||
|
|
2e1edf72b2 | ||
|
|
49461a0b25 | ||
|
|
6f83006187 | ||
|
|
cba9d9be86 | ||
|
|
42d00c7ea1 | ||
|
|
bce4015ad4 | ||
|
|
be2442e222 | ||
|
|
5fc979681c |
@@ -1,7 +1,9 @@
|
|||||||
ft_list = ["Hello", "tata!"]
|
ft_list = ["Hello", "tata!"]
|
||||||
ft_tuple = ("Hello", "toto!")
|
ft_tuple = ("Hello", "toto!")
|
||||||
ft_set = {"Hello", "tutu!"}
|
ft_set = {"Hello", "tutu!"}
|
||||||
ft_dict = {"Hello" : "titi!"}
|
ft_dict = {"Hello": "titi!"}
|
||||||
|
|
||||||
|
# doc : https://docs.python.org/3.10/tutorial/datastructures.html
|
||||||
|
|
||||||
# ft_list
|
# ft_list
|
||||||
ft_list.append("World" + ft_list[1][-1])
|
ft_list.append("World" + ft_list[1][-1])
|
||||||
@@ -10,8 +12,8 @@ ft_list.pop(1)
|
|||||||
# ft_tuple
|
# ft_tuple
|
||||||
ft_tuple = ft_tuple[0], "France!"
|
ft_tuple = ft_tuple[0], "France!"
|
||||||
|
|
||||||
# ft_set
|
# ft_set, symetric difference :
|
||||||
# ft_set = "{'Hello', 'Paris!'}" # mouhaha
|
# https://docs.python.org/3.10/library/stdtypes.html#frozenset.symmetric_difference
|
||||||
ft_set ^= {"tutu!", "Paris!"}
|
ft_set ^= {"tutu!", "Paris!"}
|
||||||
|
|
||||||
# ft_dict
|
# ft_dict
|
||||||
@@ -19,5 +21,5 @@ ft_dict["Hello"] = "42Paris!"
|
|||||||
|
|
||||||
print(ft_list)
|
print(ft_list)
|
||||||
print(ft_tuple)
|
print(ft_tuple)
|
||||||
print(ft_set)
|
print(ft_set) # set is unordered, so output order is hasardous
|
||||||
print(ft_dict)
|
print(ft_dict)
|
||||||
|
|||||||
@@ -1,16 +1,29 @@
|
|||||||
import datetime as dt
|
import datetime as dt
|
||||||
|
import time as tm
|
||||||
|
|
||||||
# https://docs.python.org/3.10/library/datetime.html?highlight=time#datetime.datetime
|
# https://docs.python.org/3.10/library/datetime.html?highlight=time#datetime.datetime
|
||||||
now = dt.datetime.now()
|
now = dt.datetime.now()
|
||||||
epoch = dt.datetime(1970, 1, 1)
|
|
||||||
|
# # for memory
|
||||||
|
# epoch = dt.datetime(1970, 1, 1)
|
||||||
|
# delta = now - epoch
|
||||||
|
# deltaInSeconds = delta.total_seconds()
|
||||||
|
|
||||||
# https://docs.python.org/3.10/library/datetime.html?highlight=time#strftime-and-strptime-behavior
|
# https://docs.python.org/3.10/library/datetime.html?highlight=time#strftime-and-strptime-behavior
|
||||||
formatDate = now.strftime("%b %d %Y")
|
format_date = now.strftime("%b %d %Y")
|
||||||
|
|
||||||
delta = now - epoch
|
epoch_delta_in_seconds = tm.time()
|
||||||
|
|
||||||
deltaInSeconds = delta.total_seconds()
|
# str.format https://docs.python.org/3.10/library/string.html#formatstrings
|
||||||
deltaInSecondsStr = "{:,.3f}".format(float(deltaInSeconds))
|
epoch_delta_in_seconds_str = "{:,.4f}".format(epoch_delta_in_seconds)
|
||||||
deltaInScientificStr = "{:.2e}".format(float(deltaInSeconds))
|
epoch_delta_in_scientific_str = "{:.2e}".format(epoch_delta_in_seconds)
|
||||||
|
|
||||||
print("Seconds since January 1, 1970: " + deltaInSecondsStr + " or " + deltaInScientificStr + " in scientific notation\n" + formatDate)
|
print(
|
||||||
|
"Seconds since January 1, 1970: ",
|
||||||
|
epoch_delta_in_seconds_str,
|
||||||
|
" or ",
|
||||||
|
epoch_delta_in_scientific_str,
|
||||||
|
" in scientific notation\n",
|
||||||
|
format_date,
|
||||||
|
sep=""
|
||||||
|
)
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ def all_thing_is_obj(object: any) -> int:
|
|||||||
|
|
||||||
# https://docs.python.org/3.10/tutorial/controlflow.html#match-statements
|
# https://docs.python.org/3.10/tutorial/controlflow.html#match-statements
|
||||||
match object:
|
match object:
|
||||||
# class patterm : check against a class (check if instance when empty arguments) https://docs.python.org/3/reference/compound_stmts.html#class-patterns
|
# class pattern, against a class (or instance if empty arguments):
|
||||||
|
# https://docs.python.org/3/reference/compound_stmts.html#class-patterns
|
||||||
case list():
|
case list():
|
||||||
print("List : ", ofType)
|
print("List : ", ofType)
|
||||||
case tuple():
|
case tuple():
|
||||||
@@ -17,4 +18,4 @@ def all_thing_is_obj(object: any) -> int:
|
|||||||
print(object + " is in the kitchen : ", ofType)
|
print(object + " is in the kitchen : ", ofType)
|
||||||
case _:
|
case _:
|
||||||
print("Type not found")
|
print("Type not found")
|
||||||
return 42
|
return 42
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from find_ft_type import all_thing_is_obj
|
|||||||
ft_list = ["Hello", "tata!"]
|
ft_list = ["Hello", "tata!"]
|
||||||
ft_tuple = ("Hello", "toto!")
|
ft_tuple = ("Hello", "toto!")
|
||||||
ft_set = {"Hello", "tutu!"}
|
ft_set = {"Hello", "tutu!"}
|
||||||
ft_dict = {"Hello" : "titi!"}
|
ft_dict = {"Hello": "titi!"}
|
||||||
|
|
||||||
all_thing_is_obj(ft_list)
|
all_thing_is_obj(ft_list)
|
||||||
all_thing_is_obj(ft_tuple)
|
all_thing_is_obj(ft_tuple)
|
||||||
@@ -11,4 +11,4 @@ all_thing_is_obj(ft_set)
|
|||||||
all_thing_is_obj(ft_dict)
|
all_thing_is_obj(ft_dict)
|
||||||
all_thing_is_obj("Brian")
|
all_thing_is_obj("Brian")
|
||||||
all_thing_is_obj("Toto")
|
all_thing_is_obj("Toto")
|
||||||
print(all_thing_is_obj(10))
|
print(all_thing_is_obj(10))
|
||||||
|
|||||||
@@ -4,18 +4,25 @@ def NULL_not_found(object: any) -> int:
|
|||||||
match object:
|
match object:
|
||||||
case None:
|
case None:
|
||||||
print("Nothing:", object, nullType)
|
print("Nothing:", object, nullType)
|
||||||
# using a guard https://docs.python.org/3/reference/compound_stmts.html#guards
|
|
||||||
# (NaN == NaN) = false https://docs.python.org/3.10/reference/expressions.html#value-comparisons "A counter-intuitive implication is that not-a-number values are not equal to themselves"
|
# using a guard :
|
||||||
|
# https://docs.python.org/3/reference/compound_stmts.html#guards
|
||||||
|
# (NaN == NaN) = false :
|
||||||
|
# https://docs.python.org/3.10/reference/expressions.html#value-comparisons
|
||||||
|
# "A counter-intuitive implication is that not-a-number values are not
|
||||||
|
# equal to themselves"
|
||||||
|
|
||||||
case float() if object != object:
|
case float() if object != object:
|
||||||
print("Cheese:", object, nullType)
|
print("Cheese:", object, nullType)
|
||||||
case bool() if object == False :
|
# need check before int(), why ?
|
||||||
|
case bool() if object is False:
|
||||||
print("Fake:", object, nullType)
|
print("Fake:", object, nullType)
|
||||||
case int() if object == 0 :
|
case int() if object == 0:
|
||||||
print("Zero:", object, nullType)
|
print("Zero:", object, nullType)
|
||||||
case str() if object == '' :
|
case str() if object == '':
|
||||||
print("Empty:", object, nullType)
|
print("Empty:", nullType)
|
||||||
case _:
|
case _:
|
||||||
print("Type not Found")
|
print("Type not Found")
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ Nothing = None
|
|||||||
Garlic = float("NaN")
|
Garlic = float("NaN")
|
||||||
Garlic2 = float("2")
|
Garlic2 = float("2")
|
||||||
Zero = 0
|
Zero = 0
|
||||||
Empty = '' # ’’
|
Empty = '' # ’’
|
||||||
Fake = False
|
Fake = False
|
||||||
Right = True
|
Right = True
|
||||||
|
|
||||||
@@ -18,4 +18,4 @@ print(NULL_not_found("Brian"))
|
|||||||
print("--")
|
print("--")
|
||||||
NULL_not_found(Garlic2)
|
NULL_not_found(Garlic2)
|
||||||
NULL_not_found(Right)
|
NULL_not_found(Right)
|
||||||
print(NULL_not_found(""))
|
print(NULL_not_found(""))
|
||||||
|
|||||||
27
d00/ex04/whatis.py
Normal file
27
d00/ex04/whatis.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
def check_parity(args: list[str]):
|
||||||
|
if len(args) == 0:
|
||||||
|
return
|
||||||
|
if len(args) > 1:
|
||||||
|
print("AssertionError: more than one argument is provided")
|
||||||
|
return
|
||||||
|
|
||||||
|
arg = args[0]
|
||||||
|
# https://docs.python.org/3.10/tutorial/errors.html
|
||||||
|
try:
|
||||||
|
number = int(arg)
|
||||||
|
except ValueError:
|
||||||
|
print("AssertionError: argument is not an integer")
|
||||||
|
return
|
||||||
|
|
||||||
|
if number % 2 == 0:
|
||||||
|
print("I'm Even.")
|
||||||
|
else:
|
||||||
|
print("I'm Odd.")
|
||||||
|
|
||||||
|
|
||||||
|
# execute module as a script :
|
||||||
|
# https://docs.python.org/3.10/tutorial/modules.html#executing-modules-as-scripts
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# https://docs.python.org/3.10/library/sys.html
|
||||||
|
import sys
|
||||||
|
check_parity(sys.argv[1:])
|
||||||
55
d00/ex05/building.py
Normal file
55
d00/ex05/building.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
def get_message(argv: list[str]) -> str:
|
||||||
|
"""check arguments and return string"""
|
||||||
|
|
||||||
|
# https://docs.python.org/3.10/reference/simple_stmts.html?highlight=assert#the-assert-statement
|
||||||
|
assert len(argv) < 3, "too much arguments"
|
||||||
|
|
||||||
|
if len(argv) == 2:
|
||||||
|
return argv[1]
|
||||||
|
|
||||||
|
# stdin https://docs.python.org/3/library/sys.html#sys.stdin
|
||||||
|
print("What is the text to count?")
|
||||||
|
msg = sys.stdin.readline(424242)
|
||||||
|
return msg
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str]):
|
||||||
|
"""displays stats on string"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
msg = get_message(argv)
|
||||||
|
except AssertionError as err:
|
||||||
|
print("AssertionError:", err)
|
||||||
|
return
|
||||||
|
|
||||||
|
char_count = len(msg)
|
||||||
|
print("The text contains", char_count, "characters:")
|
||||||
|
|
||||||
|
# filter() https://docs.python.org/3.10/library/functions.html#filter
|
||||||
|
upper_count = len(list(filter(str.isupper, msg)))
|
||||||
|
print(upper_count, "upper letters")
|
||||||
|
|
||||||
|
# generator expressions
|
||||||
|
# https://docs.python.org/3.10/reference/expressions.html#generator-expressions
|
||||||
|
lower_count = sum(1 for char in msg if char.islower())
|
||||||
|
print(lower_count, "lower letters")
|
||||||
|
|
||||||
|
# in https://docs.python.org/3/reference/expressions.html#in
|
||||||
|
punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
|
||||||
|
punctuation_count = sum(1 for char in msg if char in punctuation)
|
||||||
|
print(punctuation_count, "punctuation marks")
|
||||||
|
|
||||||
|
# array.count()
|
||||||
|
# https://docs.python.org/3.10/library/array.html?highlight=count#array.array.count
|
||||||
|
space_count = msg.count(" ") + msg.count("\n")
|
||||||
|
print(space_count, "spaces")
|
||||||
|
|
||||||
|
# list comprehension
|
||||||
|
# https://docs.python.org/3.10/tutorial/datastructures.html#list-comprehensions
|
||||||
|
digits_count = len([char for char in msg if char.isdigit()])
|
||||||
|
print(digits_count, "digits")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import sys
|
||||||
|
main(sys.argv)
|
||||||
39
d00/ex06/filterstring.py
Normal file
39
d00/ex06/filterstring.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
def get_args(argv: list[str]) -> dict[str, int]:
|
||||||
|
"""check and return arguments"""
|
||||||
|
|
||||||
|
error_msg = "the arguments are bad"
|
||||||
|
|
||||||
|
assert len(argv) == 3, error_msg
|
||||||
|
|
||||||
|
try:
|
||||||
|
msg_len = int(argv[2])
|
||||||
|
except ValueError:
|
||||||
|
raise AssertionError(error_msg)
|
||||||
|
|
||||||
|
return {"text": argv[1], "len": msg_len}
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str]):
|
||||||
|
"""filter small words in string"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
args = get_args(argv)
|
||||||
|
except AssertionError as err:
|
||||||
|
print("AssertionError:", err)
|
||||||
|
return
|
||||||
|
|
||||||
|
text = args["text"]
|
||||||
|
maxlen = args["len"]
|
||||||
|
|
||||||
|
print(text.split())
|
||||||
|
|
||||||
|
words = [word for word in text.split()] # mouhaha
|
||||||
|
|
||||||
|
filter_words = list(ft_filter(lambda word: len(word) > maxlen, words))
|
||||||
|
print(filter_words)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import sys
|
||||||
|
from ft_filter import ft_filter
|
||||||
|
main(sys.argv)
|
||||||
13
d00/ex06/ft_filter.py
Normal file
13
d00/ex06/ft_filter.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# filter https://docs.python.org/3.10/library/functions.html#filter
|
||||||
|
def ft_filter(function, iterable):
|
||||||
|
"""filter(function or None, iterable) --> filter object
|
||||||
|
|
||||||
|
Return an iterator yielding those items of iterable for which function(item)
|
||||||
|
is true. If function is None, return the items that are true."""
|
||||||
|
|
||||||
|
# iter
|
||||||
|
# https://docs.python.org/3.10/library/functions.html?highlight=iter#iter
|
||||||
|
if function is None:
|
||||||
|
return iter([item for item in iterable if item])
|
||||||
|
else:
|
||||||
|
return iter([item for item in iterable if function(item)])
|
||||||
19
d00/ex06/tester.py
Normal file
19
d00/ex06/tester.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
from ft_filter import ft_filter
|
||||||
|
|
||||||
|
print("- 0 filter :\n\n", filter.__doc__)
|
||||||
|
print("\n ft_filter :\n\n", ft_filter.__doc__)
|
||||||
|
print("")
|
||||||
|
|
||||||
|
msg = "Hello World"
|
||||||
|
|
||||||
|
print("- 1 filter :", filter(None, msg))
|
||||||
|
print(" ft_filter :", ft_filter(None, msg))
|
||||||
|
|
||||||
|
print("- 2 filter :", filter(str.isupper, msg))
|
||||||
|
print(" ft_filter :", ft_filter(str.isupper, msg))
|
||||||
|
|
||||||
|
print("- 3 filter :", list(filter(None, msg)))
|
||||||
|
print(" ft_filter :", list(ft_filter(None, msg)))
|
||||||
|
|
||||||
|
print("- 4 filter :", list(filter(str.isupper, msg)))
|
||||||
|
print(" ft_filter :", list(ft_filter(str.isupper, msg)))
|
||||||
Reference in New Issue
Block a user