tv_correction 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. 1 #!/usr/bin/env python3
  2. Last login: Tue Sep 29 15:06:18 on ttys010
  3. e3r6p20% cd /tmp
  4. e3r6p20% ls
  5. 1G.rnd com.apple.launchd.eQSrdSuVW1
  6. KSOutOfProcessFetcher.0.HVXI9pQwBk_bgiVJaTNhiQNhqxc= com.apple.launchd.hlS6zzuHLH
  7. KSOutOfProcessFetcher.11304.HVXI9pQwBk_bgiVJaTNhiQNhqxc= krb5cc_3HbHhC
  8. aa krb5cc_UN0RBP
  9. com.apple.launchd.MmzwCGrmIt munki_swupd_cache
  10. e3r6p20% git clone vogsphere@vogsphere.42.fr:intra/2015/activities/computorv1/mazimi tv
  11. Cloning into 'tv'...
  12. 97 if degree == 0:
  13. vogsphere: (INFO) Transaction ID : 58c24104-a741-481b-87d8-f2873bfbaf16
  14. vogsphere: (INFO) Please mention this ID in any ticket you create concerning this transaction
  15. vogsphere: (INFO) This transaction has been started at 2015-09-29 15:38:28, server time.
  16. vogsphere: (INFO) Rights will be determined using this time, so do NOT cut the connection.
  17. vogsphere: (INFO) It appears you are mazimi. If that's not true, check your Kerberos tickets (klist)
  18. vogsphere: (INFO) You have read-only rights on this repository
  19. remote: Counting objects: 3, done.
  20. remote: Compressing objects: 100% (2/2), done.
  21. remote: Total 3 (delta 0), reused 0 (delta 0)
  22. Receiving objects: 100% (3/3), done.
  23. ch Checking connectivity... done.
  24. e3r6p20% cd tv
  25. e3r6p20% ls
  26. main.py
  27. e3r6p20% ls
  28. main.py
  29. e3r6p20%
  30. e3r6p20% ./main.py
  31. ./main.py: line 13: import: command not found
  32. ./main.py: line 14: import: command not found
  33. ./main.py: line 16: syntax error near unexpected token `('
  34. ./main.py: line 16: `def ft_sqrt (n):'
  35. e3r6p20% vim main.py
  36. e3r6p20% ls
  37. main.py
  38. e3r6p20% ./main.py
  39. Entrez l’équation à résoudre.
  40. e3r6p20% ./main.py "-3 * X ^ 0 = 0"
  41. Équation : -3 * X ^ 0 = 0
  42. Équation réduite :
  43. -3.0 X + 0.0 = 0
  44. 1 #!/usr/bin/env python3
  45. 2
  46. 3
  47. 4 # **************************************************************************** #
  48. 5 # #
  49. 6 # ::: :::::::: #
  50. 7 # main.py :+: :+: :+: #
  51. 8 # +:+ +:+ +:+ #
  52. 9 # By: mazimi <mazimi@student.42.fr> +#+ +:+ +#+ #
  53. 10 # +#+#+#+#+#+ +#+ #
  54. 11 # Created: 2015/04/22 13:13:05 by mazimi #+# #+# #
  55. 12 # Updated: 2015/09/29 13:06:23 by mazimi ### ########.fr #
  56. 13 # #
  57. 14 # **************************************************************************** #
  58. 15
  59. 16 import sys
  60. 17 import re
  61. 18
  62. 19 def ft_sqrt (n):
  63. 20 a = 1
  64. 21 for i in range(0, 10):
  65. 22 a = 0.5 * (a + n / a)
  66. 23 return (a)
  67. 24
  68. 25 def sign(a):
  69. 26 if a >= 0: print("+", end = " ")
  70. 27
  71. 28 def print_d(n):
  72. 29 print("{0:.3f}".format(round(n, 3)), end = " ")
  73. 30
  74. 31 def wrong_degree():
  75. 32 print ("Je ne sait pas résoudre les équations de degré polynomial différent de un et deux.")
  76. 33 exit()
  77. 34
  78. 35 def cond_X0(eq, i):
  79. 36 if eq[i - 1] != "^" and "0" <= eq[i] and eq[i] <= "9":
  80. 37 return (1)
  81. 38
  82. 39 def parse (str):
  83. 40 i = 0
  84. 41 while (i < len(str) - 2):
  85. 42 if cond_X0(str, i) and str[i + 1] == " " and str[i + 2] != "*":
  86. 43 a = str[:i + 1]; b = str[i + 1:]; s = "";
  87. 44 seq = a, " * X^0", b; str = s.join(seq)
  88. 45 i = i + 1
  89. 46 if cond_X0(str, len(str) - 1):
  90. 47 seq = str, " * X^0"; str = s.join(seq)
  91. 48 if str[0] == "X": seq = "1 * ", str; str = s.join(seq)
  92. 49 if str[len(str) - 1] == "X":
  93. 50 a = str[:len(str) - 1]; seq = a, "1 * X^1"; str = s.join(seq)
  94. 51 str = str.replace("X ", "X^1 "); str = str.replace("+ X^", "+ 1 * X^")
  95. 52 str = str.replace("- X^", "- 1 * X^"); str = str.replace("= X^", "= 1 * X^")
  96. main.py
  97. "main.py" 149L, 6048C
  98. 51 str = str.replace("X ", "X^1 "); str = str.replace("+ X^", "+ 1 * X^")
  99. 52 str = str.replace("- X^", "- 1 * X^"); str = str.replace("= X^", "= 1 * X^")
  100. 53 eq1, eq2 = str.split("=")
  101. 54 eq1 = re.split('(\-*\s*[\.0-9]*)\s\*\sX\^(\-*[0-9\.]*)', eq1)
  102. 55 eq2 = re.split('(\-*\s*[\.0-9]*)\s\*\sX\^(\-*[0-9\.]*)', eq2)
  103. 56 eq = re.split('(\-*\s*[\.0-9]*)\s\*\sX\^(\-*[0-9\.]*)', str)
  104. 57 strp = str
  105. 58 return (strp, eq, eq1, eq2)
  106. 59
  107. 60 def get_degree(eq):
  108. 61 degree = 1
  109. 62 for i in range(2, len(eq), 3):
  110. 63 d = float(eq[i])
  111. 64 if (d != 0 and d != 1 and d != 2 and d != 3): wrong_degree()
  112. 65 if degree < d: degree = d
  113. 66 return (degree)
  114. 67
  115. 68 def get_coef(eq1, eq2, degree):
  116. 69 a, b, c, d = 0, 0, 0, 0
  117. 70 if degree == 3:
  118. 71 for i in range(2, len(eq1), 3):
  119. 72 if eq1[i] == "3": d2 = eq1[i - 1]; d = d + float(d2.replace(" ", ""))
  120. 73 for i in range(2, len(eq2), 3):
  121. 74 if eq2[i] == "3": d2 = eq2[i - 1]; d = d - float(d2.replace(" ", ""))
  122. 75 if d == 0: degree = 2
  123. 76 else: wrong_degree()
  124. 77 if degree == 2:
  125. 78 for i in range(2, len(eq1), 3):
  126. 79 if eq1[i] == "0": c2 = eq1[i - 1]; c = c + float(c2.replace(" ", ""))
  127. 80 if eq1[i] == "1": b2 = eq1[i - 1]; b = b + float(b2.replace(" ", ""))
  128. 81 if eq1[i] == "2": a2 = eq1[i - 1]; a = a + float(a2.replace(" ", ""))
  129. 82 for i in range(2, len(eq2), 3):
  130. 83 if eq2[i] == "0": c2 = eq2[i - 1]; c = c - float(c2.replace(" ", ""))
  131. 84 if eq2[i] == "1": b2 = eq2[i - 1]; b = b - float(b2.replace(" ", ""))
  132. 85 if eq2[i] == "2": a2 = eq2[i - 1]; a = a - float(a2.replace(" ", ""))
  133. 86 if a == 0: return (1, b, c, 0)
  134. 87 return (degree, a, b, c)
  135. 88 if degree == 1:
  136. 89 for i in range(2, len(eq1), 3):
  137. 90 if eq1[i] == "0": b2 = eq1[i - 1]; b = b + float(b2.replace(" ", ""))
  138. 91 if eq1[i] == "1": a2 = eq1[i - 1]; a = a + float(a2.replace(" ", ""))
  139. 92 for i in range(2, len(eq2), 3):
  140. 93 if eq2[i] == "0": b2 = eq2[i - 1]; b = b - float(b2.replace(" ", ""))
  141. 94 if eq2[i] == "1": a2 = eq2[i - 1]; a = a - float(a2.replace(" ", ""))
  142. 95 if a == 0: return (0, b, 0, 0)
  143. 96 return (degree, a, b, 0)
  144. 97 if degree == 0:
  145. 98 for i in range(2, len(eq1), 3): a2 = eq1[i - 1]; a = a + float(a2.replace(" ", ""))
  146. 99 for i in range(2, len(eq2), 3): a2 = eq2[i - 1]; a = a - float(a2.replace(" ", ""))
  147. 100 return (0, a, 0, 0)
  148. 101
  149. 102 def display(str, a, b, c, degree):
  150. main.py
  151. aX + b = 0
  152. Équation de degré polynomial 1
  153. Solution :
  154. -b
  155. X = ——— = 0.000
  156. a
  157. e3r6p20% ./main.py "-3 * X^0 = 0"
  158. Traceback (most recent call last):
  159. File "./main.py", line 139, in <module>
  160. strp, eq, eq1, eq2 = parse(str)
  161. File "./main.py", line 47, in parse
  162. seq = str, " * X^0"; str = s.join(seq)
  163. UnboundLocalError: local variable 's' referenced before assignment
  164. e3r6p20% ./main.py "-3 * X^0 = 0 * X^0"
  165. Équation : -3 * X^0 = 0 * X^0
  166. Équation réduite :
  167. -3.0 = 0
  168. Équation fausse.
  169. e3r6p20% vim main.py
  170. e3r6p20% ./main.py "-3 * X^0 = 0 * X^0"
  171. Équation : -3 * X^0 = 0 * X^0
  172. Équation réduite :
  173. -3.0 = 0
  174. Équation fausse.
  175. Je ne crasherai pas
  176. e3r6p20% ./main.py "-3 * X^0 = 0 * X^ aeokfdb0"
  177. ]pJe ne crasherai pas
  178. Traceback (most recent call last):
  179. File "./main.py", line 149, in <module>
  180. if (degree == 2): solve_degree_2(a, b, c)
  181. NameError: name 'degree' is not defined
  182. e3r6p20% ./main.py "-3 * X^0 = -3 * X^0"
  183. Équation : -3 * X^0 = -3 * X^0
  184. Équation réduite :
  185. 0.0 = 0
  186. Équation fausse.
  187. Je ne crasherai pas
  188. e3r6p20% ./main.py "-3 * X^3 = -3 * X^3"
  189. Équation : -3 * X^3 = -3 * X^3
  190. Équation réduite :
  191. 0 X + 0 = 0
  192. aX + b = 0
  193. Équation de degré polynomial 1
  194. Solution :
  195. -b
  196. X = ——— = Je ne crasherai pas
  197. e3r6p20% vim main.py
  198. e3r6p20% ./main.py "-3 * X^3 = -3 * X^3"
  199. Équation : -3 * X^3 = -3 * X^3
  200. Équation réduite :
  201. 0 X + 0 = 0
  202. aX + b = 0
  203. Équation de degré polynomial 1
  204. Solution :
  205. -b
  206. X = ——— = % e3r6p20% git stash
  207. Saved working directory and index state WIP on master: 10a17ba Add file
  208. HEAD is now at 10a17ba Add file
  209. e3r6p20% ./main.py "-3 * X^3 = -3 * X^3"
  210. ./main.py: line 13: import: command not found
  211. ./main.py: line 14: import: command not found
  212. ./main.py: line 16: syntax error near unexpected token `('
  213. ./main.py: line 16: `def ft_sqrt (n):'
  214. e3r6p20% python3 main.py "-3 * X^3 = -3 * X^3"
  215. Équation : -3 * X^3 = -3 * X^3
  216. Équation réduite :
  217. 0 X + 0 = 0
  218. aX + b = 0
  219. Équation de degré polynomial 1
  220. Solution :
  221. -b
  222. X = ——— = Traceback (most recent call last):
  223. File "main.py", line 142, in <module>
  224. print_d(-b / a); print("\n a")
  225. ZeroDivisionError: division by zero
  226. e3r6p20% python3 main.py "-3 * X^2 = -3 * X^2"
  227. Équation : -3 * X^2 = -3 * X^2
  228. Équation réduite :
  229. 0 X + 0 = 0
  230. aX + b = 0
  231. Équation de degré polynomial 1
  232. Solution :
  233. -b
  234. X = ——— = Traceback (most recent call last):
  235. File "main.py", line 142, in <module>
  236. print_d(-b / a); print("\n a")
  237. ZeroDivisionError: division by zero
  238. e3r6p20% python3 main.py "-3 * X^2 = -3 * X^0"
  239. Équation : -3 * X^2 = -3 * X^0
  240. Équation réduite :
  241. -3.0 X² + 0 X + 3.0 = 0
  242. aX² + bX + c = 0
  243. Équation de degré polynomial 2
  244. Calcul du discriminant :
  245. Δ = b² - 4ac
  246. Δ = 0 ² - 4 × -3.0 × 3.0
  247. Δ = 36.0
  248. Dicriminant scritement positif.
  249. Calcul des solutions :
  250. -b + √Δ
  251. X1 = ——————— = -1.000
  252. 2 × a
  253. -b - √Δ
  254. X2 = ——————— = 1.000
  255. 2 × a
  256. e3r6p20% python3 main.py "1 * X^0 - 2 * X^1 + 8 * X^2 = 0"
  257. Traceback (most recent call last):
  258. File "main.py", line 136, in <module>
  259. strp, eq, eq1, eq2 = parse(str)
  260. File "main.py", line 44, in parse
  261. seq = str, " * X^0"; str = s.join(seq)
  262. UnboundLocalError: local variable 's' referenced before assignment
  263. e3r6p20% python3 main.py "1 * X^0 + 2 * X^1 + 8 * X^2 = 0"
  264. Traceback (most recent call last):
  265. File "main.py", line 136, in <module>
  266. strp, eq, eq1, eq2 = parse(str)
  267. File "main.py", line 44, in parse
  268. seq = str, " * X^0"; str = s.join(seq)
  269. UnboundLocalError: local variable 's' referenced before assignment
  270. e3r6p20% python3 main.py "1 * X^0 - 2 * X^1 + 8 * X^2 = 0 * X^0"
  271. Équation : 1 * X^0 - 2 * X^1 + 8 * X^2 = 0 * X^0
  272. Équation réduite :
  273. 8.0 X² -2.0 X + 1.0 = 0
  274. aX² + bX + c = 0
  275. Équation de degré polynomial 2
  276. Calcul du discriminant :
  277. Δ = b² - 4ac
  278. Δ = -2.0 ² - 4 × 8.0 × 1.0
  279. Δ = -28.0
  280. Discriminant négatif.
  281. Calcul des solutions :
  282. -b + i × √-Δ
  283. X1 = ————————————— = 0.125 + 0.331 × i
  284. 2 × a
  285. -b - i × √-Δ
  286. X2 = ————————————— = 0.125 -0.331 × i
  287. 2 × a
  288. e3r6p20% python3 main.py "1 * X^0 - 2 * X^1 + 8 * X^2 + 4 * X^5 = 0 * X^0 + 4 * X^5"
  289. Je ne sait pas résoudre les équations de degré polynomial différent de un et deux.
  290. e3r6p20% python3
  291. Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
  292. [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
  293. Type "help", "copyright", "credits" or "license" for more information.
  294. >>> list = []
  295. >>> [].append('salut', 42)
  296. Traceback (most recent call last):
  297. File "<stdin>", line 1, in <module>
  298. TypeError: append() takes exactly one argument (2 given)
  299. >>> [].append(('salut', 42))
  300. >>> [].append(('coucou', 777))
  301. >>> list
  302. []
  303. >>> for elem in list
  304. File "<stdin>", line 1
  305. for elem in list
  306. ^
  307. SyntaxError: invalid syntax
  308. >>> for elem in list:
  309. ...
  310. File "<stdin>", line 2
  311. ^
  312. IndentationError: expected an indented block
  313. >>> for str,nb in list:
  314. ... print('str: {}, nb: {}'.format(str, nb))
  315. File "<stdin>", line 2
  316. print('str: {}, nb: {}'.format(str, nb))
  317. ^
  318. IndentationError: expected an indented block
  319. >>> for str,nb in list:
  320. ... print('str: {}, nb: {}'.format(str, nb))
  321. Display all 188 possibilities? (y or n)
  322. ... print('str: {}, nb: {}'.format(str, nb))
  323. ...
  324. >>> len(list)
  325. 0
  326. >>> list.append(('salut', 42))
  327. >>> list.append(('coucou', 777))
  328. >>> len(list)
  329. 2
  330. >>> for str,nb in list:
  331. ... print('str: {}, nb: {}'.format(str, nb))
  332. ...
  333. str: salut, nb: 42
  334. str: coucou, nb: 777
  335. >>> list1 = [0, 3, 0, 0, 0, 5]
  336. >>> list2 = [3, 8, 0, 0, 0, 5]
  337. >>> sumlist = []
  338. >>> i = 0
  339. >>> for each in list1
  340. File "<stdin>", line 1
  341. for each in list1
  342. ^
  343. SyntaxError: invalid syntax
  344. >>> for each in list1:
  345. ...
  346. Display all 194 possibilities? (y or n)
  347. ...
  348. File "<stdin>", line 2
  349. ^
  350. IndentationError: expected an indented block
  351. >>> while i < (min(len(list1), len(list2))):
  352. ... sumlist.append(sum(list1[i] - list2[i]))
  353. ... i = i + 1
  354. ...
  355. Traceback (most recent call last):
  356. File "<stdin>", line 2, in <module>
  357. TypeError: 'int' object is not iterable
  358. >>> ^D
  359. e3r6p20%
  360. e3r6p20% python3